lxc-config/hooks/update-hetzner-rdns

52 lines
1.5 KiB
Plaintext
Raw Normal View History

2015-01-03 16:55:03 +00:00
#!/usr/bin/env ruby
2015-01-30 08:08:18 +00:00
require "netaddr"
require "set"
2015-01-30 08:08:18 +00:00
require_relative "lib/lxc"
2015-01-03 16:55:03 +00:00
def update_hetzner_rdns6(user, password, domain, subnet, network)
2015-01-30 08:08:18 +00:00
api = Lxc::Hetzner.new(user, password)
2015-01-03 16:55:03 +00:00
rdns = api.get("/rdns")
records = {}
rdns.each do |val|
rec = val["rdns"]
cidr = NetAddr::CIDR.create(rec["ip"])
next unless cidr.version == 6 and subnet.contains?(rec["ip"])
records[rec["ip"]] = rec["ptr"]
2015-01-03 16:55:03 +00:00
end
processed_ips = Set.new
2015-01-30 08:08:18 +00:00
network.each do |host, data|
2015-01-03 16:55:03 +00:00
cidr = data["ipv6"]
next if cidr.nil?
cidr = NetAddr::CIDR.create(cidr)
next unless cidr.version == 6 and subnet.contains?(data["ipv6"])
2015-01-30 08:08:18 +00:00
hostname = data["rdns6"] || "#{host}.#{domain}"
ip = cidr.ip(Short: true)
next if processed_ips.include?(ip)
processed_ips << ip
ptr = records.delete(ip)
2015-01-03 16:55:03 +00:00
if ptr.nil? or ptr != hostname
puts "add ptr: #{ip} -> #{hostname}"
api.post("/rdns/#{ip}", ptr: hostname)
2015-01-03 16:55:03 +00:00
end
end
records.each do |ip, ptr|
puts "delete ptr: #{ip} -> #{ptr}"
2015-11-29 18:37:39 +00:00
api.delete("/rnds/#{ip}", allow_404: true)
2015-01-03 16:55:03 +00:00
end
end
2015-01-30 08:08:18 +00:00
registry = Lxc::Registry.new
registry.data["zone"] ||= {}
domain = registry.data["zone"]["domain"] || "lxc"
subnet = registry.data["zone"]["v6_subnet"]
if subnet
subnet_cidr = NetAddr::CIDR.create(subnet)
credentials = File.read(Lxc::CONFIG_ROOT.join("hetzner.key"))
user, password = credentials.split(":")
network = registry.data["network"] || {}
update_hetzner_rdns6(user, password, domain, subnet_cidr, network)
else
puts "no v6_subnet specified in container.json. skip rdns records"
end