2014-10-17 15:13:17 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2014-10-17 15:13:17 +02:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
#include "cli/nodeupdateconfigcommand.hpp"
|
|
|
|
#include "cli/nodeutility.hpp"
|
2014-10-27 17:58:39 +01:00
|
|
|
#include "cli/repositoryutility.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-11-27 18:15:14 +01:00
|
|
|
#include "base/console.hpp"
|
2014-10-17 15:13:17 +02:00
|
|
|
#include "base/application.hpp"
|
2014-11-27 18:15:14 +01:00
|
|
|
#include "base/tlsutility.hpp"
|
2014-10-28 19:17:37 +01:00
|
|
|
#include "base/json.hpp"
|
2014-11-27 18:15:14 +01:00
|
|
|
#include "base/objectlock.hpp"
|
2014-10-17 15:13:17 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-10-18 21:06:28 +02:00
|
|
|
#include <iostream>
|
2014-10-17 15:13:17 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
REGISTER_CLICOMMAND("node/update-config", NodeUpdateConfigCommand);
|
2014-10-17 15:13:17 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeUpdateConfigCommand::GetDescription(void) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:53:44 +01:00
|
|
|
return "Update Icinga 2 node config.";
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeUpdateConfigCommand::GetShortDescription(void) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:53:44 +01:00
|
|
|
return "update node config";
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
ImpersonationLevel NodeUpdateConfigCommand::GetImpersonationLevel(void) const
|
2014-10-27 17:58:39 +01:00
|
|
|
{
|
|
|
|
return ImpersonateRoot;
|
|
|
|
}
|
|
|
|
|
2014-10-17 15:13:17 +02:00
|
|
|
/**
|
2014-10-31 11:53:44 +01:00
|
|
|
* The entry point for the "node update-config" CLI command.
|
2014-10-17 15:13:17 +02:00
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeUpdateConfigCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-27 19:15:34 +01:00
|
|
|
//If there are changes pending, abort the current operation
|
|
|
|
if (RepositoryUtility::ChangeLogHasPendingChanges()) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "There are pending changes for commit.\n"
|
2014-10-29 11:09:55 +01:00
|
|
|
<< "Please review and commit them using 'icinga2 repository commit [--simulate]'\n"
|
2014-11-02 18:22:51 +01:00
|
|
|
<< "or drop them using 'icinga2 repository clear-changes' before proceeding.";
|
2014-10-27 19:15:34 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String inventory_path = NodeUtility::GetRepositoryPath() + "/inventory.index";
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr old_inventory = new Dictionary();
|
2014-10-28 19:17:37 +01:00
|
|
|
if (Utility::PathExists(inventory_path)) {
|
|
|
|
old_inventory = Utility::LoadJsonFile(inventory_path);
|
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr inventory = new Dictionary();
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-10-27 17:58:39 +01:00
|
|
|
Log(LogInformation, "cli")
|
2014-10-31 11:53:44 +01:00
|
|
|
<< "Updating node configuration for ";
|
2014-10-27 17:58:39 +01:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::PrintNodes(std::cout);
|
2014-10-27 17:58:39 +01:00
|
|
|
|
2014-10-28 19:17:37 +01:00
|
|
|
Utility::LoadExtensionLibrary("icinga");
|
|
|
|
|
|
|
|
std::vector<String> object_paths = RepositoryUtility::GetObjects();
|
2014-12-11 14:47:46 +01:00
|
|
|
std::vector<Dictionary::Ptr> nodes = NodeUtility::GetNodes();
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
/* first make sure that all nodes are valid and should not be removed */
|
2014-12-11 14:47:46 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& node, nodes) {
|
2014-10-31 11:53:44 +01:00
|
|
|
Dictionary::Ptr repository = node->Get("repository");
|
|
|
|
String zone = node->Get("zone");
|
|
|
|
String endpoint = node->Get("endpoint");
|
|
|
|
String node_name = endpoint;
|
2014-10-27 17:58:39 +01:00
|
|
|
|
2014-10-29 11:09:55 +01:00
|
|
|
/* store existing structure in index */
|
2014-10-31 11:53:44 +01:00
|
|
|
inventory->Set(endpoint, node);
|
2014-12-10 18:48:35 +01:00
|
|
|
}
|
2014-10-29 11:09:55 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
if (old_inventory) {
|
|
|
|
/* check if there are objects inside the old_inventory which do not exist anymore */
|
|
|
|
ObjectLock ulock(old_inventory);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& old_node_objs, old_inventory) {
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
String old_node_name = old_node_objs.first;
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
/* check if the node was dropped */
|
|
|
|
if (!inventory->Contains(old_node_name)) {
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Node update found old node '" << old_node_name << "'. Removing it and all of its hosts/services.";
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
//TODO Remove an node and all of his hosts
|
|
|
|
Dictionary::Ptr old_node = old_node_objs.second;
|
|
|
|
Dictionary::Ptr old_node_repository = old_node->Get("repository");
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2015-01-09 14:10:48 +01:00
|
|
|
if (old_node_repository) {
|
|
|
|
ObjectLock olock(old_node_repository);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, old_node_repository) {
|
|
|
|
String host = kv.first;
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2015-01-09 14:10:48 +01:00
|
|
|
Dictionary::Ptr host_attrs = new Dictionary();
|
|
|
|
host_attrs->Set("name", host);
|
|
|
|
RepositoryUtility::RemoveObject(host, "Host", host_attrs); //this removes all services for this host as well
|
|
|
|
}
|
2014-12-10 18:48:35 +01:00
|
|
|
}
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
String zone = old_node->Get("zone");
|
|
|
|
String endpoint = old_node->Get("endpoint");
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
Dictionary::Ptr zone_attrs = new Dictionary();
|
|
|
|
zone_attrs->Set("name", zone);
|
|
|
|
RepositoryUtility::RemoveObject(zone, "Zone", zone_attrs);
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
Dictionary::Ptr endpoint_attrs = new Dictionary();
|
|
|
|
endpoint_attrs->Set("name", endpoint);
|
|
|
|
RepositoryUtility::RemoveObject(endpoint, "Endpoint", endpoint_attrs);
|
|
|
|
} else {
|
|
|
|
/* get the current node */
|
|
|
|
Dictionary::Ptr new_node = inventory->Get(old_node_name);
|
|
|
|
Dictionary::Ptr new_node_repository = new_node->Get("repository");
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-12-10 18:48:35 +01:00
|
|
|
Dictionary::Ptr old_node = old_node_objs.second;
|
|
|
|
Dictionary::Ptr old_node_repository = old_node->Get("repository");
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2015-01-09 14:10:48 +01:00
|
|
|
if (old_node_repository) {
|
|
|
|
ObjectLock xlock(old_node_repository);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, old_node_repository) {
|
|
|
|
String old_host = kv.first;
|
2014-12-10 18:48:35 +01:00
|
|
|
|
2015-01-09 14:10:48 +01:00
|
|
|
if (old_host == "localhost") {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Ignoring host '" << old_host << "'. Please make sure to configure a unique name on your node '" << old_node_name << "'.";
|
|
|
|
continue;
|
|
|
|
}
|
2014-12-10 18:48:35 +01:00
|
|
|
|
2015-01-09 14:10:48 +01:00
|
|
|
/* check against black/whitelist before trying to remove host */
|
|
|
|
if (NodeUtility::CheckAgainstBlackAndWhiteList("blacklist", old_node_name, old_host, Empty) &&
|
|
|
|
!NodeUtility::CheckAgainstBlackAndWhiteList("whitelist", old_node_name, old_host, Empty)) {
|
|
|
|
Log(LogWarning, "cli")
|
2015-01-09 14:25:45 +01:00
|
|
|
<< "Host '" << old_node_name << "' on node '" << old_node_name << "' is blacklisted, but not whitelisted. Skipping.";
|
2015-01-09 14:10:48 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2015-01-09 14:10:48 +01:00
|
|
|
if (!new_node_repository->Contains(old_host)) {
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Node update found old host '" << old_host << "' on node '" << old_node_name << "'. Removing it.";
|
|
|
|
|
|
|
|
Dictionary::Ptr host_attrs = new Dictionary();
|
|
|
|
host_attrs->Set("name", old_host);
|
|
|
|
RepositoryUtility::RemoveObject(old_host, "Host", host_attrs); //this will remove all services for this host too
|
|
|
|
} else {
|
|
|
|
/* host exists, now check all services for this host */
|
|
|
|
Array::Ptr old_services = kv.second;
|
|
|
|
Array::Ptr new_services = new_node_repository->Get(old_host);
|
|
|
|
|
|
|
|
ObjectLock ylock(old_services);
|
|
|
|
BOOST_FOREACH(const String& old_service, old_services) {
|
|
|
|
/* check against black/whitelist before trying to remove service */
|
|
|
|
if (NodeUtility::CheckAgainstBlackAndWhiteList("blacklist", old_node_name, old_host, old_service) &&
|
|
|
|
!NodeUtility::CheckAgainstBlackAndWhiteList("whitelist", old_node_name, old_host, old_service)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Service '" << old_service << "' on host '" << old_host << "' on node '"
|
|
|
|
<< old_node_name << "' is blacklisted, but not whitelisted. Skipping.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!new_services->Contains(old_service)) {
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Node update found old service '" << old_service << "' on host '" << old_host
|
|
|
|
<< "' on node '" << old_node_name << "'. Removing it.";
|
|
|
|
|
|
|
|
Dictionary::Ptr service_attrs = new Dictionary();
|
|
|
|
service_attrs->Set("name", old_service);
|
|
|
|
service_attrs->Set("host_name", old_host);
|
|
|
|
RepositoryUtility::RemoveObject(old_service, "Service", service_attrs);
|
|
|
|
}
|
2014-12-04 17:22:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 18:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* next iterate over all nodes and add hosts/services */
|
2014-12-11 14:47:46 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& node, nodes) {
|
2014-12-10 18:48:35 +01:00
|
|
|
Dictionary::Ptr repository = node->Get("repository");
|
|
|
|
String zone = node->Get("zone");
|
|
|
|
String endpoint = node->Get("endpoint");
|
|
|
|
String node_name = endpoint;
|
2014-12-04 17:22:09 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr host_services = new Dictionary();
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-10-30 00:29:09 +01:00
|
|
|
Log(LogInformation, "cli")
|
2014-10-31 20:27:10 +01:00
|
|
|
<< "Adding host '" << zone << "' to the repository.";
|
2014-10-30 00:29:09 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr host_attrs = new Dictionary();
|
2014-10-30 00:29:09 +01:00
|
|
|
host_attrs->Set("__name", zone);
|
|
|
|
host_attrs->Set("name", zone);
|
|
|
|
host_attrs->Set("check_command", "cluster-zone");
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr host_imports = new Array();
|
2014-10-31 13:15:04 +01:00
|
|
|
host_imports->Add("satellite-host"); //default host node template
|
2014-10-30 00:29:09 +01:00
|
|
|
host_attrs->Set("import", host_imports);
|
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
if (!RepositoryUtility::AddObject(zone, "Host", host_attrs)) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot add node host '" << zone << "' to the config repository!\n";
|
|
|
|
}
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
if (repository) {
|
|
|
|
ObjectLock olock(repository);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, repository) {
|
|
|
|
String host = kv.first;
|
|
|
|
String host_pattern = host + ".conf";
|
|
|
|
bool skip_host = false;
|
2014-10-29 11:34:53 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
if (host == "localhost") {
|
2014-10-28 19:17:37 +01:00
|
|
|
Log(LogWarning, "cli")
|
2014-12-04 17:22:09 +01:00
|
|
|
<< "Ignoring host '" << host << "'. Please make sure to configure a unique name on your node '" << endpoint << "'.";
|
|
|
|
continue;
|
2014-10-28 19:17:37 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
BOOST_FOREACH(const String& object_path, object_paths) {
|
|
|
|
if (object_path.Contains(host_pattern)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Host '" << host << "' already existing. Skipping its creation.";
|
|
|
|
skip_host = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-10-31 16:21:07 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
/* host has already been created above */
|
|
|
|
if (host == zone)
|
|
|
|
skip_host = true;
|
2014-10-30 00:29:09 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
bool host_was_blacklisted = false;
|
2014-10-29 07:58:34 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
/* check against black/whitelist before trying to add host */
|
|
|
|
if (NodeUtility::CheckAgainstBlackAndWhiteList("blacklist", node_name, host, Empty) &&
|
|
|
|
!NodeUtility::CheckAgainstBlackAndWhiteList("whitelist", node_name, host, Empty)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Host '" << host << "' on node '" << node_name << "' is blacklisted, but not whitelisted. Skipping.";
|
|
|
|
skip_host = true;
|
|
|
|
host_was_blacklisted = true; //check this for services on this blacklisted host
|
2014-10-29 11:53:24 +01:00
|
|
|
}
|
2014-10-27 17:58:39 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
if (!skip_host) {
|
|
|
|
/* add a new host to the config repository */
|
|
|
|
Dictionary::Ptr host_attrs = new Dictionary();
|
|
|
|
host_attrs->Set("__name", host);
|
|
|
|
host_attrs->Set("name", host);
|
|
|
|
|
|
|
|
if (host == zone)
|
|
|
|
host_attrs->Set("check_command", "cluster-zone");
|
|
|
|
else {
|
|
|
|
host_attrs->Set("check_command", "dummy");
|
|
|
|
host_attrs->Set("zone", zone);
|
|
|
|
}
|
2014-10-28 19:46:10 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
Array::Ptr host_imports = new Array();
|
|
|
|
host_imports->Add("satellite-host"); //default host node template
|
|
|
|
host_attrs->Set("import", host_imports);
|
2014-10-27 17:58:39 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
RepositoryUtility::AddObject(host, "Host", host_attrs);
|
|
|
|
}
|
2014-10-31 16:21:07 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
/* special condition: what if the host was blacklisted before, but the services should be generated? */
|
|
|
|
if (host_was_blacklisted) {
|
|
|
|
Log(LogNotice, "cli")
|
|
|
|
<< "Host '" << host << "' was blacklisted. Won't generate any services.";
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
Array::Ptr services = kv.second;
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
if (services->GetLength() == 0) {
|
|
|
|
Log(LogNotice, "cli")
|
|
|
|
<< "Host '" << host << "' without services.";
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-27 17:58:39 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
ObjectLock xlock(services);
|
|
|
|
BOOST_FOREACH(const String& service, services) {
|
|
|
|
bool skip_service = false;
|
2014-10-28 19:17:37 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
String service_pattern = host + "/" + service + ".conf";
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& object_path, object_paths) {
|
|
|
|
if (object_path.Contains(service_pattern)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Service '" << service << "' on Host '" << host << "' already existing. Skipping its creation.";
|
|
|
|
skip_service = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check against black/whitelist before trying to add service */
|
|
|
|
if (NodeUtility::CheckAgainstBlackAndWhiteList("blacklist", endpoint, host, service) &&
|
|
|
|
!NodeUtility::CheckAgainstBlackAndWhiteList("whitelist", endpoint, host, service)) {
|
2014-10-28 19:17:37 +01:00
|
|
|
Log(LogWarning, "cli")
|
2014-12-04 17:22:09 +01:00
|
|
|
<< "Service '" << service << "' on host '" << host << "' on node '"
|
|
|
|
<< node_name << "' is blacklisted, but not whitelisted. Skipping.";
|
2014-10-29 11:53:24 +01:00
|
|
|
skip_service = true;
|
2014-10-28 19:17:37 +01:00
|
|
|
}
|
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
if (skip_service)
|
|
|
|
continue;
|
2014-10-30 00:29:09 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
/* add a new service for this host to the config repository */
|
|
|
|
Dictionary::Ptr service_attrs = new Dictionary();
|
|
|
|
String long_name = host + "!" + service; //use NameComposer?
|
|
|
|
service_attrs->Set("__name", long_name);
|
|
|
|
service_attrs->Set("name", service);
|
|
|
|
service_attrs->Set("host_name", host); //Required for host-service relation
|
|
|
|
service_attrs->Set("check_command", "dummy");
|
|
|
|
service_attrs->Set("zone", zone);
|
2014-10-27 17:58:39 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
Array::Ptr service_imports = new Array();
|
|
|
|
service_imports->Add("satellite-service"); //default service node template
|
|
|
|
service_attrs->Set("import", service_imports);
|
2014-10-28 19:46:10 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
if (!RepositoryUtility::AddObject(service, "Service", service_attrs))
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-27 17:58:39 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-28 10:54:29 +01:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
/* write a new zone and endpoint for the node */
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr endpoint_attrs = new Dictionary();
|
2014-10-28 19:17:37 +01:00
|
|
|
endpoint_attrs->Set("__name", endpoint);
|
|
|
|
endpoint_attrs->Set("name", endpoint);
|
2014-10-28 10:54:29 +01:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
Dictionary::Ptr settings = node->Get("settings");
|
2014-10-28 10:54:29 +01:00
|
|
|
|
|
|
|
if (settings) {
|
|
|
|
if (settings->Contains("host"))
|
|
|
|
endpoint_attrs->Set("host", settings->Get("host"));
|
|
|
|
if (settings->Contains("port"))
|
|
|
|
endpoint_attrs->Set("port", settings->Get("port"));
|
|
|
|
}
|
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Adding endpoint '" << endpoint << "' to the repository.";
|
|
|
|
|
2014-10-28 10:54:29 +01:00
|
|
|
if (!RepositoryUtility::AddObject(endpoint, "Endpoint", endpoint_attrs)) {
|
|
|
|
Log(LogCritical, "cli")
|
2014-10-31 11:53:44 +01:00
|
|
|
<< "Cannot add node endpoint '" << endpoint << "' to the config repository!\n";
|
2014-10-28 10:54:29 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr zone_attrs = new Dictionary();
|
|
|
|
Array::Ptr zone_members = new Array();
|
2014-10-28 10:54:29 +01:00
|
|
|
|
|
|
|
zone_members->Add(endpoint);
|
2014-10-28 19:17:37 +01:00
|
|
|
zone_attrs->Set("__name", zone);
|
|
|
|
zone_attrs->Set("name", zone);
|
2014-10-28 10:54:29 +01:00
|
|
|
zone_attrs->Set("endpoints", zone_members);
|
2014-10-29 11:09:55 +01:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
String node_parent_zone = "master"; //hardcode the name
|
2014-10-29 11:34:53 +01:00
|
|
|
String parent_zone;
|
2014-10-29 11:09:55 +01:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
if (!node->Contains("parent_zone")) {
|
2014-10-29 11:09:55 +01:00
|
|
|
Log(LogWarning, "cli")
|
2014-10-31 11:38:16 +01:00
|
|
|
<< "Node '" << endpoint << "' does not have any parent zone defined. Using 'master' as default. Please verify the generated configuration.";
|
2014-10-31 11:53:44 +01:00
|
|
|
parent_zone = node_parent_zone;
|
2014-10-29 11:09:55 +01:00
|
|
|
} else {
|
2014-10-31 11:53:44 +01:00
|
|
|
parent_zone = node->Get("parent_zone");
|
2014-10-29 11:34:53 +01:00
|
|
|
|
|
|
|
if (parent_zone.IsEmpty()) {
|
|
|
|
Log(LogWarning, "cli")
|
2014-10-31 11:38:16 +01:00
|
|
|
<< "Node '" << endpoint << "' does not have any parent zone defined. Using 'master' as default. Please verify the generated configuration.";
|
2014-10-31 11:53:44 +01:00
|
|
|
parent_zone = node_parent_zone;
|
2014-10-29 11:34:53 +01:00
|
|
|
}
|
2014-10-29 11:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
zone_attrs->Set("parent", parent_zone);
|
2014-10-28 10:54:29 +01:00
|
|
|
|
2014-12-04 17:22:09 +01:00
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Adding zone '" << zone << "' to the repository.";
|
|
|
|
|
2014-10-28 10:54:29 +01:00
|
|
|
if (!RepositoryUtility::AddObject(zone, "Zone", zone_attrs)) {
|
|
|
|
Log(LogCritical, "cli")
|
2014-10-31 11:53:44 +01:00
|
|
|
<< "Cannot add node zone '" << zone << "' to the config repository!\n";
|
2014-10-28 19:17:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
Log(LogInformation, "cli", "Committing node configuration.");
|
2014-10-27 17:58:39 +01:00
|
|
|
|
|
|
|
RepositoryUtility::PrintChangeLog(std::cout);
|
|
|
|
std::cout << "\n";
|
|
|
|
RepositoryUtility::CommitChangeLog();
|
|
|
|
|
2014-10-28 19:17:37 +01:00
|
|
|
/* store the new inventory for next run */
|
2014-11-27 18:15:14 +01:00
|
|
|
NodeUtility::CreateRepositoryPath();
|
2014-10-28 19:17:37 +01:00
|
|
|
Utility::SaveJsonFile(inventory_path, inventory);
|
|
|
|
|
2014-10-27 17:58:39 +01:00
|
|
|
std::cout << "Make sure to reload Icinga 2 for these changes to take effect." << std::endl;
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-17 15:13:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|