mirror of https://github.com/Icinga/icinga2.git
Moved configrpc code into the cibsync module.
This commit is contained in:
parent
1db53b84e6
commit
a1423371d0
|
@ -6,7 +6,6 @@ SUBDIRS = \
|
||||||
cibsync \
|
cibsync \
|
||||||
compat \
|
compat \
|
||||||
configfile \
|
configfile \
|
||||||
configrpc \
|
|
||||||
delegation \
|
delegation \
|
||||||
demo \
|
demo \
|
||||||
discovery
|
discovery
|
||||||
|
|
|
@ -36,9 +36,33 @@ string CIBSyncComponent::GetName(void) const
|
||||||
*/
|
*/
|
||||||
void CIBSyncComponent::Start(void)
|
void CIBSyncComponent::Start(void)
|
||||||
{
|
{
|
||||||
|
m_SyncingConfig = false;
|
||||||
|
|
||||||
m_Endpoint = boost::make_shared<VirtualEndpoint>();
|
m_Endpoint = boost::make_shared<VirtualEndpoint>();
|
||||||
|
|
||||||
|
/* config objects */
|
||||||
|
m_Endpoint->RegisterTopicHandler("config::FetchObjects",
|
||||||
|
boost::bind(&CIBSyncComponent::FetchObjectsHandler, this, _2));
|
||||||
|
|
||||||
|
ConfigObject::GetAllObjects()->OnObjectAdded.connect(boost::bind(&CIBSyncComponent::LocalObjectCommittedHandler, this, _2));
|
||||||
|
ConfigObject::GetAllObjects()->OnObjectCommitted.connect(boost::bind(&CIBSyncComponent::LocalObjectCommittedHandler, this, _2));
|
||||||
|
ConfigObject::GetAllObjects()->OnObjectRemoved.connect(boost::bind(&CIBSyncComponent::LocalObjectRemovedHandler, this, _2));
|
||||||
|
|
||||||
|
m_Endpoint->RegisterPublication("config::ObjectCommitted");
|
||||||
|
m_Endpoint->RegisterPublication("config::ObjectRemoved");
|
||||||
|
|
||||||
|
EndpointManager::GetInstance()->OnNewEndpoint.connect(boost::bind(&CIBSyncComponent::NewEndpointHandler, this, _2));
|
||||||
|
|
||||||
|
m_Endpoint->RegisterPublication("config::FetchObjects");
|
||||||
|
m_Endpoint->RegisterTopicHandler("config::ObjectCommitted",
|
||||||
|
boost::bind(&CIBSyncComponent::RemoteObjectCommittedHandler, this, _2, _3));
|
||||||
|
m_Endpoint->RegisterTopicHandler("config::ObjectRemoved",
|
||||||
|
boost::bind(&CIBSyncComponent::RemoteObjectRemovedHandler, this, _3));
|
||||||
|
|
||||||
|
/* service status */
|
||||||
m_Endpoint->RegisterTopicHandler("delegation::ServiceStatus",
|
m_Endpoint->RegisterTopicHandler("delegation::ServiceStatus",
|
||||||
boost::bind(&CIBSyncComponent::ServiceStatusRequestHandler, _2, _3));
|
boost::bind(&CIBSyncComponent::ServiceStatusRequestHandler, _2, _3));
|
||||||
|
|
||||||
EndpointManager::GetInstance()->RegisterEndpoint(m_Endpoint);
|
EndpointManager::GetInstance()->RegisterEndpoint(m_Endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,4 +126,173 @@ void CIBSyncComponent::ServiceStatusRequestHandler(const Endpoint::Ptr& sender,
|
||||||
CIB::UpdateTaskStatistics(now, 1);
|
CIB::UpdateTaskStatistics(now, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CIBSyncComponent::NewEndpointHandler(const Endpoint::Ptr& endpoint)
|
||||||
|
{
|
||||||
|
/* no need to sync the config with local endpoints */
|
||||||
|
if (endpoint->IsLocal())
|
||||||
|
return;
|
||||||
|
|
||||||
|
endpoint->OnSessionEstablished.connect(boost::bind(&CIBSyncComponent::SessionEstablishedHandler, this, _1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CIBSyncComponent::SessionEstablishedHandler(const Endpoint::Ptr& endpoint)
|
||||||
|
{
|
||||||
|
RequestMessage request;
|
||||||
|
request.SetMethod("config::FetchObjects");
|
||||||
|
|
||||||
|
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, endpoint, request);
|
||||||
|
}
|
||||||
|
|
||||||
|
RequestMessage CIBSyncComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
|
||||||
|
{
|
||||||
|
RequestMessage msg;
|
||||||
|
msg.SetMethod(method);
|
||||||
|
|
||||||
|
MessagePart params;
|
||||||
|
msg.SetParams(params);
|
||||||
|
|
||||||
|
params.SetProperty("name", object->GetName());
|
||||||
|
params.SetProperty("type", object->GetType());
|
||||||
|
|
||||||
|
if (includeProperties)
|
||||||
|
params.SetProperty("properties", object->GetProperties());
|
||||||
|
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CIBSyncComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
|
||||||
|
{
|
||||||
|
return (!object->IsLocal());
|
||||||
|
}
|
||||||
|
|
||||||
|
void CIBSyncComponent::FetchObjectsHandler(const Endpoint::Ptr& sender)
|
||||||
|
{
|
||||||
|
ConfigObject::Set::Ptr allObjects = ConfigObject::GetAllObjects();
|
||||||
|
|
||||||
|
for (ConfigObject::Set::Iterator ci = allObjects->Begin(); ci != allObjects->End(); ci++) {
|
||||||
|
ConfigObject::Ptr object = *ci;
|
||||||
|
|
||||||
|
if (!ShouldReplicateObject(object))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
RequestMessage request = MakeObjectMessage(object, "config::ObjectCommitted", true);
|
||||||
|
|
||||||
|
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, sender, request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CIBSyncComponent::LocalObjectCommittedHandler(const ConfigObject::Ptr& object)
|
||||||
|
{
|
||||||
|
/* don't send messages when we're currently processing a remote update */
|
||||||
|
if (m_SyncingConfig)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!ShouldReplicateObject(object))
|
||||||
|
return;
|
||||||
|
|
||||||
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
||||||
|
MakeObjectMessage(object, "config::ObjectCommitted", true));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CIBSyncComponent::LocalObjectRemovedHandler(const ConfigObject::Ptr& object)
|
||||||
|
{
|
||||||
|
/* don't send messages when we're currently processing a remote update */
|
||||||
|
if (m_SyncingConfig)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!ShouldReplicateObject(object))
|
||||||
|
return;
|
||||||
|
|
||||||
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
||||||
|
MakeObjectMessage(object, "config::ObjectRemoved", false));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CIBSyncComponent::RemoteObjectCommittedHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
||||||
|
{
|
||||||
|
MessagePart params;
|
||||||
|
if (!request.GetParams(¶ms))
|
||||||
|
return;
|
||||||
|
|
||||||
|
string name;
|
||||||
|
if (!params.GetProperty("name", &name))
|
||||||
|
return;
|
||||||
|
|
||||||
|
string type;
|
||||||
|
if (!params.GetProperty("type", &type))
|
||||||
|
return;
|
||||||
|
|
||||||
|
MessagePart properties;
|
||||||
|
if (!params.GetProperty("properties", &properties))
|
||||||
|
return;
|
||||||
|
|
||||||
|
ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
|
||||||
|
|
||||||
|
if (!object) {
|
||||||
|
object = boost::make_shared<ConfigObject>(properties.GetDictionary());
|
||||||
|
|
||||||
|
if (object->GetSource() == EndpointManager::GetInstance()->GetIdentity()) {
|
||||||
|
/* the peer sent us an object that was originally created by us -
|
||||||
|
* however if was deleted locally so we have to tell the peer to destroy
|
||||||
|
* its copy of the object. */
|
||||||
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
||||||
|
MakeObjectMessage(object, "config::ObjectRemoved", false));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* TODO: compare transaction timestamps and reject the update if our local object is newer */
|
||||||
|
|
||||||
|
object->SetProperties(properties.GetDictionary());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (object->IsLocal())
|
||||||
|
throw invalid_argument("Replicated remote object is marked as local.");
|
||||||
|
|
||||||
|
if (object->GetSource().empty())
|
||||||
|
object->SetSource(sender->GetIdentity());
|
||||||
|
|
||||||
|
try {
|
||||||
|
/* TODO: only ignore updates for _this_ object rather than all objects
|
||||||
|
* this might be relevant if the commit handler for this object
|
||||||
|
* creates other objects. */
|
||||||
|
m_SyncingConfig = true;
|
||||||
|
object->Commit();
|
||||||
|
m_SyncingConfig = false;
|
||||||
|
} catch (const std::exception& ex) {
|
||||||
|
m_SyncingConfig = false;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CIBSyncComponent::RemoteObjectRemovedHandler(const RequestMessage& request)
|
||||||
|
{
|
||||||
|
MessagePart params;
|
||||||
|
if (!request.GetParams(¶ms))
|
||||||
|
return;
|
||||||
|
|
||||||
|
string name;
|
||||||
|
if (!params.GetProperty("name", &name))
|
||||||
|
return;
|
||||||
|
|
||||||
|
string type;
|
||||||
|
if (!params.GetProperty("type", &type))
|
||||||
|
return;
|
||||||
|
|
||||||
|
ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
|
||||||
|
|
||||||
|
if (!object)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!object->IsLocal()) {
|
||||||
|
try {
|
||||||
|
m_SyncingConfig = true;
|
||||||
|
object->Unregister();
|
||||||
|
m_SyncingConfig = false;
|
||||||
|
} catch (const std::exception& ex) {
|
||||||
|
m_SyncingConfig = false;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
EXPORT_COMPONENT(cibsync, CIBSyncComponent);
|
EXPORT_COMPONENT(cibsync, CIBSyncComponent);
|
||||||
|
|
|
@ -37,6 +37,24 @@ private:
|
||||||
VirtualEndpoint::Ptr m_Endpoint;
|
VirtualEndpoint::Ptr m_Endpoint;
|
||||||
|
|
||||||
static void ServiceStatusRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
|
static void ServiceStatusRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
|
||||||
|
|
||||||
|
bool m_SyncingConfig;
|
||||||
|
|
||||||
|
void NewEndpointHandler(const Endpoint::Ptr& endpoint);
|
||||||
|
void SessionEstablishedHandler(const Endpoint::Ptr& endpoint);
|
||||||
|
|
||||||
|
void LocalObjectCommittedHandler(const ConfigObject::Ptr& object);
|
||||||
|
void LocalObjectRemovedHandler(const ConfigObject::Ptr& object);
|
||||||
|
|
||||||
|
void FetchObjectsHandler(const Endpoint::Ptr& sender);
|
||||||
|
void RemoteObjectCommittedHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
|
||||||
|
void RemoteObjectRemovedHandler(const RequestMessage& request);
|
||||||
|
|
||||||
|
static RequestMessage MakeObjectMessage(const ConfigObject::Ptr& object,
|
||||||
|
string method, bool includeProperties);
|
||||||
|
|
||||||
|
static bool ShouldReplicateObject(const ConfigObject::Ptr& object);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
## Process this file with automake to produce Makefile.in
|
|
||||||
|
|
||||||
pkglib_LTLIBRARIES = \
|
|
||||||
configrpc.la
|
|
||||||
|
|
||||||
configrpc_la_SOURCES = \
|
|
||||||
configrpccomponent.cpp \
|
|
||||||
configrpccomponent.h \
|
|
||||||
i2-configrpc.h
|
|
||||||
|
|
||||||
configrpc_la_CPPFLAGS = \
|
|
||||||
$(BOOST_CPPFLAGS) \
|
|
||||||
-I${top_srcdir}/base \
|
|
||||||
-I${top_srcdir}/jsonrpc \
|
|
||||||
-I${top_srcdir}/icinga
|
|
||||||
|
|
||||||
configrpc_la_LDFLAGS = \
|
|
||||||
$(BOOST_LDFLAGS) \
|
|
||||||
-module \
|
|
||||||
-no-undefined \
|
|
||||||
@RELEASE_INFO@ \
|
|
||||||
@VERSION_INFO@
|
|
||||||
|
|
||||||
configrpc_la_LIBADD = \
|
|
||||||
${top_builddir}/base/libbase.la \
|
|
||||||
${top_builddir}/jsonrpc/libjsonrpc.la \
|
|
||||||
${top_builddir}/icinga/libicinga.la
|
|
|
@ -1,97 +0,0 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
|
||||||
<ItemGroup Label="ProjectConfigurations">
|
|
||||||
<ProjectConfiguration Include="Debug|Win32">
|
|
||||||
<Configuration>Debug</Configuration>
|
|
||||||
<Platform>Win32</Platform>
|
|
||||||
</ProjectConfiguration>
|
|
||||||
<ProjectConfiguration Include="Release|Win32">
|
|
||||||
<Configuration>Release</Configuration>
|
|
||||||
<Platform>Win32</Platform>
|
|
||||||
</ProjectConfiguration>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ClCompile Include="configrpccomponent.cpp" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ClInclude Include="configrpccomponent.h" />
|
|
||||||
<ClInclude Include="i2-configrpc.h" />
|
|
||||||
</ItemGroup>
|
|
||||||
<PropertyGroup Label="Globals">
|
|
||||||
<ProjectGuid>{697C6D7E-3109-484C-A7AF-384D28711610}</ProjectGuid>
|
|
||||||
<Keyword>Win32Proj</Keyword>
|
|
||||||
<RootNamespace>icinga</RootNamespace>
|
|
||||||
</PropertyGroup>
|
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
|
||||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
|
||||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
|
||||||
<UseDebugLibraries>false</UseDebugLibraries>
|
|
||||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
|
||||||
</PropertyGroup>
|
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
|
||||||
<ImportGroup Label="ExtensionSettings">
|
|
||||||
</ImportGroup>
|
|
||||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
|
||||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
|
||||||
</ImportGroup>
|
|
||||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
|
||||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
|
||||||
</ImportGroup>
|
|
||||||
<PropertyGroup Label="UserMacros" />
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
|
||||||
<LinkIncremental>true</LinkIncremental>
|
|
||||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\dyn;$(IncludePath)</IncludePath>
|
|
||||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
|
||||||
</PropertyGroup>
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
|
||||||
<LinkIncremental>false</LinkIncremental>
|
|
||||||
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\dyn;$(IncludePath)</IncludePath>
|
|
||||||
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
|
|
||||||
</PropertyGroup>
|
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
|
||||||
<ClCompile>
|
|
||||||
<PrecompiledHeader>
|
|
||||||
</PrecompiledHeader>
|
|
||||||
<Optimization>Disabled</Optimization>
|
|
||||||
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
|
||||||
<WarningLevel>Level3</WarningLevel>
|
|
||||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
|
||||||
<MinimalRebuild>false</MinimalRebuild>
|
|
||||||
</ClCompile>
|
|
||||||
<Link>
|
|
||||||
<SubSystem>Windows</SubSystem>
|
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
|
||||||
<AdditionalDependencies>base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
|
||||||
</Link>
|
|
||||||
</ItemDefinitionGroup>
|
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
|
||||||
<ClCompile>
|
|
||||||
<PrecompiledHeader>
|
|
||||||
</PrecompiledHeader>
|
|
||||||
<Optimization>MaxSpeed</Optimization>
|
|
||||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
|
||||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
|
||||||
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
|
||||||
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
|
|
||||||
<WarningLevel>Level3</WarningLevel>
|
|
||||||
<MultiProcessorCompilation>true</MultiProcessorCompilation>
|
|
||||||
<MinimalRebuild>false</MinimalRebuild>
|
|
||||||
</ClCompile>
|
|
||||||
<Link>
|
|
||||||
<SubSystem>Windows</SubSystem>
|
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
|
||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
|
||||||
<OptimizeReferences>true</OptimizeReferences>
|
|
||||||
<AdditionalDependencies>base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
|
||||||
</Link>
|
|
||||||
</ItemDefinitionGroup>
|
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
|
||||||
<ImportGroup Label="ExtensionTargets">
|
|
||||||
</ImportGroup>
|
|
||||||
</Project>
|
|
|
@ -1,235 +0,0 @@
|
||||||
/******************************************************************************
|
|
||||||
* Icinga 2 *
|
|
||||||
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
||||||
* *
|
|
||||||
* 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. *
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#include "i2-configrpc.h"
|
|
||||||
|
|
||||||
using namespace icinga;
|
|
||||||
|
|
||||||
string ConfigRpcComponent::GetName(void) const
|
|
||||||
{
|
|
||||||
return "configcomponent";
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::Start(void)
|
|
||||||
{
|
|
||||||
m_Syncing = false;
|
|
||||||
|
|
||||||
EndpointManager::Ptr endpointManager = EndpointManager::GetInstance();
|
|
||||||
|
|
||||||
m_Endpoint = boost::make_shared<VirtualEndpoint>();
|
|
||||||
|
|
||||||
m_Endpoint->RegisterTopicHandler("config::FetchObjects",
|
|
||||||
boost::bind(&ConfigRpcComponent::FetchObjectsHandler, this, _2));
|
|
||||||
|
|
||||||
ConfigObject::GetAllObjects()->OnObjectAdded.connect(boost::bind(&ConfigRpcComponent::LocalObjectCommittedHandler, this, _2));
|
|
||||||
ConfigObject::GetAllObjects()->OnObjectCommitted.connect(boost::bind(&ConfigRpcComponent::LocalObjectCommittedHandler, this, _2));
|
|
||||||
ConfigObject::GetAllObjects()->OnObjectRemoved.connect(boost::bind(&ConfigRpcComponent::LocalObjectRemovedHandler, this, _2));
|
|
||||||
|
|
||||||
m_Endpoint->RegisterPublication("config::ObjectCommitted");
|
|
||||||
m_Endpoint->RegisterPublication("config::ObjectRemoved");
|
|
||||||
|
|
||||||
endpointManager->OnNewEndpoint.connect(boost::bind(&ConfigRpcComponent::NewEndpointHandler, this, _2));
|
|
||||||
|
|
||||||
m_Endpoint->RegisterPublication("config::FetchObjects");
|
|
||||||
m_Endpoint->RegisterTopicHandler("config::ObjectCommitted",
|
|
||||||
boost::bind(&ConfigRpcComponent::RemoteObjectCommittedHandler, this, _2, _3));
|
|
||||||
m_Endpoint->RegisterTopicHandler("config::ObjectRemoved",
|
|
||||||
boost::bind(&ConfigRpcComponent::RemoteObjectRemovedHandler, this, _3));
|
|
||||||
|
|
||||||
endpointManager->RegisterEndpoint(m_Endpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::Stop(void)
|
|
||||||
{
|
|
||||||
EndpointManager::Ptr mgr = EndpointManager::GetInstance();
|
|
||||||
|
|
||||||
if (mgr)
|
|
||||||
mgr->UnregisterEndpoint(m_Endpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::NewEndpointHandler(const Endpoint::Ptr& endpoint)
|
|
||||||
{
|
|
||||||
/* no need to sync the config with local endpoints */
|
|
||||||
if (endpoint->IsLocal())
|
|
||||||
return;
|
|
||||||
|
|
||||||
endpoint->OnSessionEstablished.connect(boost::bind(&ConfigRpcComponent::SessionEstablishedHandler, this, _1));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::SessionEstablishedHandler(const Endpoint::Ptr& endpoint)
|
|
||||||
{
|
|
||||||
RequestMessage request;
|
|
||||||
request.SetMethod("config::FetchObjects");
|
|
||||||
|
|
||||||
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, endpoint, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestMessage ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
|
|
||||||
{
|
|
||||||
RequestMessage msg;
|
|
||||||
msg.SetMethod(method);
|
|
||||||
|
|
||||||
MessagePart params;
|
|
||||||
msg.SetParams(params);
|
|
||||||
|
|
||||||
params.SetProperty("name", object->GetName());
|
|
||||||
params.SetProperty("type", object->GetType());
|
|
||||||
|
|
||||||
if (includeProperties)
|
|
||||||
params.SetProperty("properties", object->GetProperties());
|
|
||||||
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
|
|
||||||
{
|
|
||||||
return (!object->IsLocal());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::FetchObjectsHandler(const Endpoint::Ptr& sender)
|
|
||||||
{
|
|
||||||
ConfigObject::Set::Ptr allObjects = ConfigObject::GetAllObjects();
|
|
||||||
|
|
||||||
for (ConfigObject::Set::Iterator ci = allObjects->Begin(); ci != allObjects->End(); ci++) {
|
|
||||||
ConfigObject::Ptr object = *ci;
|
|
||||||
|
|
||||||
if (!ShouldReplicateObject(object))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
RequestMessage request = MakeObjectMessage(object, "config::ObjectCommitted", true);
|
|
||||||
|
|
||||||
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, sender, request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::LocalObjectCommittedHandler(const ConfigObject::Ptr& object)
|
|
||||||
{
|
|
||||||
/* don't send messages when we're currently processing a remote update */
|
|
||||||
if (m_Syncing)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!ShouldReplicateObject(object))
|
|
||||||
return;
|
|
||||||
|
|
||||||
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
|
||||||
MakeObjectMessage(object, "config::ObjectCommitted", true));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::LocalObjectRemovedHandler(const ConfigObject::Ptr& object)
|
|
||||||
{
|
|
||||||
/* don't send messages when we're currently processing a remote update */
|
|
||||||
if (m_Syncing)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!ShouldReplicateObject(object))
|
|
||||||
return;
|
|
||||||
|
|
||||||
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
|
||||||
MakeObjectMessage(object, "config::ObjectRemoved", false));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::RemoteObjectCommittedHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
|
||||||
{
|
|
||||||
MessagePart params;
|
|
||||||
if (!request.GetParams(¶ms))
|
|
||||||
return;
|
|
||||||
|
|
||||||
string name;
|
|
||||||
if (!params.GetProperty("name", &name))
|
|
||||||
return;
|
|
||||||
|
|
||||||
string type;
|
|
||||||
if (!params.GetProperty("type", &type))
|
|
||||||
return;
|
|
||||||
|
|
||||||
MessagePart properties;
|
|
||||||
if (!params.GetProperty("properties", &properties))
|
|
||||||
return;
|
|
||||||
|
|
||||||
ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
|
|
||||||
|
|
||||||
if (!object) {
|
|
||||||
object = boost::make_shared<ConfigObject>(properties.GetDictionary());
|
|
||||||
|
|
||||||
if (object->GetSource() == EndpointManager::GetInstance()->GetIdentity()) {
|
|
||||||
/* the peer sent us an object that was originally created by us -
|
|
||||||
* however if was deleted locally so we have to tell the peer to destroy
|
|
||||||
* its copy of the object. */
|
|
||||||
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint,
|
|
||||||
MakeObjectMessage(object, "config::ObjectRemoved", false));
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* TODO: compare transaction timestamps and reject the update if our local object is newer */
|
|
||||||
|
|
||||||
object->SetProperties(properties.GetDictionary());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (object->IsLocal())
|
|
||||||
throw invalid_argument("Replicated remote object is marked as local.");
|
|
||||||
|
|
||||||
if (object->GetSource().empty())
|
|
||||||
object->SetSource(sender->GetIdentity());
|
|
||||||
|
|
||||||
try {
|
|
||||||
/* TODO: only ignore updates for _this_ object rather than all objects
|
|
||||||
* this might be relevant if the commit handler for this object
|
|
||||||
* creates other objects. */
|
|
||||||
m_Syncing = true;
|
|
||||||
object->Commit();
|
|
||||||
m_Syncing = false;
|
|
||||||
} catch (const std::exception& ex) {
|
|
||||||
m_Syncing = false;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConfigRpcComponent::RemoteObjectRemovedHandler(const RequestMessage& request)
|
|
||||||
{
|
|
||||||
MessagePart params;
|
|
||||||
if (!request.GetParams(¶ms))
|
|
||||||
return;
|
|
||||||
|
|
||||||
string name;
|
|
||||||
if (!params.GetProperty("name", &name))
|
|
||||||
return;
|
|
||||||
|
|
||||||
string type;
|
|
||||||
if (!params.GetProperty("type", &type))
|
|
||||||
return;
|
|
||||||
|
|
||||||
ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
|
|
||||||
|
|
||||||
if (!object)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!object->IsLocal()) {
|
|
||||||
try {
|
|
||||||
m_Syncing = true;
|
|
||||||
object->Unregister();
|
|
||||||
m_Syncing = false;
|
|
||||||
} catch (const std::exception& ex) {
|
|
||||||
m_Syncing = false;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPORT_COMPONENT(configrpc, ConfigRpcComponent);
|
|
|
@ -1,58 +0,0 @@
|
||||||
/******************************************************************************
|
|
||||||
* Icinga 2 *
|
|
||||||
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
||||||
* *
|
|
||||||
* 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. *
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#ifndef CONFIGRPCCOMPONENT_H
|
|
||||||
#define CONFIGRPCCOMPONENT_H
|
|
||||||
|
|
||||||
namespace icinga
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @ingroup configrpc
|
|
||||||
*/
|
|
||||||
class ConfigRpcComponent : public Component
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual string GetName(void) const;
|
|
||||||
virtual void Start(void);
|
|
||||||
virtual void Stop(void);
|
|
||||||
|
|
||||||
private:
|
|
||||||
VirtualEndpoint::Ptr m_Endpoint;
|
|
||||||
bool m_Syncing;
|
|
||||||
|
|
||||||
void NewEndpointHandler(const Endpoint::Ptr& endpoint);
|
|
||||||
void SessionEstablishedHandler(const Endpoint::Ptr& endpoint);
|
|
||||||
|
|
||||||
void LocalObjectCommittedHandler(const ConfigObject::Ptr& object);
|
|
||||||
void LocalObjectRemovedHandler(const ConfigObject::Ptr& object);
|
|
||||||
|
|
||||||
void FetchObjectsHandler(const Endpoint::Ptr& sender);
|
|
||||||
void RemoteObjectCommittedHandler(const Endpoint::Ptr& sender, const RequestMessage& request);
|
|
||||||
void RemoteObjectRemovedHandler(const RequestMessage& request);
|
|
||||||
|
|
||||||
static RequestMessage MakeObjectMessage(const ConfigObject::Ptr& object,
|
|
||||||
string method, bool includeProperties);
|
|
||||||
|
|
||||||
static bool ShouldReplicateObject(const ConfigObject::Ptr& object);
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* CONFIGRPCCOMPONENT_H */
|
|
|
@ -1,35 +0,0 @@
|
||||||
/******************************************************************************
|
|
||||||
* Icinga 2 *
|
|
||||||
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
||||||
* *
|
|
||||||
* 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. *
|
|
||||||
******************************************************************************/
|
|
||||||
|
|
||||||
#ifndef I2CONFIGRPC_H
|
|
||||||
#define I2CONFIGRPC_H
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @defgroup configrpc ConfigRpc component
|
|
||||||
*
|
|
||||||
* The ConfigRpc component replicates configuration objects to other peers.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <i2-base.h>
|
|
||||||
#include <i2-jsonrpc.h>
|
|
||||||
#include <i2-icinga.h>
|
|
||||||
|
|
||||||
#include "configrpccomponent.h"
|
|
||||||
|
|
||||||
#endif /* I2CONFIGRPC_H */
|
|
|
@ -72,7 +72,6 @@ components/checker/Makefile
|
||||||
components/cibsync/Makefile
|
components/cibsync/Makefile
|
||||||
components/compat/Makefile
|
components/compat/Makefile
|
||||||
components/configfile/Makefile
|
components/configfile/Makefile
|
||||||
components/configrpc/Makefile
|
|
||||||
components/delegation/Makefile
|
components/delegation/Makefile
|
||||||
components/demo/Makefile
|
components/demo/Makefile
|
||||||
components/discovery/Makefile
|
components/discovery/Makefile
|
||||||
|
|
Loading…
Reference in New Issue