98 lines
2.8 KiB
Crystal
98 lines
2.8 KiB
Crystal
if ARGV.size != 2
|
|
puts "usage: #{PROGRAM_NAME} dnsmanagerd-bind9-dir powerdns-bind9-dir"
|
|
exit 0
|
|
end
|
|
|
|
class Context
|
|
class_property dnsmanagerd_dir : String = ""
|
|
class_property powerdns_dir : String = ""
|
|
end
|
|
|
|
def copy_domain_files(domain : String) : Nil
|
|
src = "#{Context.dnsmanagerd_dir}/#{domain}"
|
|
dest = "#{Context.powerdns_dir}/#{domain}"
|
|
puts "copying #{src} -> #{dest}"
|
|
i = File.info src
|
|
File.copy src, dest
|
|
rescue e : File::AccessDeniedError
|
|
puts "You don't have enough rights: #{e}"
|
|
end
|
|
|
|
def run_process(cmd : String, params : Array(String), env : Hash(String, String)) : Nil
|
|
unless Process.run(cmd, params, env,
|
|
true # clear environment
|
|
# input: Process::Redirect::Inherit,
|
|
# output: Process::Redirect::Inherit,
|
|
# error: Process::Redirect::Inherit
|
|
).success?
|
|
puts "cannot run #{cmd} #{params.join(' ')}"
|
|
end
|
|
end
|
|
|
|
def pdns_reload(domain : String) : Nil
|
|
puts "reloading a domain: pdns_control bind-reload-now #{domain}"
|
|
run_process("pdns_control", [ "bind-reload-now", domain ], { "HOME" => "/" })
|
|
end
|
|
|
|
def update_domain(domain : String) : Nil
|
|
puts "domain to reload: #{domain}"
|
|
copy_domain_files domain
|
|
pdns_reload domain
|
|
end
|
|
|
|
def pdns_add(domain : String) : Nil
|
|
puts "adding a new domain: pdns_control bind-add-zone #{Context.powerdns_dir}/#{domain}"
|
|
run_process("pdns_control",
|
|
[ "bind-add-zone", domain, "#{Context.powerdns_dir}/#{domain}" ],
|
|
{ "HOME" => "/" })
|
|
end
|
|
|
|
def add_domain(domain : String) : Nil
|
|
puts "domain to add: #{domain}"
|
|
copy_domain_files domain
|
|
pdns_add domain
|
|
end
|
|
|
|
def delete_file(path : String)
|
|
File.delete path
|
|
rescue e : File::AccessDeniedError
|
|
puts "You don't have enough rights: #{e}"
|
|
end
|
|
|
|
def del_domain(domain : String) : Nil
|
|
puts "domain to delete: #{domain}"
|
|
delete_file "#{Context.powerdns_dir}/#{domain}"
|
|
# TODO: pdns_control ???
|
|
end
|
|
|
|
Context.dnsmanagerd_dir = ARGV[0]
|
|
Context.powerdns_dir = ARGV[1]
|
|
|
|
dnsmanagerd_dir_content = Dir.children(Context.dnsmanagerd_dir).select { |d| ! d.ends_with? ".wip" }
|
|
powerdns_dir_content = Dir.children(Context.powerdns_dir)
|
|
|
|
both = dnsmanagerd_dir_content & powerdns_dir_content
|
|
both.each do |d|
|
|
i1 = File.info "#{Context.dnsmanagerd_dir}/#{d}"
|
|
i2 = File.info "#{Context.powerdns_dir}/#{d}"
|
|
|
|
if i1.modification_time > i2.modification_time
|
|
puts "has been modified: #{d}"
|
|
# Wait for a few minutes before changing anything, to avoid useless reloads.
|
|
if Time.local > i1.modification_time.shift minutes: 5
|
|
puts "file was modified more than 5 minutes ago"
|
|
update_domain d
|
|
else
|
|
puts "file has been modified less than 5 minutes ago: do not update yet"
|
|
end
|
|
else
|
|
puts "hasn't been modified: #{d}"
|
|
end
|
|
end
|
|
|
|
to_add = dnsmanagerd_dir_content - powerdns_dir_content
|
|
to_add.each { |d| add_domain d }
|
|
|
|
to_delete = powerdns_dir_content - dnsmanagerd_dir_content
|
|
to_delete.each { |d| del_domain d }
|