Make the "agent list" and "agent remove" commands work

refs #7245
This commit is contained in:
Gunnar Beutner 2014-10-24 12:04:14 +02:00
parent 489badcea8
commit c719333b60
4 changed files with 48 additions and 60 deletions

View File

@ -63,15 +63,10 @@ int AgentListCommand::Run(const boost::program_options::variables_map& vm, const
<< "Ignoring parameters: " << boost::algorithm::join(ap, " ");
}
if (vm.count("batch")) {
if (vm.count("batch"))
AgentUtility::PrintAgentsJson(std::cout);
std::cout << "\n";
return 0;
}
std::cout << "Configured agents: \n";
else
AgentUtility::PrintAgents(std::cout);
std::cout << "\n";
return 0;
}

View File

@ -45,7 +45,7 @@ String AgentRemoveCommand::GetShortDescription(void) const
std::vector<String> AgentRemoveCommand::GetPositionalSuggestions(const String& word) const
{
return AgentUtility::GetFieldCompletionSuggestions(word);
return AgentUtility::GetAgentCompletionSuggestions(word);
}
/**

View File

@ -54,18 +54,15 @@ String AgentUtility::GetAgentSettingsFile(const String& name)
return GetRepositoryPath() + "/" + SHA256(name) + ".settings";
}
std::vector<String> AgentUtility::GetFieldCompletionSuggestions(const String& word)
std::vector<String> AgentUtility::GetAgentCompletionSuggestions(const String& word)
{
std::vector<String> cache;
std::vector<String> suggestions;
GetAgents(cache);
BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
String agent_name = agent->Get("endpoint");
std::sort(cache.begin(), cache.end());
BOOST_FOREACH(const String& suggestion, cache) {
if (suggestion.Find(word) == 0)
suggestions.push_back(suggestion);
if (agent_name.Find(word) == 0)
suggestions.push_back(agent_name);
}
return suggestions;
@ -73,43 +70,46 @@ std::vector<String> AgentUtility::GetFieldCompletionSuggestions(const String& wo
void AgentUtility::PrintAgents(std::ostream& fp)
{
std::vector<String> agents;
GetAgents(agents);
bool first = false;
BOOST_FOREACH(const String& agent, agents) {
Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent));
fp << "Agent Name: " << agent << "\n";
BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
if (first)
first = false;
else
fp << "\n";
if (agent_obj) {
fp << "Endpoint: " << agent_obj->Get("endpoint") << "\n";
fp << "Zone: " << agent_obj->Get("zone") << "\n";
fp << "Repository: ";
fp << std::setw(4);
PrintAgentRepository(fp, agent_obj->Get("repository"));
fp << std::setw(0) << "\n";
fp << "Agent '"
<< ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << agent->Get("endpoint") << ConsoleColorTag(Console_Normal)
<< "' (last seen: " << Utility::FormatDateTime("%c", agent->Get("seen")) << ")\n";
PrintAgentRepository(fp, agent->Get("repository"));
}
}
fp << "All agents: " << boost::algorithm::join(agents, " ") << "\n";
}
void AgentUtility::PrintAgentRepository(std::ostream& fp, const Dictionary::Ptr& repository)
{
//TODO better formatting
fp << JsonSerialize(repository);
ObjectLock olock(repository);
BOOST_FOREACH(const Dictionary::Pair& kv, repository) {
fp << std::setw(4) << " "
<< "* Host '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << kv.first << ConsoleColorTag(Console_Normal) << "'\n";
Array::Ptr services = kv.second;
ObjectLock xlock(services);
BOOST_FOREACH(const String& service, services) {
fp << std::setw(8) << " " << "* Service '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << service << ConsoleColorTag(Console_Normal) << "'\n";
}
}
}
void AgentUtility::PrintAgentsJson(std::ostream& fp)
{
std::vector<String> agents;
GetAgents(agents);
Dictionary::Ptr result = make_shared<Dictionary>();
BOOST_FOREACH(const String& agent, agents) {
Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent));
if (agent_obj) {
fp << JsonSerialize(agent_obj);
}
BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
result->Set(agent->Get("endpoint"), agent);
}
fp << JsonSerialize(result);
}
bool AgentUtility::AddAgent(const String& name)
@ -230,34 +230,27 @@ Dictionary::Ptr AgentUtility::GetAgentFromRepository(const String& filename)
String content((std::istreambuf_iterator<char>(fp)), std::istreambuf_iterator<char>());
std::cout << "Content: " << content << "\n";
fp.close();
return JsonDeserialize(content);
}
bool AgentUtility::GetAgents(std::vector<String>& agents)
std::vector<Dictionary::Ptr> AgentUtility::GetAgents(void)
{
String path = GetRepositoryPath();
std::vector<Dictionary::Ptr> agents;
if (!Utility::Glob(path + "/*.repo",
boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) {
Log(LogCritical, "cli")
<< "Cannot access path '" << path << "'.";
return false;
}
Utility::Glob(GetRepositoryPath() + "/*.repo",
boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile);
return true;
return agents;
}
void AgentUtility::CollectAgents(const String& agent_file, std::vector<String>& agents)
void AgentUtility::CollectAgents(const String& agent_file, std::vector<Dictionary::Ptr>& agents)
{
String agent = Utility::BaseName(agent_file);
boost::algorithm::replace_all(agent, ".repo", "");
Dictionary::Ptr agent = GetAgentFromRepository(agent_file);
Log(LogDebug, "cli")
<< "Adding agent: " << agent;
if (!agent)
return;
agents.push_back(agent);
}

View File

@ -39,7 +39,7 @@ public:
static String GetRepositoryPath(void);
static String GetAgentRepositoryFile(const String& name);
static String GetAgentSettingsFile(const String& name);
static std::vector<String> GetFieldCompletionSuggestions(const String& word);
static std::vector<String> GetAgentCompletionSuggestions(const String& word);
static void PrintAgents(std::ostream& fp);
static void PrintAgentsJson(std::ostream& fp);
@ -52,7 +52,7 @@ public:
static bool WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item);
static Dictionary::Ptr GetAgentFromRepository(const String& filename);
static bool GetAgents(std::vector<String>& agents);
static std::vector<Dictionary::Ptr> GetAgents(void);
static bool CreateBackupFile(const String& target);
@ -67,7 +67,7 @@ public:
private:
AgentUtility(void);
static bool RemoveAgentFile(const String& path);
static void CollectAgents(const String& agent_file, std::vector<String>& agents);
static void CollectAgents(const String& agent_file, std::vector<Dictionary::Ptr>& agents);
static void SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object);
static void FormatValue(std::ostream& fp, const Value& val);