]> granicus.if.org Git - icinga2/commitdiff
Allow to disable conf.d inclusion through node wizard/setup
authorMichael Insel <michael@email.de>
Wed, 18 Apr 2018 18:22:04 +0000 (20:22 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Tue, 8 May 2018 14:31:59 +0000 (16:31 +0200)
This implements a function to disable the conf.d directory through the node wizard/setup.

refs #4508

lib/cli/nodesetupcommand.cpp
lib/cli/nodeutility.cpp
lib/cli/nodeutility.hpp
lib/cli/nodewizardcommand.cpp

index 590b08faeb7ce6432238f2e4a1b583c982492f6e..260d0134958d941f33a292fda9ad908151192940 100644 (file)
@@ -66,7 +66,8 @@ void NodeSetupCommand::InitParameters(boost::program_options::options_descriptio
                ("accept-config", "Accept config from master")
                ("accept-commands", "Accept commands from master")
                ("master", "Use setup for a master instance")
-               ("global_zones", po::value<std::vector<std::string> >(), "The names of the additional global zones.");
+               ("global_zones", po::value<std::vector<std::string> >(), "The names of the additional global zones.")
+               ("dont-disable-confd", "Disables the conf.d directory during the setup");
 
        hiddenDesc.add_options()
                ("master_zone", po::value<std::string>(), "DEPRECATED: The name of the master zone")
@@ -244,8 +245,22 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
        Log(LogInformation, "cli")
                << "Edit the api feature config file '" << apipath << "' and set a secure 'ticket_salt' attribute.";
 
-       /* tell the user to reload icinga2 */
+       if (!vm.count("dont-disable-confd")) {
+               /* Disable conf.d inclusion */
+               NodeUtility::UpdateConfiguration("\"conf.d\"", false, true);
+
+               String apiUsersFilePath = Application::GetSysconfDir() + "/icinga2/conf.d/api-users.conf";
+               std::ifstream apiUsersFile(apiUsersFilePath);
 
+               /* Include api-users.conf */
+               if(apiUsersFile)
+                       NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false);
+               else
+                       Log(LogWarning, "cli")
+                               << "Included file dosen't exist " << apiUsersFilePath;
+       }
+
+       /* tell the user to reload icinga2 */
        Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
 
        return 0;
@@ -555,5 +570,22 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
                Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
        }
 
+       if (!vm.count("dont-disable-confd")) {
+
+               /* Disable conf.d inclusion */
+               NodeUtility::UpdateConfiguration("\"conf.d\"", false, true);
+
+               String apiUsersFilePath = Application::GetSysconfDir() + "/icinga2/conf.d/api-users.conf";
+               std::ifstream apiUsersFile(apiUsersFilePath);
+
+               if(apiUsersFile)
+                       NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false);
+               else
+                       Log(LogWarning, "cli", "Included file dosen't exist " + apiUsersFilePath);
+       }
+
+       /* tell the user to reload icinga2 */
+       Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
+
        return 0;
 }
index 645e19d947d1d951d68b935f248a870ef15780b8..ac58f939ddade6cd4452d01fe2c44f095045c87e 100644 (file)
@@ -265,6 +265,74 @@ void NodeUtility::SerializeObject(std::ostream& fp, const Dictionary::Ptr& objec
        fp << "}\n\n";
 }
 
+/*
+ * include = false, will comment out the include statement
+ * include = true, will add an include statement or uncomment a statement if one is existing
+ * resursive = false, will search for a non-resursive include statement
+ * recursive = true, will search for a resursive include statement
+ */
+void NodeUtility::UpdateConfiguration(const String& value, const bool& include, const bool& recursive)
+{
+       String configurationFile = Application::GetSysconfDir() + "/icinga2/icinga2.conf";
+
+       Log(LogInformation, "cli")
+               << "Updating' " << value << "' include in '" << configurationFile << "'.";
+
+       NodeUtility::CreateBackupFile(configurationFile);
+
+       std::ifstream ifp(configurationFile.CStr());
+       std::fstream ofp;
+       String tempFile = Utility::CreateTempFile(configurationFile + ".XXXXXX", 0644, ofp);
+
+       String affectedInclude = value;
+
+       recursive ? affectedInclude = "include_recursive " + affectedInclude : affectedInclude = "include " + affectedInclude;
+
+       bool found = false;
+
+       std::string line;
+
+       while (std::getline(ifp, line)) {
+               if(include) {
+                       if (line.find("//" + affectedInclude) != std::string::npos || line.find("// " + affectedInclude) != std::string::npos) {
+                               found = true;
+                               ofp << affectedInclude + "\n";
+                       } else if (line.find(affectedInclude) != std::string::npos) {
+                               found = true;
+                               
+                               Log(LogInformation, "cli")
+                                       << "Include statement '" + affectedInclude + "' already set.";
+                                       
+                               ofp << line << "\n";
+                       } else
+                               ofp << line << "\n";
+               } else {
+                       if (line.find(affectedInclude) != std::string::npos) {
+                               found = true;
+                               ofp << "// " + affectedInclude + "\n";
+                       } else
+                               ofp << line << "\n";
+               }
+       }
+
+       if (include && !found)
+               ofp << affectedInclude + "\n";
+
+       ifp.close();
+       ofp.close();
+
+#ifdef _WIN32
+       _unlink(configurationFile.CStr());
+#endif /* _WIN32 */
+
+       if (rename(tempFile.CStr(), configurationFile.CStr()) < 0) {
+               BOOST_THROW_EXCEPTION(posix_error()
+                       << boost::errinfo_api_function("rename")
+                       << boost::errinfo_errno(errno)
+                       << boost::errinfo_file_name(configurationFile));
+       }
+}
+
 void NodeUtility::UpdateConstant(const String& name, const String& value)
 {
        String constantsConfPath = NodeUtility::GetConstantsConfPath();
index 2ba33c2f366ef595a886635f3e5db529b36c85d0..5ce662dfd5d3d2334ee647ab721fbb2f563e89a0 100644 (file)
@@ -44,6 +44,7 @@ public:
 
        static bool WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects);
 
+       static void UpdateConfiguration(const String& value, const bool& include, const bool& recursive);
        static void UpdateConstant(const String& name, const String& value);
 
        /* node setup helpers */
index e6cdb827c89d80f0b2f8f864f7a9294d0ff67528..aebfd87a3a397d0026febd77a9352906a0a966dd 100644 (file)
@@ -104,7 +104,8 @@ int NodeWizardCommand::Run(const boost::program_options::variables_map& vm,
         * 9. enable ApiListener feature
         * 10. generate zones.conf with endpoints and zone objects
         * 11. set NodeName = cn in constants.conf
-        * 12. reload icinga2, or tell the user to
+        * 12. disable conf.d directory?
+        * 13. reload icinga2, or tell the user to
         */
 
        std::string answer;
@@ -615,6 +616,24 @@ wizard_global_zone_loop_start:
                Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
        }
 
+       /* Disable conf.d inclusion */
+       std::cout << "\nDo you want to disable the inclusion of the conf.d directory [Y/n]: ";
+
+       std::getline(std::cin, answer);
+       boost::algorithm::to_lower(answer);
+       choice = answer;
+
+       if (choice.Contains("n"))
+               Log(LogInformation, "cli")
+                       << "The deactivation of the conf.d directory was skipped.";
+       else {
+               std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
+                       << "Disable the inclusion of the conf.d directory...\n"
+                       << ConsoleColorTag(Console_Normal);
+
+               NodeUtility::UpdateConfiguration("\"conf.d\"", false, true);
+       }
+
        return 0;
 }
 
@@ -788,6 +807,12 @@ wizard_global_zone_loop_start:
                        << 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);
 
@@ -795,5 +820,39 @@ wizard_global_zone_loop_start:
 
        NodeUtility::UpdateConstant("TicketSalt", salt);
 
+       /* Disable conf.d inclusion */
+       std::cout << "\nDo you want to disable the inclusion of the conf.d directory [Y/n]: ";
+
+       std::getline(std::cin, answer);
+       boost::algorithm::to_lower(answer);
+       choice = answer;
+
+       if (choice.Contains("n"))
+               Log(LogInformation, "cli")
+                       << "The deactivation of the conf.d directory was skipped.";
+       else {
+               std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
+                       << "Disable the inclusion of the conf.d directory...\n"
+                       << ConsoleColorTag(Console_Normal);
+
+               NodeUtility::UpdateConfiguration("\"conf.d\"", false, true);
+
+               /* Include api-users.conf */
+               String apiUsersFilePath = Application::GetSysconfDir() + "/icinga2/conf.d/api-users.conf";
+               std::ifstream apiUsersFile(apiUsersFilePath);
+
+               std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
+                       << "Checking if api-users.conf exist...\n"
+                       << ConsoleColorTag(Console_Normal);
+
+                               if(apiUsersFile)
+                                       NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false);
+                               else
+                                       Log(LogWarning, "cli")
+                                               << "Included file dosen't exist " << apiUsersFilePath;
+       }
+
+       std::cout << "Done.\n\n";
+
        return 0;
 }