Fix style and logging from review; enhance output

refs #4508
This commit is contained in:
Michael Friedrich 2018-05-08 16:06:10 +02:00
parent b8fc3a3ffa
commit f0f0b47057
7 changed files with 63 additions and 47 deletions

View File

@ -236,7 +236,9 @@ Enabling feature api. Make sure to restart Icinga 2 for these changes to take ef
Master zone name [master]:
Default global zones: global-templates director-global
Do you want to specify additional global zones? [y/N]: N
Please specify the API bind host/port (optional):
Bind Host []:
Bind Port []:
@ -548,6 +550,7 @@ Press `Enter` or choose `n`, if you don't want to add any additional.
```
Reconfiguring Icinga...
Default global zones: global-templates director-global
Do you want to specify additional global zones? [y/N]: N
```

View File

@ -40,7 +40,12 @@ using namespace icinga;
String ApiSetupUtility::GetConfdPath()
{
return Application::GetSysconfDir() + "/icinga2/conf.d";
return Application::GetSysconfDir() + "/icinga2/conf.d";
}
String ApiSetupUtility::GetApiUsersConfPath()
{
return ApiSetupUtility::GetConfdPath() + "/api-users.conf";
}
bool ApiSetupUtility::SetupMaster(const String& cn, bool prompt_restart)

View File

@ -45,6 +45,7 @@ public:
static bool SetupMasterUpdateConstants(const String& cn);
static String GetConfdPath();
static String GetApiUsersConfPath();
private:
ApiSetupUtility();

View File

@ -66,7 +66,7 @@ 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 to 'global-templates' and 'director-global'.")
("disable-confd", "Disables the conf.d directory during the setup");
hiddenDesc.add_options()
@ -247,22 +247,23 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
if (vm.count("disable-confd")) {
/* Disable conf.d inclusion */
if (NodeUtility::UpdateConfiguration("\"conf.d\"", false, true))
if (NodeUtility::UpdateConfiguration("\"conf.d\"", false, true)) {
Log(LogInformation, "cli")
<< "Disabled conf.d inclusion";
else
} else {
Log(LogWarning, "cli")
<< "Tried to disable conf.d inclusion but failed, possibly it's already disabled.";
String apiUsersFilePath = Application::GetSysconfDir() + "/icinga2/conf.d/api-users.conf";
std::ifstream apiUsersFile(apiUsersFilePath);
}
/* Include api-users.conf */
if(apiUsersFile)
String apiUsersFilePath = ApiSetupUtility::GetApiUsersConfPath();
if (Utility::PathExists(apiUsersFilePath)) {
NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false);
else
} else {
Log(LogWarning, "cli")
<< "Included file dosen't exist " << apiUsersFilePath;
}
}
/* tell the user to reload icinga2 */
@ -576,17 +577,8 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
}
if (vm.count("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 */

View File

@ -272,12 +272,12 @@ void NodeUtility::SerializeObject(std::ostream& fp, const Dictionary::Ptr& objec
* recursive = true, will search for a resursive include statement
* Returns true on success, false if option was not found
*/
bool NodeUtility::UpdateConfiguration(const String& value, const bool& include, const bool& recursive)
bool NodeUtility::UpdateConfiguration(const String& value, bool include, bool recursive)
{
String configurationFile = Application::GetSysconfDir() + "/icinga2/icinga2.conf";
Log(LogInformation, "cli")
<< "Updating' " << value << "' include in '" << configurationFile << "'.";
<< "Updating ' " << value << "' include in '" << configurationFile << "'.";
NodeUtility::CreateBackupFile(configurationFile);
@ -287,37 +287,49 @@ bool NodeUtility::UpdateConfiguration(const String& value, const bool& include,
String affectedInclude = value;
recursive ? affectedInclude = "include_recursive " + affectedInclude : affectedInclude = "include " + affectedInclude;
if (recursive)
affectedInclude = "include_recursive " + affectedInclude;
else
affectedInclude = "include " + affectedInclude;
bool found = false;
std::string line;
while (std::getline(ifp, line)) {
if(include) {
if (include) {
if (line.find("//" + affectedInclude) != std::string::npos || line.find("// " + affectedInclude) != std::string::npos) {
found = true;
ofp << affectedInclude + "\n";
ofp << "// Added by the node setup CLI command on "
<< Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime())
<< "\n" + affectedInclude + "\n";
} else if (line.find(affectedInclude) != std::string::npos) {
found = true;
Log(LogInformation, "cli")
<< "Include statement '" + affectedInclude + "' already set.";
ofp << line << "\n";
} else
} else {
ofp << line << "\n";
}
} else {
if (line.find(affectedInclude) != std::string::npos) {
found = true;
ofp << "// " + affectedInclude + "\n";
} else
ofp << "// Disabled by the node setup CLI command on "
<< Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime())
<< "\n// " + affectedInclude + "\n";
} else {
ofp << line << "\n";
}
}
}
if (include && !found)
ofp << affectedInclude + "\n";
if (include && !found) {
ofp << "// Added by the node setup CLI command on "
<< Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime())
<< "\n" + affectedInclude + "\n";
}
ifp.close();
ofp.close();

View File

@ -44,7 +44,7 @@ public:
static bool WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects);
static bool UpdateConfiguration(const String& value, const bool& include, const bool& recursive);
static bool UpdateConfiguration(const String& value, bool include, bool recursive);
static void UpdateConstant(const String& name, const String& value);
/* node setup helpers */

View File

@ -527,6 +527,7 @@ wizard_ticket:
/* Global zones. */
std::vector<String> globalZones { "global-templates", "director-global" };
std::cout << "\nDefault global zones: " << boost::algorithm::join(globalZones, " ");
std::cout << "\nDo you want to specify additional global zones? [y/N]: ";
std::getline(std::cin, answer);
@ -625,18 +626,21 @@ wizard_global_zone_loop_start:
if (choice.Contains("n"))
Log(LogInformation, "cli")
<< "The deactivation of the conf.d directory was skipped.";
<< "conf.d directory has not been disabled.";
else {
std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
<< "Disabling the inclusion of the conf.d directory...\n"
<< ConsoleColorTag(Console_Normal);
if(!NodeUtility::UpdateConfiguration("\"conf.d\"", false, true)) {
if (!NodeUtility::UpdateConfiguration("\"conf.d\"", false, true)) {
std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundRed)
<< "Failed to disable conf.d inclusion, it may already be disabled.\n"
<< "Failed to disable the conf.d inclusion, it may already have been disabled.\n"
<< ConsoleColorTag(Console_Normal);
}
/* Satellite/Clients should not include the api-users.conf file.
* The configuration should instead be managed via config sync or automation tools.
*/
}
return 0;
@ -707,6 +711,7 @@ int NodeWizardCommand::MasterSetup() const
/* Global zones. */
std::vector<String> globalZones { "global-templates", "director-global" };
std::cout << "\nDefault global zones: " << boost::algorithm::join(globalZones, " ");
std::cout << "\nDo you want to specify additional global zones? [y/N]: ";
std::getline(std::cin, answer);
@ -813,7 +818,7 @@ wizard_global_zone_loop_start:
}
Log(LogInformation, "cli", "Updating constants.conf.");
NodeUtility::CreateBackupFile(NodeUtility::GetConstantsConfPath());
NodeUtility::UpdateConstant("NodeName", cn);
@ -832,34 +837,32 @@ wizard_global_zone_loop_start:
if (choice.Contains("n"))
Log(LogInformation, "cli")
<< "The deactivation of the conf.d directory was skipped.";
<< "conf.d directory has not been disabled.";
else {
std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
<< "Disable the inclusion of the conf.d directory...\n"
<< "Disabling the inclusion of the conf.d directory...\n"
<< ConsoleColorTag(Console_Normal);
if (!NodeUtility::UpdateConfiguration("\"conf.d\"", false, true)) {
std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundRed)
<< "Failed to disable conf.d inclusion, it may already be disabled.\n"
<< "Failed to disable the conf.d inclusion, it may already have been disabled.\n"
<< ConsoleColorTag(Console_Normal);
}
/* 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"
<< "Checking if the api-users.conf file exists...\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;
if (Utility::PathExists(apiUsersFilePath)) {
NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false);
} else {
Log(LogWarning, "cli")
<< "Included file '" << apiUsersFilePath << "' does not exist.";
}
}
std::cout << "Done.\n\n";
return 0;
}