From 0d405cba98309eea54c00f8b079e987323c84b2c Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 27 May 2019 17:46:43 +0200 Subject: [PATCH] SyncruleCommand: show number of expected changes Sample output when there are changes: icingaweb2@web:~$ icingacli director syncrule check --id 36 There are pending changes for this Sync Rule. You should trigger a new Sync Run. Expected modifications: 2x create, 0x modify, 0x delete icingaweb2@web:~$ echo $? 1 ...when there are no changes: icingaweb2@web:~$ icingacli director syncrule check --id 34 This Sync Rule is in sync icingaweb2@web:~$ echo $? 0 ...and when something failes: icingaweb2@web:~$ icingacli director syncrule check --id 32 This Sync Rule failed: No data has been imported for "Benutzergruppen aus Nagios" yet icingaweb2@web:~$ echo $? 2 fixes #1849 --- application/clicommands/SyncruleCommand.php | 67 ++++++++++++++++++++- library/Director/Objects/SyncRule.php | 3 +- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/application/clicommands/SyncruleCommand.php b/application/clicommands/SyncruleCommand.php index fe58238a..961683df 100644 --- a/application/clicommands/SyncruleCommand.php +++ b/application/clicommands/SyncruleCommand.php @@ -3,7 +3,9 @@ namespace Icinga\Module\Director\Clicommands; use Icinga\Module\Director\Cli\Command; +use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\SyncRule; +use RuntimeException; /** * Deal with Director Sync Rules @@ -59,8 +61,47 @@ class SyncruleCommand extends Command public function checkAction() { $rule = $this->getSyncRule(); - $rule->checkForChanges(); + $hasChanges = $rule->checkForChanges(); $this->showSyncStateDetails($rule); + if ($hasChanges) { + $mods = $this->getExpectedModificationCounts($rule); + printf( + "Expected modifications: %dx create, %dx modify, %dx delete\n", + $mods->modify, + $mods->create, + $mods->delete + ); + } + + exit($this->getSyncStateExitCode($rule)); + } + + protected function getExpectedModificationCounts(SyncRule $rule) + { + $modifications = $rule->getExpectedModifications(); + + $create = 0; + $modify = 0; + $delete = 0; + + /** @var IcingaObject $object */ + foreach ($modifications as $object) { + if ($object->hasBeenLoadedFromDb()) { + if ($object->shouldBeRemoved()) { + $delete++; + } else { + $modify++; + } + } else { + $create++; + } + } + + return (object) [ + 'create' => $create, + 'modify' => $modify, + 'delete' => $delete, + ]; } /** @@ -103,7 +144,6 @@ class SyncruleCommand extends Command /** * @param SyncRule $rule - * @throws \Icinga\Exception\IcingaException */ protected function showSyncStateDetails(SyncRule $rule) { @@ -113,7 +153,6 @@ class SyncruleCommand extends Command /** * @param SyncRule $rule * @return string - * @throws \Icinga\Exception\IcingaException */ protected function getSyncStateDescription(SyncRule $rule) { @@ -128,6 +167,28 @@ class SyncruleCommand extends Command . ' trigger a new Sync Run.'; case 'failing': return 'This Sync Rule failed: '. $rule->get('last_error_message'); + default: + throw new RuntimeException('Invalid sync state: ' . $rule->get('sync_state')); + } + } + + /** + * @param SyncRule $rule + * @return string + */ + protected function getSyncStateExitCode(SyncRule $rule) + { + switch ($rule->get('sync_state')) { + case 'unknown': + return 3; + case 'in-sync': + return 0; + case 'pending-changes': + return 1; + case 'failing': + return 2; + default: + throw new RuntimeException('Invalid sync state: ' . $rule->get('sync_state')); } } } diff --git a/library/Director/Objects/SyncRule.php b/library/Director/Objects/SyncRule.php index 5698cc81..cb04b179 100644 --- a/library/Director/Objects/SyncRule.php +++ b/library/Director/Objects/SyncRule.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Objects; use Icinga\Application\Benchmark; use Icinga\Data\Filter\Filter; +use Icinga\Exception\IcingaException; use Icinga\Module\Director\Data\Db\DbObject; use Icinga\Module\Director\Db; use Icinga\Module\Director\DirectorObject\Automation\ExportInterface; @@ -184,7 +185,7 @@ class SyncRule extends DbObject implements ExportInterface /** * @return IcingaObject[] - * @throws Exception + * @throws IcingaException */ public function getExpectedModifications() {