Skip to content

Commit

Permalink
Merge pull request #1843 from gianlucapiccolo/deploy-hook
Browse files Browse the repository at this point in the history
Add deployment hooks
  • Loading branch information
Thomas-Gelf authored Apr 24, 2019
2 parents 6bcb4a4 + 4a4808e commit 099c53a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
25 changes: 18 additions & 7 deletions library/Director/Core/CoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Exception;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Hook\DeploymentHook;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\DirectorDeploymentLog;
use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Web\Hook;
use RuntimeException;

class CoreApi implements DeploymentApiInterface
Expand Down Expand Up @@ -816,6 +818,12 @@ public function dumpConfig(IcingaConfig $config, Db $db, $packageName = null)
// 'module_name' => $moduleName,
));

/** @var DeploymentHook[] $hooks */
$hooks = Hook::all('director/Deployment');
foreach ($hooks as $hook) {
$hook->beforeDeploy($deployment);
}

$this->assertPackageExists($packageName);

$response = $this->client()->post('config/stages/' . urlencode($packageName), [
Expand All @@ -826,20 +834,23 @@ public function dumpConfig(IcingaConfig $config, Db $db, $packageName = null)
// $deployment->duration_ms = $duration;
$deployment->set('duration_dump', $duration);

$succeeded = 'n';
if ($response->succeeded()) {
if ($stage = $response->getResult('stage', ['package' => $packageName])) { // Status?
$deployment->set('stage_name', key($stage));
$deployment->set('dump_succeeded', 'y');
} else {
$deployment->set('dump_succeeded', 'n');
$succeeded = 'y';
}
} else {
$deployment->set('dump_succeeded', 'n');
}

$deployment->set('dump_succeeded', $succeeded);
$deployment->store($db);

return $deployment->set('dump_succeeded', 'y');
if ($succeeded === 'y') {
foreach ($hooks as $hook) {
$hook->onSuccessfullDump($deployment);
}
}

return $deployment;
}

protected function shortenStartupLog($log)
Expand Down
28 changes: 28 additions & 0 deletions library/Director/Hook/DeploymentHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Icinga\Module\Director\Hook;

use Icinga\Module\Director\Objects\DirectorDeploymentLog;

abstract class DeploymentHook
{
/**
* Please override this method if you want to change the behaviour
* of the deploy (stop it by throwing an exception for some reason)
*
* @param DirectorDeploymentLog $deployment
*/
public function beforeDeploy(DirectorDeploymentLog $deployment)
{
}

/**
* Please override this method if you want to trigger custom actions
* on a successfull dump of the Icinga configuration
*
* @param DirectorDeploymentLog $deployment
*/
public function onSuccessfullDump(DirectorDeploymentLog $deployment)
{
}
}

0 comments on commit 099c53a

Please sign in to comment.