Browse Source

Revised routines, more structure, validate no responce from release-url

Michael Honkoop 1 tuần trước cách đây
mục cha
commit
4bb141f5c4
1 tập tin đã thay đổi với 60 bổ sung24 xóa
  1. 60 24
      localchecks/nextcloud-updates.sh

+ 60 - 24
localchecks/nextcloud-updates.sh

@@ -64,12 +64,13 @@ SERVICE="NextCloud"
 # All configureable parameters are above this section.
 ######################
 
-# check if jq package is present on the system
-if ! command -v curl >/dev/null 2>&1; then
-    echo "2 \"$SERVICE\" - $SERVICE localcheck requires jq package to be installed"
-    echo "2 \"$SERVICE Apps\" - $SERVICE Apps  localcheck requires jq package to be installed"
-    exit 0
-fi
+for COMMAND in sudo php jq curl; do
+    if ! command -v "$COMMAND" >/dev/null 2>&1; then
+        echo "2 \"$SERVICE\" - $SERVICE localcheck requires $COMMAND command to be installed"
+        echo "2 \"$SERVICE Apps\" - $SERVICE localcheck requires $COMMAND command to be installed"
+        exit 0
+    fi
+done
 
 # check if configured apache user exists
 if ! id -u "$APACHEUSER" >/dev/null 2>&1; then
@@ -88,37 +89,72 @@ fi
 # Get current installed Nextcloud version-information
 # in v29.x.x version gives more detail, which is not given by the check on installed version, so switched to versionstring instead.
 
-NCINSTALLED=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ status --output=json | jq -r '.versionstring' | cut -d. -f1-"$VERSION_FIELDS")
+NCINSTALLED=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ status --output=json | jq -r '.versionstring // empty' | cut -d. -f1-"$VERSION_FIELDS")
 
-# Get latest avaiable (core) version information
+# Get latest avaiable (core) version information from release-url
 # In v29.x.x this has changed outputs <name-of-instance> <version>,
+NCRELEASEDATA=$(curl -fsS --connect-timeout 5 --max-time 10 "$RELEASEURL" 2>/dev/null)
+CURL_RC=$?
 
-NCAVAILABLE=$(curl -s --connect-timeout 5 --fail-with-body "$RELEASEURL" | sed -e 's/<[^>]*>//g' | grep -o "^nextcloud.*.zip" | tail -n1 | cut -d'-' -f 2 | sed 's/.\{4\}$//' | cut -d. -f1-"$VERSION_FIELDS")
+# If curl failed, report UNKNOWN
+if [ "$CURL_RC" -ne 0 ]; then
+    echo "3 \"$SERVICE\" - Unable to retrieve data from release-URL for $SERVICE Core (curl exit code $CURL_RC)"
+    echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
+    exit 0
+fi
 
-# if the check on the release-url is not returning a version - report status
-if [ -z "$NCAVAILABLE" ]; then
-    echo "1 \"$SERVICE\" - No data received from release-URL, can not determine latest release-version for $SERVICE Core"
+# If the release URL returned an empty response, report UNKNOWN
+if [ -z "$NCRELEASEDATA" ]; then
+    echo "3 \"$SERVICE\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Core"
+    echo "3 \"$SERVICE Apps\" - No data received from release-URL, cannot determine latest release-version for $SERVICE Apps"
+    exit 0
+fi
 
-# compare resuts of installed and available version and report update available if they differ
-elif [ "$NCINSTALLED" != "$NCAVAILABLE" ]; then
-   echo "1 \"$SERVICE\" - A new $SERVICE Core version ($NCAVAILABLE) is available, running Core version is: $NCINSTALLED please upgrade"
-else
-    sudo -u "$APACHEUSER" php "$BASEPATH"/occ status -e
-    if [ $? -eq 0 ]; then
-        echo "0 \"$SERVICE\" - $SERVICE all up to date"
-    elif [ $? -eq 1 ]; then
+# Extract all available Nextcloud release versions from the release data,
+# sort them as versions and select the highest version.
+NCAVAILABLE=$(printf '%s\n' "$NCRELEASEDATA" | grep -oE 'nextcloud-[0-9]+\.[0-9]+\.[0-9]+\.zip' | sed -E 's/^nextcloud-([0-9]+\.[0-9]+\.[0-9]+)\.zip$/\1/' | sort -V | tail -n1)
+
+# Apply configured version-comparison granularity to the available version
+NCAVAILABLE=$(printf '%s\n' "$NCAVAILABLE" | cut -d. -f1-"$VERSION_FIELDS")
+
+# Compare installed and available versions.
+if [ "$NCINSTALLED" = "$NCAVAILABLE" ]; then
+    # Versions match, so check the actual Nextcloud core status.
+    sudo -u "$APACHEUSER" php "$BASEPATH"/occ status -e >/dev/null 2>&1
+    STATUS_RC=$?
+
+    if [ "$STATUS_RC" -eq 0 ]; then
+        echo "0 \"$SERVICE\" - $SERVICE Core is latest version ($NCAVAILABLE)"
+    elif [ "$STATUS_RC" -eq 1 ]; then
         echo "1 \"$SERVICE\" - $SERVICE maintenance-mode is enabled"
-    elif [ $? -eq 2 ]; then
+    elif [ "$STATUS_RC" -eq 2 ]; then
         echo "2 \"$SERVICE\" - $SERVICE Core needs updating"
+    else
+        echo "3 \"$SERVICE\" - Unable to determine $SERVICE Core status (occ status exit code $STATUS_RC)"
     fi
+# Installed version is lower than available version
+elif [ "$(printf '%s\n%s\n' "$NCINSTALLED" "$NCAVAILABLE" | sort -V | head -n1)" = "$NCINSTALLED" ]; then
+    echo "1 \"$SERVICE\" - A new $SERVICE Core version ($NCAVAILABLE) is available, running Core version is: $NCINSTALLED, please upgrade"
+# Installed version is higher than available version (happens when a release is retracted)
+else
+    echo "2 \"$SERVICE\" - $SERVICE Core version ($NCAVAILABLE) is lower than the running Core version $NCINSTALLED, please investigate."
 fi
 
 APPRESULT=$(sudo -u "$APACHEUSER" php "$BASEPATH"/occ app:update --showonly)
+APP_RC=$?
+
+# If occ failed, report UNKNOWN
+if [ "$APP_RC" -ne 0 ]; then
+    echo "3 \"$SERVICE Apps\" - Unable to determine available app updates (occ app:update exit code $APP_RC)"
+    exit 0
+fi
+
+# Check if all apps are up to date
 if [[ "$APPRESULT" == *"All apps are up-to-date"* ]]; then
     echo "0 \"$SERVICE Apps\" - $SERVICE Apps all up to date"
     exit 0
 else
-    UPDATECOUNT=$(echo "$APPRESULT" | wc -l)
-    echo "1 \"$SERVICE Apps\" - $UPDATECOUNT $SERVICE Apps requires updates"
+    UPDATECOUNT=$(printf '%s\n' "$APPRESULT" | wc -l)
+    echo "1 \"$SERVICE Apps\" - $UPDATECOUNT $SERVICE Apps require updates"
     exit 0
-fi
+fi