Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deploy option for Sync jobs #2181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 49 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\Deployment\ConditionalConfigRenderer;
use Icinga\Module\Director\Deployment\ConditionalDeployment;

class SyncJob extends JobHook
{
Expand All @@ -14,17 +16,38 @@ 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');

$shouldDeploy = false;
switch ($this->getSetting('deploy', 'never')) {
case 'side_effect_free':
$shouldDeploy = $db->countActivitiesSinceLastDeployedConfig() == 0;
break;
case 'always':
$shouldDeploy = true;
break;
}

$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) {
$deployer = new ConditionalDeployment($db);
$renderer = new ConditionalConfigRenderer($db);
$deployer->deploy($renderer->getConfig());
}
}

Expand All @@ -35,7 +58,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 +72,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 +122,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