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, " "); << "Ignoring parameters: " << boost::algorithm::join(ap, " ");
} }
if (vm.count("batch")) { if (vm.count("batch"))
AgentUtility::PrintAgentsJson(std::cout); AgentUtility::PrintAgentsJson(std::cout);
std::cout << "\n"; else
return 0;
}
std::cout << "Configured agents: \n";
AgentUtility::PrintAgents(std::cout); AgentUtility::PrintAgents(std::cout);
std::cout << "\n";
return 0; return 0;
} }

View File

@ -45,7 +45,7 @@ String AgentRemoveCommand::GetShortDescription(void) const
std::vector<String> AgentRemoveCommand::GetPositionalSuggestions(const String& word) 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"; 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; 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()); if (agent_name.Find(word) == 0)
suggestions.push_back(agent_name);
BOOST_FOREACH(const String& suggestion, cache) {
if (suggestion.Find(word) == 0)
suggestions.push_back(suggestion);
} }
return suggestions; return suggestions;
@ -73,43 +70,46 @@ std::vector<String> AgentUtility::GetFieldCompletionSuggestions(const String& wo
void AgentUtility::PrintAgents(std::ostream& fp) void AgentUtility::PrintAgents(std::ostream& fp)
{ {
std::vector<String> agents; bool first = false;
GetAgents(agents);
BOOST_FOREACH(const String& agent, agents) { BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent)); if (first)
fp << "Agent Name: " << agent << "\n"; first = false;
else
fp << "\n";
if (agent_obj) { fp << "Agent '"
fp << "Endpoint: " << agent_obj->Get("endpoint") << "\n"; << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << agent->Get("endpoint") << ConsoleColorTag(Console_Normal)
fp << "Zone: " << agent_obj->Get("zone") << "\n"; << "' (last seen: " << Utility::FormatDateTime("%c", agent->Get("seen")) << ")\n";
fp << "Repository: ";
fp << std::setw(4); PrintAgentRepository(fp, agent->Get("repository"));
PrintAgentRepository(fp, agent_obj->Get("repository"));
fp << std::setw(0) << "\n";
} }
}
fp << "All agents: " << boost::algorithm::join(agents, " ") << "\n";
} }
void AgentUtility::PrintAgentRepository(std::ostream& fp, const Dictionary::Ptr& repository) void AgentUtility::PrintAgentRepository(std::ostream& fp, const Dictionary::Ptr& repository)
{ {
//TODO better formatting ObjectLock olock(repository);
fp << JsonSerialize(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) void AgentUtility::PrintAgentsJson(std::ostream& fp)
{ {
std::vector<String> agents; Dictionary::Ptr result = make_shared<Dictionary>();
GetAgents(agents);
BOOST_FOREACH(const String& agent, agents) { BOOST_FOREACH(const Dictionary::Ptr& agent, GetAgents()) {
Dictionary::Ptr agent_obj = GetAgentFromRepository(GetAgentRepositoryFile(agent)); result->Set(agent->Get("endpoint"), agent);
if (agent_obj) {
fp << JsonSerialize(agent_obj);
}
} }
fp << JsonSerialize(result);
} }
bool AgentUtility::AddAgent(const String& name) 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>()); String content((std::istreambuf_iterator<char>(fp)), std::istreambuf_iterator<char>());
std::cout << "Content: " << content << "\n";
fp.close(); fp.close();
return JsonDeserialize(content); 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", Utility::Glob(GetRepositoryPath() + "/*.repo",
boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile)) { boost::bind(&AgentUtility::CollectAgents, _1, boost::ref(agents)), GlobFile);
Log(LogCritical, "cli")
<< "Cannot access path '" << path << "'.";
return false;
}
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); Dictionary::Ptr agent = GetAgentFromRepository(agent_file);
boost::algorithm::replace_all(agent, ".repo", "");
Log(LogDebug, "cli") if (!agent)
<< "Adding agent: " << agent; return;
agents.push_back(agent); agents.push_back(agent);
} }

View File

@ -39,7 +39,7 @@ public:
static String GetRepositoryPath(void); static String GetRepositoryPath(void);
static String GetAgentRepositoryFile(const String& name); static String GetAgentRepositoryFile(const String& name);
static String GetAgentSettingsFile(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 PrintAgents(std::ostream& fp);
static void PrintAgentsJson(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 bool WriteAgentToRepository(const String& filename, const Dictionary::Ptr& item);
static Dictionary::Ptr GetAgentFromRepository(const String& filename); 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); static bool CreateBackupFile(const String& target);
@ -67,7 +67,7 @@ public:
private: private:
AgentUtility(void); AgentUtility(void);
static bool RemoveAgentFile(const String& path); 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 SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object);
static void FormatValue(std::ostream& fp, const Value& val); static void FormatValue(std::ostream& fp, const Value& val);