]> granicus.if.org Git - icinga2/commitdiff
Update NodeName/ZoneName constants with 'api setup' 5811/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 28 Nov 2017 12:38:53 +0000 (13:38 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 28 Nov 2017 12:38:53 +0000 (13:38 +0100)
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
lib/cli/apisetuputility.hpp
lib/cli/nodesetupcommand.cpp
lib/cli/nodeutility.cpp
lib/cli/nodeutility.hpp
lib/cli/nodewizardcommand.cpp

index b658f9339937691b0cc892ebe9989da0a48664af..e4865e374b0d3beed487a955b61919050ea72f01 100644 (file)
@@ -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);
+}
index 605af2a8d72a29cac60c1fc1bd354800e9c8c985..a31fe39bc810c1d88567913ba2237bd328983c57 100644 (file)
@@ -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);
 
index 4895898dfcb6ba3483192a9921d7ab0482cbd716..c4952aeaf9d43bd91f9d67c141a401833d62a175 100644 (file)
@@ -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<std::string>());
 
index 945ef0d3bf2680397cf0fa60be91e972ec770b20..b4b1e63962785b97fb724dc89ea7ca02a87bb229 100644 (file)
 
 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,17 +288,19 @@ 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";
+       String constantsConfPath = NodeUtility::GetConstantsConfPath();
 
-       std::ifstream ifp(constantsFile.CStr());
+       Log(LogInformation, "cli")
+           << "Updating '" << name << "' constant in '" << constantsConfPath << "'.";
+
+       NodeUtility::CreateBackupFile(constantsConfPath);
+
+       std::ifstream ifp(constantsConfPath.CStr());
        std::fstream ofp;
-       String tempFile = Utility::CreateTempFile(constantsFile + ".XXXXXX", 0644, ofp);
+       String tempFile = Utility::CreateTempFile(constantsConfPath + ".XXXXXX", 0644, ofp);
 
        bool found = false;
 
-       Log(LogInformation, "cli")
-           << "Updating constants file '" << constantsFile << "'.";
-
        std::string line;
        while (std::getline(ifp, line)) {
                if (line.find("const " + name + " = ") != std::string::npos) {
@@ -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));
        }
 }
index be345d3b81a84ab90cf22dce4a5f51a96b485dbe..eaf30b008ecb3fdecc8ca602ef9f3480c19d9c6f 100644 (file)
@@ -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);
index 5d7b630b7961291ab8810709d743a0fcb5244035..8bad0ffdbcd54c01867f3c92e5028912e1ea9082 100644 (file)
@@ -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);