[icinga-checkins] icinga.org: icingaweb2-module-director/fiddle/mfrosch: ConfigJob: add quite some logic, rework form

git at icinga.org git at icinga.org
Tue Jun 7 15:59:12 CEST 2016


Module: icingaweb2-module-director
Branch: fiddle/mfrosch
Commit: a279ef1b56217cbbb0939430406458bca0078e15
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=a279ef1b56217cbbb0939430406458bca0078e15

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed May 25 11:37:38 2016 +0200

ConfigJob: add quite some logic, rework form

---

 library/Director/Job/ConfigJob.php |  141 ++++++++++++++++++++++++++++++++++--
 1 file changed, 133 insertions(+), 8 deletions(-)

diff --git a/library/Director/Job/ConfigJob.php b/library/Director/Job/ConfigJob.php
index 585f75e..284849b 100644
--- a/library/Director/Job/ConfigJob.php
+++ b/library/Director/Job/ConfigJob.php
@@ -3,19 +3,141 @@
 namespace Icinga\Module\Director\Job;
 
 use Icinga\Application\Benchmark;
+use Icinga\Exception\IcingaException;
 use Icinga\Module\Director\IcingaConfig\IcingaConfig;
 use Icinga\Module\Director\Hook\JobHook;
-use Icinga\Module\Director\Web\Form\QuickForm;
+use Icinga\Module\Director\Objects\DirectorDeploymentLog;
 use Icinga\Module\Director\Util;
+use Icinga\Module\Director\Web\Form\QuickForm;
 
 class ConfigJob extends JobHook
 {
+    protected $lastDeployment;
+
+    protected $api;
+
     public function run()
     {
+        $db = $this->db();
+
+        if ($this->shouldGenerate()) {
+            $config = IcingaConfig::generate($db);
+        } else {
+            $config = $this->loadLatestActivityConfig();
+        }
+
+        if ($this->shouldDeploy($config)) {
+            $this->deploy($config);
+        }
+    }
+
+    protected function api()
+    {
+        if ($this->api === null) {
+            $this->api = $this->db()->getDeploymentEndpoint()->api();
+        }
+
+        return $this->api;
+    }
+
+    protected function loadLatestActivityConfig()
+    {
+        $db = $this->db();
+
+        return IcingaConfig::loadByActivityChecksum(
+            $db->getLastActivityChecksum(),
+            $db
+        );
+    }
+
+    protected function shouldGenerate()
+    {
+        return $this->getSetting('force_generate')
+                // -> last config?!
+            || $this->db()->countActivitiesSinceLastDeployedConfig() > 0;
+    }
+
+    protected function shouldDeploy(IcingaConfig $config)
+    {
+        $db = $this->db();
+
+        if ($this->getSetting('deploy_when_changed') !== 'y') {
+            return false;
+        }
+        $api = $this->api();
+        $api->collectLogFiles($db);
+
+        if (! DirectorDeploymentLog::hasDeployments($db)) {
+            return true;
+        }
+
+        if ($this->isWithinGracePeriod()) {
+            return false;
+        }
+
+        if ($this->lastDeployment()->configEquals($config)) {
+            return false;
+        }
+
+        // $current = $api->getActiveChecksum($db);
+        // TODO: no current, but last deployment
+        return true;
+    }
+
+    protected function deploy(IcingaConfig $config)
+    {
+        $db = $this->db();
+        $api = $this->api();
+        $api->wipeInactiveStages($db);
+
+        $checksum = $config->getHexChecksum();
+        if ($api->dumpConfig($config, $db)) {
+            $this->printf("Config '%s' has been deployed\n", $checksum);
+            $api->collectLogFiles($db);
+        } else {
+            $this->fail(sprintf("Failed to deploy config '%s'\n", $checksum));
+        }
+    }
+
+    protected function getGracePeriodStart()
+    {
+        return time() - $this->getSetting('grace_period');
+    }
+
+    protected function isWithinGracePeriod()
+    {
+        if ($deployment = $this->lastDeployment()) {
+            return $deployment->getDeploymentTimestamp() > $this->getGracePeriodStart();
+        }
+
+        return false;
+    }
+
+    protected function lastDeployment()
+    {
+        if ($this->lastDeployment === null) {
+            $this->lastDeployment = DirectorDeploymentLog::loadLatest($this->db());
+        }
+
+        return $this->lastDeployment;
     }
 
     public static function addSettingsFormFields(QuickForm $form)
     {
+        $form->addElement('select', 'force_generate', array(
+            'label'        => $form->translate('Force rendering'),
+            'description'  => $form->translate(
+                'Whether rendering should be forced. If not enforced, this'
+                . ' job re-renders the configuration only when there have been'
+                . ' activities since the last rendered config'
+            ),
+            'value'        => 'n',
+            'multiOptions' => array(
+                'y'  => $form->translate('Yes'),
+                'n'  => $form->translate('No'),
+            )
+        ));
+
         $form->addElement('select', 'deploy_when_changed', array(
             'label'        => $form->translate('Deploy modified config'),
             'description'  => $form->translate(
@@ -28,13 +150,16 @@ class ConfigJob extends JobHook
             )
         ));
 
-        return $form;
-    }
-
+        $form->addElement('text', 'grace_period', array(
+            'label' => $form->translate('Grace period'),
+            'description' => $form->translate(
+                'When deploying configuration, wait at least this amount of'
+                . ' seconds unless the next deployment should take place'
+            ),
+            'value' => 600,
+        ));
 
-    public function isPending()
-    {
-        return false;
+        return $form;
     }
 
     public static function getDescription(QuickForm $form)
@@ -48,7 +173,7 @@ class ConfigJob extends JobHook
     /**
      * Re-render the current configuration
      */
-    public function renderAction()
+    public function renderConfig()
     {
         $config = new IcingaConfig($this->db());
         Benchmark::measure('Rendering config');



More information about the icinga-checkins mailing list