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

@ -27,9 +27,9 @@ namespace icinga
enum BlackAndWhitelistCommandType
{
BlackAndWhitelistCommandAdd,
BlackAndWhitelistCommandRemove,
BlackAndWhitelistCommandList
BlackAndWhitelistCommandAdd,
BlackAndWhitelistCommandRemove,
BlackAndWhitelistCommandList
};
/**

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,13 +193,13 @@ 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";
std::ofstream fp(tempFilename.CStr(), std::ofstream::out | std::ostream::trunc);
fp << JsonSerialize(item);
fp.close();
std::ofstream fp(tempFilename.CStr(), std::ofstream::out | std::ostream::trunc);
fp << JsonSerialize(item);
fp.close();
#ifdef _WIN32
_unlink(filename.CStr());
@ -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

@ -37,7 +37,7 @@ public:
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual std::vector<String> GetPositionalSuggestions(const String& word) const;
virtual std::vector<String> GetPositionalSuggestions(const String& word) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
};

View File

@ -37,7 +37,7 @@ public:
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual std::vector<String> GetPositionalSuggestions(const String& word) const;
virtual std::vector<String> GetPositionalSuggestions(const String& word) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
};

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;

View File

@ -36,22 +36,22 @@ namespace icinga
class ObjectListCommand : public CLICommand
{
public:
DECLARE_PTR_TYPEDEFS(ObjectListCommand);
DECLARE_PTR_TYPEDEFS(ObjectListCommand);
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual void InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual void InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
private:
static void PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter);
static void PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0);
static void PrintHints(std::ostream& fp, const Dictionary::Ptr& hints, int indent = 0);
static void PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent = 0);
static void PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count);
static void PrintValue(std::ostream& fp, const Value& val);
static void PrintArray(std::ostream& fp, const Array::Ptr& arr);
static void PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter);
static void PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent = 0);
static void PrintHints(std::ostream& fp, const Dictionary::Ptr& hints, int indent = 0);
static void PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent = 0);
static void PrintTypeCounts(std::ostream& fp, const std::map<String, int>& type_count);
static void PrintValue(std::ostream& fp, const Value& val);
static void PrintArray(std::ostream& fp, const Array::Ptr& arr);
};
}

View File

@ -262,17 +262,17 @@ int PkiUtility::RequestCertificate(const String& host, const String& port, const
bool PkiUtility::CopyCertFile(const String& source, const String& target)
{
/*
if (PathExists(target)) {
Log(LogWarning, "Utility")
<< "Target file '" << target << "' already exists.";
return false;
}
if (PathExists(target)) {
Log(LogWarning, "Utility")
<< "Target file '" << target << "' already exists.";
return false;
}
std::ifstream ifs(source, std::ios::binary);
std::ofstream ofs(target, std::ios::binary);
std::ifstream ifs(source, std::ios::binary);
std::ofstream ofs(target, std::ios::binary);
ofs << ifs.rdbuf();
ofs << ifs.rdbuf();
*/
return true;
return true;
}

View File

@ -36,13 +36,13 @@ namespace icinga
class VariableGetCommand : public CLICommand
{
public:
DECLARE_PTR_TYPEDEFS(VariableGetCommand);
DECLARE_PTR_TYPEDEFS(VariableGetCommand);
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
void InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
boost::program_options::options_description& hiddenDesc) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
};
}

View File

@ -38,9 +38,9 @@ class VariableListCommand : public CLICommand
public:
DECLARE_PTR_TYPEDEFS(VariableListCommand);
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
virtual String GetDescription(void) const;
virtual String GetShortDescription(void) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
private:
static void PrintVariable(std::ostream& fp, const String& message);