From f2671357110d802cf6e3de11af564a67f7078b07 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 28 Nov 2017 13:38:53 +0100 Subject: [PATCH] Update NodeName/ZoneName constants with 'api setup' This commit also moves the constants.conf backup logic into NodeUtility::UpdateConstant() where it belongs. Logging has been slightly adopted too. fixes #5763 --- lib/cli/apisetuputility.cpp | 9 +++++++++ lib/cli/apisetuputility.hpp | 1 + lib/cli/nodesetupcommand.cpp | 8 -------- lib/cli/nodeutility.cpp | 31 +++++++++++++++++++------------ lib/cli/nodeutility.hpp | 2 ++ lib/cli/nodewizardcommand.cpp | 12 ------------ 6 files changed, 31 insertions(+), 32 deletions(-) diff --git a/lib/cli/apisetuputility.cpp b/lib/cli/apisetuputility.cpp index b658f9339..e4865e374 100644 --- a/lib/cli/apisetuputility.cpp +++ b/lib/cli/apisetuputility.cpp @@ -54,6 +54,9 @@ bool ApiSetupUtility::SetupMaster(const String& cn, bool prompt_restart) if (!SetupMasterEnableApi()) return false; + if (!SetupMasterUpdateConstants(cn)) + return false; + if (prompt_restart) { std::cout << "Done.\n\n"; std::cout << "Now restart your Icinga 2 daemon to finish the installation!\n\n"; @@ -207,3 +210,9 @@ bool ApiSetupUtility::SetupMasterEnableApi(void) return true; } + +bool ApiSetupUtility::SetupMasterUpdateConstants(const String& cn) +{ + NodeUtility::UpdateConstant("NodeName", cn); + NodeUtility::UpdateConstant("ZoneName", cn); +} diff --git a/lib/cli/apisetuputility.hpp b/lib/cli/apisetuputility.hpp index 605af2a8d..a31fe39bc 100644 --- a/lib/cli/apisetuputility.hpp +++ b/lib/cli/apisetuputility.hpp @@ -42,6 +42,7 @@ public: static bool SetupMasterCertificates(const String& cn); static bool SetupMasterApiUser(void); static bool SetupMasterEnableApi(void); + static bool SetupMasterUpdateConstants(const String& cn); static String GetConfdPath(void); diff --git a/lib/cli/nodesetupcommand.cpp b/lib/cli/nodesetupcommand.cpp index 4895898df..c4952aeaf 100644 --- a/lib/cli/nodesetupcommand.cpp +++ b/lib/cli/nodesetupcommand.cpp @@ -213,10 +213,6 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v << "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!"; } - Log(LogInformation, "cli", "Updating constants.conf."); - - NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf"); - NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("ZoneName", cn); @@ -440,10 +436,6 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm, << "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!"; } - Log(LogInformation, "cli", "Updating constants.conf."); - - NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf"); - NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("ZoneName", vm["zone"].as()); diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp index 945ef0d3b..b4b1e6396 100644 --- a/lib/cli/nodeutility.cpp +++ b/lib/cli/nodeutility.cpp @@ -43,6 +43,11 @@ using namespace icinga; +String NodeUtility::GetConstantsConfPath(void) +{ + return Application::GetSysconfDir() + "/icinga2/constants.conf"; +} + /* * Node Setup helpers */ @@ -240,7 +245,7 @@ bool NodeUtility::CreateBackupFile(const String& target, bool is_private) String backup = target + ".orig"; if (Utility::PathExists(backup)) { - Log(LogWarning, "cli") + Log(LogInformation, "cli") << "Backup file '" << backup << "' already exists. Skipping backup."; return false; } @@ -283,16 +288,18 @@ void NodeUtility::SerializeObject(std::ostream& fp, const Dictionary::Ptr& objec void NodeUtility::UpdateConstant(const String& name, const String& value) { - String constantsFile = Application::GetSysconfDir() + "/icinga2/constants.conf"; - - std::ifstream ifp(constantsFile.CStr()); - std::fstream ofp; - String tempFile = Utility::CreateTempFile(constantsFile + ".XXXXXX", 0644, ofp); - - bool found = false; + String constantsConfPath = NodeUtility::GetConstantsConfPath(); Log(LogInformation, "cli") - << "Updating constants file '" << constantsFile << "'."; + << "Updating '" << name << "' constant in '" << constantsConfPath << "'."; + + NodeUtility::CreateBackupFile(constantsConfPath); + + std::ifstream ifp(constantsConfPath.CStr()); + std::fstream ofp; + String tempFile = Utility::CreateTempFile(constantsConfPath + ".XXXXXX", 0644, ofp); + + bool found = false; std::string line; while (std::getline(ifp, line)) { @@ -310,13 +317,13 @@ void NodeUtility::UpdateConstant(const String& name, const String& value) ofp.close(); #ifdef _WIN32 - _unlink(constantsFile.CStr()); + _unlink(constantsConfPath.CStr()); #endif /* _WIN32 */ - if (rename(tempFile.CStr(), constantsFile.CStr()) < 0) { + if (rename(tempFile.CStr(), constantsConfPath.CStr()) < 0) { BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("rename") << boost::errinfo_errno(errno) - << boost::errinfo_file_name(constantsFile)); + << boost::errinfo_file_name(constantsConfPath)); } } diff --git a/lib/cli/nodeutility.hpp b/lib/cli/nodeutility.hpp index be345d3b8..eaf30b008 100644 --- a/lib/cli/nodeutility.hpp +++ b/lib/cli/nodeutility.hpp @@ -37,6 +37,8 @@ namespace icinga class I2_CLI_API NodeUtility { public: + static String GetConstantsConfPath(void); + static bool CreateBackupFile(const String& target, bool is_private = false); static bool WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects); diff --git a/lib/cli/nodewizardcommand.cpp b/lib/cli/nodewizardcommand.cpp index 5d7b630b7..8bad0ffdb 100644 --- a/lib/cli/nodewizardcommand.cpp +++ b/lib/cli/nodewizardcommand.cpp @@ -516,12 +516,6 @@ wizard_ticket: << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!"; } - Log(LogInformation, "cli", "Updating constants.conf."); - - String constants_file = Application::GetSysconfDir() + "/icinga2/constants.conf"; - - NodeUtility::CreateBackupFile(constants_file); - NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("ZoneName", cn); @@ -674,12 +668,6 @@ int NodeWizardCommand::MasterSetup(void) const << Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!"; } - Log(LogInformation, "cli", "Updating constants.conf."); - - String constants_file = Application::GetSysconfDir() + "/icinga2/constants.conf"; - - NodeUtility::CreateBackupFile(constants_file); - NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("ZoneName", cn);