From 0eca58ffa8f29670f1bba8486da941db14a02534 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 3 Jul 2024 16:03:59 +0200 Subject: [PATCH] PowerDNS: script to sync stuff. --- Makefile | 6 +- shard.yml | 2 + tools/powerdns-sync.cr | 123 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 tools/powerdns-sync.cr diff --git a/Makefile b/Makefile index 3358784..2e97fde 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,10 @@ build-write-template-zone-file: bin/write-template-zone-file zone-basic-template-file: build-write-template-zone-file $(Q)./bin/write-template-zone-file $(DOMAIN) +bin/powerdns-sync: tools/powerdns-sync.cr + $(Q)-shards build powerdns-sync $(OPTS) +build-powerdns-sync: bin/powerdns-sync + VERBOSITY ?= 4 run-client-verbosity:; $(Q)./bin/dnsmanager-client admin maintenance verbosity $(VERBOSITY) $(LOGIN_OPT) run-client-domain-add:; $(Q)./bin/dnsmanager-client user domain add $(DOMAIN) $(LOGIN_OPT) @@ -65,7 +69,7 @@ bin/token-handler: tools/token-handler.cr; $(Q)shards build token-handler $(OPTS build-token-handler: bin/token-handler run-token-handler: bin/token-handler; $(Q)./bin/token-handler $(PORT) $(ADDR) -build: build-server build-client build-token-handler +build: build-server build-client build-token-handler build-powerdns-sync print-messages:; cat src/requests/*.cr | ./bin/get-messages.awk print-message-numbers:; make -s print-messages | grep -E "^[0-9]" | sort -n diff --git a/shard.yml b/shard.yml index 25e65ed..49ee27b 100644 --- a/shard.yml +++ b/shard.yml @@ -33,5 +33,7 @@ targets: main: tools/write-template-zone-file.cr token-handler: main: tools/token-handler.cr + powerdns-sync: + main: tools/powerdns-sync.cr license: ISC diff --git a/tools/powerdns-sync.cr b/tools/powerdns-sync.cr new file mode 100644 index 0000000..d586a2e --- /dev/null +++ b/tools/powerdns-sync.cr @@ -0,0 +1,123 @@ +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_file(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 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" => "/" }, + 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}" + end +end + +def update_domain(domain : String) : Nil + puts "domain to reload: #{domain}" + copy_file 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 + [ "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 +end + +def add_domain(domain : String) : Nil + puts "domain to add: #{domain}" + copy_file 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) + +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}" + i2 = File.info "#{Context.powerdns_dir}/#{d}" + + if i1.modification_time > i2.modification_time + puts "has been modified: #{d}" + update_domain d + 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_delete = powerdns_dir_content - dnsmanagerd_dir_content +to_delete.each do |d| + del_domain d +end