Clean up code a bit

This commit is contained in:
Gunnar Beutner 2014-10-22 08:06:06 +02:00
parent 69bf146469
commit 5e7e49708c
11 changed files with 60 additions and 68 deletions

View File

@ -56,7 +56,8 @@ int AgentAddCommand::Run(const boost::program_options::variables_map& vm, const
}
if (!AgentUtility::AddAgent(ap[0])) {
Log(LogCritical, "cli", "Cannot add agent '" + ap[0] + "'.");
Log(LogCritical, "cli")
<< "Cannot add agent '" << ap[0] << "'.";
return 1;
}

View File

@ -100,13 +100,11 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
<< "Generating new CA.";
if (PkiUtility::NewCa() > 0) {
Log(LogWarning, "cli")
<< "Found CA, skipping and using the existing one.\n";
Log(LogWarning, "cli", "Found CA, skipping and using the existing one.\n");
}
/* 2. Generate a self signed certificate */
Log(LogInformation, "cli")
<< "Generating new self-signed certificate.";
Log(LogInformation, "cli", "Generating new self-signed certificate.");
String local_pki_path = PkiUtility::GetLocalPkiPath();
@ -126,8 +124,7 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
String cafile = PkiUtility::GetLocalCaPath() + "/ca.crt";
if (PkiUtility::NewCert(cn, keyfile, Empty, certfile) > 0) {
Log(LogCritical, "cli")
<< "Failed to create self-signed certificate";
Log(LogCritical, "cli", "Failed to create self-signed certificate");
}
/* 3. Copy certificates to /etc/icinga2/pki */
@ -148,14 +145,12 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
/* 4. read zones.conf and update with zone + endpoint information */
Log(LogInformation, "cli")
<< "Generating zone and object configuration.";
Log(LogInformation, "cli", "Generating zone and object configuration.");
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
/* 5. enable the ApiListener config (verifiy its data) */
Log(LogInformation, "cli")
<< "Enabling the APIListener feature.";
Log(LogInformation, "cli", "Enabling the APIListener feature.");
String api_path = FeatureUtility::GetFeaturesEnabledPath() + "/api.conf";
@ -166,8 +161,7 @@ int AgentSetupCommand::SetupMaster(const boost::program_options::variables_map&
<< "Edit the api feature config file '" << api_path << "' and set a secure 'ticket_salt' attribute.";
/* 6. tell the user to reload icinga2 */
Log(LogInformation, "cli")
<< "Make sure to restart Icinga 2.";
Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
return 0;
}
@ -214,8 +208,7 @@ int AgentSetupCommand::SetupAgent(const boost::program_options::variables_map& v
String pki_path = PkiUtility::GetPkiPath();
/* 4. pki request a signed certificate from the master */
Log(LogInformation, "cli")
<< "Requesting a signed certificate from the master.";
Log(LogInformation, "cli", "Requesting a signed certificate from the master.");
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
@ -226,20 +219,17 @@ int AgentSetupCommand::SetupAgent(const boost::program_options::variables_map& v
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
/* 6. generate local zones.conf with zone+endpoint */
Log(LogInformation, "cli")
<< "Generating zone and object configuration.";
Log(LogInformation, "cli", "Generating zone and object configuration.");
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
/* 7. update constants.conf with NodeName = CN */
Log(LogInformation, "cli")
<< "Updating configuration with NodeName constant.";
Log(LogInformation, "cli", "Updating configuration with NodeName constant.");
std::cout << ConsoleColorTag(Console_ForegroundRed | Console_Bold) << "PLACEHOLDER" << ConsoleColorTag(Console_Normal) << std::endl;
/* 8. tell the user to reload icinga2 */
Log(LogInformation, "cli")
<< "Make sure to restart Icinga 2.";
Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
return 0;
}

View File

@ -117,7 +117,7 @@ bool AgentUtility::AddAgent(const String& name)
if (Utility::PathExists(path) ) {
Log(LogCritical, "cli")
<< "Cannot add agent repo. '" + path + "' already exists.\n";
<< "Cannot add agent repo. '" << path << "' already exists.\n";
return false;
}
@ -147,13 +147,13 @@ bool AgentUtility::RemoveAgent(const String& name)
{
if (!RemoveAgentFile(GetAgentRepositoryFile(name))) {
Log(LogCritical, "cli")
<< "Cannot remove agent repo. '" + GetAgentRepositoryFile(name) + "' does not exist.\n";
<< "Cannot remove agent repo. '" << GetAgentRepositoryFile(name) << "' does not exist.\n";
return false;
}
if (Utility::PathExists(GetAgentSettingsFile(name))) {
if (!RemoveAgentFile(GetAgentSettingsFile(name))) {
Log(LogWarning, "cli")
<< "Cannot remove agent settings. '" + GetAgentSettingsFile(name) + "' does not exist.\n";
<< "Cannot remove agent settings. '" << GetAgentSettingsFile(name) << "' does not exist.\n";
return false;
}
}
@ -164,13 +164,13 @@ bool AgentUtility::RemoveAgent(const String& name)
bool AgentUtility::RemoveAgentFile(const String& path)
{
if (!Utility::PathExists(path)) {
Log(LogCritical, "cli", "Cannot remove '" + path + "'. Does not exist.");
Log(LogCritical, "cli", "Cannot remove '" << path << "'. Does not exist.");
return false;
}
if (unlink(path.CStr()) < 0) {
Log(LogCritical, "cli", "Cannot remove file '" + path +
"'. Failed with error code " + Convert::ToString(errno) + ", \"" + Utility::FormatErrorNumber(errno) + "\".");
Log(LogCritical, "cli", "Cannot remove file '" << path <<
"'. Failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) + "\".");
return false;
}
@ -193,7 +193,7 @@ bool AgentUtility::SetAgentAttribute(const String& name, const String& attr, con
bool AgentUtility::WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item)
{
Log(LogInformation, "cli", "Dumping agent to file '" + filename + "'");
Log(LogInformation, "cli", "Dumping agent to file '" << filename << "'");
String tempFilename = filename + ".tmp";
@ -238,7 +238,7 @@ bool AgentUtility::GetAgents(std::vector<String>& agents)
if (!Utility::Glob(path + "/*.repo",
boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) {
Log(LogCritical, "cli", "Cannot access path '" + path + "'.");
Log(LogCritical, "cli", "Cannot access path '" << path << "'.");
return false;
}
@ -250,6 +250,6 @@ void AgentUtility::CollectAgents(const String& agent_file, std::vector<String>&
String agent = Utility::BaseName(agent_file);
boost::algorithm::replace_all(agent, ".repo", "");
Log(LogDebug, "cli", "Adding agent: " + agent);
Log(LogDebug, "cli", "Adding agent: " << agent);
agents.push_back(agent);
}

View File

@ -24,6 +24,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <fstream>
using namespace icinga;