52 lines
1.5 KiB
Ruby
Executable File
52 lines
1.5 KiB
Ruby
Executable File
#!/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
|
|
|
|
template_path = Lxc::CONFIG_ROOT.join("hooks/templates/higgsboson.tk.zone.erb")
|
|
template = Lxc::Template.new(template_path)
|
|
serial = Time.new.to_i
|
|
resolver = Resolver.new
|
|
zones = [
|
|
["zones/higgsboson.tk.zone", :pub],
|
|
["zones/internal-eve.higgsboson.tk.zone", :eve],
|
|
["zones/internal-eva.higgsboson.tk.zone", :eva],
|
|
]
|
|
zones.each do |zone, type|
|
|
template.write(zone, resolver: resolver, serial: serial, type: type)
|
|
end
|
|
Lxc::Utils.sh("lxc-attach", "-n", "dns", "--", "rndc", "reload")
|