From f49276372a53bb4f7a2ab6a19cb9f1ef2396b8e2 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 27 Apr 2018 18:08:22 +0200 Subject: [PATCH] cli: add SyncruleCommand fixes #1476 --- .../clicommands/ImportsourceCommand.php | 11 +- application/clicommands/SyncruleCommand.php | 130 ++++++++++++++++++ doc/60-CLI.md | 40 +++++- doc/82-Changelog.md | 1 + 4 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 application/clicommands/SyncruleCommand.php diff --git a/application/clicommands/ImportsourceCommand.php b/application/clicommands/ImportsourceCommand.php index 725f2ece..3f60c9ae 100644 --- a/application/clicommands/ImportsourceCommand.php +++ b/application/clicommands/ImportsourceCommand.php @@ -61,7 +61,7 @@ class ImportsourceCommand extends Command { $source = $this->getImportSource(); $source->checkForChanges(); - $this->showImportStateDetailsAndExit($source); + $this->showImportStateDetails($source); } /** @@ -86,7 +86,7 @@ class ImportsourceCommand extends Command if ($source->runImport()) { print "New data has been imported\n"; - $this->showImportStateDetailsAndExit($source); + $this->showImportStateDetails($source); } else { print "Nothing has been changed, imported data is still up to date\n"; } @@ -104,14 +104,9 @@ class ImportsourceCommand extends Command * @param ImportSource $source * @throws \Icinga\Exception\IcingaException */ - protected function showImportStateDetailsAndExit(ImportSource $source) + protected function showImportStateDetails(ImportSource $source) { echo $this->getImportStateDescription($source) . "\n"; - if ($source->get('import_state') === 'failing') { - exit(1); - } else { - exit(0); - } } /** diff --git a/application/clicommands/SyncruleCommand.php b/application/clicommands/SyncruleCommand.php new file mode 100644 index 00000000..e2108cc2 --- /dev/null +++ b/application/clicommands/SyncruleCommand.php @@ -0,0 +1,130 @@ +db()); + if (empty($rules)) { + echo "No Sync Rule has been defined\n"; + + return; + } + + printf("%4s | %s\n", 'ID', 'Sync Rule name'); + printf("-----+%s\n", str_repeat('-', 64)); + + foreach ($rules as $rule) { + $state = $rule->get('sync_state'); + printf("%4d | %s\n", $rule->get('id'), $rule->get('rule_name')); + printf(" | -> %s%s\n", $state, $state === 'failing' ? ': ' . $rule->get('last_error_message') : ''); + } + } + + /** + * Check a given Sync Rule for changes + * + * This command runs a complete Sync in memory but doesn't persist eventual changes. + * + * USAGE + * + * icingacli director syncrule check --id + * + * OPTIONS + * + * --id A Sync Rule ID. Use the list command to figure out + * --benchmark Show timing and memory usage details + */ + public function checkAction() + { + $rule = $this->getSyncRule(); + $rule->checkForChanges(); + $this->showSyncStateDetails($rule); + } + + /** + * Trigger a Sync Run for a given Sync Rule + * + * This command builds new objects according your Sync Rule, compares them + * with existing ones and persists eventual changes. + * + * USAGE + * + * icingacli director syncrule run --id + * + * OPTIONS + * + * --id A Sync Rule ID. Use the list command to figure out + * --benchmark Show timing and memory usage details + */ + public function runAction() + { + $rule = $this->getSyncRule(); + + if ($rule->applyChanges()) { + print "New data has been imported\n"; + $this->showSyncStateDetails($rule); + } else { + print "Nothing has been changed, imported data is still up to date\n"; + } + } + + /** + * @return SyncRule + */ + protected function getSyncRule() + { + return SyncRule::load($this->params->getRequired('id'), $this->db()); + } + + /** + * @param SyncRule $rule + * @throws \Icinga\Exception\IcingaException + */ + protected function showSyncStateDetails(SyncRule $rule) + { + echo $this->getSyncStateDescription($rule) . "\n"; + } + + /** + * @param SyncRule $rule + * @return string + * @throws \Icinga\Exception\IcingaException + */ + protected function getSyncStateDescription(SyncRule $rule) + { + switch ($rule->get('sync_state')) { + case 'unknown': + return "It's currently unknown whether we are in sync with this rule." + . ' You should either check for changes or trigger a new Sync Run.'; + case 'in-sync': + return 'This Sync Rule is in sync'; + case 'pending-changes': + return 'There are pending changes for this Sync Rule. You should' + . ' trigger a new Sync Run.'; + case 'failing': + return 'This Sync Rule failed: '. $rule->get('last_error_message'); + } + } +} diff --git a/doc/60-CLI.md b/doc/60-CLI.md index dc07be25..ee2d3cf6 100644 --- a/doc/60-CLI.md +++ b/doc/60-CLI.md @@ -461,13 +461,51 @@ fresh data. In case data didn't change, nothing is going to be stored. `icingacli director importsource run --id ` -#### Options +##### Options | Option | Description | |---------------|---------------------------------------------------------| | `--id ` | An Import Source ID. Use the list command to figure out | | `--benchmark` | Show timing and memory usage details | +### Sync Rules + +#### List defined Sync Rules + +This shows a table with your defined Sync Rules, their IDs and current +state. As triggering a Sync requires an ID, this is where you can look +up the desired ID. + +`icingacli director syncrule list` + +#### Check a given Sync Rule for changes + +This command runs a complete Sync in memory but doesn't persist eventual +changes. + +`icingacli director syncrule check --id ` + +##### Options + +| Option | Description | +|---------------|----------------------------------------------------| +| `--id ` | A Sync Rule ID. Use the list command to figure out | +| `--benchmark` | Show timing and memory usage details | + +#### Trigger a Sync Run for a given Sync Rule + +This command builds new objects according your Sync Rule, compares them +with existing ones and persists eventual changes. + +`icingacli director syncrule run --id ` + +##### Options + +| Option | Description | +|---------------|----------------------------------------------------| +| `--id ` | A Sync Rule ID. Use the list command to figure out | +| `--benchmark` | Show timing and memory usage details | + ### Running Jobs The `jobs` command runs pending Import and Sync jobs. Please note that we have planned a scheduler configurable through the Icinga Director web interface, but diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index a6eb1720..a988cabd 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -23,6 +23,7 @@ before switching to a new version. ### CLI * FEATURE: Director Health Check Plugin (#1278) * FEATURE: Show and trigger Import Sources (#1474) +* FEATURE: Show and trigger Sync Rules ( #1476) ### Import and Sync * FIX: Sync is very powerful and allows for actions not available in the GUI. It