From 97513965e6b025aac527ba8be3be5daaff66c9be Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 10 Aug 2018 10:15:41 +0200 Subject: [PATCH] Introduce IcingaApplication#environment Precedence as follows: - DEnvironment=... - const Environment = ... - object IcingaApplication "app" { environment = "..." } The wrapped script constant handling is required since we cannot directly link from libremote (SNI handling) to libicinga where the object resides. Instead we'll use the Application class helpers for hiding the ScriptGlobal calls. --- lib/base/CMakeLists.txt | 2 +- lib/base/application-environment.cpp | 34 ++++++++++++++++++++++++++++ lib/base/application.hpp | 3 +++ lib/icinga/icingaapplication.cpp | 10 ++++++++ lib/icinga/icingaapplication.hpp | 3 +++ lib/icinga/icingaapplication.ti | 6 +++++ lib/remote/apilistener.cpp | 5 ++-- lib/remote/apilistener.ti | 4 ---- 8 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 lib/base/application-environment.cpp diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index c16cda028..e784a3957 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -27,7 +27,7 @@ mkclass_target(sysloglogger.ti sysloglogger-ti.cpp sysloglogger-ti.hpp) set(base_SOURCES i2-base.hpp - application.cpp application.hpp application-ti.hpp application-version.cpp + application.cpp application.hpp application-ti.hpp application-version.cpp application-environment.cpp array.cpp array.hpp array-script.cpp base64.cpp base64.hpp boolean.cpp boolean.hpp boolean-script.cpp diff --git a/lib/base/application-environment.cpp b/lib/base/application-environment.cpp new file mode 100644 index 000000000..a70ea886f --- /dev/null +++ b/lib/base/application-environment.cpp @@ -0,0 +1,34 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) * + * * + * 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 "base/application.hpp" +#include "base/scriptglobal.hpp" + +using namespace icinga; + +String Application::GetAppEnvironment() +{ + Value defaultValue = Empty; + return ScriptGlobal::Get("Environment", &defaultValue); +} + +void Application::SetAppEnvironment(const String& name) +{ + ScriptGlobal::Set("Environment", name); +} diff --git a/lib/base/application.hpp b/lib/base/application.hpp index dbb14c38d..88fc51ab6 100644 --- a/lib/base/application.hpp +++ b/lib/base/application.hpp @@ -107,6 +107,9 @@ public: static String GetAppVersion(); static String GetAppSpecVersion(); + static String GetAppEnvironment(); + static void SetAppEnvironment(const String& name); + static double GetStartTime(); static void SetStartTime(double ts); diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp index b792cdfe7..20a800514 100644 --- a/lib/icinga/icingaapplication.cpp +++ b/lib/icinga/icingaapplication.cpp @@ -293,6 +293,16 @@ String IcingaApplication::GetNodeName() const return ScriptGlobal::Get("NodeName"); } +String IcingaApplication::GetEnvironment() const +{ + return Application::GetAppEnvironment(); +} + +void IcingaApplication::SetEnvironment(const String& value, bool suppress_events, const Value& cookie) +{ + Application::SetAppEnvironment(value); +} + void IcingaApplication::ValidateVars(const Lazy& lvalue, const ValidationUtils& utils) { MacroProcessor::ValidateCustomVars(this, lvalue()); diff --git a/lib/icinga/icingaapplication.hpp b/lib/icinga/icingaapplication.hpp index eac7f5903..ba8b77cf3 100644 --- a/lib/icinga/icingaapplication.hpp +++ b/lib/icinga/icingaapplication.hpp @@ -50,6 +50,9 @@ public: String GetNodeName() const; + String GetEnvironment() const override; + void SetEnvironment(const String& value, bool suppress_events = false, const Value& cookie = Empty) override; + void ValidateVars(const Lazy& lvalue, const ValidationUtils& utils) override; private: diff --git a/lib/icinga/icingaapplication.ti b/lib/icinga/icingaapplication.ti index 61d4ecd4c..b9f0381b3 100644 --- a/lib/icinga/icingaapplication.ti +++ b/lib/icinga/icingaapplication.ti @@ -26,6 +26,12 @@ namespace icinga class IcingaApplication : Application { + [config, no_storage, virtual] String environment { + get; + set; + default {{{ return Application::GetAppEnvironment(); }}} + }; + [config] bool enable_notifications { default {{{ return true; }}} }; diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index 5e0d2a1bf..8c0abf570 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -441,10 +441,11 @@ void ApiListener::NewClientHandlerInternal(const Socket::Ptr& client, const Stri TlsStream::Ptr tlsStream; - String environmentName = GetEnvironment(); + String environmentName = Application::GetAppEnvironment(); + String serverName = hostname; - if (!environmentName.IsEmpty() && environmentName != "") + if (!environmentName.IsEmpty()) serverName += ":" + environmentName; { diff --git a/lib/remote/apilistener.ti b/lib/remote/apilistener.ti index 05a330990..04e483270 100644 --- a/lib/remote/apilistener.ti +++ b/lib/remote/apilistener.ti @@ -48,10 +48,6 @@ class ApiListener : ConfigObject default {{{ return Application::GetConst("ApiBindPort", "5665"); }}} }; - [config] String environment { - default {{{ return Application::GetConst("Environment"); }}} - }; - [config] bool accept_config; [config] bool accept_commands;