Webhook support, misc fixes

* Added support for slack (mattermost) webhooks
* Misc fixes
This commit is contained in:
Chris 2022-03-08 01:04:03 +01:00
parent 6cdb155dc5
commit 1f44d9f44b
5 changed files with 53 additions and 11 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/vendor
/composer.lock
/*.phar

View File

@ -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
```
```

View File

@ -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);

View File

@ -0,0 +1,13 @@
<?php
namespace NoccyLabs\FreshDocker\Hooks;
use GuzzleHttp\Client;
use RuntimeException;
interface HookInterface
{
public function sendMessage(string $text, array $options);
}

View File

@ -5,7 +5,7 @@ namespace NoccyLabs\FreshDocker\Hooks;
use GuzzleHttp\Client;
use RuntimeException;
class SlackHook
class SlackHook implements HookInterface
{
private array $options = [];
@ -49,6 +49,10 @@ class SlackHook
if (array_key_exists('channel', $options)) $body['channel'] = $options['channel'];
if (array_key_exists('username', $options)) $body['username'] = $options['username'];
if (array_key_exists('icon_url', $options)) $body['icon_url'] = $options['icon_url'];
if (array_key_exists('info', $options)) {
if (!array_key_exists('props', $body)) $body['props'] = [];
$body['props']['card'] = $options['info'];
}
$body['text'] = $text;
return $body;