diff --git a/lib/cli/nodesetupcommand.cpp b/lib/cli/nodesetupcommand.cpp index 590b08fae..260d01349 100644 --- a/lib/cli/nodesetupcommand.cpp +++ b/lib/cli/nodesetupcommand.cpp @@ -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 >(), "The names of the additional global zones."); + ("global_zones", po::value >(), "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(), "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; } diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp index 645e19d94..ac58f939d 100644 --- a/lib/cli/nodeutility.cpp +++ b/lib/cli/nodeutility.cpp @@ -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(); diff --git a/lib/cli/nodeutility.hpp b/lib/cli/nodeutility.hpp index 2ba33c2f3..5ce662dfd 100644 --- a/lib/cli/nodeutility.hpp +++ b/lib/cli/nodeutility.hpp @@ -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 */ diff --git a/lib/cli/nodewizardcommand.cpp b/lib/cli/nodewizardcommand.cpp index e6cdb827c..aebfd87a3 100644 --- a/lib/cli/nodewizardcommand.cpp +++ b/lib/cli/nodewizardcommand.cpp @@ -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; }