Skip to content

Commit

Permalink
ServiceCommand: allow magic overrides
Browse files Browse the repository at this point in the history
fixes #2560
  • Loading branch information
Thomas-Gelf committed Jul 12, 2022
1 parent 922b193 commit 0505531
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
60 changes: 60 additions & 0 deletions application/clicommands/ServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions doc/60-CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions library/Director/Cli/ObjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0505531

Please sign in to comment.