From 2d8d7af051864e6a93cbfe5f9c2c22df3ecb3b0c Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 16 Jun 2016 14:22:54 +0200 Subject: [PATCH] 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 284849be..56f57658 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) - ); - } - } - } }