Skip to content

Commit

Permalink
Cli commands: Add basic agent command set
Browse files Browse the repository at this point in the history
refs #7248
  • Loading branch information
Michael Friedrich committed Oct 17, 2014
1 parent d16670c commit f9209ec
Show file tree
Hide file tree
Showing 19 changed files with 1,056 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

set(cli_SOURCES
agentaddcommand.cpp agentblackandwhitelistcommand.cpp agentlistcommand.cpp agentremovecommand.cpp
agentsetcommand.cpp agentsetupcommand.cpp agentupdateconfigcommand.cpp agentwizardcommand.cpp agentutility.cpp
featureenablecommand.cpp featuredisablecommand.cpp featurelistcommand.cpp
objectlistcommand.cpp
pkinewcacommand.cpp pkinewcertcommand.cpp pkisigncsrcommand.cpp pkirequestcommand.cpp pkiticketcommand.cpp
Expand Down
62 changes: 62 additions & 0 deletions lib/cli/agentaddcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/

#include "cli/agentaddcommand.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <fstream>
#include <vector>
#include <string>
#include <unistd.h>

using namespace icinga;
namespace po = boost::program_options;

REGISTER_CLICOMMAND("agent/add", AgentAddCommand);

String AgentAddCommand::GetDescription(void) const
{
return "Add Icinga 2 agent.";
}

String AgentAddCommand::GetShortDescription(void) const
{
return "add agent";
}

/**
* The entry point for the "agent add" CLI command.
*
* @returns An exit status.
*/
int AgentAddCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
if (ap.empty()) {
Log(LogCritical, "cli", "No agent name provided.");
return 1;
}

//ap[0] must contain name

return 0;
}
46 changes: 46 additions & 0 deletions lib/cli/agentaddcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/

#ifndef AGENTADDCOMMAND_H
#define AGENTADDCOMMAND_H

#include "base/qstring.hpp"
#include "base/clicommand.hpp"

namespace icinga
{

/**
* The "agent add" command.
*
* @ingroup cli
*/
class AgentAddCommand : public CLICommand
{
public:
DECLARE_PTR_TYPEDEFS(AgentAddCommand);

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

}

#endif /* AGENTADDCOMMAND_H */
124 changes: 124 additions & 0 deletions lib/cli/agentblackandwhitelistcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/

#include "cli/agentblackandwhitelistcommand.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
#include "base/tlsutility.hpp"
#include <boost/algorithm/string/case_conv.hpp>
#include <fstream>

using namespace icinga;

REGISTER_BLACKANDWHITELIST_CLICOMMAND("whitelist");
REGISTER_BLACKANDWHITELIST_CLICOMMAND("blacklist");

RegisterBlackAndWhitelistCLICommandHelper::RegisterBlackAndWhitelistCLICommandHelper(const String& type)
{
String ltype = type;
boost::algorithm::to_lower(ltype);

std::vector<String> name;
name.push_back("agent");
name.push_back(ltype);
name.push_back("add");
CLICommand::Register(name, make_shared<BlackAndWhitelistCommand>(type, BlackAndWhitelistCommandAdd));

name[2] = "remove";
CLICommand::Register(name, make_shared<BlackAndWhitelistCommand>(type, BlackAndWhitelistCommandRemove));

name[2] = "list";
CLICommand::Register(name, make_shared<BlackAndWhitelistCommand>(type, BlackAndWhitelistCommandList));
}

BlackAndWhitelistCommand::BlackAndWhitelistCommand(const String& type, BlackAndWhitelistCommandType command)
: m_Type(type), m_Command(command)
{ }

String BlackAndWhitelistCommand::GetDescription(void) const
{
String description;

switch (m_Command) {
case BlackAndWhitelistCommandAdd:
description = "Adds a new";
break;
case BlackAndWhitelistCommandRemove:
description = "Removes a";
break;
case BlackAndWhitelistCommandList:
description = "Lists all";
break;
}

description += " " + m_Type + " filter";

if (m_Command == BlackAndWhitelistCommandList)
description += "s";

return description;
}

String BlackAndWhitelistCommand::GetShortDescription(void) const
{
String description;

switch (m_Command) {
case BlackAndWhitelistCommandAdd:
description = "adds a new";
break;
case BlackAndWhitelistCommandRemove:
description = "removes a";
break;
case BlackAndWhitelistCommandList:
description = "lists all";
break;
}

description += " " + m_Type + " filter";

if (m_Command == BlackAndWhitelistCommandList)
description += "s";

return description;
}

void BlackAndWhitelistCommand::InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const
{
visibleDesc.add_options()
("agent", po::value<std::string>(), "The name of the agent")
("host", po::value<std::string>(), "The name of the host")
("service", po::value<std::string>(), "The name of the service");

if (m_Command == BlackAndWhitelistCommandAdd) {
//TODO: call list functionality
}
}

/**
* The entry point for the "agent <whitelist/blacklist> <add/remove/list>" CLI command.
*
* @returns An exit status.
*/
int BlackAndWhitelistCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
return 0;
}
77 changes: 77 additions & 0 deletions lib/cli/agentblackandwhitelistcommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/

#ifndef BLACKANDWHITELISTCOMMAND_H
#define BLACKANDWHITELISTCOMMAND_H

#include "base/qstring.hpp"
#include "base/clicommand.hpp"

namespace icinga
{

enum BlackAndWhitelistCommandType
{
BlackAndWhitelistCommandAdd,
BlackAndWhitelistCommandRemove,
BlackAndWhitelistCommandList
};

/**
* The "repository <type> <add/remove/list>" command.
*
* @ingroup cli
*/
class BlackAndWhitelistCommand : public CLICommand
{
public:
DECLARE_PTR_TYPEDEFS(BlackAndWhitelistCommand);

BlackAndWhitelistCommand(const String& type, BlackAndWhitelistCommandType command);

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:
String m_Type;
BlackAndWhitelistCommandType m_Command;
};

/**
* Helper class for registering repository CLICommand implementation classes.
*
* @ingroup cli
*/
class I2_BASE_API RegisterBlackAndWhitelistCLICommandHelper
{
public:
RegisterBlackAndWhitelistCLICommandHelper(const String& type);
};

#define REGISTER_BLACKANDWHITELIST_CLICOMMAND(type) \
namespace { namespace UNIQUE_NAME(cli) { \
I2_EXPORT icinga::RegisterBlackAndWhitelistCLICommandHelper l_RegisterBlackAndWhitelistCLICommand(type); \
} }

}

#endif /* BLACKANDWHITELISTCOMMAND_H */
59 changes: 59 additions & 0 deletions lib/cli/agentlistcommand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/

#include "cli/agentlistcommand.hpp"
#include "base/logger_fwd.hpp"
#include "base/clicommand.hpp"
#include "base/application.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <fstream>
#include <vector>
#include <string>
#include <unistd.h>

using namespace icinga;
namespace po = boost::program_options;

REGISTER_CLICOMMAND("agent/list", AgentListCommand);

String AgentListCommand::GetDescription(void) const
{
return "Lists all Icinga 2 agents.";
}

String AgentListCommand::GetShortDescription(void) const
{
return "lists all agents";
}

/**
* The entry point for the "agent list" CLI command.
*
* @returns An exit status.
*/
int AgentListCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
if (!ap.empty()) {
Log(LogWarning, "cli", "Ignoring parameters: " + boost::algorithm::join(ap, " "));
}

return 0;
}

0 comments on commit f9209ec

Please sign in to comment.