From 1f44d9f44b7a1465d798489485c073b140e7527d Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Tue, 8 Mar 2022 01:04:03 +0100 Subject: [PATCH] Webhook support, misc fixes * Added support for slack (mattermost) webhooks * Misc fixes --- .gitignore | 3 +++ README.md | 5 +++-- bin/freshdocker | 37 +++++++++++++++++++++++++++++-------- src/Hooks/HookInterface.php | 13 +++++++++++++ src/Hooks/SlackHook.php | 6 +++++- 5 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 src/Hooks/HookInterface.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2be2e62 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor +/composer.lock +/*.phar diff --git a/README.md b/README.md index 24786e9..6e069e3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Fresh: Keeping your docker stacks up to date +## Building +Build using NoccyLabs Pharlite. ## Usage @@ -24,7 +26,6 @@ Update everything and up any new containers in current dir: $ fresh.phar ``` - Manually pulling when changed: ``` @@ -32,4 +33,4 @@ if ! fresh.phar --check; then docker-compose pull docker-compose up -d fi -``` \ No newline at end of file +``` diff --git a/bin/freshdocker b/bin/freshdocker index ae38347..ba647a8 100755 --- a/bin/freshdocker +++ b/bin/freshdocker @@ -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); diff --git a/src/Hooks/HookInterface.php b/src/Hooks/HookInterface.php new file mode 100644 index 0000000..183c4df --- /dev/null +++ b/src/Hooks/HookInterface.php @@ -0,0 +1,13 @@ +