First deployment status api implementation

This commit is contained in:
Gianluca Piccolo 2020-10-07 14:54:02 +02:00 committed by Thomas Gelf
parent 3f8f7bd1eb
commit 23214529f1
2 changed files with 117 additions and 8 deletions

View File

@ -131,16 +131,40 @@ class ConfigController extends ActionController
if ($this->sendNotFoundUnlessRestApi()) {
return;
}
$activeConfiguration = null;
$lastActivityLogChecksum = null;
$configChecksum = null;
$api = $this->api();
$status = new DeploymentStatus($this->db(), $api);
$stageName = $api->getActiveStageName();
$checksum = $status->getConfigChecksumForStageName($stageName);
$this->sendJson($this->getResponse(), (object) [
'active_configuration' => (object) [
'active_stage_name' => $stageName,
'active_checksum' => $checksum
],
]);
if ($stageName = $api->getActiveStageName()) {
$activityLogChecksum = DirectorDeploymentLog::getRelatedToActiveStage($api, $this->db());
$lastActivityLogChecksum = bin2hex($activityLogChecksum->last_activity_checksum);
$configChecksum = $status->getConfigChecksumForStageName($stageName);
$activeConfiguration = [
'stage_name' => $stageName,
'config_checksum' => ($configChecksum) ? : null,
'activity_log_checksum' => $lastActivityLogChecksum
];
}
$result = [
'active_configuration' => (object) $activeConfiguration,
];
if ($configChecksumsListToVerify = $this->params->get('config_checksums')) {
$result['configuration'] = $status->getDeploymentStatusForConfigChecksums(
explode(',', $configChecksumsListToVerify),
$configChecksum
);
}
if ($activityLogChecksumsListToVerify = $this->params->get('activity_log_checksums')) {
$result['activity'] = $status->getDeploymentStatusForActivityLogChecksums(
explode(',', $activityLogChecksumsListToVerify),
$lastActivityLogChecksum
);
}
$this->sendJson($this->getResponse(), (object) $result);
}
/**

View File

@ -27,4 +27,89 @@ class DeploymentStatus
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] = 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(
array('g' => 'director_generated_config'),
array('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(
array('a' => 'director_activity_log'),
array('id' => 'a.id')
)->where('a.checksum = ?', hex2bin($singleActivityLogChecksum));
if ($singleActivityLogData = $db->fetchOne($generatedConfigQuery)) {
if ($lastDeploymentActivityLogId = $db->getLastDeploymentActivityLogId()) {
if ($singleActivityLogData->id > $lastDeploymentActivityLogId) {
$status = 'undeployed';
} else {
$status = 'deployed';
}
}
}
}
}
return $results;
}
/**
* @param $db
* @param array $binaryConfigChecksums
* @return mixed
*/
public function getDeployedConfigs(array $binaryConfigChecksums)
{
$db = $this->db->getDbAdapter();
$deploymentLogQuery = $db->select()->from(
array('l' => 'director_deployment_log'),
array(
'checksum' => $this->db->dbHexFunc('l.config_checksum'),
'deployed' => 'l.startup_succeeded'
)
)->where('l.config_checksum IN (?)', $binaryConfigChecksums);
return $db->fetchPairs($deploymentLogQuery);
}
}