]> granicus.if.org Git - icinga2/commitdiff
Rework sysconfig file
authorJean Flach <jean-marcel.flach@icinga.com>
Wed, 23 May 2018 11:37:29 +0000 (13:37 +0200)
committerJean Flach <jean-marcel.flach@icinga.com>
Wed, 23 May 2018 13:18:42 +0000 (15:18 +0200)
The sysconfig file now only contains the defaults as comments, changes
made there are given to the init.d script, prepare-dirs, safe-reload and
Icinga 2 itself. If nothing is set in the sysconfig file (as is the
default as all lines are commented out) the defaults are used.

fixes #6255

etc/initsystem/CMakeLists.txt
etc/initsystem/icinga2.init.d.cmake
etc/initsystem/icinga2.sysconfig.cmake
etc/initsystem/prepare-dirs.cmake [moved from etc/initsystem/prepare-dirs with 59% similarity]
etc/initsystem/safe-reload.cmake [moved from etc/initsystem/safe-reload with 73% similarity]
icinga-app/icinga.cpp
lib/base/utility.cpp
lib/base/utility.hpp

index e1bebb0097650ccac9f23ab168f6d04207330c0c..dd57ab1a0ddfd7a9ca4c279d92dfda1359ab6c2e 100644 (file)
@@ -15,6 +15,7 @@
 # along with this program; if not, write to the Free Software Foundation
 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
+
 if(NOT WIN32)
   configure_file(icinga2.sysconfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/initsystem/icinga2.sysconfig @ONLY)
   get_filename_component(ICINGA2_SYSCONFIGFILE_NAME ${ICINGA2_SYSCONFIGFILE} NAME)
@@ -26,8 +27,10 @@ if(NOT WIN32)
     PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ
   )
 
+  configure_file(prepare-dirs.cmake ${CMAKE_CURRENT_BINARY_DIR}/initsystem/prepare-dirs @ONLY)
+  configure_file(safe-reload.cmake ${CMAKE_CURRENT_BINARY_DIR}/initsystem/safe-reload @ONLY)
   install(
-    FILES prepare-dirs safe-reload
+    FILES ${CMAKE_CURRENT_BINARY_DIR}/initsystem/prepare-dirs ${CMAKE_CURRENT_BINARY_DIR}/initsystem/safe-reload
     DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/icinga2
     PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
   )
@@ -37,7 +40,7 @@ if(NOT WIN32)
     "Force install both the systemd service definition file and the SysV initscript in parallel, regardless of how USE_SYSTEMD is set. Only use this for special packaging purposes and if you know what you are doing" OFF)
 
   if (NOT USE_SYSTEMD OR INSTALL_SYSTEMD_SERVICE_AND_INITSCRIPT)
-    configure_file(icinga2.init.d.cmake ${CMAKE_CURRENT_BINARY_DIR}/initsystem/icinga2)
+         configure_file(icinga2.init.d.cmake ${CMAKE_CURRENT_BINARY_DIR}/initsystem/icinga2 @ONLY)
     install(
       FILES ${CMAKE_CURRENT_BINARY_DIR}/initsystem/icinga2
       DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/init.d
index bc41ccf67ad7852a218ae95873e519a673562aba..03c88a632cbb174e0ac5a116bae7a74d26e7a3e3 100644 (file)
@@ -20,24 +20,29 @@ SYSCONFIGFILE=@ICINGA2_SYSCONFIGFILE@
 if [ -f $SYSCONFIGFILE ]; then
        . $SYSCONFIGFILE
 else
-       echo "Can't load system specific defines from $SYSCONFIGFILE."
-       exit 6
+       echo "Couldn't load system specific defines from $SYSCONFIGFILE. Using defaults."
 fi
 
-test -x $DAEMON || exit 5
+# Set defaults, to overwrite see "@ICINGA2_SYSCONFIGFILE@"
 
-if [ ! -e $ICINGA2_CONFIG_FILE ]; then
-       echo "Config file '$ICINGA2_CONFIG_FILE' does not exist."
-       exit 6
-fi
+: ${ICINGA2_USER:="@ICINGA2_USER@"}
+: ${ICINGA2_GROUP:="@ICINGA2_GROUP@"}
+: ${ICINGA2_COMMAND_GROUP:="@ICINGA2_COMMAND_GROUP@"}
+: ${DAEMON:="@CMAKE_INSTALL_FULL_SBINDIR@/icinga2"}
+: ${ICINGA2_CONFIG_FILE:="@CMAKE_INSTALL_FULL_SYSCONFDIR@/icinga2/icinga2.conf"}
+: ${ICINGA2_ERROR_LOG:=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/error.log}
+: ${ICINGA2_STARTUP_LOG:=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/startup.log}
+: ${ICINGA2_PID_FILE:="@ICINGA2_RUNDIR@/icinga2/icinga2.pid"}
 
-if [ ! $ICINGA2_USER ]; then
-       echo "Could not fetch \$ICINGA2_USER. Exiting."
-       exit 6
+# Load extra environment variables
+if [ -f /etc/default/icinga2 ]; then
+       . /etc/default/icinga2
 fi
 
-if [ ! $ICINGA2_GROUP ]; then
-       echo "Could not fetch \$ICINGA2_GROUP. Exiting."
+test -x $DAEMON || exit 5
+
+if [ ! -e $ICINGA2_CONFIG_FILE ]; then
+       echo "Config file '$ICINGA2_CONFIG_FILE' does not exist."
        exit 6
 fi
 
@@ -52,15 +57,10 @@ elif [ -f /etc/init.d/functions ]; then
        . /etc/init.d/functions
 fi
 
-# Load extra environment variables
-if [ -f /etc/default/icinga2 ]; then
-       . /etc/default/icinga2
-fi
-
 # Start Icinga 2
 start() {
        printf "Starting Icinga 2: "
-       @CMAKE_INSTALL_PREFIX@/lib/icinga2/prepare-dirs $SYSCONFIGFILE
+       @CMAKE_INSTALL_PREFIX@/lib/icinga2/prepare-dirs
 
        if ! $DAEMON daemon -c $ICINGA2_CONFIG_FILE -d -e $ICINGA2_ERROR_LOG > $ICINGA2_STARTUP_LOG 2>&1; then
                echo "Error starting Icinga. Check '$ICINGA2_STARTUP_LOG' for details."
index a8335b4eb6939fa5221a1429d6a9fd06152e93ce..d5d7497f173092c6b56791b5cfaba1450178d236 100644 (file)
@@ -1,13 +1,16 @@
-DAEMON=@CMAKE_INSTALL_FULL_SBINDIR@/icinga2
-ICINGA2_CONFIG_FILE=@CMAKE_INSTALL_FULL_SYSCONFDIR@/icinga2/icinga2.conf
-ICINGA2_RUN_DIR=@ICINGA2_RUNDIR@
-ICINGA2_STATE_DIR=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@
-ICINGA2_PID_FILE=@ICINGA2_RUNDIR@/icinga2/icinga2.pid
-ICINGA2_LOG_DIR=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2
-ICINGA2_ERROR_LOG=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/error.log
-ICINGA2_STARTUP_LOG=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/startup.log
-ICINGA2_LOG=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/icinga2.log
-ICINGA2_CACHE_DIR=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/cache/icinga2
-ICINGA2_USER=@ICINGA2_USER@
-ICINGA2_GROUP=@ICINGA2_GROUP@
-ICINGA2_COMMAND_GROUP=@ICINGA2_COMMAND_GROUP@
+#This is the default environment Icinga 2 runs with.
+#Make your changes here.
+
+#DAEMON=@CMAKE_INSTALL_FULL_SBINDIR@/icinga2
+#ICINGA2_CONFIG_FILE=@CMAKE_INSTALL_FULL_SYSCONFDIR@/icinga2/icinga2.conf
+#ICINGA2_RUN_DIR=@ICINGA2_RUNDIR@
+#ICINGA2_STATE_DIR=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@
+#ICINGA2_PID_FILE=@ICINGA2_RUNDIR@/icinga2/icinga2.pid
+#ICINGA2_LOG_DIR=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2
+#ICINGA2_ERROR_LOG=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/error.log
+#ICINGA2_STARTUP_LOG=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/startup.log
+#ICINGA2_LOG=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2/icinga2.log
+#ICINGA2_CACHE_DIR=@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/cache/icinga2
+#ICINGA2_USER=@ICINGA2_USER@
+#ICINGA2_GROUP=@ICINGA2_GROUP@
+#ICINGA2_COMMAND_GROUP=@ICINGA2_COMMAND_GROUP@
similarity index 59%
rename from etc/initsystem/prepare-dirs
rename to etc/initsystem/prepare-dirs.cmake
index 74bb85a5be167e13a24f87ee015e597e78f00857..7465d6619e7cf814dd73151ad2a44a8f6511db8e 100644 (file)
@@ -3,25 +3,15 @@
 # This script prepares directories and files needed for running Icinga2
 #
 
-# load system specific defines
-SYSCONFIGFILE=$1
-if [ -f "$SYSCONFIGFILE" ]; then
-       . $SYSCONFIGFILE
-else
-       echo "Error: You need to supply the path to the Icinga2 sysconfig file as parameter."
-       exit 1
-fi
-
+# Set defaults, to overwrite see "@ICINGA2_SYSCONFIGFILE@"
 
-if [ ! $ICINGA2_USER ]; then
-       echo "Could not fetch \$ICINGA2_USER. Exiting."
-       exit 6
-fi
-
-if [ ! $ICINGA2_GROUP ]; then
-       echo "Could not fetch \$ICINGA2_GROUP. Exiting."
-       exit 6
-fi
+: ${ICINGA2_USER:="@ICINGA2_USER@"}
+: ${ICINGA2_GROUP:="@ICINGA2_GROUP@"}
+: ${ICINGA2_COMMAND_GROUP:="@ICINGA2_COMMAND_GROUP@"}
+: ${ICINGA2_RUN_DIR:="@ICINGA2_RUNDIR@"}
+: ${ICINGA2_LOG_DIR:="@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/log/icinga2"}
+: ${ICINGA2_STATE_DIR:="@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/cache/icinga2"}
+: ${ICINGA2_CACHE_DIR:="@CMAKE_INSTALL_FULL_LOCALSTATEDIR@/cache/icinga2"}
 
 getent passwd $ICINGA2_USER >/dev/null 2>&1 || (echo "Icinga user '$ICINGA2_USER' does not exist. Exiting." && exit 6)
 getent group $ICINGA2_GROUP >/dev/null 2>&1 || (echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting." && exit 6)
@@ -35,20 +25,10 @@ if [ ! -e "$ICINGA2_RUN_DIR"/icinga2 ]; then
        chown -R $ICINGA2_USER:$ICINGA2_COMMAND_GROUP "$ICINGA2_RUN_DIR"/icinga2
 fi
 
-# Could be undefined in installations where sysconf is not overridden on upgrade
-if [ -z "$ICINGA2_LOG_DIR" ]; then
-       ICINGA2_LOG_DIR=$(dirname -- "$ICINGA2_LOG")
-fi
-
 test -e "$ICINGA2_LOG_DIR" || install -m 750 -o $ICINGA2_USER -g $ICINGA2_COMMAND_GROUP -d "$ICINGA2_LOG_DIR"
 
 if type restorecon >/dev/null 2>&1; then
        restorecon -R "$ICINGA2_RUN_DIR"/icinga2/
 fi
 
-# Add a fallback if the user did not specify this directory in the sysconfig file
-if [ -z "$ICINGA2_CACHE_DIR" ]; then
-       ICINGA2_CACHE_DIR="$ICINGA2_STATE_DIR"/cache/icinga2
-fi
-
 test -e "$ICINGA2_CACHE_DIR" || install -m 750 -o $ICINGA2_USER -g $ICINGA2_COMMAND_GROUP -d "$ICINGA2_CACHE_DIR"
similarity index 73%
rename from etc/initsystem/safe-reload
rename to etc/initsystem/safe-reload.cmake
index 2fd44b73d3b77625bd8936a0626d8e726e0d195a..57e10b1aca358413a18a1c1b0a08ae33d22a3fce 100644 (file)
@@ -1,12 +1,7 @@
 #!/bin/sh
-# load system specific defines
-SYSCONFIGFILE=$1
-if [ ! -f "$SYSCONFIGFILE" ]; then
-       echo "Error: You need to supply the path to the Icinga2 sysconfig file as a parameter."
-       exit 1
-fi
 
-. $SYSCONFIGFILE
+: ${ICINGA2_PID_FILE:="@ICINGA2_RUNDIR@/icinga2/icinga2.pid"}
+: ${DAEMON:="@CMAKE_INSTALL_FULL_SBINDIR@/icinga2"}
 
 printf "Validating config files: "
 
index 321d7f74fbbd4ba53ac0078dc474c2d47b684e87..44d7ab1320beec2030ca73fad499842f0d53cfa7 100644 (file)
@@ -162,11 +162,11 @@ static int Main()
        }
 #endif /* _WIN32 */
 
-       String icingaUser = Utility::GetFromSysconfig("ICINGA2_USER");
+       String icingaUser = Utility::GetFromEnvironment("ICINGA2_USER");
        if (icingaUser.IsEmpty())
                icingaUser = ICINGA_USER;
 
-       String icingaGroup = Utility::GetFromSysconfig("ICINGA2_GROUP");
+       String icingaGroup = Utility::GetFromEnvironment("ICINGA2_GROUP");
        if (icingaGroup.IsEmpty())
                icingaGroup = ICINGA_GROUP;
 
@@ -174,7 +174,7 @@ static int Main()
        Application::DeclareRunAsGroup(icingaGroup);
 
 #ifdef RLIMIT_NOFILE
-       String rLimitFiles = Utility::GetFromSysconfig("ICINGA2_RLIMIT_FILES");
+       String rLimitFiles = Utility::GetFromEnvironment("ICINGA2_RLIMIT_FILES");
        if (rLimitFiles.IsEmpty())
                Application::DeclareRLimitFiles(Application::GetDefaultRLimitFiles());
        else {
@@ -182,14 +182,14 @@ static int Main()
                        Application::DeclareRLimitFiles(Convert::ToLong(rLimitFiles));
                } catch (const std::invalid_argument& ex) {
                        std::cout
-                               << "Error while parsing \"ICINGA2_RLIMIT_FILES\" from sysconfig: " << ex.what() << '\n';
+                               << "Error setting \"ICINGA2_RLIMIT_FILES\": " << ex.what() << '\n';
                        return EXIT_FAILURE;
                }
        }
 #endif /* RLIMIT_NOFILE */
 
 #ifdef RLIMIT_NPROC
-       String rLimitProcesses = Utility::GetFromSysconfig("ICINGA2_RLIMIT_PROCESSES");
+       String rLimitProcesses = Utility::GetFromEnvironment("ICINGA2_RLIMIT_PROCESSES");
        if (rLimitProcesses.IsEmpty())
                Application::DeclareRLimitProcesses(Application::GetDefaultRLimitProcesses());
        else {
@@ -197,14 +197,14 @@ static int Main()
                        Application::DeclareRLimitProcesses(Convert::ToLong(rLimitProcesses));
                } catch (const std::invalid_argument& ex) {
                        std::cout
-                               << "Error while parsing \"ICINGA2_RLIMIT_PROCESSES\" from sysconfig: " << ex.what() << '\n';
+                               << "Error setting \"ICINGA2_RLIMIT_PROCESSES\": " << ex.what() << '\n';
                        return EXIT_FAILURE;
                }
        }
 #endif /* RLIMIT_NPROC */
 
 #ifdef RLIMIT_STACK
-       String rLimitStack = Utility::GetFromSysconfig("ICINGA2_RLIMIT_STACK");
+       String rLimitStack = Utility::GetFromEnvironment("ICINGA2_RLIMIT_STACK");
        if (rLimitStack.IsEmpty())
                Application::DeclareRLimitStack(Application::GetDefaultRLimitStack());
        else {
@@ -212,7 +212,7 @@ static int Main()
                        Application::DeclareRLimitStack(Convert::ToLong(rLimitStack));
                } catch (const std::invalid_argument& ex) {
                        std::cout
-                               << "Error while parsing \"ICINGA2_RLIMIT_STACK\" from sysconfig: " << ex.what() << '\n';
+                               << "Error setting \"ICINGA2_RLIMIT_STACK\": " << ex.what() << '\n';
                        return EXIT_FAILURE;
                }
        }
index 2d1fb896586446a3375d42988393fbb5935e4fb8..8d2162011b483cedbff59c7f7a7112c2210da47f 100644 (file)
@@ -1936,37 +1936,16 @@ String Utility::GetIcingaDataPath()
 
 #endif /* _WIN32 */
 
-String Utility::GetFromSysconfig(const String& env)
+String Utility::GetFromEnvironment(const String& env)
 {
 #ifndef _WIN32
-       String sysconf = Application::GetSysconfigFile();
-       if (sysconf.IsEmpty())
-               return "";
-
-       if (!Utility::PathExists(sysconf))
-               return "";
-
-       String cmdInner = ". " + EscapeShellArg(sysconf) + " 2>&1 >/dev/null;echo \"$" + env + "\"";
-       String cmd = "sh -c " + EscapeShellArg(cmdInner);
-
-       FILE *fp = popen(cmd.CStr(), "r");
-
-       if (!fp)
-               return "";
-
-       char line[1024];
-       String out;
-
-       if (fgets(line, sizeof(line), fp))
-               out = line;
+       const char *envValue = getenv(env.CStr());
+       if (envValue == NULL)
+               return String();
        else
-               return "";
-
-       pclose(fp);
-
-       return out.Trim();
-#else
-       //TODO: Figure out how to do this on windows
-       return "";
+               return String(envValue);
+#else /* _WIN32 */
+       // While getenv exists on Windows, we don't set them. Therefore there is no reason to read them.
+       return String();
 #endif /* _WIN32 */
 }
index 97bb834b89b5d076384ae8add6523187616f5e10..8311f8d54537477648d8fbad090b5b2f776ea483 100644 (file)
@@ -147,7 +147,7 @@ public:
        static String GetIcingaDataPath();
 #endif /* _WIN32 */
 
-       static String GetFromSysconfig(const String& env);
+       static String GetFromEnvironment(const String& env);
 
 #ifdef I2_DEBUG
        static void SetTime(double);