From 96eb6056822e32e9e8e27d5f2fac920c3ba35d1c Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 15 Dec 2024 18:38:41 +0100 Subject: [PATCH] Graceful exit. --- src/client/lib/dnsmanager-client.cr | 5 +++++ src/client/main.cr | 7 +++++++ src/client/parser.cr | 8 ++++++++ src/requests/admin.cr | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/src/client/lib/dnsmanager-client.cr b/src/client/lib/dnsmanager-client.cr index 5c7a619..71ba8e9 100644 --- a/src/client/lib/dnsmanager-client.cr +++ b/src/client/lib/dnsmanager-client.cr @@ -181,6 +181,11 @@ class DNSManager::Client < IPC parse_message [ Response::Success ], read end + def admin_exit + request = Request::Exit.new + send_now request + end + # # Utils # diff --git a/src/client/main.cr b/src/client/main.cr index f63d6ae..1e58c6b 100644 --- a/src/client/main.cr +++ b/src/client/main.cr @@ -32,6 +32,7 @@ class Actions @the_call["admin-migration-script"] = ->admin_migration_script @the_call["admin-generate-all-zonefiles"] = ->admin_generate_all_zonefiles @the_call["admin-provide-domain"] = ->admin_provide_domain + @the_call["admin-exit"] = ->admin_exit # Domain operations. @the_call["user-domain-add"] = ->user_domain_add @@ -155,6 +156,12 @@ class Actions puts "error for provide_domain: #{e.message}" end + def admin_exit + @dnsmanagerd.admin_exit + rescue e + puts "error for provide_domain: #{e.message}" + end + def user_domain_add domains = Context.args.not_nil! domains.each do |domain| diff --git a/src/client/parser.cr b/src/client/parser.cr index 705e8a3..ca034e7 100644 --- a/src/client/parser.cr +++ b/src/client/parser.cr @@ -131,6 +131,14 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth) parser.banner = "COMMAND: admin migration-script user-db.txt" unrecognized_args_to_context_args.call parser, 1, nil end + + # Kill the service. + parser.on("exit", "Kill the service.") do + Baguette::Log.info "kill the service." + Context.command = "admin-exit" + parser.banner = "COMMAND: exit" + unrecognized_args_to_context_args.call parser, nil, 0 + end end # User section. diff --git a/src/requests/admin.cr b/src/requests/admin.cr index 6cf0e0a..0710259 100644 --- a/src/requests/admin.cr +++ b/src/requests/admin.cr @@ -91,4 +91,23 @@ class DNSManager::Request end end DNSManager.requests << GenerateZoneFile + + IPC::JSON.message Exit, 248 do + def initialize + end + + def to_s(io : IO) + super io + end + + def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON + user = dnsmanagerd.get_logged_user event + return Response::ErrorUserNotLogged.new unless user + dnsmanagerd.storage.user_must_be_admin! user.uid + + Baguette::Log.warning "exit requested, bye" + exit 0 + end + end + DNSManager.requests << Exit end