Skip to content

Commit

Permalink
CLI: Add basic agent command functionality (add, remove)
Browse files Browse the repository at this point in the history
Prepare for set, setup, wizard, *list.
Change repository files to .repo, rename .peer to .settings

refs #7248
refs #7255
  • Loading branch information
Michael Friedrich committed Oct 21, 2014
1 parent 4518716 commit e293092
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 23 deletions.
4 changes: 2 additions & 2 deletions agent/icinga2-discover-agent.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ if cn == None:

ssl_sock.close()

repository_file = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/api/repository/" + hashlib.sha256(cn).hexdigest()
repository_file = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/api/repository/" + hashlib.sha256(cn).hexdigest() + ".repo"
fp = open(repository_file, "w")
repository_info = { "endpoint": cn, "seen": time.time(), "zone": cn, "repository": {} }
json.dump(repository_info, fp)
fp.close()

peer_file = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/agent/repository/" + hashlib.sha256(cn).hexdigest() + ".peer"
peer_file = "@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/lib/icinga2/agent/repository/" + hashlib.sha256(cn).hexdigest() + ".settings"
fp = open(peer_file, "w")
peer_info = { "agent_host": host, "agent_port": port }
json.dump(peer_info, fp)
Expand Down
4 changes: 2 additions & 2 deletions agent/icinga2-list-agents.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ for root, dirs, files in os.walk(repository_dir):
if len(file) != 64:
continue

fp = open(root + file, "r")
fp = open(root + file + ".repo", "r")
repository_info = json.load(fp)
fp.close()

Expand All @@ -41,7 +41,7 @@ for root, dirs, files in os.walk(repository_dir):
repository[repository_info["endpoint"]] = repository_info

try:
fp = open(root + file + ".peer", "r")
fp = open(root + file + ".settings", "r")
peer_info = json.load(fp)
fp.close()

Expand Down
7 changes: 6 additions & 1 deletion lib/cli/agentaddcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
******************************************************************************/

#include "cli/agentaddcommand.hpp"
#include "cli/agentutility.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down Expand Up @@ -53,7 +55,10 @@ int AgentAddCommand::Run(const boost::program_options::variables_map& vm, const
return 1;
}

//ap[0] must contain name
if (!AgentUtility::AddAgent(ap[0])) {
Log(LogCritical, "cli", "Cannot add agent '" + ap[0] + "'.");
return 1;
}

return 0;
}
2 changes: 2 additions & 0 deletions lib/cli/agentblackandwhitelistcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "base/logger.hpp"
#include "base/application.hpp"
#include <boost/algorithm/string/case_conv.hpp>
#include <iostream>
#include <fstream>

using namespace icinga;
Expand Down Expand Up @@ -119,5 +120,6 @@ void BlackAndWhitelistCommand::InitParameters(boost::program_options::options_de
*/
int BlackAndWhitelistCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
Log(LogWarning, "cli", "TODO: Not implemented yet.");
return 0;
}
20 changes: 20 additions & 0 deletions lib/cli/agentlistcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
******************************************************************************/

#include "cli/agentlistcommand.hpp"
#include "cli/agentutility.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include "base/console.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand All @@ -41,6 +44,13 @@ String AgentListCommand::GetShortDescription(void) const
return "lists all agents";
}

void AgentListCommand::InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const
{
visibleDesc.add_options()
("batch", "list agents in json");
}

/**
* The entry point for the "agent list" CLI command.
*
Expand All @@ -53,5 +63,15 @@ int AgentListCommand::Run(const boost::program_options::variables_map& vm, const
<< "Ignoring parameters: " << boost::algorithm::join(ap, " ");
}

if (vm.count("batch")) {
AgentUtility::PrintAgentsJson(std::cout);
std::cout << "\n";
return 0;
}

std::cout << "Configured agents: \n";
AgentUtility::PrintAgents(std::cout);
std::cout << "\n";

return 0;
}
2 changes: 2 additions & 0 deletions lib/cli/agentlistcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class AgentListCommand : public CLICommand

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;
};

Expand Down
16 changes: 13 additions & 3 deletions lib/cli/agentremovecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
******************************************************************************/

#include "cli/agentremovecommand.hpp"
#include "cli/agentutility.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand All @@ -33,12 +35,17 @@ REGISTER_CLICOMMAND("agent/remove", AgentRemoveCommand);

String AgentRemoveCommand::GetDescription(void) const
{
return "Remove Icinga 2 agent.";
return "Removes Icinga 2 agent.";
}

String AgentRemoveCommand::GetShortDescription(void) const
{
return "remove agent";
return "removes agent";
}

std::vector<String> AgentRemoveCommand::GetPositionalSuggestions(const String& word) const
{
return AgentUtility::GetFieldCompletionSuggestions(word);
}

/**
Expand All @@ -53,7 +60,10 @@ int AgentRemoveCommand::Run(const boost::program_options::variables_map& vm, con
return 1;
}

//ap[0] must contain name
if (!AgentUtility::RemoveAgent(ap[0])) {
Log(LogCritical, "cli", "Cannot remove agent '" + ap[0] + "'.");
return 1;
}

return 0;
}
1 change: 1 addition & 0 deletions lib/cli/agentremovecommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AgentRemoveCommand : public CLICommand

virtual String GetDescription(void) const;
virtual String GetShortDescription(void) 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;
};

Expand Down
11 changes: 5 additions & 6 deletions lib/cli/agentsetcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand All @@ -33,18 +34,18 @@ REGISTER_CLICOMMAND("agent/set", AgentSetCommand);

String AgentSetCommand::GetDescription(void) const
{
return "Lists all Icinga 2 agents.";
return "Set agent attribute(s).";
}

String AgentSetCommand::GetShortDescription(void) const
{
return "lists all agents";
return "set agent attributes";
}

void AgentSetCommand::InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const
{
/* Command doesn't support any parameters. */

}

/**
Expand All @@ -59,9 +60,7 @@ int AgentSetCommand::Run(const boost::program_options::variables_map& vm, const
return 1;
}

//ap[0] must contain name
//ap[1] must contain attr
//ap[2] must contain val
Log(LogWarning, "cli", "TODO: Not implemented yet.");

return 0;
}
28 changes: 26 additions & 2 deletions lib/cli/agentsetupcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand All @@ -45,9 +46,14 @@ void AgentSetupCommand::InitParameters(boost::program_options::options_descripti
boost::program_options::options_description& hiddenDesc) const
{
visibleDesc.add_options()
("master", po::value<std::string>(), "The name of the Icinga 2 master")
("zone", po::value<std::string>(), "The name of the local zone")
("master_zone", po::value<std::string>(), "The name of the master zone")
("listen", po::value<std::string>(), "Listen on host:port");
("endpoint", po::value<std::vector<std::string> >(), "Connect to remote endpoint(s) on host,port")
("listen", po::value<std::string>(), "Listen on host,port")
("ticket", po::value<std::string>(), "Generated ticket number for this request")
("trustedcert", po::value<std::string>(), "Trusted master certificate file")
("cn", po::value<std::string>(), "The certificate's common name")
("master", po::value<std::string>(), "Use setup for a master instance");
}

/**
Expand All @@ -62,5 +68,23 @@ int AgentSetupCommand::Run(const boost::program_options::variables_map& vm, cons
<< "Ignoring parameters: " << boost::algorithm::join(ap, " ");
}

Log(LogWarning, "cli", "TODO: Not implemented yet.");

if (vm.count("master")) {
SetupMaster(vm, ap);
} else {
SetupAgent(vm, ap);
}

return 0;
}

bool AgentSetupCommand::SetupMaster(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap)
{
return true;
}

bool AgentSetupCommand::SetupAgent(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap)
{
return true;
}
4 changes: 4 additions & 0 deletions lib/cli/agentsetupcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class AgentSetupCommand : public CLICommand
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 bool SetupMaster(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap);
static bool SetupAgent(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap);
};

}
Expand Down
3 changes: 3 additions & 0 deletions lib/cli/agentupdateconfigcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
#include <fstream>
#include <vector>

Expand Down Expand Up @@ -53,5 +54,7 @@ int AgentUpdateConfigCommand::Run(const boost::program_options::variables_map& v
<< "Ignoring parameters: " << boost::algorithm::join(ap, " ");
}

Log(LogWarning, "cli", "TODO: Not implemented yet.");

return 0;
}

0 comments on commit e293092

Please sign in to comment.