Skip to content

Commit

Permalink
Merge branch 'Icinga:master' into allow-all-variables
Browse files Browse the repository at this point in the history
  • Loading branch information
v0tti authored Sep 8, 2022
2 parents a5d3407 + 1682175 commit 4547c39
Show file tree
Hide file tree
Showing 85 changed files with 1,851 additions and 537 deletions.
3 changes: 2 additions & 1 deletion application/clicommands/ConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ public function deployAction()
printf("Config '%s' has been deployed\n", $checksum);
} else {
echo $deployer->getNoDeploymentReason() . "\n";
return;
}

if ($timeout = $this->getWaitTime()) {
$deployed = $deployer->waitForStartupAfterDeploy($deployment, $timeout);
if ($deployed !== true) {
$this->fail("Failed to deploy config '%s': %s\n", $checksum, $deployed);
$this->fail("Waiting for Icinga restart failed '%s': %s\n", $checksum, $deployed);
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions application/clicommands/ServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Icinga\Module\Director\Clicommands;

use Icinga\Cli\Params;
use Icinga\Module\Director\Cli\ObjectCommand;
use Icinga\Module\Director\DirectorObject\Lookup\ServiceFinder;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Resolver\OverrideHelper;
use InvalidArgumentException;

/**
* Manage Icinga Services
Expand All @@ -13,6 +17,53 @@
*/
class ServiceCommand extends ObjectCommand
{
public function setAction()
{
if (($host = $this->params->get('host')) && $this->params->shift('allow-overrides')) {
if ($this->setServiceProperties($host)) {
return;
}
}

parent::setAction();
}

protected function setServiceProperties($hostname)
{
$serviceName = $this->getName();
$host = IcingaHost::load($hostname, $this->db());
$service = ServiceFinder::find($host, $serviceName);
if ($service->requiresOverrides()) {
self::checkForOverrideSafety($this->params);
$properties = $this->remainingParams();
unset($properties['host']);
OverrideHelper::applyOverriddenVars($host, $serviceName, $properties);
$this->persistChanges($host, 'Host', $hostname . " (Overrides for $serviceName)", 'modified');
return true;
}

return false;
}

protected static function checkForOverrideSafety(Params $params)
{
if ($params->shift('replace')) {
throw new InvalidArgumentException('--replace is not available for Variable Overrides');
}
$appends = self::stripPrefixedProperties($params, 'append-');
$remove = self::stripPrefixedProperties($params, 'remove-');
OverrideHelper::assertVarsForOverrides($appends);
OverrideHelper::assertVarsForOverrides($remove);
if (!empty($appends)) {
throw new InvalidArgumentException('--append- is not available for Variable Overrides');
}
if (!empty($remove)) {
throw new InvalidArgumentException('--remove- is not available for Variable Overrides');
}
// Alternative, untested:
// $this->appendToArrayProperties($object, $appends);
// $this->removeProperties($object, $remove);
}

protected function load($name)
{
Expand Down
7 changes: 4 additions & 3 deletions application/clicommands/SyncruleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Clicommands;

use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\SyncRule;
use RuntimeException;
Expand Down Expand Up @@ -98,9 +99,9 @@ protected function getExpectedModificationCounts(SyncRule $rule)
}

return (object) [
'create' => $create,
'modify' => $modify,
'delete' => $delete,
DirectorActivityLog::ACTION_CREATE => $create,
DirectorActivityLog::ACTION_MODIFY => $modify,
DirectorActivityLog::ACTION_DELETE => $delete,
];
}

Expand Down
8 changes: 5 additions & 3 deletions application/controllers/BasketController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use gipfl\Web\Widget\Hint;
use Icinga\Date\DateFormatter;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\Basket;
use Icinga\Module\Director\DirectorObject\Automation\BasketSnapshot;
Expand Down Expand Up @@ -247,6 +248,7 @@ public function snapshotAction()
$json = $snapshot->getJsonDump();
$this->addSingleTab($this->translate('Snapshot'));
$all = Json::decode($json);
$exporter = new Exporter($this->db());
$fieldResolver = new BasketSnapshotFieldResolver($all, $connection);
foreach ($all as $type => $objects) {
if ($type === 'Datafield') {
Expand Down Expand Up @@ -284,7 +286,7 @@ public function snapshotAction()
);
continue;
}
$currentExport = $current->export();
$currentExport = $exporter->export($current);
$fieldResolver->tweakTargetIds($currentExport);

// Ignore originalId
Expand Down Expand Up @@ -366,7 +368,7 @@ public function snapshotobjectAction()
)
*/
]);

$exporter = new Exporter($this->db());
$json = $snapshot->getJsonDump();
$this->addSingleTab($this->translate('Snapshot'));
$objects = Json::decode($json);
Expand All @@ -385,7 +387,7 @@ public function snapshotobjectAction()
if ($current === null) {
$current = '';
} else {
$exported = $current->export();
$exported = $exporter->export($current);
$fieldResolver->tweakTargetIds($exported);
unset($exported->originalId);
CompareBasketObject::normalize($exported);
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/ImportsourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use gipfl\Web\Widget\Hint;
use Icinga\Module\Director\Data\Exporter;
use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Forms\ImportRowModifierForm;
use Icinga\Module\Director\Forms\ImportSourceForm;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function indexAction()
$this->addMainActions();
$source = $this->getImportSource();
if ($this->params->get('format') === 'json') {
$this->sendJson($this->getResponse(), $source->export());
$this->sendJson($this->getResponse(), (new Exporter($this->db()))->export($source));
return;
}
$this->addTitle(
Expand Down
14 changes: 11 additions & 3 deletions application/controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Controllers;

use Exception;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Data\Db\DbObjectStore;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db\Branch\UuidLookup;
Expand Down Expand Up @@ -250,9 +251,16 @@ protected function loadObject()
}

$key = $this->getLegacyKey();
$uuid = UuidLookup::findServiceUuid($this->db(), $this->getBranch(), 'object', $key, $this->host, $this->set);
$this->params->set('uuid', $uuid->toString());
parent::loadObject();
// Hint: not passing 'object' as type, we still have name-based links in previews and similar
$uuid = UuidLookup::findServiceUuid($this->db(), $this->getBranch(), null, $key, $this->host, $this->set);
if ($uuid === null) {
if (! $this->params->get('allowOverrides')) {
throw new NotFoundError('Not found');
}
} else {
$this->params->set('uuid', $uuid->toString());
parent::loadObject();
}
}

protected function addOptionalHostTabs()
Expand Down
2 changes: 1 addition & 1 deletion application/forms/DeploymentLinkForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function setup()
);
}

$this->setAttrib('class', 'inline');
$this->setAttrib('class', 'gipfl-inline-form');
$this->addHtml(Icon::create('wrench'));
try {
// As this is shown for single objects, ignore errors caused by an
Expand Down
4 changes: 2 additions & 2 deletions application/forms/ImportRowModifierForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ protected function addSettings($class = null)

if ($class !== null) {
if (! class_exists($class)) {
throw new RuntimeException(
throw new RuntimeException(sprintf(
'The hooked class "%s" for this property modifier does no longer exist',
$class
);
));
}

$class::addSettingsFormFields($this);
Expand Down
2 changes: 1 addition & 1 deletion application/forms/KickstartForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected function storeResourceConfig()
)
);
$this->addHtmlHint(
Html::tag('pre', null, $config),
Html::tag('pre', null, (string) $config),
array('name' => 'HINT_config_store')
);

Expand Down
13 changes: 9 additions & 4 deletions application/forms/SyncCheckForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Icinga\Module\Director\Forms;

use Icinga\Module\Director\Objects\DirectorActivityLog;
use Icinga\Module\Director\Objects\SyncRule;
use Icinga\Module\Director\Web\Form\DirectorForm;

Expand Down Expand Up @@ -31,16 +32,20 @@ public function onSuccess()
$this->notifySuccess(
$this->translate(('This Sync Rule would apply new changes'))
);
$sum = array('create' => 0, 'modify' => 0, 'delete' => 0);
$sum = [
DirectorActivityLog::ACTION_CREATE => 0,
DirectorActivityLog::ACTION_MODIFY => 0,
DirectorActivityLog::ACTION_DELETE => 0
];

// TODO: Preview them? Like "hosta, hostb and 4 more would be...
foreach ($this->rule->getExpectedModifications() as $object) {
if ($object->shouldBeRemoved()) {
$sum['delete']++;
$sum[DirectorActivityLog::ACTION_DELETE]++;
} elseif (! $object->hasBeenLoadedFromDb()) {
$sum['create']++;
$sum[DirectorActivityLog::ACTION_CREATE]++;
} elseif ($object->hasBeenModified()) {
$sum['modify']++;
$sum[DirectorActivityLog::ACTION_MODIFY]++;
}
}

Expand Down
29 changes: 18 additions & 11 deletions doc/60-CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ icingacli director host create localhost \
--json '{ "address": "127.0.0.1", "vars": { "test": [ "one", "two" ] } }'
```

Passing JSON via STDIN is also possible:

```shell
icingacli director host create localhost --json < my-host.json
```


### Delete a specific object

Expand Down Expand Up @@ -137,6 +143,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 Expand Up @@ -184,16 +191,16 @@ in JSON format.

#### Options

| Option | Description |
|-----------------|---------------------------------------------------------|
| `--resolved` | Resolve all inherited properties and show a flat object |
| | object |
| `--json` | Use JSON format |
| `--no-pretty` | JSON is pretty-printed per default (for PHP >= 5.4) |
| | Use this flag to enforce unformatted JSON |
| `--no-defaults` | Per default JSON output skips null or default values |
| | With this flag you will get all properties |

| Option | Description |
|-------------------|------------------------------------------------------|
| `--resolved` | Resolve all inherited properties and show a flat |
| | object |
| `--json` | Use JSON format |
| `--no-pretty` | JSON is pretty-printed per default (for PHP >= 5.4) |
| | Use this flag to enforce unformatted JSON |
| `--no-defaults` | Per default JSON output skips null or default values |
| | With this flag you will get all properties |
| `--with-services` | For hosts only, also shows attached services |

### Clone an existing object

Expand Down Expand Up @@ -464,7 +471,7 @@ Config with checksum b330febd0820493fb12921ad8f5ea42102a5c871 already exists

| Option | Description |
|----------------------------|------------------------------------------------------------------|
| `checksum <checksum>` | Optionally deploy a specific configuration |
| `--checksum <checksum>` | Optionally deploy a specific configuration |
| `--force` | Force a deployment, even when the configuration hasn't changed |
| `--wait <seconds>` | Optionally wait until Icinga completed it's restart |
| `--grace-period <seconds>` | Do not deploy if a deployment took place less than <seconds> ago |
Expand Down
Loading

0 comments on commit 4547c39

Please sign in to comment.