lxc-config/hooks/lib/lxc/rdns.rb

39 lines
935 B
Ruby
Raw Normal View History

2015-01-30 08:08:18 +00:00
module Lxc
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]
2015-08-05 15:33:37 +00:00
# only allowed characters in FQDN
name = name.gsub(/[^a-zA-Z0-9\-]/, "-")
2015-01-30 08:08:18 +00:00
yield name, host_part
end
end
def name
@subnet.arpa.gsub(/\.$/, "")
end
def write_zone_file(path)
2015-08-11 21:32:26 +00:00
zone_template = Template.new(CONFIG_ROOT.join("hooks/templates/rdns-zone.erb"))
2015-01-30 08:08:18 +00:00
zone_template.write(path.join("zones", name), zone: self, data: data)
end
end
end