2015-03-30 22:02:32 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
require "resolv"
|
|
|
|
require_relative "lib/lxc"
|
|
|
|
|
|
|
|
class Resolver
|
|
|
|
def initialize
|
|
|
|
@stub_resolver = Resolv::DNS.new
|
|
|
|
@cache = {}
|
|
|
|
end
|
|
|
|
def resolve(name, delegated_subdomain, typeclass)
|
|
|
|
if name == "@"
|
|
|
|
fqdn = delegated_subdomain
|
|
|
|
else
|
|
|
|
fqdn = "#{name}.#{delegated_subdomain}"
|
|
|
|
end
|
|
|
|
result = @cache[fqdn + typeclass.to_s] ||= @stub_resolver.getresource(fqdn, typeclass)
|
|
|
|
if result == :no_record
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
rescue Resolv::ResolvError => e
|
|
|
|
puts "warning: #{e}"
|
|
|
|
@cache[fqdn + typeclass.to_s] = :no_record
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
def a(name, delegated_subdomain)
|
|
|
|
result = resolve(name, delegated_subdomain, Resolv::DNS::Resource::IN::A)
|
|
|
|
return "" unless result
|
|
|
|
"#{name} A #{result.address.to_s}\n"
|
|
|
|
end
|
|
|
|
def aaaa(name, delegated_subdomain)
|
|
|
|
result = resolve(name, delegated_subdomain, Resolv::DNS::Resource::IN::AAAA)
|
|
|
|
return "" unless result
|
|
|
|
"#{name} AAAA #{result.address.to_s}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-11 21:32:26 +00:00
|
|
|
template_path = Lxc::CONFIG_ROOT.join("hooks/templates/higgsboson.tk.zone.erb")
|
2015-03-30 22:02:32 +00:00
|
|
|
template = Lxc::Template.new(template_path)
|
|
|
|
serial = Time.new.to_i
|
|
|
|
resolver = Resolver.new
|
|
|
|
zones = [
|
2015-08-11 21:39:00 +00:00
|
|
|
[Lxc::CONFIG_ROOT.join("zones/higgsboson.tk.zone"), :pub],
|
|
|
|
[Lxc::CONFIG_ROOT.join("zones/internal-eve.higgsboson.tk.zone"), :eve],
|
|
|
|
[Lxc::CONFIG_ROOT.join("zones/internal-eva.higgsboson.tk.zone"), :eva],
|
2015-03-30 22:02:32 +00:00
|
|
|
]
|
|
|
|
zones.each do |zone, type|
|
|
|
|
template.write(zone, resolver: resolver, serial: serial, type: type)
|
|
|
|
end
|
|
|
|
Lxc::Utils.sh("lxc-attach", "-n", "dns", "--", "rndc", "reload")
|