[icinga-checkins] icinga.org: icingaweb2-module-director/master: ConfigJob: logic fixes, code cleanup

git at icinga.org git at icinga.org
Thu Jun 16 15:09:51 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 2d8d7af051864e6a93cbfe5f9c2c22df3ecb3b0c
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=2d8d7af051864e6a93cbfe5f9c2c22df3ecb3b0c

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Jun 16 14:22:54 2016 +0200

ConfigJob: logic fixes, code cleanup

---

 library/Director/Job/ConfigJob.php |   95 ++++++++++--------------------------
 1 file changed, 27 insertions(+), 68 deletions(-)

diff --git a/library/Director/Job/ConfigJob.php b/library/Director/Job/ConfigJob.php
index 284849b..56f5765 100644
--- a/library/Director/Job/ConfigJob.php
+++ b/library/Director/Job/ConfigJob.php
@@ -60,7 +60,6 @@ class ConfigJob extends JobHook
     protected function shouldDeploy(IcingaConfig $config)
     {
         $db = $this->db();
-
         if ($this->getSetting('deploy_when_changed') !== 'y') {
             return false;
         }
@@ -75,7 +74,7 @@ class ConfigJob extends JobHook
             return false;
         }
 
-        if ($this->lastDeployment()->configEquals($config)) {
+        if ($this->getActiveChecksum() === $config->getChecksum()) {
             return false;
         }
 
@@ -86,6 +85,7 @@ class ConfigJob extends JobHook
 
     protected function deploy(IcingaConfig $config)
     {
+        $this->clearLastDeployment();
         $db = $this->db();
         $api = $this->api();
         $api->wipeInactiveStages($db);
@@ -104,6 +104,17 @@ class ConfigJob extends JobHook
         return time() - $this->getSetting('grace_period');
     }
 
+    public function getRemainingGraceTime()
+    {
+        if ($this->isWithinGracePeriod()) {
+            return $deployment->getDeploymentTimestamp()
+                + $this->getSetting('grace_period'))
+                - time();
+        }
+
+        return 0;
+    }
+
     protected function isWithinGracePeriod()
     {
         if ($deployment = $this->lastDeployment()) {
@@ -113,6 +124,14 @@ class ConfigJob extends JobHook
         return false;
     }
 
+    protected function getActiveChecksum()
+    {
+        return DirectorDeploymentLog::getConfigChecksumForStageName(
+            $db,
+            $api->getActiveStageName()
+        );
+    }
+
     protected function lastDeployment()
     {
         if ($this->lastDeployment === null) {
@@ -122,6 +141,12 @@ class ConfigJob extends JobHook
         return $this->lastDeployment;
     }
 
+    protected function clearLastDeployment()
+    {
+        $this->lastDeployment = null;
+        return $this;
+    }
+
     public static function addSettingsFormFields(QuickForm $form)
     {
         $form->addElement('select', 'force_generate', array(
@@ -169,70 +194,4 @@ class ConfigJob extends JobHook
             . 'Icinga 2 configuration'
         );
     }
-
-    /**
-     * Re-render the current configuration
-     */
-    public function renderConfig()
-    {
-        $config = new IcingaConfig($this->db());
-        Benchmark::measure('Rendering config');
-        if ($config->hasBeenModified()) {
-            Benchmark::measure('Config rendered, storing to db');
-            $config->store();
-            Benchmark::measure('All done');
-            $checksum = $config->getHexChecksum();
-            $this->printf(
-                "New config with checksum %s has been generated\n",
-                $checksum
-            );
-        } else {
-            $checksum = $config->getHexChecksum();
-            $this->printf(
-                "Config with checksum %s already exists\n",
-                $checksum
-            );
-        }
-    }
-
-    /**
-     * Deploy the current configuration
-     *
-     * Does nothing if config didn't change unless you provide
-     * the --force parameter
-     */
-    public function deployAction()
-    {
-        $api = $this->api();
-        $db = $this->db();
-
-        $checksum = $this->params->get('checksum');
-        if ($checksum) {
-            $config = IcingaConfig::load(Util::hex2binary($checksum), $db);
-        } else {
-            $config = IcingaConfig::generate($db);
-            $checksum = $config->getHexChecksum();
-        }
-
-        $api->wipeInactiveStages($db);
-        $current = $api->getActiveChecksum($db);
-        if ($current === $checksum) {
-            if ($this->params->get('force')) {
-                echo "Config matches active stage, deploying anyway\n";
-            } else {
-                echo "Config matches active stage, nothing to do\n";
-
-                return;
-            }
-
-        } else {
-            if ($api->dumpConfig($config, $db)) {
-                $this->printf("Config '%s' has been deployed\n", $checksum);
-            } else {
-                $this->fail(
-                    sprintf("Failed to deploy config '%s'\n", $checksum)
-                );
-            }
-        }
-    }
 }



More information about the icinga-checkins mailing list