Webhook support, misc fixes
* Added support for slack (mattermost) webhooks * Misc fixes
This commit is contained in:
		@@ -12,23 +12,37 @@ use NoccyLabs\FreshDocker\State\PersistentState;
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
$opts = getopt("hd:c:i:q", [ "help", "dir:", "config:", "image:", "pull", "credentials:", "check", "slack:", "quiet" ]);
 | 
			
		||||
$opts = getopt("hd:c:i:qv", [ "help", "dir:", "config:", "image:", "pull", "credentials:", "check", "slack:", "quiet", "prune" ]);
 | 
			
		||||
 | 
			
		||||
if (array_key_exists('h', $opts) || array_key_exists('help', $opts)) {
 | 
			
		||||
 | 
			
		||||
    printf("fresh.phar - (c) 2022, NoccyLabs / GPL v3 or later\n");
 | 
			
		||||
    printf("Check for updates to docker images or compose stacks.\n\n");
 | 
			
		||||
    printf("Usage:\n    %s [options]\n\n", basename($argv[0]));
 | 
			
		||||
    printf("Options:\n");
 | 
			
		||||
    printf("  General:\n");
 | 
			
		||||
    foreach([
 | 
			
		||||
        '-h,--help' => "Show this help",
 | 
			
		||||
        '-q,--quiet' => "Don't show any output (except dockers)",
 | 
			
		||||
        '-d,--dir DIR' => "Change working directory to DIR",
 | 
			
		||||
        '-c,--config FILE' => "Use a custom configuration (not implemented)",
 | 
			
		||||
        '-i,--image REF' => "Check a specific image instead of using config/docker-compose",
 | 
			
		||||
        '--pull' => "Only pull if new, don't up",
 | 
			
		||||
        '--credentials TYPE' => "Select credentials loader (auto or basic)",
 | 
			
		||||
        '--check' => "Only check, set exit code 1 if not fresh",
 | 
			
		||||
        '-q,--quiet' => "Don't show any output (except dockers)",
 | 
			
		||||
    ] as $a=>$b)
 | 
			
		||||
        printf("     %-20s %s\n", $a, $b);
 | 
			
		||||
        '--prune' => "Prune dangling images after pull and up", 
 | 
			
		||||
    ] as $a=>$b) printf("    %-20s %s\n", $a, $b);
 | 
			
		||||
 | 
			
		||||
    printf("  Webhooks:\n");
 | 
			
		||||
    foreach([
 | 
			
		||||
        '--slack URL' => "Notify a Slack webhook when updating",
 | 
			
		||||
    ] as $a=>$b) printf("    %-20s %s\n", $a, $b);
 | 
			
		||||
 | 
			
		||||
    printf("  Configuration:\n");
 | 
			
		||||
    foreach([
 | 
			
		||||
        '-c,--config FILE' => "Use a custom configuration (not implemented)",
 | 
			
		||||
        '--credentials TYPE' => "Select credentials loader (auto or basic)",
 | 
			
		||||
    ] as $a=>$b) printf("    %-20s %s\n", $a, $b);
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    exit(0);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -38,6 +52,8 @@ $path = realpath($opts['d'] ?? ($opts['dir'] ?? getcwd()));
 | 
			
		||||
$onlyPull = array_key_exists('p', $opts) || array_key_exists('pull', $opts);
 | 
			
		||||
$onlyCheck = array_key_exists('check', $opts);
 | 
			
		||||
$quiet = array_key_exists('q', $opts) || array_key_exists('quiet', $opts);
 | 
			
		||||
$verbose = array_key_exists('v', $opts);
 | 
			
		||||
$prune = array_key_exists('prune', $opts);
 | 
			
		||||
 | 
			
		||||
$slackUrl = array_key_exists('slack', $opts) ? $opts['slack'] : null;
 | 
			
		||||
$slackHook = null;
 | 
			
		||||
@@ -87,7 +103,7 @@ foreach ($checks as $check) {
 | 
			
		||||
 | 
			
		||||
    $credentials = $credentialsLoader->getCredentials($reg);
 | 
			
		||||
    if (!$credentials) {
 | 
			
		||||
        //printf("notice: missing credentials for %s, skipping image %s\n", $reg, $check);
 | 
			
		||||
        if ($verbose) printf("%s: missing credentials for %s\n", $ref->getImage(), $ref->getRegistry());
 | 
			
		||||
        continue;
 | 
			
		||||
    }
 | 
			
		||||
    $client = new RegistryV2Client($reg, $credentials);
 | 
			
		||||
@@ -119,7 +135,12 @@ if ($update) {
 | 
			
		||||
        $slackHook->sendMessage($msg, []);
 | 
			
		||||
    }
 | 
			
		||||
    passthru("docker-compose pull");
 | 
			
		||||
    if (!$onlyPull) passthru("docker-compose up -d");
 | 
			
		||||
    if (!$onlyPull) {
 | 
			
		||||
        passthru("docker-compose up -d");
 | 
			
		||||
        if ($prune) {
 | 
			
		||||
            passthru("docker image prune -f");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
exit($update?1:0);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user