Improved hooks and scripts

* Invoke hooks before and after updating
* Scripts can now be run --before and --after
This commit is contained in:
2022-03-19 23:22:08 +01:00
parent e77e61d0b0
commit 35f88303fa
2 changed files with 53 additions and 12 deletions

View File

@ -30,7 +30,8 @@ class Refresher
],
'Hooks' => [
'slack' => [ null, 'slack:', "Notify a slack webhook when updating", "FRESH_SLACK" ],
'script' => [ null, 'after:', "Invoke script after updating", "FRESH_AFTER" ],
'script-after' => [ null, 'after:', "Invoke script after updating", "FRESH_AFTER" ],
'script-before' => [ null, 'before:', "Invoke script before updating", "FRESH_BEFORE" ],
],
'Config' => [
'config' => [ 'c:', 'config:', "Use custom configuration file", "FRESH_CONFIG" ],
@ -175,9 +176,11 @@ class Refresher
}
if ($updated !== null) {
$this->callHooks($updated);
$this->doUpdate($updated);
$this->callScript();
$this->callHooks($updated, 'before');
$this->callScript('before');
$this->doUpdate();
$this->callHooks($updated, 'after');
$this->callScript('after');
}
exit(($updated === null) ? 0 : 1);
@ -362,13 +365,22 @@ class Refresher
}
}
private function callHooks(array $updated)
private function callHooks(array $updated, string $event)
{
$images = [];
foreach ($updated as $u) {
$images[] = sprintf("%s/%s:%s", $u->ref->getRegistry(), $u->ref->getImage(), $u->ref->getTag());
switch ($event) {
case 'before':
$images = [];
foreach ($updated as $u) {
$images[] = sprintf("%s/%s:%s", $u->ref->getRegistry(), $u->ref->getImage(), $u->ref->getTag());
}
$msg = "Deploying updated containers:\n* ".join("\n* ", $images);
break;
case 'after':
$msg = "Deploy complete";
break;
default:
return;
}
$msg = "Deploying updated containers:\n* ".join("\n* ", $images);
foreach ($this->hooks as $hook) {
$hook->sendMessage($msg, []);
@ -376,9 +388,9 @@ class Refresher
}
private function callScript()
private function callScript(string $event)
{
$script = $this->options['script'];
$script = $this->options["script-{$event}"] ?? null;
if ($script) {
$this->exec($script);
}