This implements a function to disable the conf.d directory through the node wizard/setup.
refs #4508
("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")
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;
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;
}
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();
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 */
* 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;
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;
}
<< 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);
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;
}