Skip to content

Commit

Permalink
Update node-update.sh (#525)
Browse files Browse the repository at this point in the history
* Update node-update.sh

* Update node-update.sh

fix names. yes filename !== command name

* Update node-update.sh

---------

Co-authored-by: Ingo Fischer <github@fischer-ka.de>
  • Loading branch information
Grothesk242 and Apollon77 committed May 24, 2024
1 parent 4731d27 commit d53a930
Showing 1 changed file with 81 additions and 34 deletions.
115 changes: 81 additions & 34 deletions node-update.sh
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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" ]];
Expand All @@ -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;
Expand All @@ -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' ;-)
# ------------------------------


Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -240,17 +261,24 @@ 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;
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:]]* ]];
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit d53a930

Please sign in to comment.