From cd989f9c3aff968e19a3aeabc4eb9085787a6673 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:43:12 +0200 Subject: Adding upstream version 1.10.2. Signed-off-by: Daniel Baumann --- .../Deployment/ConditionalConfigRenderer.php | 64 +++++++ .../Director/Deployment/ConditionalDeployment.php | 190 +++++++++++++++++++++ .../Director/Deployment/DeploymentGracePeriod.php | 61 +++++++ library/Director/Deployment/DeploymentInfo.php | 59 +++++++ library/Director/Deployment/DeploymentStatus.php | 164 ++++++++++++++++++ 5 files changed, 538 insertions(+) create mode 100644 library/Director/Deployment/ConditionalConfigRenderer.php create mode 100644 library/Director/Deployment/ConditionalDeployment.php create mode 100644 library/Director/Deployment/DeploymentGracePeriod.php create mode 100644 library/Director/Deployment/DeploymentInfo.php create mode 100644 library/Director/Deployment/DeploymentStatus.php (limited to 'library/Director/Deployment') diff --git a/library/Director/Deployment/ConditionalConfigRenderer.php b/library/Director/Deployment/ConditionalConfigRenderer.php new file mode 100644 index 0000000..0b24418 --- /dev/null +++ b/library/Director/Deployment/ConditionalConfigRenderer.php @@ -0,0 +1,64 @@ +db = $connection; + } + + public function forceRendering($force = true) + { + $this->forceRendering = $force; + + return $this; + } + + public function getConfig() + { + if ($this->shouldGenerate()) { + return IcingaConfig::generate($this->db); + } + + return $this->loadLatestActivityConfig(); + } + + protected function loadLatestActivityConfig() + { + $db = $this->db; + + return IcingaConfig::loadByActivityChecksum($db->getLastActivityChecksum(), $db); + } + + protected function shouldGenerate() + { + return $this->forceRendering || !$this->configForLatestActivityExists(); + } + + protected function configForLatestActivityExists() + { + $db = $this->db; + try { + $latestActivity = DirectorActivityLog::loadLatest($db); + } catch (NotFoundError $e) { + return false; + } + + return IcingaConfig::existsForActivityChecksum( + bin2hex($latestActivity->get('checksum')), + $db + ); + } +} diff --git a/library/Director/Deployment/ConditionalDeployment.php b/library/Director/Deployment/ConditionalDeployment.php new file mode 100644 index 0000000..0f64028 --- /dev/null +++ b/library/Director/Deployment/ConditionalDeployment.php @@ -0,0 +1,190 @@ +setLogger(new NullLogger()); + $this->db = $connection; + if ($api === null) { + $this->api = $connection->getDeploymentEndpoint()->api(); + } else { + $this->api = $api; + } + $this->refresh(); + } + + /** + * @param IcingaConfig $config + * @return ?DirectorDeploymentLog + */ + public function deploy(IcingaConfig $config) + { + $this->hasBeenForced = false; + if ($this->shouldDeploy($config)) { + return $this->reallyDeploy($config); + } elseif ($this->force) { + $deployment = $this->reallyDeploy($config); + $this->hasBeenForced = true; + + return $deployment; + } + + return null; + } + + /** + * @param bool $force + * @return $this + */ + public function force($force = true) + { + $this->force = $force; + return $this; + } + + public function setGracePeriod(DeploymentGracePeriod $gracePeriod) + { + $this->gracePeriod = $gracePeriod; + return $this; + } + + public function refresh() + { + $this->api->collectLogFiles($this->db); + $this->api->wipeInactiveStages($this->db); + } + + public function waitForStartupAfterDeploy(DirectorDeploymentLog $deploymentLog, $timeout) + { + $startTime = time(); + while ((time() - $startTime) <= $timeout) { + $deploymentFromDB = DirectorDeploymentLog::load($deploymentLog->getId(), $this->db); + $stageCollected = $deploymentFromDB->get('stage_collected'); + if ($stageCollected === null) { + usleep(500000); + continue; + } + if ($stageCollected === 'n') { + return 'stage has not been collected (Icinga "lost" the deployment)'; + } + if ($deploymentFromDB->get('startup_succeeded') === 'y') { + return true; + } + return 'deployment failed during startup (usually a Configuration Error)'; + } + return 'deployment timed out (while waiting for an Icinga restart)'; + } + + /** + * @return string|null + */ + public function getNoDeploymentReason() + { + return $this->noDeploymentReason; + } + + public function hasBeenForced() + { + return $this->hasBeenForced; + } + + protected function shouldDeploy(IcingaConfig $config) + { + $this->noDeploymentReason = null; + if ($this->hasNeverDeployed()) { + return true; + } + + if ($this->isWithinGracePeriod()) { + $this->noDeploymentReason = 'Grace period is active'; + return false; + } + + if ($this->deployedConfigMatches($config)) { + $this->noDeploymentReason = 'Config matches last deployed one'; + return false; + } + + if ($this->getActiveChecksum() === $config->getHexChecksum()) { + $this->noDeploymentReason = 'Config matches active stage'; + return false; + } + + return true; + } + + protected function hasNeverDeployed() + { + return !DirectorDeploymentLog::hasDeployments($this->db); + } + + protected function isWithinGracePeriod() + { + return $this->gracePeriod && $this->gracePeriod->isActive(); + } + + protected function deployedConfigMatches(IcingaConfig $config) + { + if ($deployment = DirectorDeploymentLog::optionalLatest($this->db)) { + return $deployment->getConfigHexChecksum() === $config->getHexChecksum(); + } + + return false; + } + + protected function getActiveChecksum() + { + return DirectorDeploymentLog::getConfigChecksumForStageName( + $this->db, + $this->api->getActiveStageName() + ); + } + + /** + * @param IcingaConfig $config + * @return bool|DirectorDeploymentLog + * @throws IcingaException + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + protected function reallyDeploy(IcingaConfig $config) + { + $checksum = $config->getHexChecksum(); + $this->logger->info(sprintf('Director ConfigJob ready to deploy "%s"', $checksum)); + if ($deployment = $this->api->dumpConfig($config, $this->db)) { + $this->logger->notice(sprintf('Director ConfigJob deployed config "%s"', $checksum)); + return $deployment; + } else { + throw new IcingaException('Failed to deploy config "%s"', $checksum); + } + } +} diff --git a/library/Director/Deployment/DeploymentGracePeriod.php b/library/Director/Deployment/DeploymentGracePeriod.php new file mode 100644 index 0000000..6cde25a --- /dev/null +++ b/library/Director/Deployment/DeploymentGracePeriod.php @@ -0,0 +1,61 @@ +graceTimeSeconds = $graceTimeSeconds; + $this->db = $db; + } + + /** + * Whether we're still within a grace period + * @return bool + */ + public function isActive() + { + if ($deployment = $this->lastDeployment()) { + return $deployment->getDeploymentTimestamp() > $this->getGracePeriodStart(); + } + + return false; + } + + protected function getGracePeriodStart() + { + return time() - $this->graceTimeSeconds; + } + + public function getRemainingGraceTime() + { + if ($this->isActive()) { + if ($deployment = $this->lastDeployment()) { + return $deployment->getDeploymentTimestamp() - $this->getGracePeriodStart(); + } else { + return null; + } + } + + return 0; + } + + protected function lastDeployment() + { + return DirectorDeploymentLog::optionalLatest($this->db); + } +} diff --git a/library/Director/Deployment/DeploymentInfo.php b/library/Director/Deployment/DeploymentInfo.php new file mode 100644 index 0000000..77d52de --- /dev/null +++ b/library/Director/Deployment/DeploymentInfo.php @@ -0,0 +1,59 @@ +db = $db; + } + + public function setObject(IcingaObject $object) + { + $this->object = $object; + return $this; + } + + public function getTotalChanges() + { + if ($this->totalChanges === null) { + $this->totalChanges = $this->db->countActivitiesSinceLastDeployedConfig(); + } + + return $this->totalChanges; + } + + public function getSingleObjectChanges() + { + if ($this->objectChanges === null) { + if ($this->object === null) { + $this->objectChanges = 0; + } else { + $this->objectChanges = $this->db + ->countActivitiesSinceLastDeployedConfig($this->object); + } + } + + return $this->objectChanges; + } + + public function hasUndeployedChanges() + { + return $this->getSingleObjectChanges() > 0 && $this->getTotalChanges() > 0; + } +} diff --git a/library/Director/Deployment/DeploymentStatus.php b/library/Director/Deployment/DeploymentStatus.php new file mode 100644 index 0000000..ae850c6 --- /dev/null +++ b/library/Director/Deployment/DeploymentStatus.php @@ -0,0 +1,164 @@ +db = $db; + $this->api = $api; + } + + public function getDeploymentStatus($configs = null, $activities = null) + { + try { + if (DirectorDeploymentLog::hasUncollected($this->db)) { + $this->api->collectLogFiles($this->db); + } + } catch (Exception $e) { + // Ignore eventual issues while talking to Icinga + } + + $activeConfiguration = null; + $lastActivityLogChecksum = null; + $configChecksum = null; + if ($stageName = $this->api->getActiveStageName()) { + $activityLogChecksum = DirectorDeploymentLog::getRelatedToActiveStage($this->api, $this->db); + if ($activityLogChecksum === null) { + $activeConfiguration = [ + 'stage_name' => $stageName, + 'config' => null, + 'activity' => null + ]; + } else { + $lastActivityLogChecksum = bin2hex($activityLogChecksum->get('last_activity_checksum')); + $configChecksum = $this->getConfigChecksumForStageName($stageName); + $activeConfiguration = [ + 'stage_name' => $stageName, + 'config' => ($configChecksum) ? : null, + 'activity' => $lastActivityLogChecksum + ]; + } + } + $result = [ + 'active_configuration' => (object) $activeConfiguration, + ]; + + if ($configs) { + $result['configs'] = (object) $this->getDeploymentStatusForConfigChecksums( + explode(',', $configs), + $configChecksum + ); + } + + if ($activities) { + $result['activities'] = (object) $this->getDeploymentStatusForActivityLogChecksums( + explode(',', $activities), + $lastActivityLogChecksum + ); + } + return (object) $result; + } + + public function getConfigChecksumForStageName($stageName) + { + $db = $this->db->getDbAdapter(); + $query = $db->select()->from( + ['l' => 'director_deployment_log'], + ['checksum' => $this->db->dbHexFunc('l.config_checksum')] + )->where('l.stage_name = ?', $stageName); + + return $db->fetchOne($query); + } + + public function getDeploymentStatusForConfigChecksums($configChecksums, $activeConfigChecksum) + { + $db = $this->db->getDbAdapter(); + $results = array_combine($configChecksums, array_map(function () { + return 'unknown'; + }, $configChecksums)); + $binaryConfigChecksums = []; + foreach ($configChecksums as $singleConfigChecksum) { + $binaryConfigChecksums[$singleConfigChecksum] = $this->db->quoteBinary(hex2bin($singleConfigChecksum)); + } + $deployedConfigs = $this->getDeployedConfigs(array_values($binaryConfigChecksums)); + + foreach ($results as $singleChecksum => &$status) { + // active if it's equal to the provided active + if ($singleChecksum === $activeConfigChecksum) { + $status = 'active'; + } else { + if (isset($deployedConfigs[$singleChecksum])) { + $status = ($deployedConfigs[$singleChecksum] === 'y') ? 'deployed' : 'failed'; + } else { + // check if it's in generated_config table it is undeployed + $generatedConfigQuery = $db->select()->from( + ['g' => 'director_generated_config'], + ['checksum' => 'g.checksum'] + )->where('g.checksum = ?', $binaryConfigChecksums[$singleChecksum]); + if ($db->fetchOne($generatedConfigQuery)) { + $status = 'undeployed'; + } + } + // otherwise leave unknown + } + } + + return $results; + } + + public function getDeploymentStatusForActivityLogChecksums($activityLogChecksums, $activeActivityLogChecksum) + { + $db = $this->db->getDbAdapter(); + $results = array_combine($activityLogChecksums, array_map(function () { + return 'unknown'; + }, $activityLogChecksums)); + + foreach ($results as $singleActivityLogChecksum => &$status) { + // active if it's equal to the provided active + if ($singleActivityLogChecksum === $activeActivityLogChecksum) { + $status = 'active'; + } else { + // get last deployed activity id and check if it's less than the passed one + $generatedConfigQuery = $db->select()->from( + ['a' => 'director_activity_log'], + ['id' => 'a.id'] + )->where('a.checksum = ?', $this->db->quoteBinary(hex2bin($singleActivityLogChecksum))); + if ($singleActivityLogData = $db->fetchOne($generatedConfigQuery)) { + if ($lastDeploymentActivityLogId = $this->db->getLastDeploymentActivityLogId()) { + if ((int) $singleActivityLogData > $lastDeploymentActivityLogId) { + $status = 'undeployed'; + } else { + $status = 'deployed'; + } + } + } + } + } + return $results; + } + + /** + * @param array $binaryConfigChecksums + * @return array + */ + public function getDeployedConfigs(array $binaryConfigChecksums) + { + $db = $this->db->getDbAdapter(); + $deploymentLogQuery = $db->select()->from(['l' => 'director_deployment_log'], [ + 'checksum' => $this->db->dbHexFunc('l.config_checksum'), + 'deployed' => 'l.startup_succeeded' + ])->where('l.config_checksum IN (?)', $binaryConfigChecksums); + return $db->fetchPairs($deploymentLogQuery); + } +} -- cgit v1.2.3