2015-01-22 12:10:32 +01:00
|
|
|
/******************************************************************************
|
2016-01-12 08:29:59 +01:00
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2016-01-12 08:29:59 +01: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. *
|
|
|
|
******************************************************************************/
|
2015-01-22 12:10:32 +01:00
|
|
|
|
|
|
|
#include "cli/daemonutility.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#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 {
|
|
|
|
ScriptFrame frame;
|
|
|
|
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;
|
|
|
|
|
2015-07-21 16:10:13 +02:00
|
|
|
String zonesEtcDir = Application::GetZonesDir();
|
|
|
|
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(). */
|
|
|
|
String packagesVarDir = Application::GetLocalStateDir() + "/lib/icinga2/api/packages";
|
|
|
|
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 */
|
|
|
|
String zonesVarDir = Application::GetLocalStateDir() + "/lib/icinga2/api/zones";
|
|
|
|
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;
|
|
|
|
|
2017-05-11 14:21:30 +02:00
|
|
|
Type::Ptr appType = Type::GetByName(ScriptGlobal::Get("ApplicationType", &Empty));
|
2015-09-22 18:18:29 +02:00
|
|
|
|
|
|
|
if (ConfigItem::GetItems(appType).empty()) {
|
|
|
|
ConfigItemBuilder::Ptr builder = new ConfigItemBuilder();
|
|
|
|
builder->SetType(appType);
|
|
|
|
builder->SetName("app");
|
2016-08-28 11:00:27 +02:00
|
|
|
builder->AddExpression(new ImportDefaultTemplatesExpression());
|
2015-09-22 18:18:29 +02:00
|
|
|
ConfigItem::Ptr item = builder->Compile();
|
|
|
|
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
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
WorkQueue upq(25000, Application::GetConcurrency());
|
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;
|
|
|
|
}
|