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

43 lines
1.1 KiB
Ruby
Raw Normal View History

2015-01-30 08:08:18 +00:00
module Lxc
class RdnsZone
2015-12-13 19:24:00 +00:00
def initialize(data, addr_field, subnet)
2015-01-30 08:08:18 +00:00
@data = data
2015-12-13 19:24:00 +00:00
@addr_field = addr_field
2015-01-30 08:08:18 +00:00
@subnet = NetAddr::CIDR.create(subnet)
end
attr_reader :data
def [](key)
(data["zone"] || {})[key]
end
def pointers(&blk)
@data["network"].each do |name, host|
2015-12-13 19:24:00 +00:00
ip = host[@addr_field]
2015-01-30 08:08:18 +00:00
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-12-13 19:24:00 +00:00
# <%= addr %> PTR <%= fqdn(name) %>.<%= data["zone"]["domain"] %>.
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-12-13 19:24:00 +00:00
domain = data["zone"]["#{@addr_field}-domain"]
zone_template.write(path.join("zones", name),
zone: self,
data: data,
domain: domain)
2015-01-30 08:08:18 +00:00
end
end
end