]> granicus.if.org Git - icinga2/commitdiff
Implement initial api object sync for newly connected endpoints
authorMichael Friedrich <michael.friedrich@netways.de>
Tue, 15 Sep 2015 14:09:56 +0000 (16:09 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 17 Sep 2015 12:20:44 +0000 (14:20 +0200)
TODO: Figure out how to deal with Shutdown() deactivating and
therefore deleting all api created objects.

refs #9927

lib/remote/apilistener-configsync.cpp
lib/remote/apilistener-filesync.cpp
lib/remote/apilistener.cpp
lib/remote/apilistener.hpp

index f244938ae4917bb64c3740a8d6b4c279a2376440..a0e992971918561c81b747eeda322bbdbc88d9fa 100644 (file)
@@ -36,6 +36,7 @@ REGISTER_APIFUNCTION(DeleteObject, config, &ApiListener::ConfigDeleteObjectAPIHa
 
 void ApiListener::StaticInitialize(void)
 {
+       //TODO: Figure out how to delete objects during runtime, but not on shutdown (object inactive)
        ConfigObject::OnActiveChanged.connect(&ApiListener::ConfigUpdateObjectHandler);
        ConfigObject::OnVersionChanged.connect(&ApiListener::ConfigUpdateObjectHandler);
 }
@@ -293,3 +294,35 @@ void ApiListener::DeleteConfigObject(const ConfigObject::Ptr& object, const Mess
        else
                RelayMessage(origin, object, message, false);
 }
+
+void ApiListener::SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient)
+{
+       Endpoint::Ptr endpoint = aclient->GetEndpoint();
+       ASSERT(endpoint);
+
+       Zone::Ptr azone = endpoint->GetZone();
+       Zone::Ptr lzone = Zone::GetLocalZone();
+
+       /* only sync objects in the same zone for now */
+       if (azone->GetName() != lzone->GetName()) {
+               Log(LogWarning, "ApiListener")
+                   << "Skipping object sync to endpoint '" << endpoint->GetName()
+                   << "' in zone '" << azone->GetName() << "'. Not in the same zone '"
+                   << lzone->GetName() << "'.";
+               return;
+       }
+
+       Log(LogInformation, "ApiListener")
+           << "Syncing runtime objects to endpoint '" << endpoint->GetName() << "'.";
+
+       //TODO get the active stage for "_api" and all objects instead of fetching all objects in memory?
+       BOOST_FOREACH(const ConfigType::Ptr& dt, ConfigType::GetTypes()) {
+               BOOST_FOREACH(const ConfigObject::Ptr& object, dt->GetObjects()) {
+                       if (object->GetPackage() != "_api")
+                               continue;
+
+                       /* send the config object to the connected client */
+                       UpdateConfigObject(object, MessageOrigin::Ptr(), aclient);
+               }
+       }
+}
index 7a998be4877952ce3f3fafe5d6bb9b214e7000c9..74b55e020666ddba8c476864aad952e2962fde63 100644 (file)
@@ -188,9 +188,9 @@ void ApiListener::SendConfigUpdate(const JsonRpcConnection::Ptr& aclient)
                        continue;
                }
 
-               if (zone->IsGlobal())
-                       Log(LogInformation, "ApiListener")
-                           << "Syncing global zone '" << zone->GetName() << "'.";
+               Log(LogInformation, "ApiListener")
+                   << "Syncing " << (zone->IsGlobal() ? "global " : "")
+                   << "zone '" << zone->GetName() << "' to endpoint '" << endpoint->GetName() << "'.";
 
                configUpdate->Set(zone->GetName(), LoadConfigDir(zonesDir + "/" + zone->GetName()));
        }
index 78db2575640653cc3b7580cc3730a4aef7b1a6a8..e2598333213a56e4de93fabc71e4a98e1fe7f180 100644 (file)
@@ -380,7 +380,10 @@ void ApiListener::NewClientHandlerInternal(const Socket::Ptr& client, const Stri
                                ReplayLog(aclient);
                        }
 
+                       /* sync zone file config */
                        SendConfigUpdate(aclient);
+                       /* sync runtime config */
+                       SendRuntimeConfigObjects(aclient);
                } else
                        AddAnonymousClient(aclient);
        } else {
index 88943a18ff0efb81461abce1475ce04c4c485da2..786714b5aa7c3702279cb84e5effe29683984164 100644 (file)
@@ -136,6 +136,7 @@ private:
            const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
        void DeleteConfigObject(const ConfigObject::Ptr& object, const MessageOrigin::Ptr& origin,
            const JsonRpcConnection::Ptr& client = JsonRpcConnection::Ptr());
+       void SendRuntimeConfigObjects(const JsonRpcConnection::Ptr& aclient);
 };
 
 }