Merge pull request #1843 from gianlucapiccolo/deploy-hook

Add deployment hooks
This commit is contained in:
Thomas Gelf 2019-04-24 13:38:43 +02:00 committed by GitHub
commit 099c53a008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 7 deletions

View File

@ -5,11 +5,13 @@ namespace Icinga\Module\Director\Core;
use Exception; use Exception;
use Icinga\Exception\NotFoundError; use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Hook\DeploymentHook;
use Icinga\Module\Director\IcingaConfig\IcingaConfig; use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaCommand; use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\DirectorDeploymentLog; use Icinga\Module\Director\Objects\DirectorDeploymentLog;
use Icinga\Module\Director\Objects\IcingaZone; use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Web\Hook;
use RuntimeException; use RuntimeException;
class CoreApi implements DeploymentApiInterface class CoreApi implements DeploymentApiInterface
@ -816,6 +818,12 @@ constants
// 'module_name' => $moduleName, // 'module_name' => $moduleName,
)); ));
/** @var DeploymentHook[] $hooks */
$hooks = Hook::all('director/Deployment');
foreach ($hooks as $hook) {
$hook->beforeDeploy($deployment);
}
$this->assertPackageExists($packageName); $this->assertPackageExists($packageName);
$response = $this->client()->post('config/stages/' . urlencode($packageName), [ $response = $this->client()->post('config/stages/' . urlencode($packageName), [
@ -826,20 +834,23 @@ constants
// $deployment->duration_ms = $duration; // $deployment->duration_ms = $duration;
$deployment->set('duration_dump', $duration); $deployment->set('duration_dump', $duration);
$succeeded = 'n';
if ($response->succeeded()) { if ($response->succeeded()) {
if ($stage = $response->getResult('stage', ['package' => $packageName])) { // Status? if ($stage = $response->getResult('stage', ['package' => $packageName])) { // Status?
$deployment->set('stage_name', key($stage)); $deployment->set('stage_name', key($stage));
$deployment->set('dump_succeeded', 'y'); $succeeded = 'y';
} else {
$deployment->set('dump_succeeded', 'n');
} }
} else {
$deployment->set('dump_succeeded', 'n');
} }
$deployment->set('dump_succeeded', $succeeded);
$deployment->store($db); $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) protected function shortenStartupLog($log)

View File

@ -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)
{
}
}