From de137f70aebfe93f444b5627e43f8d4d85638f2a Mon Sep 17 00:00:00 2001 From: Simon L Date: Thu, 17 Nov 2022 13:38:09 +0100 Subject: [PATCH] add an AIO outdated notification Signed-off-by: Simon L --- Containers/mastercontainer/cron.sh | 3 +++ Containers/nextcloud/Dockerfile | 2 ++ Containers/nextcloud/notify-all.sh | 27 +++++++++++++++++++ php/src/Cron/OutdatedNotification.php | 26 +++++++++++++++++++ php/src/Docker/DockerActionManager.php | 36 ++++++++++++++++++++++++-- 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 Containers/nextcloud/notify-all.sh create mode 100644 php/src/Cron/OutdatedNotification.php diff --git a/Containers/mastercontainer/cron.sh b/Containers/mastercontainer/cron.sh index 1fe5ff8d..f8d09c4c 100644 --- a/Containers/mastercontainer/cron.sh +++ b/Containers/mastercontainer/cron.sh @@ -41,6 +41,9 @@ while true; do # Check for updates and send notification if yes sudo -u www-data php /var/www/docker-aio/php/src/Cron/UpdateNotification.php + # Check if AIO is outdated + sudo -u www-data php /var/www/docker-aio/php/src/Cron/OutdatedNotification.php + # Remove sessions older than 24h find "/mnt/docker-aio-config/session/" -mindepth 1 -mmin +1440 -delete diff --git a/Containers/nextcloud/Dockerfile b/Containers/nextcloud/Dockerfile index ccc7d8ca..b860683c 100644 --- a/Containers/nextcloud/Dockerfile +++ b/Containers/nextcloud/Dockerfile @@ -227,12 +227,14 @@ RUN set -ex; \ COPY start.sh / COPY notify.sh / +COPY notify-all.sh / RUN set -ex; \ chmod +x /start.sh && \ chmod +x /entrypoint.sh && \ chmod +r /upgrade.exclude && \ chmod +x /cron.sh && \ chmod +x /notify.sh && \ + chmod +x /notify-all.sh && \ chmod +x /activate-collabora.sh RUN set -ex; \ diff --git a/Containers/nextcloud/notify-all.sh b/Containers/nextcloud/notify-all.sh new file mode 100644 index 00000000..b11130d1 --- /dev/null +++ b/Containers/nextcloud/notify-all.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +if [[ "$EUID" = 0 ]]; then + COMMAND=(sudo -E -u www-data php /var/www/html/occ) +else + COMMAND=(php /var/www/html/occ) +fi + +SUBJECT="$1" +MESSAGE="$2" + +if [ "$("${COMMAND[@]}" config:app:get notifications enabled)" = "no" ]; then + echo "Cannot send notification as notification app is not enabled." + exit 1 +fi + +echo "Posting notifications to all users..." +NC_USERS=$("${COMMAND[@]}" user:list | sed 's|^ - ||g' | sed 's|:.*||') +mapfile -t NC_USERS <<< "$NC_USERS" +for user in "${NC_USERS[@]}" +do + echo "Posting '$SUBJECT' to: $user" + "${COMMAND[@]}" notification:generate "$user" "$NC_DOMAIN: $SUBJECT" -l "$MESSAGE" +done + +echo "Done!" +exit 0 \ No newline at end of file diff --git a/php/src/Cron/OutdatedNotification.php b/php/src/Cron/OutdatedNotification.php new file mode 100644 index 00000000..e652ba3a --- /dev/null +++ b/php/src/Cron/OutdatedNotification.php @@ -0,0 +1,26 @@ +get(\AIO\Docker\DockerActionManager::class); +/** @var \AIO\ContainerDefinitionFetcher $containerDefinitionFetcher */ +$containerDefinitionFetcher = $container->get(\AIO\ContainerDefinitionFetcher::class); + +$id = 'nextcloud-aio-nextcloud'; +$nextcloudContainer = $containerDefinitionFetcher->GetContainerById($id); + +$isNextcloudImageOutdated = $dockerActionManger->isNextcloudImageOutdated(); + +if ($isNextcloudImageOutdated === true) { + $dockerActionManger->sendNotification($nextcloudContainer, 'AIO is outdated!', 'Please open the AIO interface or ask an administrator to update it. If you do not want to do it manually each time, you can enable the daily backup feature from the AIO interface which automatically updates all containers.', '/notify-all.sh'); +} + diff --git a/php/src/Docker/DockerActionManager.php b/php/src/Docker/DockerActionManager.php index 8eee831c..9550dd44 100644 --- a/php/src/Docker/DockerActionManager.php +++ b/php/src/Docker/DockerActionManager.php @@ -540,7 +540,7 @@ class DockerActionManager return true; } - public function sendNotification(Container $container, string $subject, string $message) : void + public function sendNotification(Container $container, string $subject, string $message, string $file = '/notify.sh') : void { if ($this->GetContainerStartingState($container) instanceof RunningState) { @@ -558,7 +558,7 @@ class DockerActionManager 'Tty' => true, 'Cmd' => [ 'bash', - '/notify.sh', + $file, $subject, $message ], @@ -739,4 +739,36 @@ class DockerActionManager } return false; } + + private function GetCreatedTimeOfNextcloudImage() : ?string { + $imageName = 'nextcloud/aio-nextcloud' . ':' . $this->GetCurrentChannel(); + try { + $imageUrl = $this->BuildApiUrl(sprintf('images/%s/json', $imageName)); + $imageOutput = json_decode($this->guzzleClient->get($imageUrl)->getBody()->getContents(), true); + + if (!isset($imageOutput['Created'])) { + error_log('Created is not set of image ' . $imageName); + return null; + } + + return str_replace('T', ' ', $imageOutput['Created']); + } catch (\Exception $e) { + return null; + } + } + + public function isNextcloudImageOutdated() : bool { + $createdTime = $this->GetCreatedTimeOfNextcloudImage(); + + if ($createdTime === null) { + return false; + } + + // If the image is older than 90 days, it is outdated. + if ((time() - (60 * 60 * 24 * 90)) > strtotime($createdTime)) { + return true; + } + + return false; + } }