From 4606f852ccded6e0017f1a826acd4a454929317d Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 28 Apr 2024 12:56:45 +0200 Subject: [PATCH] Client: can be built again. Add a makefile rule to generate all zones. --- Makefile | 3 +++ src/client/parser.cr | 2 ++ src/service.cr | 5 +++++ src/storage.cr | 40 +++++++++++++++++++++++----------------- src/storage/userid.cr | 1 + 5 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 src/service.cr create mode 100644 src/storage/userid.cr diff --git a/Makefile b/Makefile index 86ff815..fd69515 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,9 @@ run-client-rr-update-a: run-client-rr-del: $(Q)$(LD_P) ./bin/dnsmanager-client user rr del $(DOMAIN) $(RRID) $(LOGIN_OPT) +run-admin-generate-all-zonefiles: + $(Q)$(LD_P) ./bin/dnsmanager-client admin genall $(LOGIN_OPT) + ################## ### SETUP COMMANDS ################## diff --git a/src/client/parser.cr b/src/client/parser.cr index 2c65de9..31f8df5 100644 --- a/src/client/parser.cr +++ b/src/client/parser.cr @@ -59,11 +59,13 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth) # either we test with the exact expected number of arguments or the least. if exact = nexact if args.size != exact + Baguette::Log.error "Wrong number of parameters: expected #{exact}, got #{args.size}" Baguette::Log.error "#{parser}" exit 1 end elsif least = at_least if args.size < least + Baguette::Log.error "Wrong number of parameters: expected at least #{least}, got #{args.size}" Baguette::Log.error "#{parser}" exit 1 end diff --git a/src/service.cr b/src/service.cr new file mode 100644 index 0000000..018071c --- /dev/null +++ b/src/service.cr @@ -0,0 +1,5 @@ +require "ipc" + +# Useful to enable the client to be built. +class DNSManager::Service < IPC +end diff --git a/src/storage.cr b/src/storage.cr index ed1948b..bd5dd03 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -2,11 +2,10 @@ require "json" require "uuid" require "uuid/json" require "baguette-crystal-base" +require "./service.cr" require "dodb" -alias UserDataID = UInt32 - class DNSManager::Storage getter domains : DODB::CachedDataBase(Domain) getter domains_by_name : DODB::Index(Domain) @@ -72,26 +71,33 @@ class DNSManager::Storage end end - # Only an admin can access this function. - def generate_all_zonefiles() : IPC::JSON - Baguette::Log.info "writing all zone files in #{@zonefiledir}/" - zones.each do |zone| - # TODO: safe write. - File.open("#{@zonefiledir}/#{zone.domain}", "w") do |file| - zone.to_bind9 file - end + def generate_zonefile_(domain : String) : Nil + zone = zone_must_exist! domain + + # Safe write. + filename_final = "#{@zonefiledir}/#{zone.domain}" + filename_wip = "#{filename_final}.wip" + Baguette::Log.info "writing zone file #{filename_final}" + + File.open(filename_wip, "w") do |file| + zone.to_bind9 file end - Response::Success.new + + # Rename WIP filename to final file name. + File.rename filename_wip, filename_final end # Only an admin can access this function. def generate_zonefile(domain : String) : IPC::JSON - zone = zone_must_exist! domain + generate_zonefile_ domain + Response::Success.new + end - Baguette::Log.info "writing zone file #{@zonefiledir}/#{zone.domain}" - # TODO: safe write. - File.open("#{@zonefiledir}/#{zone.domain}", "w") do |file| - zone.to_bind9 file + # Only an admin can access this function. + def generate_all_zonefiles() : IPC::JSON + Baguette::Log.info "writing all zone files in #{@zonefiledir}/" + zones.each do |zone| + generate_zonefile_ zone.domain end Response::Success.new end @@ -195,7 +201,7 @@ class DNSManager::Storage @domains_by_name.update_or_create domain_name, domain Response::DomainChanged.new domain else - Response::Error.new "You are't the only owner." + Response::Error.new "You are not the only owner." end end diff --git a/src/storage/userid.cr b/src/storage/userid.cr new file mode 100644 index 0000000..dc9a4b7 --- /dev/null +++ b/src/storage/userid.cr @@ -0,0 +1 @@ +alias UserDataID = UInt32