Use the ZoneName and NodeName constants for 'node setup' and 'node wizard'

refs #10716
This commit is contained in:
Gunnar Beutner 2015-11-25 17:41:03 +01:00
parent 7ecc6f782e
commit b76fee5b98
6 changed files with 63 additions and 76 deletions

View File

@ -122,6 +122,8 @@ void ConfigWriter::EmitValue(std::ostream& fp, int indentLevel, const Value& val
EmitArray(fp, indentLevel, val);
else if (val.IsObjectType<Dictionary>())
EmitScope(fp, indentLevel, val);
else if (val.IsObjectType<ConfigIdentifier>())
EmitIdentifier(fp, static_cast<ConfigIdentifier::Ptr>(val)->GetName(), false);
else if (val.IsString())
EmitString(fp, val);
else if (val.IsNumber())
@ -260,3 +262,12 @@ const std::vector<String>& ConfigWriter::GetKeywords(void)
return keywords;
}
ConfigIdentifier::ConfigIdentifier(const String& identifier)
: m_Name(identifier)
{ }
String ConfigIdentifier::GetName(void) const
{
return m_Name;
}

View File

@ -28,6 +28,24 @@
namespace icinga
{
/**
* A config identifier.
*
* @ingroup base
*/
class I2_BASE_API ConfigIdentifier : public Object
{
public:
DECLARE_PTR_TYPEDEFS(ConfigIdentifier);
ConfigIdentifier(const String& name);
String GetName(void) const;
private:
String m_Name;
};
/**
* A configuration writer.
*

View File

@ -154,13 +154,11 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
<< "'api' feature already enabled.\n";
}
NodeUtility::GenerateNodeMasterIcingaConfig(cn);
/* read zones.conf and update with zone + endpoint information */
/* write zones.conf and update with zone + endpoint information */
Log(LogInformation, "cli", "Generating zone and object configuration.");
NodeUtility::GenerateNodeMasterIcingaConfig(cn);
NodeUtility::GenerateNodeMasterIcingaConfig();
/* update the ApiListener config - SetupMaster() will always enable it */
@ -220,6 +218,7 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", cn);
String salt = RandomString(16);
@ -433,7 +432,7 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
Log(LogInformation, "cli", "Generating zone and object configuration.");
NodeUtility::GenerateNodeIcingaConfig(vm["endpoint"].as<std::vector<std::string> >(), cn, vm["zone"].as<std::string>());
NodeUtility::GenerateNodeIcingaConfig(vm["endpoint"].as<std::vector<std::string> >());
/* update constants.conf with NodeName = CN */
if (cn != Utility::GetFQDN()) {
@ -446,6 +445,7 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", vm["zone"].as<std::string>());
/* tell the user to reload icinga2 */

View File

@ -33,6 +33,7 @@
#include "base/objectlock.hpp"
#include "base/console.hpp"
#include "base/exception.hpp"
#include "base/configwriter.hpp"
#include <boost/foreach.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/join.hpp>
@ -254,7 +255,7 @@ void NodeUtility::CollectNodes(const String& node_file, std::vector<Dictionary::
* Node Setup helpers
*/
int NodeUtility::GenerateNodeIcingaConfig(const std::vector<std::string>& endpoints, const String& nodename, const String& zonename)
int NodeUtility::GenerateNodeIcingaConfig(const std::vector<std::string>& endpoints)
{
Array::Ptr my_config = new Array();
@ -306,17 +307,16 @@ int NodeUtility::GenerateNodeIcingaConfig(const std::vector<std::string>& endpoi
Dictionary::Ptr my_endpoint = new Dictionary();
Dictionary::Ptr my_zone = new Dictionary();
my_endpoint->Set("__name", nodename);
my_endpoint->Set("__name", new ConfigIdentifier("NodeName"));
my_endpoint->Set("__type", "Endpoint");
Array::Ptr my_zone_members = new Array();
my_zone_members->Add(nodename);
my_zone_members->Add(new ConfigIdentifier("NodeName"));
my_zone->Set("__name", zonename);
my_zone->Set("__name", new ConfigIdentifier("ZoneName"));
my_zone->Set("__type", "Zone");
my_zone->Set("parent", master_zone_name); //set the master zone as parent
my_zone->Set("// This is the local node", nodename);
my_zone->Set("endpoints", my_zone_members);
/* store the local config */
@ -331,7 +331,7 @@ int NodeUtility::GenerateNodeIcingaConfig(const std::vector<std::string>& endpoi
return 0;
}
int NodeUtility::GenerateNodeMasterIcingaConfig(const String& nodename)
int NodeUtility::GenerateNodeMasterIcingaConfig(void)
{
Array::Ptr my_config = new Array();
@ -340,16 +340,13 @@ int NodeUtility::GenerateNodeMasterIcingaConfig(const String& nodename)
Dictionary::Ptr my_master_zone = new Dictionary();
Array::Ptr my_master_zone_members = new Array();
my_master_endpoint->Set("__name", nodename);
my_master_endpoint->Set("__name", new ConfigIdentifier("NodeName"));
my_master_endpoint->Set("__type", "Endpoint");
my_master_zone_members->Add(nodename);
my_master_zone_members->Add(new ConfigIdentifier("NodeName"));
String zonename = VariableUtility::GetVariable("ZoneName");
my_master_zone->Set("__name", zonename);
my_master_zone->Set("__name", new ConfigIdentifier("ZoneName"));
my_master_zone->Set("__type", "Zone");
my_master_zone->Set("// This is the local master zone", zonename);
my_master_zone->Set("endpoints", my_master_zone_members);
/* store the local config */
@ -364,10 +361,6 @@ int NodeUtility::GenerateNodeMasterIcingaConfig(const String& nodename)
return 0;
}
/*
* This is ugly and requires refactoring into a generic config writer class.
* TODO.
*/
bool NodeUtility::WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects)
{
Log(LogInformation, "cli")
@ -403,10 +396,7 @@ bool NodeUtility::WriteNodeConfigObjects(const String& filename, const Array::Pt
ObjectLock olock(objects);
BOOST_FOREACH(const Dictionary::Ptr& object, objects) {
String name = object->Get("__name");
String type = object->Get("__type");
SerializeObject(fp, name, type, object);
SerializeObject(fp, object);
}
fp << std::endl;
@ -622,60 +612,29 @@ bool NodeUtility::CreateBackupFile(const String& target, bool is_private)
return true;
}
void NodeUtility::SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object)
void NodeUtility::SerializeObject(std::ostream& fp, const Dictionary::Ptr& object)
{
fp << "object " << type << " \"" << name << "\" {\n";
fp << "object ";
ConfigWriter::EmitIdentifier(fp, object->Get("__type"), false);
fp << " ";
ConfigWriter::EmitValue(fp, 0, object->Get("__name"));
fp << " {\n";
ObjectLock olock(object);
BOOST_FOREACH(const Dictionary::Pair& kv, object) {
if (kv.first == "__type" || kv.first == "__name")
continue;
fp << "\t" << kv.first << " = ";
FormatValue(fp, kv.second);
fp << "\n";
fp << "\t";
ConfigWriter::EmitIdentifier(fp, kv.first, true);
fp << " = ";
ConfigWriter::EmitValue(fp, 1, kv.second);
fp << ";\n";
}
fp << "}\n\n";
}
void NodeUtility::FormatValue(std::ostream& fp, const Value& val)
{
if (val.IsObjectType<Array>()) {
FormatArray(fp, val);
return;
}
if (val.IsString()) {
fp << "\"" << Convert::ToString(val) << "\"";
return;
}
fp << Convert::ToString(val);
}
void NodeUtility::FormatArray(std::ostream& fp, const Array::Ptr& arr)
{
bool first = true;
fp << "[ ";
if (arr) {
ObjectLock olock(arr);
BOOST_FOREACH(const Value& value, arr) {
if (first)
first = false;
else
fp << ", ";
FormatValue(fp, value);
}
}
if (!first)
fp << " ";
fp << "]";
}
void NodeUtility::UpdateConstant(const String& name, const String& value)
{
String constantsFile = Application::GetSysconfDir() + "/icinga2/constants.conf";

View File

@ -59,8 +59,8 @@ public:
static void UpdateConstant(const String& name, const String& value);
/* node setup helpers */
static int GenerateNodeIcingaConfig(const std::vector<std::string>& endpoints, const String& nodename, const String& zonename);
static int GenerateNodeMasterIcingaConfig(const String& nodename);
static int GenerateNodeIcingaConfig(const std::vector<std::string>& endpoints);
static int GenerateNodeMasterIcingaConfig(void);
/* black/whitelist */
static String GetBlackAndWhiteListPath(const String& type);
@ -79,9 +79,7 @@ private:
static Dictionary::Ptr LoadNodeFile(const String& node_file);
static void CollectNodes(const String& node_file, std::vector<Dictionary::Ptr>& nodes);
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 FormatArray(std::ostream& fp, const Array::Ptr& arr);
static void SerializeObject(std::ostream& fp, const Dictionary::Ptr& object);
};
}

View File

@ -420,7 +420,7 @@ wizard_ticket:
/* apilistener config */
Log(LogInformation, "cli", "Generating local zones.conf.");
NodeUtility::GenerateNodeIcingaConfig(endpoints, cn, local_zone);
NodeUtility::GenerateNodeIcingaConfig(endpoints);
if (cn != Utility::GetFQDN()) {
Log(LogWarning, "cli")
@ -471,7 +471,7 @@ wizard_ticket:
else
std::cout << "'api' feature already enabled.\n";
NodeUtility::GenerateNodeMasterIcingaConfig(cn);
NodeUtility::GenerateNodeMasterIcingaConfig();
/* apilistener config */
std::cout << ConsoleColorTag(Console_Bold) << "Please specify the API bind host/port (optional):\n";
@ -543,6 +543,7 @@ wizard_ticket:
NodeUtility::CreateBackupFile(constants_file);
NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", cn);
String salt = RandomString(16);