dn42-scripts/scripts/dns

57 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env ruby
2015-01-13 07:19:32 +00:00
require_relative "utils"
2015-01-26 07:20:08 +00:00
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)
2015-01-26 09:05:29 +00:00
atomic_write(rdns_path, zone_template.render(zone: self, data: data))
2015-01-26 07:20:08 +00:00
end
end
2015-01-13 07:19:32 +00:00
registry = Registry.new
2015-01-26 09:05:29 +00:00
registry.data["zone"]["serial"] += 1
registry.save
2015-01-26 07:20:08 +00:00
root_path = Pathname.new(File.expand_path("../..", __FILE__))
zone_template = Template.new(root_path.join("templates/dn42-zone.erb"))
2015-01-26 09:05:29 +00:00
zone = registry.data["zone"] || {}
result = zone_template.render(data: registry.data, zone: zone)
2015-01-26 07:20:08 +00:00
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