dyndnsd/lib/dyndnsd/generator/bind.rb

42 lines
1.1 KiB
Ruby
Raw Normal View History

2020-03-06 21:33:29 +01:00
# frozen_string_literal: true
2013-04-27 14:07:14 +02:00
module Dyndnsd
module Generator
class Bind
2020-02-28 15:13:28 +01:00
# @param domain [String]
2020-07-18 14:45:56 +02:00
# @param updater_params [Hash{String => Object}]
def initialize(domain, updater_params)
2013-04-27 15:08:36 +02:00
@domain = domain
2020-07-18 14:45:56 +02:00
@ttl = updater_params['ttl']
@dns = updater_params['dns']
@email_addr = updater_params['email_addr']
@additional_zone_content = updater_params['additional_zone_content']
2013-04-27 14:07:14 +02:00
end
2020-02-28 15:13:28 +01:00
# @param db [Dyndnsd::Database]
# @return [String]
def generate(db)
2013-04-27 14:07:14 +02:00
out = []
out << "$TTL #{@ttl}"
2013-04-27 15:08:36 +02:00
out << "$ORIGIN #{@domain}."
2018-02-23 12:54:43 +01:00
out << ''
out << "@ IN SOA #{@dns} #{@email_addr} ( #{db['serial']} 3h 5m 1w 1h )"
2013-04-27 14:07:14 +02:00
out << "@ IN NS #{@dns}"
2018-02-23 12:54:43 +01:00
out << ''
db['hosts'].each do |hostname, ips|
ips.each do |ip|
ip = IPAddr.new(ip).native
2018-02-23 12:54:43 +01:00
type = ip.ipv6? ? 'AAAA' : 'A'
name = hostname.chomp(".#{@domain}")
out << "#{name} IN #{type} #{ip}"
end
2013-04-27 14:07:14 +02:00
end
2018-02-23 12:54:43 +01:00
out << ''
out << @additional_zone_content
2018-02-23 12:54:43 +01:00
out << ''
2013-04-27 14:07:14 +02:00
out.join("\n")
end
end
end
end