Improved hooks and scripts
* Invoke hooks before and after updating * Scripts can now be run --before and --after
This commit is contained in:
		@@ -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);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user