2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2012-06-06 14:38:28 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "config/configitem.h"
|
|
|
|
#include "config/configcompilercontext.h"
|
2013-08-29 10:40:43 +02:00
|
|
|
#include "base/application.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-08-28 08:18:58 +02:00
|
|
|
#include "base/debug.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-06-01 16:49:33 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::mutex ConfigItem::m_Mutex;
|
2012-07-27 16:05:02 +02:00
|
|
|
ConfigItem::ItemMap ConfigItem::m_Items;
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the ConfigItem class.
|
|
|
|
*
|
|
|
|
* @param type The object type.
|
|
|
|
* @param name The name of the item.
|
2013-03-11 12:04:10 +01:00
|
|
|
* @param unit The unit of the item.
|
|
|
|
* @param abstract Whether the item is a template.
|
2012-09-19 12:32:39 +02:00
|
|
|
* @param exprl Expression list for the item.
|
|
|
|
* @param parents Parent objects for the item.
|
|
|
|
* @param debuginfo Debug information.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
ConfigItem::ConfigItem(const String& type, const String& name,
|
2013-08-20 11:06:04 +02:00
|
|
|
bool abstract, const ExpressionList::Ptr& exprl,
|
2013-03-16 21:18:53 +01:00
|
|
|
const std::vector<String>& parents, const DebugInfo& debuginfo)
|
2013-10-16 13:37:54 +02:00
|
|
|
: m_Type(type), m_Name(name), m_Abstract(abstract), m_Validated(false),
|
2013-03-19 07:09:06 +01:00
|
|
|
m_ExpressionList(exprl), m_ParentNames(parents), m_DebugInfo(debuginfo)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the type of the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The type.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String ConfigItem::GetType(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Type;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the name of the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String ConfigItem::GetName(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2013-03-11 12:04:10 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the item is abstract.
|
|
|
|
*
|
|
|
|
* @returns true if the item is abstract, false otherwise.
|
|
|
|
*/
|
|
|
|
bool ConfigItem::IsAbstract(void) const
|
|
|
|
{
|
|
|
|
return m_Abstract;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the debug information for the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The debug information.
|
|
|
|
*/
|
2012-07-06 11:22:38 +02:00
|
|
|
DebugInfo ConfigItem::GetDebugInfo(void) const
|
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the expression list for the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The expression list.
|
|
|
|
*/
|
2012-06-06 14:38:28 +02:00
|
|
|
ExpressionList::Ptr ConfigItem::GetExpressionList(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
|
|
|
return m_ExpressionList;
|
|
|
|
}
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
void ConfigItem::Link(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
m_LinkedExpressionList = make_shared<ExpressionList>();
|
2013-03-19 07:09:06 +01:00
|
|
|
|
|
|
|
BOOST_FOREACH(const String& name, m_ParentNames) {
|
2013-08-20 11:06:04 +02:00
|
|
|
ConfigItem::Ptr parent = ConfigItem::GetObject(m_Type, name);
|
2012-06-05 15:05:15 +02:00
|
|
|
|
|
|
|
if (!parent) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream message;
|
2012-09-19 12:32:39 +02:00
|
|
|
message << "Parent object '" << name << "' does not"
|
|
|
|
" exist (" << m_DebugInfo << ")";
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, message.str());
|
|
|
|
} else {
|
|
|
|
parent->Link();
|
2013-03-19 07:09:06 +01:00
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
ExpressionList::Ptr pexprl = parent->GetLinkedExpressionList();
|
|
|
|
m_LinkedExpressionList->AddExpression(Expression("", OperatorExecute, pexprl, m_DebugInfo));
|
|
|
|
}
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
m_LinkedExpressionList->AddExpression(Expression("", OperatorExecute, m_ExpressionList, m_DebugInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionList::Ptr ConfigItem::GetLinkedExpressionList(void) const
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
return m_LinkedExpressionList;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Commits the configuration item by creating or updating a DynamicObject
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* @returns The DynamicObject that was created/updated.
|
|
|
|
*/
|
2013-03-02 09:07:47 +01:00
|
|
|
DynamicObject::Ptr ConfigItem::Commit(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
String type, name;
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "base", "Commit called for ConfigItem Type=" + GetType() + ", Name=" + GetName());
|
2013-01-31 15:26:54 +01:00
|
|
|
|
2013-02-08 21:05:08 +01:00
|
|
|
/* Make sure the type is valid. */
|
2013-03-02 09:07:47 +01:00
|
|
|
DynamicType::Ptr dtype = DynamicType::GetByName(GetType());
|
2013-02-08 21:05:08 +01:00
|
|
|
|
|
|
|
if (!dtype)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Type '" + GetType() + "' does not exist."));
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (m_DynamicObject.lock() || dtype->GetObject(m_Name))
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("An object with type '" + GetType() + "' and name '" + GetName() + "' already exists."));
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (IsAbstract())
|
|
|
|
return DynamicObject::Ptr();
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
/* Create a fake update in the format that
|
2013-08-20 11:06:04 +02:00
|
|
|
* DynamicObject::Deserialize expects. */
|
2013-11-06 08:51:56 +01:00
|
|
|
Dictionary::Ptr attrs = make_shared<Dictionary>();
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
Link();
|
|
|
|
|
2013-11-06 08:51:56 +01:00
|
|
|
Dictionary::Ptr properties = make_shared<Dictionary>();
|
2013-03-19 07:09:06 +01:00
|
|
|
GetLinkedExpressionList()->Execute(properties);
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(properties);
|
|
|
|
|
|
|
|
String key;
|
|
|
|
Value data;
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(boost::tie(key, data), properties) {
|
2013-08-20 11:06:04 +02:00
|
|
|
attrs->Set(key, data);
|
2013-03-01 12:07:52 +01:00
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Ptr dobj = dtype->CreateObject(attrs);
|
|
|
|
dobj->Register();
|
2012-07-06 11:22:38 +02:00
|
|
|
|
|
|
|
return dobj;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
/**
|
|
|
|
* Registers the configuration item.
|
|
|
|
*/
|
|
|
|
void ConfigItem::Register(void)
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Items[std::make_pair(m_Type, m_Name)] = GetSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the DynamicObject that belongs to the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The DynamicObject.
|
|
|
|
*/
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr ConfigItem::GetDynamicObject(void) const
|
2012-07-06 11:22:38 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
return m_DynamicObject.lock();
|
2012-07-06 11:22:38 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a configuration item by type and name.
|
|
|
|
*
|
|
|
|
* @param type The type of the ConfigItem that is to be looked up.
|
|
|
|
* @param name The name of the ConfigItem that is to be looked up.
|
|
|
|
* @returns The configuration item.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
ConfigItem::Ptr ConfigItem::GetObject(const String& type, const String& name)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2012-06-05 15:05:15 +02:00
|
|
|
|
2013-02-22 08:12:43 +01:00
|
|
|
ConfigItem::ItemMap::iterator it;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
it = m_Items.find(std::make_pair(type, name));
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-02-22 08:12:43 +01:00
|
|
|
if (it != m_Items.end())
|
|
|
|
return it->second;
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-02-05 13:06:42 +01:00
|
|
|
return ConfigItem::Ptr();
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
2013-01-30 23:02:46 +01:00
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
void ConfigItem::ValidateItem(void)
|
2013-01-30 23:02:46 +01:00
|
|
|
{
|
2013-10-16 13:37:54 +02:00
|
|
|
if (m_Validated)
|
|
|
|
return;
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigType::Ptr ctype = ConfigType::GetByName(GetType());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
if (!ctype) {
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(false, "No validation type found for object '" + GetName() + "' of type '" + GetType() + "'");
|
|
|
|
|
|
|
|
return;
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-09-24 13:13:14 +02:00
|
|
|
|
|
|
|
ctype->ValidateItem(GetSelf());
|
2013-10-16 13:37:54 +02:00
|
|
|
|
|
|
|
m_Validated = true;
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
bool ConfigItem::ActivateItems(bool validateOnly)
|
2013-08-20 11:06:04 +02:00
|
|
|
{
|
2013-09-24 13:13:14 +02:00
|
|
|
if (ConfigCompilerContext::GetInstance()->HasErrors())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Log(LogInformation, "config", "Linking config items...");
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
ConfigItem::Ptr item;
|
|
|
|
BOOST_FOREACH(boost::tie(boost::tuples::ignore, item), m_Items) {
|
2013-09-24 13:13:14 +02:00
|
|
|
item->Link();
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-10-16 13:37:54 +02:00
|
|
|
if (ConfigCompilerContext::GetInstance()->HasErrors())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Log(LogInformation, "config", "Validating config items (step 1)...");
|
|
|
|
|
|
|
|
BOOST_FOREACH(boost::tie(boost::tuples::ignore, item), m_Items) {
|
|
|
|
item->ValidateItem();
|
|
|
|
}
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
if (ConfigCompilerContext::GetInstance()->HasErrors())
|
|
|
|
return false;
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Log(LogInformation, "config", "Activating config items");
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
std::vector<DynamicObject::Ptr> objects;
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(boost::tie(boost::tuples::ignore, item), m_Items) {
|
|
|
|
DynamicObject::Ptr object = item->Commit();
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (object)
|
|
|
|
objects.push_back(object);
|
|
|
|
}
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-08-29 19:05:06 +02:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, objects) {
|
|
|
|
object->OnConfigLoaded();
|
|
|
|
}
|
|
|
|
|
2013-10-16 13:37:54 +02:00
|
|
|
Log(LogInformation, "config", "Validating config items (step 2)...");
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
BOOST_FOREACH(boost::tie(boost::tuples::ignore, item), m_Items) {
|
|
|
|
item->ValidateItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ConfigCompilerContext::GetInstance()->HasErrors())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (validateOnly)
|
|
|
|
return true;
|
|
|
|
|
2013-08-29 10:40:43 +02:00
|
|
|
/* restore the previous program state */
|
|
|
|
DynamicObject::RestoreObjects(Application::GetStatePath());
|
|
|
|
|
2013-08-29 19:25:34 +02:00
|
|
|
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
|
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, type->GetObjects()) {
|
|
|
|
if (object->IsActive())
|
|
|
|
continue;
|
2013-08-29 19:05:06 +02:00
|
|
|
|
2013-08-29 19:25:34 +02:00
|
|
|
Log(LogDebug, "config", "Activating object '" + object->GetName() + "' of type '" + object->GetType()->GetName() + "'");
|
|
|
|
object->Start();
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-08-29 19:25:34 +02:00
|
|
|
ASSERT(object->IsActive());
|
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|
2013-09-24 13:13:14 +02:00
|
|
|
|
|
|
|
return true;
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void ConfigItem::DiscardItems(void)
|
|
|
|
{
|
|
|
|
m_Items.clear();
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|