Feature #1225 ยป route53.patch
lib/dns_api.rb 2011-12-30 13:23:09.000000000 -0500 | ||
---|---|---|
require "proxy/dns/bind"
|
||
require "proxy/dns/aws"
|
||
class SmartProxy
|
||
def setup(opts)
|
||
@server = Proxy::DNS::Bind.new(opts.merge(:server => SETTINGS.dns_server))
|
||
if SETTINGS.dns_backend == 'AWS'
|
||
puts "Using AWS dns backend"
|
||
@server = Proxy::DNS::Aws.new(opts.merge(:server => SETTINGS.dns_server))
|
||
else
|
||
puts "Using BIND dns backend"
|
||
@server = Proxy::DNS::Bind.new(opts.merge(:server => SETTINGS.dns_server))
|
||
end
|
||
end
|
||
post "/dns/" do
|
lib/proxy/dns/aws.rb 2011-12-30 17:27:37.000000000 -0500 | ||
---|---|---|
require "proxy/dns"
|
||
require 'resolv'
|
||
require 'route53'
|
||
module Proxy::DNS
|
||
class Aws < Record
|
||
include Proxy::Util
|
||
attr_reader :resolver
|
||
def initialize options = {}
|
||
raise "Route53: dns_aws_secretkey and dns_aws_accesskey must be set." unless defined? SETTINGS.dns_aws_accesskey and defined? SETTINGS.dns_aws_secretkey
|
||
super(options)
|
||
end
|
||
# create({ :fqdn => "node01.lab", :value => "192.168.100.2"}
|
||
# create({ :fqdn => "node01.lab", :value => "3.100.168.192.in-addr.arpa",
|
||
# :type => "PTR"}
|
||
def create
|
||
conn = Route53::Connection.new(SETTINGS.dns_aws_accesskey,SETTINGS.dns_aws_secretkey)
|
||
|
||
@resolver = Resolv::DNS.new(:nameserver => @server)
|
||
case @type
|
||
when "A"
|
||
domain = @fqdn.split('.',2).last + '.'
|
||
zone = conn.get_zones(name=domain)[0]
|
||
if ip = dns_find(@fqdn)
|
||
raise(Proxy::DNS::Collision, "#{@fqdn} is already used by #{ip}") unless ip == @value
|
||
else
|
||
new_record = Route53::DNSRecord.new(@fqdn, 'A', @ttl, [@value], zone)
|
||
resp = new_record.create
|
||
raise "AWS Response Error: #{resp}" if resp.error?
|
||
end
|
||
when "PTR"
|
||
domain = @value.split('.',2).last + '.'
|
||
zone = conn.get_zones(name=domain)[0]
|
||
if name = dns_find(@value)
|
||
raise(Proxy::DNS::Collision, "#{@value} is already used by #{name}") unless name == @fqdn
|
||
else
|
||
new_record = Route53::DNSRecord.new(@value, 'PTR', @ttl, [@fqdn], zone)
|
||
resp = new_record.create
|
||
raise "AWS Response Error: #{resp}" if resp.error?
|
||
end
|
||
end
|
||
end
|
||
# remove({ :fqdn => "node01.lab", :value => "192.168.100.2"}
|
||
def remove
|
||
conn = Route53::Connection.new(SETTINGS.dns_aws_accesskey,SETTINGS.dns_aws_secretkey)
|
||
case @type
|
||
when "A"
|
||
domain = @fqdn.split('.',2).last + '.'
|
||
zone = conn.get_zones(name=domain)[0]
|
||
recordset = zone.get_records
|
||
recordset.each do |rec|
|
||
if rec.name == @fqdn + '.'
|
||
resp = rec.delete
|
||
raise "AWS Response Error: #{resp}" if resp.error?
|
||
return
|
||
end
|
||
end
|
||
when "PTR"
|
||
domain = @value.split('.',2).last + '.'
|
||
zone = conn.get_zones(name=domain)[0]
|
||
recordset = zone.get_records
|
||
recordset.each do |rec|
|
||
if rec.name == @value + '.'
|
||
resp = rec.delete
|
||
raise "AWS Response Error: #{resp}" if resp.error?
|
||
return
|
||
end
|
||
end
|
||
end
|
||
end
|
||
private
|
||
def dns_find key
|
||
if match = key.match(/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/)
|
||
resolver.getname(match[1..4].reverse.join(".")).to_s
|
||
else
|
||
resolver.getaddress(key).to_s
|
||
end
|
||
rescue Resolv::ResolvError
|
||
false
|
||
end
|
||
end
|
||
end
|