dn42-scripts/scripts/dns

57 lines
1.5 KiB
Ruby
Executable File

#!/usr/bin/env ruby
require_relative "utils"
require "netaddr"
class RdnsZone
def initialize(data, subnet)
@data = data
@subnet = NetAddr::CIDR.create(subnet)
end
attr_reader :data
def [](key)
(data["zone"] || {})[key]
end
def pointers(&blk)
version = @subnet.version
@data["network"].each do |name, host|
ip = host["ipv#{version}"]
next unless ip
arpa = NetAddr::CIDR.create(ip).arpa
next unless arpa.end_with?(@subnet.arpa)
host_part = arpa[0, arpa.size - @subnet.arpa.size - 1]
yield name, host_part
end
end
def name
@subnet.arpa.gsub(/\.$/, "")
end
def write_zone_file(root_path)
zone_template = Template.new(root_path.join("templates/rdns-zone.erb"))
rdns_path = root_path.join("zones", name)
atomic_write(rdns_path, zone_template.render(zone: self, data: data))
end
end
registry = Registry.new
registry.data["zone"]["serial"] += 1
registry.save
root_path = Pathname.new(File.expand_path("../..", __FILE__))
zone_template = Template.new(root_path.join("templates/dn42-zone.erb"))
zone = registry.data["zone"] || {}
result = zone_template.render(data: registry.data, zone: zone)
atomic_write(root_path.join("zones/dn42.zone"), result)
if subnet = try(registry.data, "zone", "v4_subnet")
RdnsZone.new(registry.data, subnet).write_zone_file(root_path)
end
if subnet = try(registry.data, "zone", "v6_subnet")
RdnsZone.new(registry.data, subnet).write_zone_file(root_path)
end