icingaweb2-module-director/library/Director/Job/ConfigJob.php

224 lines
6.1 KiB
PHP
Raw Normal View History

2016-04-22 09:53:59 +02:00
<?php
namespace Icinga\Module\Director\Job;
2016-06-28 14:07:25 +02:00
use Exception;
use Icinga\Exception\IcingaException;
2016-04-22 09:53:59 +02:00
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Hook\JobHook;
2016-06-28 14:07:25 +02:00
use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\DirectorDeploymentLog;
2016-04-22 09:53:59 +02:00
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Form\QuickForm;
2016-04-22 09:53:59 +02:00
class ConfigJob extends JobHook
{
protected $lastDeployment;
protected $api;
2016-04-22 09:53:59 +02:00
public function run()
{
$db = $this->db();
2016-06-28 14:07:25 +02:00
$this->clearLastDeployment();
if ($this->shouldGenerate()) {
$config = IcingaConfig::generate($db);
} else {
$config = $this->loadLatestActivityConfig();
}
if ($this->shouldDeploy($config)) {
$this->deploy($config);
}
$this->clearLastDeployment();
}
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') === 'y'
|| ! $this->configForLatestActivityExists();
}
protected function configForLatestActivityExists()
{
$db = $this->db();
return IcingaConfig::existsForActivityChecksum(
2016-06-28 14:07:25 +02:00
Util::binary2hex(DirectorActivityLog::loadLatest($db)->checksum),
$db
);
}
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->getActiveChecksum() === $config->getHexChecksum()) {
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();
2016-06-17 13:48:35 +02:00
$this->info('Director ConfigJob ready to deploy "%s"', $checksum);
if ($api->dumpConfig($config, $db)) {
2016-06-17 13:48:35 +02:00
$this->info('Director ConfigJob deployed config "%s"', $checksum);
2016-06-28 14:07:25 +02:00
// TODO: Loop and try multiple times?
sleep(2);
try {
$api->collectLogFiles($db);
} catch (Exception $e) {
// Ignore those errors, Icinga may be reloading
}
} else {
2016-06-17 13:48:35 +02:00
throw new IcingaException('Failed to deploy config "%s"', $checksum);
}
}
protected function getGracePeriodStart()
{
return time() - $this->getSetting('grace_period');
}
2016-06-16 14:22:54 +02:00
public function getRemainingGraceTime()
{
if ($this->isWithinGracePeriod()) {
2016-11-01 18:28:36 +01:00
if ($deployment = $this->lastDeployment()) {
return $deployment->getDeploymentTimestamp()
2016-06-16 14:52:50 +02:00
+ $this->getSetting('grace_period')
2016-06-16 14:22:54 +02:00
- time();
2016-11-01 18:28:36 +01:00
} else {
return null;
}
2016-06-16 14:22:54 +02:00
}
return 0;
}
protected function isWithinGracePeriod()
{
if ($deployment = $this->lastDeployment()) {
return $deployment->getDeploymentTimestamp() > $this->getGracePeriodStart();
}
return false;
}
2016-06-16 14:22:54 +02:00
protected function getActiveChecksum()
{
return DirectorDeploymentLog::getConfigChecksumForStageName(
2016-06-16 15:27:14 +02:00
$this->db(),
2016-06-16 15:34:14 +02:00
$this->api()->getActiveStageName()
2016-06-16 14:22:54 +02:00
);
}
protected function lastDeployment()
{
if ($this->lastDeployment === null) {
$this->lastDeployment = DirectorDeploymentLog::loadLatest($this->db());
}
return $this->lastDeployment;
2016-04-22 09:53:59 +02:00
}
2016-06-16 14:22:54 +02:00
protected function clearLastDeployment()
{
$this->lastDeployment = null;
return $this;
}
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(
'This allows you to immediately deploy a modified configuration'
),
'value' => 'n',
'multiOptions' => array(
'y' => $form->translate('Yes'),
'n' => $form->translate('No'),
)
));
$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,
));
return $form;
2016-04-22 09:53:59 +02:00
}
public static function getDescription(QuickForm $form)
{
return $form->translate(
'The Config job allows you to generate and eventually deploy your'
2016-06-28 12:27:33 +02:00
. ' Icinga 2 configuration'
2016-04-22 09:53:59 +02:00
);
}
}