From b633dbe74065198a790d04e4ff42b8f245d0544e Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Thu, 4 Jul 2024 14:10:14 +0200 Subject: [PATCH] Powerdns-sync: wait for a few minutes before updating the zone. --- tools/powerdns-sync.cr | 72 ++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 49 deletions(-) diff --git a/tools/powerdns-sync.cr b/tools/powerdns-sync.cr index d586a2e..9671849 100644 --- a/tools/powerdns-sync.cr +++ b/tools/powerdns-sync.cr @@ -8,7 +8,7 @@ class Context class_property powerdns_dir : String = "" end -def copy_file(domain : String) : Nil +def copy_domain_files(domain : String) : Nil src = "#{Context.dnsmanagerd_dir}/#{domain}" dest = "#{Context.powerdns_dir}/#{domain}" puts "copying #{src} -> #{dest}" @@ -18,47 +18,38 @@ rescue e : File::AccessDeniedError puts "You don't have enough rights: #{e}" end -def pdns_reload(domain : String) : Nil - puts "reloading a domain: pdns_control bind-reload-now #{domain}" - unless Process.run("pdns_control", - # PARAMETERS - [ "bind-reload-now", domain ], - # ENV - { "HOME" => "/" }, +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 pdns_control bind-reload-now #{domain}" + ).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_file 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}" - unless Process.run("pdns_control", - # PARAMETERS + run_process("pdns_control", [ "bind-add-zone", domain, "#{Context.powerdns_dir}/#{domain}" ], - # ENV - { "HOME" => "/" }, - true # clear environment - # input: Process::Redirect::Inherit, - # output: Process::Redirect::Inherit, - # error: Process::Redirect::Inherit - ).success? - puts "cannot run pdns_control bind-add-zone #{Context.powerdns_dir}/#{domain}" - end + { "HOME" => "/" }) end def add_domain(domain : String) : Nil puts "domain to add: #{domain}" - copy_file domain + copy_domain_files domain pdns_add domain end @@ -80,25 +71,6 @@ 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) -if dnsmanagerd_dir_content.size < 1 - puts "There is no entries in the dnsmanagerd bind9 directory" - puts "Assuming a configuration error" - exit 1 -end - -if powerdns_dir_content.size < 1 - puts "There is no entries in the powerdns bind9 directory" - puts "Assuming a configuration error" - exit 2 -end - -#dnsmanagerd_dir_content.each do |d| -# puts "dnsmanagerd_dir_content: #{d}" -#end -#powerdns_dir_content.each do |d| -# puts "powerdns_dir_content: #{d}" -#end - both = dnsmanagerd_dir_content & powerdns_dir_content both.each do |d| i1 = File.info "#{Context.dnsmanagerd_dir}/#{d}" @@ -106,18 +78,20 @@ both.each do |d| if i1.modification_time > i2.modification_time puts "has been modified: #{d}" - update_domain 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 do |d| - add_domain d -end +to_add.each { |d| add_domain d } to_delete = powerdns_dir_content - dnsmanagerd_dir_content -to_delete.each do |d| - del_domain d -end +to_delete.each { |d| del_domain d }