From a46a36ee4151237d9c56a2d955ebf166057302d4 Mon Sep 17 00:00:00 2001 From: szaimen Date: Tue, 30 Nov 2021 13:11:19 +0100 Subject: [PATCH] add sendnotifications and update checker cron Signed-off-by: szaimen Signed-off-by: Lukas Reschke --- Containers/nextcloud/Dockerfile | 1 + Containers/nextcloud/notify.sh | 2 +- php/src/Cron/cron.php | 30 +++++++++++++++++ php/src/Docker/DockerActionManager.php | 45 ++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 php/src/Cron/cron.php diff --git a/Containers/nextcloud/Dockerfile b/Containers/nextcloud/Dockerfile index 20b646c3..4f932a77 100644 --- a/Containers/nextcloud/Dockerfile +++ b/Containers/nextcloud/Dockerfile @@ -213,6 +213,7 @@ RUN set -ex; \ openssl \ gnupg \ dirmngr \ + git \ ; \ rm -rf /var/lib/apt/lists/* diff --git a/Containers/nextcloud/notify.sh b/Containers/nextcloud/notify.sh index 8871c1d7..3b0875b7 100644 --- a/Containers/nextcloud/notify.sh +++ b/Containers/nextcloud/notify.sh @@ -3,7 +3,7 @@ SUBJECT="$1" MESSAGE="$2" -if [ "$(php /var/www/html/occ config:app:get notificaations enabled)" = "no" ]; then +if [ "$(php /var/www/html/occ config:app:get notifications enabled)" = "no" ]; then echo "Cannot send notification as notification app is not enabled." exit 1 fi diff --git a/php/src/Cron/cron.php b/php/src/Cron/cron.php new file mode 100644 index 00000000..ab40653a --- /dev/null +++ b/php/src/Cron/cron.php @@ -0,0 +1,30 @@ +get(\AIO\Docker\DockerActionManager::class); +/** @var \AIO\ContainerDefinitionFetcher $containerDefinitionFetcher */ +$containerDefinitionFetcher = $container->get(\AIO\ContainerDefinitionFetcher::class); + +$id = 'nextcloud-aio-nextcloud'; +$nextcloudContainer = $containerDefinitionFetcher->GetContainerById($id); + +$isMastercontainerUpdateAvailable = $dockerActionManger->IsMastercontainerUpdateAvailable(); +$isAnyUpdateAvailable = $dockerActionManger->isAnyUpdateAvailable(); + +if ($isMastercontainerUpdateAvailable === true) { + $dockerActionManger->sendNotification($nextcloudContainer, 'Mastercontainer update available!', 'Please open your management interface to update it.'); +} + +if ($isAnyUpdateAvailable === true) { + $dockerActionManger->sendNotification($nextcloudContainer, 'Container updates available!', 'Please open your management interface to update them.'); +} diff --git a/php/src/Docker/DockerActionManager.php b/php/src/Docker/DockerActionManager.php index 9372ec3a..05addf7f 100644 --- a/php/src/Docker/DockerActionManager.php +++ b/php/src/Docker/DockerActionManager.php @@ -367,6 +367,51 @@ class DockerActionManager } } + public function sendNotification(Container $container, string $subject, string $message) + { + if ($this->GetContainerStartingState($container) instanceof RunningState) { + + $containerName = $container->GetIdentifier(); + + // schedule the exec + $url = $this->BuildApiUrl(sprintf('/containers/%s/exec', urlencode($containerName))); + $response = json_decode( + $this->guzzleClient->request( + 'POST', + $url, + [ + 'json' => [ + 'AttachStdout' => true, + 'Tty' => true, + 'Cmd' => [ + 'bash', + '/notify.sh', + $subject, + $message + ], + ], + ] + )->getBody()->getContents() + ); + + // get the id from the response + $id = $response['Id']; + + // start the exec + $url = $this->BuildApiUrl(sprintf('/exec/%s/start', $id)); + $this->guzzleClient->request( + 'POST', + $url, + [ + 'json' => [ + 'Detach' => false, + 'Tty' => true, + ], + ] + ); + } + } + public function DisconnectContainerFromNetwork(Container $container) {