From 050553164db4840af53fa6ae258612d840302427 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 12 Jul 2022 11:27:21 +0200 Subject: [PATCH] ServiceCommand: allow magic overrides fixes #2560 --- application/clicommands/ServiceCommand.php | 60 ++++++++++++++++++++++ doc/60-CLI.md | 1 + library/Director/Cli/ObjectCommand.php | 11 +++- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/application/clicommands/ServiceCommand.php b/application/clicommands/ServiceCommand.php index c947064e2..8ef48fc45 100644 --- a/application/clicommands/ServiceCommand.php +++ b/application/clicommands/ServiceCommand.php @@ -3,7 +3,9 @@ namespace Icinga\Module\Director\Clicommands; use Icinga\Module\Director\Cli\ObjectCommand; +use Icinga\Module\Director\DirectorObject\Lookup\ServiceFinder; use Icinga\Module\Director\Objects\IcingaHost; +use RuntimeException; /** * Manage Icinga Services @@ -13,6 +15,64 @@ */ class ServiceCommand extends ObjectCommand { + public function setAction() + { + if (($host = $this->params->get('host')) && $this->params->shift('allow-overrides')) { + $name = $this->getName(); + $host = IcingaHost::load($host, $this->db()); + $service = ServiceFinder::find($host, $name); + if ($service->requiresOverrides()) { + $this->params->shift('host'); + if ($this->params->shift('replace')) { + throw new RuntimeException('--replace is not available for Variable Overrides'); + } + $appends = $this->stripPrefixedProperties($this->params, 'append-'); + $remove = $this->stripPrefixedProperties($this->params, 'remove-'); + $properties = $this->remainingParams(); + self::assertVarsForOverrides($appends); + self::assertVarsForOverrides($remove); + self::assertVarsForOverrides($properties); + $current = $host->getOverriddenServiceVars($name); + foreach ($properties as $key => $value) { + if ($key === 'vars') { + foreach ($value as $k => $v) { + $current->$k = $v; + } + } else { + $current->{substr($key, 5)} = $value; + } + } + + if (! empty($appends)) { + throw new RuntimeException('--append- is not available for Variable Overrides'); + } + if (! empty($remove)) { + throw new RuntimeException('--remove- is not available for Variable Overrides'); + } + // Alternative, untested: + // $this->appendToArrayProperties($object, $appends); + // $this->removeProperties($object, $remove); + + $host->overrideServiceVars($name, $current); + $this->persistChanges($host, 'Host', $host->getObjectName() . " (Overrides for $name)", 'modified'); + } + } + + parent::setAction(); + } + + protected static function assertVarsForOverrides($properties) + { + if (empty($properties)) { + return; + } + + foreach ($properties as $key => $value) { + if ($key !== 'vars' && substr($key, 0, 5) !== 'vars.') { + throw new RuntimeException("Only Custom Variables can be set based on Variable Overrides"); + } + } + } protected function load($name) { diff --git a/doc/60-CLI.md b/doc/60-CLI.md index 12b84daaf..4a3bf31e6 100644 --- a/doc/60-CLI.md +++ b/doc/60-CLI.md @@ -137,6 +137,7 @@ Use this command to modify specific properties of an existing Icinga object. | `--json` | Otherwise provide all options as a JSON string | | `--replace` | Replace all object properties with the given ones | | `--auto-create` | Create the object in case it does not exist | +| `--allow-overrides` | Set variable overrides for virtual Services | #### Examples diff --git a/library/Director/Cli/ObjectCommand.php b/library/Director/Cli/ObjectCommand.php index b14642d79..27cbc73fe 100644 --- a/library/Director/Cli/ObjectCommand.php +++ b/library/Director/Cli/ObjectCommand.php @@ -4,6 +4,8 @@ use Icinga\Cli\Params; use Icinga\Exception\MissingParameterException; +use Icinga\Module\Director\Data\Db\DbObject; +use Icinga\Module\Director\Data\Exporter; use Icinga\Module\Director\IcingaConfig\IcingaConfig; use Icinga\Module\Director\Objects\IcingaObject; use InvalidArgumentException; @@ -208,12 +210,17 @@ public function setAction() $this->appendToArrayProperties($object, $appends); $this->removeProperties($object, $remove); + $this->persistChanges($object, $this->getType(), $name, $action); + } + + protected function persistChanges(DbObject $object, $type, $name, $action) + { if ($object->hasBeenModified() && $object->store()) { - printf("%s '%s' has been %s\n", $this->getType(), $this->name, $action); + printf("%s '%s' has been %s\n", $type, $name, $action); exit(0); } - printf("%s '%s' has not been modified\n", $this->getType(), $name); + printf("%s '%s' has not been modified\n", $type, $name); exit(0); }