mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-09-26 11:19:16 +02:00
ConfigJob: logic fixes, code cleanup
This commit is contained in:
parent
3ad56d3a98
commit
2d8d7af051
@ -60,7 +60,6 @@ class ConfigJob extends JobHook
|
|||||||
protected function shouldDeploy(IcingaConfig $config)
|
protected function shouldDeploy(IcingaConfig $config)
|
||||||
{
|
{
|
||||||
$db = $this->db();
|
$db = $this->db();
|
||||||
|
|
||||||
if ($this->getSetting('deploy_when_changed') !== 'y') {
|
if ($this->getSetting('deploy_when_changed') !== 'y') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -75,7 +74,7 @@ class ConfigJob extends JobHook
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->lastDeployment()->configEquals($config)) {
|
if ($this->getActiveChecksum() === $config->getChecksum()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,6 +85,7 @@ class ConfigJob extends JobHook
|
|||||||
|
|
||||||
protected function deploy(IcingaConfig $config)
|
protected function deploy(IcingaConfig $config)
|
||||||
{
|
{
|
||||||
|
$this->clearLastDeployment();
|
||||||
$db = $this->db();
|
$db = $this->db();
|
||||||
$api = $this->api();
|
$api = $this->api();
|
||||||
$api->wipeInactiveStages($db);
|
$api->wipeInactiveStages($db);
|
||||||
@ -104,6 +104,17 @@ class ConfigJob extends JobHook
|
|||||||
return time() - $this->getSetting('grace_period');
|
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()
|
protected function isWithinGracePeriod()
|
||||||
{
|
{
|
||||||
if ($deployment = $this->lastDeployment()) {
|
if ($deployment = $this->lastDeployment()) {
|
||||||
@ -113,6 +124,14 @@ class ConfigJob extends JobHook
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getActiveChecksum()
|
||||||
|
{
|
||||||
|
return DirectorDeploymentLog::getConfigChecksumForStageName(
|
||||||
|
$db,
|
||||||
|
$api->getActiveStageName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
protected function lastDeployment()
|
protected function lastDeployment()
|
||||||
{
|
{
|
||||||
if ($this->lastDeployment === null) {
|
if ($this->lastDeployment === null) {
|
||||||
@ -122,6 +141,12 @@ class ConfigJob extends JobHook
|
|||||||
return $this->lastDeployment;
|
return $this->lastDeployment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function clearLastDeployment()
|
||||||
|
{
|
||||||
|
$this->lastDeployment = null;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public static function addSettingsFormFields(QuickForm $form)
|
public static function addSettingsFormFields(QuickForm $form)
|
||||||
{
|
{
|
||||||
$form->addElement('select', 'force_generate', array(
|
$form->addElement('select', 'force_generate', array(
|
||||||
@ -169,70 +194,4 @@ class ConfigJob extends JobHook
|
|||||||
. 'Icinga 2 configuration'
|
. '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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user