2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-01-22 12:10:32 +01:00
|
|
|
|
|
|
|
#include "cli/daemonutility.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/application.hpp"
|
2018-09-04 15:17:34 +02:00
|
|
|
#include "base/scriptglobal.hpp"
|
2015-01-22 12:10:32 +01:00
|
|
|
#include "config/configcompiler.hpp"
|
|
|
|
#include "config/configcompilercontext.hpp"
|
|
|
|
#include "config/configitembuilder.hpp"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-17 16:08:57 +02:00
|
|
|
static bool ExecuteExpression(Expression *expression)
|
2015-01-22 12:10:32 +01:00
|
|
|
{
|
|
|
|
if (!expression)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
2018-01-03 10:19:24 +01:00
|
|
|
ScriptFrame frame(true);
|
2015-01-22 12:10:32 +01:00
|
|
|
expression->Evaluate(frame);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "config", DiagnosticInformation(ex));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
static void IncludeZoneDirRecursive(const String& path, const String& package, bool& success)
|
2015-01-22 12:10:32 +01:00
|
|
|
{
|
|
|
|
String zoneName = Utility::BaseName(path);
|
|
|
|
|
2015-07-26 17:46:47 +02:00
|
|
|
/* register this zone path for cluster config sync */
|
|
|
|
ConfigCompiler::RegisterZoneDir("_etc", path, zoneName);
|
|
|
|
|
2017-12-15 05:34:46 +01:00
|
|
|
std::vector<std::unique_ptr<Expression> > expressions;
|
2017-11-23 06:51:48 +01:00
|
|
|
Utility::GlobRecursive(path, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile);
|
2017-12-15 05:34:46 +01:00
|
|
|
DictExpression expr(std::move(expressions));
|
2015-01-22 12:10:32 +01:00
|
|
|
if (!ExecuteExpression(&expr))
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
static void IncludeNonLocalZone(const String& zonePath, const String& package, bool& success)
|
2015-01-22 12:10:32 +01:00
|
|
|
{
|
2015-12-11 19:54:17 +01:00
|
|
|
/* Note: This include function must not call RegisterZoneDir().
|
|
|
|
* We do not need to copy it for cluster config sync. */
|
2015-01-22 12:10:32 +01:00
|
|
|
|
2015-12-11 19:54:17 +01:00
|
|
|
String zoneName = Utility::BaseName(zonePath);
|
|
|
|
|
|
|
|
/* Check whether this node already has an authoritative config version
|
|
|
|
* from zones.d in etc or api package directory, or a local marker file)
|
|
|
|
*/
|
|
|
|
if (ConfigCompiler::HasZoneConfigAuthority(zoneName) || Utility::PathExists(zonePath + "/.authoritative")) {
|
2015-12-18 11:53:56 +01:00
|
|
|
Log(LogNotice, "config")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Ignoring non local config include for zone '" << zoneName << "': We already have an authoritative copy included.";
|
2015-01-22 12:10:32 +01:00
|
|
|
return;
|
2015-12-11 19:54:17 +01:00
|
|
|
}
|
2015-01-22 12:10:32 +01:00
|
|
|
|
2017-12-15 05:34:46 +01:00
|
|
|
std::vector<std::unique_ptr<Expression> > expressions;
|
2017-11-23 06:51:48 +01:00
|
|
|
Utility::GlobRecursive(zonePath, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile);
|
2017-12-15 05:34:46 +01:00
|
|
|
DictExpression expr(std::move(expressions));
|
2015-12-11 19:54:17 +01:00
|
|
|
if (!ExecuteExpression(&expr))
|
|
|
|
success = false;
|
2015-08-17 16:08:57 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
static void IncludePackage(const String& packagePath, bool& success)
|
2015-08-17 16:08:57 +02:00
|
|
|
{
|
2015-12-11 19:54:17 +01:00
|
|
|
/* Note: Package includes will register their zones
|
|
|
|
* for config sync inside their generated config. */
|
2015-08-28 17:58:29 +02:00
|
|
|
String packageName = Utility::BaseName(packagePath);
|
2015-12-11 19:54:17 +01:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
if (Utility::PathExists(packagePath + "/include.conf")) {
|
2017-12-15 05:34:46 +01:00
|
|
|
std::unique_ptr<Expression> expr = ConfigCompiler::CompileFile(packagePath + "/include.conf",
|
2017-12-19 15:50:05 +01:00
|
|
|
String(), packageName);
|
2015-12-11 19:54:17 +01:00
|
|
|
|
2017-12-15 05:34:46 +01:00
|
|
|
if (!ExecuteExpression(&*expr))
|
2015-08-17 16:08:57 +02:00
|
|
|
success = false;
|
|
|
|
}
|
2015-01-22 12:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DaemonUtility::ValidateConfigFiles(const std::vector<std::string>& configs, const String& objectsFile)
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
if (!objectsFile.IsEmpty())
|
|
|
|
ConfigCompilerContext::GetInstance()->OpenObjectsFile(objectsFile);
|
|
|
|
|
|
|
|
if (!configs.empty()) {
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& configPath : configs) {
|
2017-08-22 10:26:23 +02:00
|
|
|
try {
|
2017-12-15 05:34:46 +01:00
|
|
|
std::unique_ptr<Expression> expression = ConfigCompiler::CompileFile(configPath, String(), "_etc");
|
|
|
|
success = ExecuteExpression(&*expression);
|
2017-08-22 10:26:23 +02:00
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "cli", "Could not compile config files: " + DiagnosticInformation(ex, false));
|
|
|
|
Application::Exit(1);
|
|
|
|
}
|
2015-01-22 12:10:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 19:54:17 +01:00
|
|
|
/* Load cluster config files from /etc/icinga2/zones.d.
|
|
|
|
* This should probably be in libremote but
|
|
|
|
* unfortunately moving it there is somewhat non-trivial. */
|
2015-03-02 09:41:18 +01:00
|
|
|
success = true;
|
|
|
|
|
2018-08-09 15:37:23 +02:00
|
|
|
String zonesEtcDir = Configuration::ZonesDir;
|
2015-07-21 16:10:13 +02:00
|
|
|
if (!zonesEtcDir.IsEmpty() && Utility::PathExists(zonesEtcDir))
|
2017-11-23 06:51:48 +01:00
|
|
|
Utility::Glob(zonesEtcDir + "/*", std::bind(&IncludeZoneDirRecursive, _1, "_etc", std::ref(success)), GlobDirectory);
|
2015-07-21 16:10:13 +02:00
|
|
|
|
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
|
2015-12-11 19:54:17 +01:00
|
|
|
/* Load package config files - they may contain additional zones which
|
|
|
|
* are authoritative on this node and are checked in HasZoneConfigAuthority(). */
|
2018-08-09 15:37:23 +02:00
|
|
|
String packagesVarDir = Configuration::DataDir + "/api/packages";
|
2015-12-11 19:54:17 +01:00
|
|
|
if (Utility::PathExists(packagesVarDir))
|
2017-11-23 06:51:48 +01:00
|
|
|
Utility::Glob(packagesVarDir + "/*", std::bind(&IncludePackage, _1, std::ref(success)), GlobDirectory);
|
2015-03-02 09:41:18 +01:00
|
|
|
|
2015-07-21 16:10:13 +02:00
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
|
2015-12-11 19:54:17 +01:00
|
|
|
/* Load cluster synchronized configuration files */
|
2018-08-09 15:37:23 +02:00
|
|
|
String zonesVarDir = Configuration::DataDir + "/api/zones";
|
2015-12-11 19:54:17 +01:00
|
|
|
if (Utility::PathExists(zonesVarDir))
|
2017-11-23 06:51:48 +01:00
|
|
|
Utility::Glob(zonesVarDir + "/*", std::bind(&IncludeNonLocalZone, _1, "_cluster", std::ref(success)), GlobDirectory);
|
2015-07-21 16:10:13 +02:00
|
|
|
|
2015-01-22 12:10:32 +01:00
|
|
|
if (!success)
|
|
|
|
return false;
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
Namespace::Ptr systemNS = ScriptGlobal::Get("System");
|
2018-09-04 15:17:34 +02:00
|
|
|
VERIFY(systemNS);
|
|
|
|
|
|
|
|
/* This is initialized inside the IcingaApplication class. */
|
2018-08-07 13:55:41 +02:00
|
|
|
Value vAppType;
|
|
|
|
VERIFY(systemNS->Get("ApplicationType", &vAppType));
|
|
|
|
|
|
|
|
Type::Ptr appType = Type::GetByName(vAppType);
|
2015-09-22 18:18:29 +02:00
|
|
|
|
|
|
|
if (ConfigItem::GetItems(appType).empty()) {
|
2018-01-10 17:20:33 +01:00
|
|
|
ConfigItemBuilder builder;
|
|
|
|
builder.SetType(appType);
|
|
|
|
builder.SetName("app");
|
|
|
|
builder.AddExpression(new ImportDefaultTemplatesExpression());
|
|
|
|
ConfigItem::Ptr item = builder.Compile();
|
2015-09-22 18:18:29 +02:00
|
|
|
item->Register();
|
|
|
|
}
|
|
|
|
|
2015-01-22 12:10:32 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-18 11:12:14 +01:00
|
|
|
bool DaemonUtility::LoadConfigFiles(const std::vector<std::string>& configs,
|
2017-12-19 15:50:05 +01:00
|
|
|
std::vector<ConfigItem::Ptr>& newItems,
|
|
|
|
const String& objectsFile, const String& varsfile)
|
2015-01-22 12:10:32 +01:00
|
|
|
{
|
2015-11-19 19:38:20 +01:00
|
|
|
ActivationScope ascope;
|
|
|
|
|
2016-08-05 09:09:20 +02:00
|
|
|
if (!DaemonUtility::ValidateConfigFiles(configs, objectsFile)) {
|
|
|
|
ConfigCompilerContext::GetInstance()->CancelObjectsFile();
|
2015-01-22 12:10:32 +01:00
|
|
|
return false;
|
2016-08-05 09:09:20 +02:00
|
|
|
}
|
2015-01-22 12:10:32 +01:00
|
|
|
|
2018-08-09 15:37:23 +02:00
|
|
|
WorkQueue upq(25000, Configuration::Concurrency);
|
2016-08-09 12:39:07 +02:00
|
|
|
upq.SetName("DaemonUtility::LoadConfigFiles");
|
2015-11-19 19:38:20 +01:00
|
|
|
bool result = ConfigItem::CommitItems(ascope.GetContext(), upq, newItems);
|
2015-01-22 12:10:32 +01:00
|
|
|
|
2016-08-05 09:09:20 +02:00
|
|
|
if (!result) {
|
|
|
|
ConfigCompilerContext::GetInstance()->CancelObjectsFile();
|
2015-01-22 12:10:32 +01:00
|
|
|
return false;
|
2016-08-05 09:09:20 +02:00
|
|
|
}
|
2015-01-22 12:10:32 +01:00
|
|
|
|
|
|
|
ConfigCompilerContext::GetInstance()->FinishObjectsFile();
|
2017-08-22 10:26:23 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
ScriptGlobal::WriteToFile(varsfile);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogCritical, "cli", "Could not write vars file: " + DiagnosticInformation(ex, false));
|
|
|
|
Application::Exit(1);
|
|
|
|
}
|
2015-02-15 13:09:53 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|