Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deployment hooks #1843

Merged
merged 1 commit into from Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 18 additions & 7 deletions library/Director/Core/CoreApi.php
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
@@ -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)
{
}
}