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 May 13, 2022
2 parents d52b7af + 4692b28 commit ab64c99
Show file tree
Hide file tree
Showing 258 changed files with 12,532 additions and 3,361 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ or code.
* Make sure your code conforms to the [PSR-2: Coding Style Guide](http://www.php-fig.org/psr/psr-2/)
* [Unit-Tests](doc/93-Testing.md) would be great
* Send a [Pull Request](https://github.com/Icinga/icingaweb2-module-director/pulls)
(it will automatically be tested on Travis-CI)
* We try hard to keep our master always green: [![Build Status](https://travis-ci.org/Icinga/icingaweb2-module-director.svg?branch=master)](https://travis-ci.org/Icinga/icingaweb2-module-director)


Addons
------
Expand Down
31 changes: 31 additions & 0 deletions application/clicommands/BasketCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Icinga\Date\DateFormatter;
use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\DirectorObject\Automation\Basket;
use Icinga\Module\Director\DirectorObject\Automation\BasketSnapshot;
use Icinga\Module\Director\DirectorObject\ObjectPurgeHelper;

/**
* Export Director Config Objects
Expand Down Expand Up @@ -79,14 +81,43 @@ public function snapshotAction()
* icingacli director basket restore < basket-dump.json
*
* OPTIONS
* --purge <ObjectType>[,<ObjectType] Purge objects of the
* Given types. WARNING: this removes ALL objects that are
* not shipped with the given basket
* --force Purge refuses to purge Objects in case there are
* no Objects of a given ObjectType in the provided basket
* unless forced to do so
*/
public function restoreAction()
{
if ($purge = $this->params->get('purge')) {
$purge = explode(',', $purge);
ObjectPurgeHelper::assertObjectTypesAreEligibleForPurge($purge);
}
$json = file_get_contents('php://stdin');
BasketSnapshot::restoreJson($json, $this->db());
if ($purge) {
$this->purgeObjectTypes(Json::decode($json), $purge, $this->params->get('force'));
}
echo "Objects from Basket Snapshot have been restored\n";
}

protected function purgeObjectTypes($objects, array $types, $force = false)
{
$helper = new ObjectPurgeHelper($this->db());
if ($force) {
$helper->force();
}
foreach ($types as $type) {
list($className, $typeFilter) = BasketSnapshot::getClassAndObjectTypeForType($type);
$helper->purge(
isset($objects->$type) ? (array) $objects->$type : [],
$className,
$typeFilter
);
}
}

/**
*/
protected function requireBasket()
Expand Down
63 changes: 48 additions & 15 deletions application/clicommands/ConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Icinga\Application\Benchmark;
use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Deployment\ConditionalDeployment;
use Icinga\Module\Director\Deployment\DeploymentGracePeriod;
use Icinga\Module\Director\Deployment\DeploymentStatus;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Import\SyncUtils;
Expand Down Expand Up @@ -86,12 +88,23 @@ protected function enableDbProfiler()
/**
* Deploy the current configuration
*
* Does nothing if config didn't change unless you provide
* the --force parameter
* USAGE
*
* icingacli director config deploy [--checksum <checksum>] [--force] [--wait <seconds>]
* [--grace-period <seconds>]
*
* OPTIONS
*
* --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
*/
public function deployAction()
{
$api = $this->api();
$db = $this->db();

$checksum = $this->params->get('checksum');
Expand All @@ -102,24 +115,31 @@ public function deployAction()
$checksum = $config->getHexChecksum();
}

$api->wipeInactiveStages($db);
$current = $api->getActiveChecksum($db);
if ($current === $checksum) {
$deployer = new ConditionalDeployment($db, $this->api());
$deployer->force((bool) $this->params->get('force'));
if ($graceTime = $this->params->get('grace-period')) {
$deployer->setGracePeriod(new DeploymentGracePeriod((int) $graceTime, $db));
if ($this->params->get('force')) {
echo "Config matches active stage, deploying anyway\n";
} else {
echo "Config matches active stage, nothing to do\n";

return;
fwrite(STDERR, "WARNING: force overrides Grace period\n");
}
}
$deployer->refresh();

if ($api->dumpConfig($config, $db)) {
if ($deployment = $deployer->deploy($config)) {
if ($deployer->hasBeenForced()) {
echo $deployer->getNoDeploymentReason() . ", deploying anyway\n";
}
printf("Config '%s' has been deployed\n", $checksum);
} else {
$this->fail(
sprintf("Failed to deploy config '%s'\n", $checksum)
);
echo $deployer->getNoDeploymentReason() . "\n";
return;
}

if ($timeout = $this->getWaitTime()) {
$deployed = $deployer->waitForStartupAfterDeploy($deployment, $timeout);
if ($deployed !== true) {
$this->fail("Waiting for Icinga restart failed '%s': %s\n", $checksum, $deployed);
}
}
}

Expand All @@ -142,4 +162,17 @@ public function deploymentstatusAction()
echo Json::encode($result, JSON_PRETTY_PRINT) . "\n";
}
}

protected function getWaitTime()
{
if ($timeout = $this->params->get('wait')) {
if (!ctype_digit($timeout)) {
$this->fail("--wait must be the number of seconds to wait'");
}

return (int) $timeout;
}

return null;
}
}
3 changes: 2 additions & 1 deletion application/clicommands/ImportsourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Icinga\Application\Benchmark;
use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Core\Json;
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Objects\ImportSource;

Expand Down Expand Up @@ -90,7 +91,7 @@ public function fetchAction()
$data = $hook->fetchData();
$source->applyModifiers($data);
Benchmark::measure(sprintf('Got %d rows, ready to dump JSON', count($data)));
echo json_encode($data, JSON_PRETTY_PRINT);
echo Json::encode($data, JSON_PRETTY_PRINT);
}

/**
Expand Down
113 changes: 113 additions & 0 deletions application/controllers/BranchController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Icinga\Module\Director\Controllers;

use gipfl\Diff\HtmlRenderer\SideBySideDiff;
use gipfl\Diff\PhpDiff;
use gipfl\IcingaWeb2\Widget\NameValueTable;
use Icinga\Module\Director\Data\Db\DbObjectStore;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db\Branch\BranchActivity;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\PlainObjectRenderer;
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Web\Controller\BranchHelper;
use Icinga\Module\Director\Web\Widget\IcingaConfigDiff;
use ipl\Html\Html;

class BranchController extends ActionController
{
use BranchHelper;

public function init()
{
parent::init();
IcingaObject::setDbObjectStore(new DbObjectStore($this->db(), $this->getBranch()));
}

protected function checkDirectorPermissions()
{
}

public function activityAction()
{
$this->assertPermission('director/showconfig');
$ts = $this->params->getRequired('ts');
$this->addSingleTab($this->translate('Activity'));
$this->addTitle($this->translate('Branch Activity'));
$activity = BranchActivity::load($ts, $this->db());
$this->content()->add($this->prepareActivityInfo($activity));
$this->showActivity($activity);
}

protected function prepareActivityInfo(BranchActivity $activity)
{
$table = new NameValueTable();
$table->addNameValuePairs([
$this->translate('Author') => $activity->getAuthor(),
$this->translate('Date') => date('Y-m-d H:i:s', $activity->getTimestamp()),
$this->translate('Action') => $activity->getAction()
. ' ' . preg_replace('/^icinga_/', '', $activity->getObjectTable())
. ' ' . $activity->getObjectName(),
// $this->translate('Actions') => ['Undo form'],
]);
return $table;
}

protected function leftFromActivity(BranchActivity $activity)
{
if ($activity->isActionCreate()) {
return null;
}
$object = DbObjectTypeRegistry::newObject($activity->getObjectTable(), [], $this->db());
foreach ($activity->getFormerProperties()->jsonSerialize() as $key => $value) {
$object->set($key, $value);
}

return $object;
}

protected function rightFromActivity(BranchActivity $activity)
{
if ($activity->isActionDelete()) {
return null;
}
$object = DbObjectTypeRegistry::newObject($activity->getObjectTable(), [], $this->db());
if (! $activity->isActionCreate()) {
foreach ($activity->getFormerProperties()->jsonSerialize() as $key => $value) {
$object->set($key, $value);
}
}
foreach ($activity->getModifiedProperties()->jsonSerialize() as $key => $value) {
$object->set($key, $value);
}

return $object;
}

protected function showActivity(BranchActivity $activity)
{
$left = $this->leftFromActivity($activity);
$right = $this->rightFromActivity($activity);
if ($left instanceof IcingaObject || $right instanceof IcingaObject) {
$this->content()->add(new IcingaConfigDiff(
$left ? $left->toSingleIcingaConfig() : $this->createEmptyConfig(),
$right ? $right->toSingleIcingaConfig() : $this->createEmptyConfig()
));
} else {
$this->content()->add([
Html::tag('h3', $this->translate('Modification')),
new SideBySideDiff(new PhpDiff(
PlainObjectRenderer::render($left->getProperties()),
PlainObjectRenderer::render($right->getProperties())
))
]);
}
}

protected function createEmptyConfig()
{
return new IcingaConfig($this->db());
}
}
34 changes: 29 additions & 5 deletions application/controllers/CommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Icinga\Module\Director\Controllers;

use gipfl\Web\Widget\Hint;
use Icinga\Module\Director\Objects\IcingaCommandArgument;
use Icinga\Module\Director\Web\Table\BranchedIcingaCommandArgumentTable;
use ipl\Html\Html;
use Icinga\Module\Director\Forms\IcingaCommandArgumentForm;
use Icinga\Module\Director\Objects\IcingaCommand;
Expand All @@ -22,9 +24,14 @@ public function init()
parent::init();
$o = $this->object;
if ($o && ! $o->isExternal()) {
if ($this->getBranch()->isBranch()) {
$urlParams = ['uuid' => $o->getUniqueId()->toString()];
} else {
$urlParams = ['name' => $o->getObjectName()];
}
$this->tabs()->add('arguments', [
'url' => 'director/command/arguments',
'urlParams' => ['name' => $o->getObjectName()],
'urlParams' => $urlParams,
'label' => 'Arguments'
]);
}
Expand Down Expand Up @@ -83,16 +90,33 @@ public function argumentsAction()
$o = $this->object;
$this->tabs()->activate('arguments');
$this->addTitle($this->translate('Command arguments: %s'), $o->getObjectName());
$form = IcingaCommandArgumentForm::load()->setCommandObject($o);
if ($id = $p->shift('argument_id')) {
$form = (new IcingaCommandArgumentForm)
->setBranch($this->getBranch())
->setCommandObject($o);
if ($argument = $p->shift('argument')) {
$this->addBackLink('director/command/arguments', [
'name' => $p->get('name')
]);
$form->loadObject($id);
if ($this->branch->isBranch()) {
$arguments = $o->arguments();
$argument = $arguments->get($argument);
// IcingaCommandArgument::create((array) $arguments->get($argument)->toFullPlainObject());
// $argument->setBeingLoadedFromDb();
} else {
$argument = IcingaCommandArgument::load([
'command_id' => $o->get('id'),
'argument_name' => $argument
], $this->db());
}
$form->setObject($argument);
}
$form->handleRequest();
$this->content()->add([$form]);
IcingaCommandArgumentTable::create($o)->renderTo($this);
if ($this->branch->isBranch()) {
(new BranchedIcingaCommandArgumentTable($o, $this->getBranch()))->renderTo($this);
} else {
(new IcingaCommandArgumentTable($o, $this->getBranch()))->renderTo($this);
}
}

protected function hasBasketSupport()
Expand Down
Loading

0 comments on commit ab64c99

Please sign in to comment.