<< "'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 */
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
NodeUtility::UpdateConstant("NodeName", cn);
+ NodeUtility::UpdateConstant("ZoneName", cn);
String salt = RandomString(16);
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()) {
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 */
#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>
* 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();
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 */
return 0;
}
-int NodeUtility::GenerateNodeMasterIcingaConfig(const String& nodename)
+int NodeUtility::GenerateNodeMasterIcingaConfig(void)
{
Array::Ptr my_config = new Array();
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 */
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")
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;
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 << "}\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 << "\t";
+ ConfigWriter::EmitIdentifier(fp, kv.first, true);
+ fp << " = ";
+ ConfigWriter::EmitValue(fp, 1, kv.second);
+ fp << ";\n";
}
- 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 << "]";
+ fp << "}\n\n";
}
void NodeUtility::UpdateConstant(const String& name, const String& value)
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);
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);
};
}