From d53a93020bb103bd01118993b3f7496ed8423514 Mon Sep 17 00:00:00 2001 From: Grothesk242 <57298681+Grothesk242@users.noreply.github.com> Date: Fri, 24 May 2024 16:46:25 +0200 Subject: [PATCH] Update node-update.sh (#525) * Update node-update.sh * Update node-update.sh fix names. yes filename !== command name * Update node-update.sh --------- Co-authored-by: Ingo Fischer --- node-update.sh | 115 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 81 insertions(+), 34 deletions(-) diff --git a/node-update.sh b/node-update.sh index 3afaf7e8..6157170c 100644 --- a/node-update.sh +++ b/node-update.sh @@ -1,10 +1,10 @@ #!/bin/bash -# iobroker node-update +# iobroker nodejs-update # written to help updating and fixing nodejs on linux (Debian based Distros) #To be manually changed: -VERSION="2023-10-13" -NODE_MAJOR=18 #recommended major nodejs version for ioBroker, please adjust if the recommendation changes. This is only the target for fallback. +VERSION="2024-05-23" +NODE_MAJOR=20 #recommended major nodejs version for ioBroker, please adjust if the recommendation changes. This is only the target for fallback. ## Excluding systems: SYSTDDVIRT=$(systemd-detect-virt 2>/dev/null); @@ -17,8 +17,9 @@ then exit 1; elif [ "$(id -u)" -eq 0 ]; then - echo -e "You should not use root directly on your system!\nBetter use your standard user!\n\n"; - sleep 3; + echo -e "This script must not be run as root! \nPlease use your standard user!" + unset LC_ALL; + exit 1; fi; if [[ $SYSTDDVIRT = "wsl" ]]; @@ -28,12 +29,18 @@ if [[ $SYSTDDVIRT = "wsl" ]]; exit 1; fi; +if [ -z $(type -P apt-get) ] + then + echo "Only a Debian-based Linux is supported" + unset LC_ALL; + exit 1; +fi; ### Starting the skript -echo -e "ioBroker node-update v$VERSION is starting. Please be patient!"; +echo -e "ioBroker nodejs-update v$VERSION is starting. Please be patient!"; HOST=$(hostname) NODERECOM=$(iobroker state getValue system.host."$HOST".versions.nodeNewestNext); #reading node version from iob states. If successful, no fallback required. -if [[ $NODERECOM != [[:digit:]]*.[[:digit:]]*.[[:digit:]]* ]]; #check if a semver'd nodejs installation is found +if [[ $NODERECOM != [[:digit:]]*.[[:digit:]]*.[[:digit:]]* ]]; #check if a semvered nodejs installation is found then NODERECOMNF=1; #marker for 'no recommended version found' fi; @@ -47,7 +54,7 @@ NODE_MAJOR=$1; NODERECOM=CUSTOM; fi; # ------------------------------ -# functions for ioBroker node-update - Code borrowed from 'iob installer' ;-) +# functions for ioBroker nodejs-update - Code borrowed from 'iob installer' ;-) # ------------------------------ @@ -105,8 +112,16 @@ fi; USER_GROUP="$USER" fi +if + [[ "$INSTALL_CMD" != "apt-get" ]]; +then + echo "Non-Debian-based Systems are not supported, exiting"; + unset LC_ALL; + exit; +fi; + clear; -echo -e "ioBroker nodejs updater $VERSION"; +echo -e "ioBroker nodejs fixer $VERSION"; if [[ -n "$NODERECOM" ]] && [[ "$NODERECOM" = [[:digit:]]*.[[:digit:]]*.[[:digit:]]* ]]; then @@ -115,34 +130,40 @@ if [[ -n "$NODERECOM" ]] && [[ "$NODERECOM" = [[:digit:]]*.[[:digit:]]*.[[:digit elif [[ "$NODERECOM" == CUSTOM ]] then - echo -e "You requested to intall latest version from nodejs v$1 tree." + echo -e "You requested to install latest version from nodejs v$1 tree." else NODERECOMNF=1; echo -e "No recommendation for a nodejs version found on your system. We recommend to install latest version from nodejs v$NODE_MAJOR tree."; fi; -echo ""; -echo "Your current setup is:"; + echo ""; + echo "Your current setup is:"; + if [[ -f /usr/bin/nodejs ]]; -then -echo -e "$(type -p nodejs) \t$(nodejs -v)"; + then + echo -e "$(type -p nodejs) \t$(nodejs -v)"; fi; -echo -e "$(type -p node) \t\t$(node -v)"; -echo -e "$(type -p npm) \t\t$(npm -v)"; -echo -e "$(type -p npx) \t\t$(npx -v)"; + echo -e "$(type -p node) \t\t$(node -v)"; + echo -e "$(type -p npm) \t\t$(npm -v)"; + echo -e "$(type -p npx) \t\t$(npx -v)"; + if [[ -f /usr/bin/corepack ]] -then -echo -e "$(type -p corepack) \t$(corepack -v)"; + then + echo -e "$(type -p corepack) \t$(corepack -v)"; fi; + PATHNODEJS=$(type -p nodejs); PATHNODE=$(type -p node); PATHNPM=$(type -p npm); PATHNPX=$(type -p npx); + if [[ -f /usr/bin/corepack ]]; then PATHCOREPACK=$(type -p corepack); fi; + if [[ -f /usr/bin/nodejs ]]; then VERNODEJS=$(nodejs -v); fi; + VERNODE=$(node -v); VERNPM=$(npm -v); VERNPX=$(npx -v); @@ -204,7 +225,7 @@ if if [[ -f /usr/bin/nodejs && "$PATHNODEJS" != "/usr/bin/nodejs" ]]; then - echo "*** Deleting $PATHNODEJS ***"; + echo -e "*** Deleting $PATHNODEJS ***"; $SUDOX rm "$(type -p nodejs)"; fi if @@ -240,7 +261,7 @@ fi; if [[ "$INSTALL_CMD" != "apt-get" ]]; then - echo "Non-Debian-based Systems are not supported yet, exiting"; + echo "Non-Debian-based Systems are not supported, exiting"; unset LC_ALL; exit; fi; @@ -248,9 +269,16 @@ VERNODE=$(node -v); if [[ "$VERNODE" = "v$NODERECOM" ]]; then echo -e "\033[32mNothing to do\033[0m - Your version is the recommended one."; -echo -e "\nYou can now keep your whole system up-to-date using the usual 'sudo apt update && sudo apt full-upgrade' commands." -echo "Please DO NOT USE node version managers like 'nvm', 'n' and others in parallel. They will break your current installation!" +echo -e "\n***You can now keep your whole system up-to-date using the usual 'sudo apt update && sudo apt full-upgrade' commands. ***" +echo "*** DO NOT USE node version managers like 'nvm', 'n' and others in parallel. They will break your current installation! ***" +echo -e "\n *** DO NOT use 'nodejs-update' as part of a regular update process! ***"; unset LC_ALL; +if [[ -f "/var/run/reboot-required" ]]; + then + echo ""; + echo "This system needs to be REBOOTED NOW!"; + echo ""; +fi; exit; fi; if [[ "$VERNODE" != "v$NODERECOM" ]] && [[ "$NODERECOM" == [[:digit:]]*.[[:digit:]]*.[[:digit:]]* ]]; @@ -261,13 +289,19 @@ then if [[ "$char" = "y" ]] || [[ "$char" = "Y" ]] then - echo "Trying to update your installation now. Please be patient." + echo "Trying to fix your installation now. Please be patient." # Finding nodesource.gpg or nodesource.key and deleting. Current key is pulled in later. - $SUDOX rm "$($SUDOX find / \( -path /proc -o -path /dev -o -path /sys -o -path /lost+found -o -path /mnt \) -prune -false -o -name nodesource.[gk]* -print)"; + $SUDOX rm "$($SUDOX find / \( -path /proc -o -path /dev -o -path /sys -o -path /lost+found -o -path /mnt -o -path /run \) -prune -false -o -name nodesource.[gk]* -print)"; # Deleting nodesource.list Will be recreated later. $SUDOX rm /etc/apt/sources.list.d/nodesource.lis*; else - echo "We are not updating your installation. Exiting."; + echo "We are not fixing your installation. Exiting."; + if [[ -f "/var/run/reboot-required" ]]; + then + echo ""; + echo "This system needs to be REBOOTED NOW!"; + echo ""; + fi; exit; fi; fi; @@ -281,13 +315,20 @@ if if [[ "$char" = "y" ]] || [[ "$char" = "Y" ]] then - echo "Trying to update your installation now. Please be patient." + echo "Trying to fix your installation now. Please be patient." # Finding nodesource.gpg or nodesource.key and deleting. Current key is pulled in later. $SUDOX rm "$($SUDOX find / \( -path /proc -o -path /dev -o -path /sys -o -path /lost+found -o -path /mnt \) -prune -false -o -name nodesource.[gk]* -print)"; # Deleting nodesource.list Will be recreated later. $SUDOX rm /etc/apt/sources.list.d/nodesource.lis*; else - echo "We are not updating your installation. Exiting."; + echo "We are not fixing your installation. Exiting."; + + if [[ -f "/var/run/reboot-required" ]]; + then + echo ""; + echo "This system needs to be REBOOTED NOW!"; + echo ""; + fi; exit; fi; @@ -319,12 +360,12 @@ fi; echo -e "\n*** Creating new /etc/apt/sources.list.d/nodesource.list and pinning source" echo ""; echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" | $SUDOX tee /etc/apt/sources.list.d/nodesource.list; - echo -e "Package: nodejs\nPin: origin deb.nodesource.com\nPin-Priority: 1001" | $SUDOX tee /etc/apt/preferences.d/nodejs.pref + echo -e "Package: nodejs\nPin: origin deb.nodesource.com\nPin-Priority: 1001" | sudo tee /etc/apt/preferences.d/nodejs.pref echo -e "\n*** These repos are active after the adjustments:" $SUDOX "$INSTALL_CMD" update; echo ""; - echo "Installing the recommended nodejs version now!"; + echo "Installing nodejs now!"; echo ""; if [ "$NODEINSTMAJOR" -gt "$NODE_MAJOR" ] && [[ "$NODERECOM" == [[:digit:]]*.[[:digit:]]*.[[:digit:]]* ]] then @@ -350,18 +391,24 @@ fi; fi; if [ "$SYSTDDVIRT" != "none" ]; then - echo "Installing the recommended nodejs version!"; + echo "Installing nodejs now!"; $SUDOX $INSTALL_CMD update -qq; $SUDOX $INSTALL_CMD -qq --allow-downgrades upgrade nodejs; echo -e "\n*** You need to manually restart your container/virtual machine now! *** "; - echo -e "\nWe tried our best to update or fix your nodejs. Please run 'iob diag' again to verify."; + echo -e "\nWe tried our best to fix your nodejs. Please run 'iob diag' again to verify."; unset LC_ALL; + if [[ -f "/var/run/reboot-required" ]]; + then + echo ""; + echo "This system needs to be REBOOTED NOW!"; + echo ""; + fi; exit; else - echo "Installing the recommended nodejs version!"; + echo "Installing the nodejs!"; $SUDOX $INSTALL_CMD update -qq; $SUDOX $INSTALL_CMD -qq --allow-downgrades upgrade nodejs; - echo -e "\nWe tried our best to update or fix your nodejs. Please run iob diag again to verify."; + echo -e "\nWe tried our best to fix your nodejs. Please run iob diag again to verify."; echo -e "\n*** RESTARTING ioBroker NOW! *** \n Please refresh or restart your browser in a few moments."; iob restart; fi;