2014-08-18 08:29:43 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
require 'json'
|
|
|
|
require 'erb'
|
|
|
|
require 'netaddr'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'pathname'
|
2015-01-18 18:22:19 +00:00
|
|
|
require 'ostruct'
|
2014-08-18 08:29:43 +00:00
|
|
|
|
|
|
|
LXC_ROOT = Pathname.new("/etc/lxc")
|
|
|
|
ZONE_PATH = LXC_ROOT.join("zones")
|
|
|
|
TEMPLATE_PATH = LXC_ROOT.join("templates")
|
|
|
|
CONTAINER_DATA = LXC_ROOT.join("container.json")
|
2015-01-18 18:22:19 +00:00
|
|
|
LXC_ZONE = ZONE_PATH.join("eve.higgsboson.tk.zone")
|
2014-08-18 08:29:43 +00:00
|
|
|
DNS_CONTAINER = "dns"
|
|
|
|
|
|
|
|
def atomic_write(path, content)
|
2015-01-28 21:22:30 +00:00
|
|
|
dir = File.dirname(path)
|
|
|
|
unless Dir.exist?(dir)
|
|
|
|
FileUtils.mkdir_p(dir)
|
|
|
|
end
|
2014-08-18 08:29:43 +00:00
|
|
|
temp_path = path.to_s + ".tmp"
|
|
|
|
File.open(temp_path, 'w+') do |f|
|
|
|
|
f.write(content)
|
|
|
|
end
|
|
|
|
|
|
|
|
FileUtils.mv(temp_path, path)
|
|
|
|
end
|
|
|
|
|
2015-01-18 18:22:19 +00:00
|
|
|
class ZoneData < OpenStruct
|
2014-08-18 08:29:43 +00:00
|
|
|
def get_binding
|
|
|
|
binding
|
|
|
|
end
|
|
|
|
|
|
|
|
def ip(subnet)
|
|
|
|
NetAddr::CIDR.create(subnet).ip(Short: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pointers(&block)
|
2015-01-18 18:22:19 +00:00
|
|
|
subnet_arpa = subnet.arpa
|
|
|
|
version = subnet.version
|
2014-08-18 08:29:43 +00:00
|
|
|
|
|
|
|
data["network"].each do |name, data|
|
|
|
|
next unless data["ipv#{version}"]
|
|
|
|
arpa = NetAddr::CIDR.create(data["ipv#{version}"]).arpa
|
|
|
|
addr = arpa[0, arpa.size - subnet_arpa.size - 1]
|
|
|
|
yield addr, name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-18 18:22:19 +00:00
|
|
|
def reverse_zone(data, subnet)
|
|
|
|
subnet = NetAddr::CIDR.create(subnet)
|
|
|
|
zone_data = ZoneData.new(data: data, subnet: subnet).get_binding
|
|
|
|
rdns_zone_template = File.read(TEMPLATE_PATH.join("rdns-zone.erb"))
|
|
|
|
rdns_path = ZONE_PATH.join(subnet.arpa.gsub(/\.$/, ""))
|
|
|
|
template = ERB.new(rdns_zone_template, nil, '-').result(zone_data)
|
|
|
|
[rdns_path, template]
|
|
|
|
end
|
|
|
|
|
2015-01-03 16:57:39 +00:00
|
|
|
def main
|
|
|
|
json = JSON.load(File.open(CONTAINER_DATA))
|
|
|
|
json["zone"]["serial"] += 1
|
|
|
|
|
2015-01-18 18:22:19 +00:00
|
|
|
zone_data = ZoneData.new(data: json)
|
2014-08-18 08:29:43 +00:00
|
|
|
|
2015-01-03 16:57:39 +00:00
|
|
|
lxc_zone_template = File.read(TEMPLATE_PATH.join("lxc-zone.erb"))
|
|
|
|
zone = ERB.new(lxc_zone_template, nil, '-').result(zone_data.get_binding)
|
2014-08-18 08:29:43 +00:00
|
|
|
|
2015-01-03 16:57:39 +00:00
|
|
|
atomic_write(LXC_ZONE, zone)
|
2015-01-18 18:22:19 +00:00
|
|
|
atomic_write(*reverse_zone(json, json["zone"]["v4_subnet"]))
|
|
|
|
atomic_write(*reverse_zone(json, json["zone"]["v6_subnet"]))
|
2015-01-03 16:57:39 +00:00
|
|
|
atomic_write(CONTAINER_DATA, JSON.pretty_generate(json))
|
2014-08-18 08:29:43 +00:00
|
|
|
|
2015-01-18 18:24:23 +00:00
|
|
|
system("lxc-attach", "-n", DNS_CONTAINER, "--", "rndc", "reload")
|
2015-01-03 16:57:39 +00:00
|
|
|
end
|
2014-08-18 08:29:43 +00:00
|
|
|
|
2015-01-03 16:57:39 +00:00
|
|
|
main
|