Skip to content

Commit

Permalink
Add deploy option for Sync jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
araujorm committed Feb 3, 2023
1 parent d905318 commit 7096885
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions library/Director/Job/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Exception\IcingaException;

class SyncJob extends JobHook
{
Expand All @@ -14,17 +16,39 @@ class SyncJob extends JobHook
/**
* @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
* @throws \Icinga\Exception\IcingaException
*/
public function run()
{
$db = $this->db();
$id = $this->getSetting('rule_id');

$madeChanges = false;
if ($id === '__ALL__') {
foreach (SyncRule::loadAll($db) as $rule) {
$this->runForRule($rule);
if ($this->runForRule($rule)) {
$madeChanges = true;
}
}
} else {
$this->runForRule(SyncRule::loadWithAutoIncId((int) $id, $db));
$madeChanges = $this->runForRule(SyncRule::loadWithAutoIncId((int) $id, $db));
}

if ($madeChanges) {
$shouldDeploy = false;
switch ($this->getSetting('deploy', 'never')) {
case 'side_effect_free':
$shouldDeploy = $db->countActivitiesSinceLastDeployedConfig() == 0;
break;
case 'always':
$shouldDeploy = true;
break;
}
if ($shouldDeploy) {
$deployer = new ConditionalDeployment($db);
$renderer = new ConditionalConfigRenderer($db);
$deployer->deploy($renderer->getConfig());
}
}
}

Expand All @@ -35,7 +59,8 @@ public function run()
public function exportSettings()
{
$settings = [
'apply_changes' => $this->getSetting('apply_changes') === 'y'
'apply_changes' => $this->getSetting('apply_changes') === 'y',
'deploy' => in_array($this->getSetting('deploy'), array('side_effect_free','always'), true)
];
$id = $this->getSetting('rule_id');
if ($id !== '__ALL__') {
Expand All @@ -48,14 +73,16 @@ public function exportSettings()

/**
* @param SyncRule $rule
* @return bool
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
*/
protected function runForRule(SyncRule $rule)
{
if ($this->getSetting('apply_changes') === 'y') {
$rule->applyChanges();
return $rule->applyChanges();
} else {
$rule->checkForChanges();
return false;
}
}

Expand Down Expand Up @@ -96,13 +123,32 @@ public static function addSettingsFormFields(QuickForm $form)
. ' job still makes sense. You will be made aware of available changes'
. ' in your Director GUI.'
),
'class' => 'autosubmit',
'value' => 'n',
'multiOptions' => array(
'y' => $form->translate('Yes'),
'n' => $form->translate('No'),
)
));

if ($form->getSentOrObjectSetting('apply_changes') === 'y') {
$form->addElement('select', 'deploy', array(
'label' => $form->translate('Deploy'),
'description' => $form->translate(
'In case you also want the configuration to be automatically deployed'
. ' when changes are made by this Sync job. For safety, the deploy is'
. ' normally only made if no other pending changes already exist, but'
. ' you can choose "Yes (always)" to override that.'
),
'value' => 'never',
'multiOptions' => array(
'side_effect_free' => $form->translate('Yes (side effect free)'),
'always' => $form->translate('Yes (always)'),
'never' => $form->translate('Never'),
)
));
}

if ((string) $form->getSentOrObjectValue('job_name') !== '') {
if (($ruleId = $form->getSentValue('rule_id')) && array_key_exists($ruleId, $rules)) {
$name = sprintf('Sync job: %s', $rules[$ruleId]);
Expand Down

0 comments on commit 7096885

Please sign in to comment.