Browse Source

Improve linux-update.sh with better error handling

- Add proper error handling with set -euo pipefail
- Support for GITHUB_REPO environment variable
- Better error messages and timeout handling
- Cleanup trap for safer execution
- Enhanced curl with failure detection
mgomon 8 months ago
parent
commit
2c9459f35a
1 changed files with 65 additions and 27 deletions
  1. 65 27
      scripts/linux-update.sh

+ 65 - 27
scripts/linux-update.sh

@@ -1,30 +1,68 @@
 #!/usr/bin/env bash
-if [ -z "$1" ]
-  then
-  echo 'No branch setup.. using v2-master'
-  BRANCH="v2-master"
-elif [ "$1" == "v2-develop" ] || [ "$1" == "develop" ] || [ "$1" == "dev" ]
-  then
-  BRANCH="v2-develop"
-elif [ "$1" == "v2-master" ] || [ "$1" == "master" ]
-  then
-  BRANCH="v2-master"
+
+# Organizr Linux Update Script
+# Docker-compatible automated update script
+
+set -euo pipefail
+
+# Configuration
+GITHUB_REPO="${GITHUB_REPO:-metalcated/Organizr}"
+
+# Determine branch
+if [ -z "${1:-}" ]; then
+    echo "No branch specified, using v2-master"
+    BRANCH="v2-master"
+elif [ "$1" == "v2-develop" ] || [ "$1" == "develop" ] || [ "$1" == "dev" ]; then
+    BRANCH="v2-develop"
+elif [ "$1" == "v2-master" ] || [ "$1" == "master" ]; then
+    BRANCH="v2-master"
 else
-  echo "$1 is not a valid branch, exiting"
-  exit 1
+    echo "$1 is not a valid branch, exiting"
+    exit 1
 fi
-SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
-UPGRADEPATH=$SCRIPTPATH"/upgrade"
-UPGRADEFILE=$SCRIPTPATH"/upgrade/upgrade.zip"
-FOLDER=$UPGRADEPATH"/Organizr-"${BRANCH#v}
-URL=https://github.com/causefx/Organizr/archive/${BRANCH}.zip
-mkdir -p $UPGRADEPATH                                                  && \
-curl -sSL ${URL} > $UPGRADEFILE                                        && \
-unzip $UPGRADEFILE -d $UPGRADEPATH                                     && \
-cd $FOLDER                                                             && \
-cp -r ./ $SCRIPTPATH/../                                               && \
-cd $SCRIPTPATH                                                         && \
-rm $UPGRADEFILE                                                        && \
-rm -rf $FOLDER                                                         && \
-rm -rf $UPGRADEPATH                                                    && \
-exit 0
+
+# Setup paths
+SCRIPTPATH="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P)"
+UPGRADEPATH="$SCRIPTPATH/upgrade"
+UPGRADEFILE="$UPGRADEPATH/upgrade.zip"
+FOLDER="$UPGRADEPATH/Organizr-${BRANCH#v}"
+URL="https://github.com/$GITHUB_REPO/archive/${BRANCH}.zip"
+
+echo "Updating Organizr from $GITHUB_REPO:$BRANCH"
+
+# Cleanup function
+cleanup() {
+    rm -rf "$UPGRADEPATH" 2>/dev/null || true
+}
+trap cleanup EXIT
+
+# Create upgrade directory
+mkdir -p "$UPGRADEPATH"
+
+# Download with error handling
+echo "Downloading update..."
+if ! curl -sSL --fail --connect-timeout 30 "$URL" -o "$UPGRADEFILE"; then
+    echo "Error: Failed to download update from $URL"
+    exit 1
+fi
+
+# Extract with error handling  
+echo "Extracting files..."
+if ! unzip -q "$UPGRADEFILE" -d "$UPGRADEPATH"; then
+    echo "Error: Failed to extract update files"
+    exit 1
+fi
+
+# Verify extraction
+if [ ! -d "$FOLDER" ]; then
+    echo "Error: Expected folder not found: $FOLDER"
+    exit 1
+fi
+
+# Apply update
+echo "Applying update..."
+cd "$FOLDER"
+cp -r ./* "$SCRIPTPATH/../"
+
+# Cleanup is handled by trap
+echo "Update completed successfully"