From 7d546aa2d87671187dda66737a6bcf3a5391e3eb Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Tue, 17 Nov 2020 09:31:45 +0100 Subject: [PATCH] ApiListener: merge new config validation and actication functions Merge AsyncTryActivateZonesStage and TryActivateZonesStageCallback and name the result TryActivateZonesStage. The old split was a leftover from the one being a callback function with no actual meaningful separation. --- lib/remote/apilistener-filesync.cpp | 81 +++++++++++++---------------- lib/remote/apilistener.hpp | 4 +- 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/lib/remote/apilistener-filesync.cpp b/lib/remote/apilistener-filesync.cpp index f2160a6ed..ecac5a112 100644 --- a/lib/remote/apilistener-filesync.cpp +++ b/lib/remote/apilistener-filesync.cpp @@ -544,7 +544,7 @@ void ApiListener::HandleConfigUpdate(const MessageOrigin::Ptr& origin, const Dic Log(LogInformation, "ApiListener") << "Received configuration updates (" << count << ") from endpoint '" << fromEndpointName << "' are different to production, triggering validation and reload."; - AsyncTryActivateZonesStage(relativePaths); + TryActivateZonesStage(relativePaths); } else { Log(LogInformation, "ApiListener") << "Received configuration updates (" << count << ") from endpoint '" << fromEndpointName @@ -554,17 +554,44 @@ void ApiListener::HandleConfigUpdate(const MessageOrigin::Ptr& origin, const Dic } /** - * Callback for stage config validation. - * When validation was successful, the configuration is copied from - * stage to production and a restart is triggered. - * On failure, there's no restart and this is logged. + * Spawns a new validation process with 'System.ZonesStageVarDir' set to override the config validation zone dirs with + * our current stage. Then waits for the validation result and if it was successful, the configuration is copied from + * stage to production and a restart is triggered. On validation failure, there is no restart and this is logged. + * + * The caller of this function must hold m_ConfigSyncStageLock. * - * @param pr Result of the validation process. * @param relativePaths Collected paths including the zone name, which are copied from stage to current directories. */ -void ApiListener::TryActivateZonesStageCallback(const ProcessResult& pr, - const std::vector& relativePaths) +void ApiListener::TryActivateZonesStage(const std::vector& relativePaths) { + VERIFY(Application::GetArgC() >= 1); + + /* Inherit parent process args. */ + Array::Ptr args = new Array({ + Application::GetExePath(Application::GetArgV()[0]), + }); + + for (int i = 1; i < Application::GetArgC(); i++) { + String argV = Application::GetArgV()[i]; + + if (argV == "-d" || argV == "--daemonize") + continue; + + args->Add(argV); + } + + args->Add("--validate"); + + // Set the ZonesStageDir. This creates our own local chroot without any additional automated zone includes. + args->Add("--define"); + args->Add("System.ZonesStageVarDir=" + GetApiZonesStageDir()); + + Process::Ptr process = new Process(Process::PrepareCommand(args)); + process->SetTimeout(Application::GetReloadTimeout()); + + process->Run(); + const ProcessResult& pr = process->WaitForResult(); + String apiZonesDir = GetApiZonesDir(); String apiZonesStageDir = GetApiZonesStageDir(); @@ -628,44 +655,6 @@ void ApiListener::TryActivateZonesStageCallback(const ProcessResult& pr, listener->UpdateLastFailedZonesStageValidation(pr.Output); } -/** - * Spawns a new validation process and waits for its output. - * Sets 'System.ZonesStageVarDir' to override the config validation zone dirs with our current stage. - * - * @param relativePaths Required for later file operations in the callback. Provides the zone name plus path in a list. - */ -void ApiListener::AsyncTryActivateZonesStage(const std::vector& relativePaths) -{ - VERIFY(Application::GetArgC() >= 1); - - /* Inherit parent process args. */ - Array::Ptr args = new Array({ - Application::GetExePath(Application::GetArgV()[0]), - }); - - for (int i = 1; i < Application::GetArgC(); i++) { - String argV = Application::GetArgV()[i]; - - if (argV == "-d" || argV == "--daemonize") - continue; - - args->Add(argV); - } - - args->Add("--validate"); - - // Set the ZonesStageDir. This creates our own local chroot without any additional automated zone includes. - args->Add("--define"); - args->Add("System.ZonesStageVarDir=" + GetApiZonesStageDir()); - - Process::Ptr process = new Process(Process::PrepareCommand(args)); - process->SetTimeout(Application::GetReloadTimeout()); - - process->Run(); - const ProcessResult& pr = process->WaitForResult(); - TryActivateZonesStageCallback(pr, relativePaths); -} - /** * Update the structure from the last failed validation output. * Uses the current timestamp. diff --git a/lib/remote/apilistener.hpp b/lib/remote/apilistener.hpp index 541241960..3e2049dd7 100644 --- a/lib/remote/apilistener.hpp +++ b/lib/remote/apilistener.hpp @@ -199,9 +199,7 @@ private: static ConfigDirInformation LoadConfigDir(const String& dir); static void ConfigGlobHandler(ConfigDirInformation& config, const String& path, const String& file); - static void TryActivateZonesStageCallback(const ProcessResult& pr, - const std::vector& relativePaths); - static void AsyncTryActivateZonesStage(const std::vector& relativePaths); + static void TryActivateZonesStage(const std::vector& relativePaths); static String GetChecksum(const String& content); static bool CheckConfigChange(const ConfigDirInformation& oldConfig, const ConfigDirInformation& newConfig);