lxc-config/hooks/update-hetzner-rdns

36 lines
1006 B
Ruby
Executable File

#!/usr/bin/env ruby
require "netaddr"
require_relative "lib/lxc"
def update_hetzner_rdns6(user, password, domain, network)
api = Lxc::Hetzner.new(user, password)
rdns = api.get("/rdns")
records = {}
rdns.each do |val|
rec = val["rdns"]
cidr = NetAddr::CIDR.create(rec["ip"])
next if cidr.version == 4
records[cidr.ip] = rec["ptr"]
end
network.each do |host, data|
cidr = data["ipv6"]
next if cidr.nil?
ipv6 = NetAddr::CIDR.create(cidr).ip
hostname = data["rdns6"] || "#{host}.#{domain}"
ptr = records.delete(ipv6)
if ptr.nil? or ptr != hostname
api.post("/rdns/#{ipv6}", ptr: hostname)
end
end
records.each do |ip, ptr|
api.delete("/rnds/#{ip}")
end
end
credentials = File.read(Lxc::CONFIG_ROOT.join("hetzner.key"))
user, password = credentials.split(":")
registry = Lxc::Registry.new
registry.data["zone"] ||= {}
domain = registry.data["zone"]["domain"]
update_hetzner_rdns6(user, password, domain, registry.data["network"] || {})