Skip to content

Commit

Permalink
update to install as a different user for RPM
Browse files Browse the repository at this point in the history
- [x] install for another user by setting environment variable e.g. `NODERED_USER=test`
- [x] suggest user `nodered` during install when environment variable `NODERED_USER` is not set.
- [x] comment low memory NODE OPTIONS
- [x] correct environmentFile, to /etc/node-red/environment
- [x] add some color to differentiate users
- [x] can be started as root or normal user, will prompt for password for sudo
  • Loading branch information
dgoo2308 committed Feb 17, 2022
1 parent c3bf6b3 commit 3be6ae0
Showing 1 changed file with 89 additions and 43 deletions.
132 changes: 89 additions & 43 deletions rpm/update-nodejs-and-nodered
Expand Up @@ -16,23 +16,55 @@

# Node-RED Installer for RPM based systems

unset NODERED_HOME
umask 0022
echo -ne "\033[2 q"
echo -en "\n **************************************\n"
echo -en " *** \e[1;91mNode-RED\e[0;97m \e[1;97mRPM install Script\e[0m ***\n"
echo -en " **************************************\n"

if [ "$EUID" == "0" ]
then echo -en "\nRoot user detected. Typically install as a normal user. No need for sudo.\r\n\r\n"
read -p "Are you really sure you want to install as root ? (y/N) ? " yn
if [ -n "$NODERED_USER" ]
then
echo -en "\nThe variable \e[1;33m'\$NODERED_USER'\e[0m is set as \e[1;32m'$NODERED_USER'\e[0m\r\n\r\n"
read -p $'\e[0;97mAre you really sure you want to install as the target user \e[1;32m'"'$NODERED_USER'"$'\e[0;97m? (y/N) ?\e[0m' yn
case $yn in
[Yy]* )
;;
* )
exit
;;
esac
fi

if [ -z "$NODERED_USER" ]
then echo -en "\e[0;97m\nThe \"\e[1;33m\$NODERED_USER\e[0;97m\" variable is not set and this script is run as user '\e[1;33m$USER\e[0;97m'.\r\n\r\n"
read -p $'\e[0;97mWould you like to change to the recommended the target user to \e[1;92m\'nodered\'\e[0;97m? (\e[1;32my\e[0m/N) ' yn
case $yn in
[Yy]* )
export NODERED_USER=nodered
;;
* )
;;
esac
fi

if [ "$EUID" == "0" ] && [ -z "$NODERED_USER" ]
then echo -en "\nThe \e[1;91mroot\e[0m user is detected as the target user for node-red install.\r\n\r\n"
read -p $'\e[0;97mAre you really sure you want to install as the target user \e[1;91mroot\e[0;97m ? (y/N) ?\e[0m' yn
case $yn in
[Yy]* )
[[ -n "$NODERED_HOME" ]] || export NODERED_HOME=/root;
;;
* )
exit
;;
esac
fi

[[ -n "$NODERED_USER" ]] || export NODERED_USER=$USER;

OPENFW="NO"
read -r -t 15 -p "Would you like to add Node-RED port 1880 to the firewall public zone ? [y/N] ? " response
read -r -t 15 -p $'\e[0;97mWould you like to add Node-RED port 1880 to the firewall public zone ? [y/N] ? ' response
if [[ "$response" =~ ^([yY])+$ ]]; then
OPENFW="YES"
fi
Expand All @@ -41,28 +73,26 @@ if curl -f https://www.npmjs.com/package/node-red >/dev/null 2>&1; then
host=`hostname`
echo -e '\033]2;'$host : Node-RED update'\007'
echo " "
echo "This script will do an install of node.js, Node-RED and the service packages to auto-run Node-RED"
echo -e "This script will do an install of node.js, Node-RED "
echo -e " and the service packages to auto-run Node-RED to run as user \e[1;33m'$NODERED_USER'\e[0m"
echo " "

read -p "Are you really sure you want to do this ? [y/N] ? " yn
read -p $'\e[0;97mAre you really sure you want to do this ? [y/N] ?\e[0m' yn
case $yn in
[Yy]* )
echo ""

# this script assumes that $HOME is the folder of the user that runs node-red
# that $USER is the user name and the group name to use when running is the
# primary group of that user
# if this is not correct then edit the lines below
NODERED_HOME=$HOME
NODERED_USER=$USER
NODERED_GROUP=`id -gn`

sudo ()
{
[[ $EUID = 0 ]] || set -- command sudo "$@"
"$@"
}

echo ""
# this script assumes that $HOME is the folder of the user that runs node-red
# that $USER is the user name and the group name to use when running is the
# primary group of that user
# if this is not correct then edit the lines below
[[ -n "$NODERED_USER" ]] || export NODERED_USER=$USER;
[[ -n "$NODERED_HOME" ]] || export NODERED_HOME=/home/$NODERED_USER;
sudo useradd $NODERED_USER
NODERED_GROUP=`id -gn $NODERED_USER`
MYOS=$(cat /etc/*release | grep "^ID=" | cut -d = -f 2)
versions='fedora"centos"rhel"ol"almalinux"rocky"miraclelinux"'
if [[ $versions != *"$MYOS"* ]]; then
Expand All @@ -74,9 +104,9 @@ case $yn in
GLOBAL="true"
TICK='\033[1;32m\u2714\033[0m'
CROSS='\033[1;31m\u2718\033[0m'
cd "$NODERED_HOME" || exit 1
sudo cd "$NODERED_HOME" || exit 1
clear
echo -e '\nRunning nodejs and Node-RED install for user '$USER' at '$HOME' on '$MYOS'\n'
echo -e "\n\e[0;97mRunning nodejs and Node-RED install for user '\e[1;33m$NODERED_USER\e[0;97m' at '$NODERED_HOME' on $MYOS\e[0m\n"
time1=$(date)
echo "" | sudo tee -a /var/log/nodered-install.log >>/dev/null
echo "***************************************" | sudo tee -a /var/log/nodered-install.log >>/dev/null
Expand All @@ -98,7 +128,7 @@ case $yn in
echo -ne " Stop Node-RED $CHAR\r\n"

# ensure ~/.config dir is owned by the user
sudo chown -Rf $NODERED_USER:$NODERED_GROUP $NODERED_HOME/.config/
sudo chown -Rf $NODERED_USER:$NODERED_GROUP $NODERED_HOME/.config
echo "Now install nodejs" | sudo tee -a /var/log/nodered-install.log >>/dev/null
if [ $MYOS = "fedora" ] || [ $MYOS = "almalinux" ] || [ $MYOS = "rocky" ] || [ $MYOS = "miraclelinux" ]; then
sudo dnf module reset -y nodejs 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null
Expand Down Expand Up @@ -129,20 +159,18 @@ case $yn in

# and install Node-RED
echo "Now install Node-RED" | sudo tee -a /var/log/nodered-install.log >>/dev/null
if [[ $GLOBAL == "true" ]]; then
if sudo npm i -g --unsafe-perm --no-progress node-red@latest 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null; then CHAR=$TICK; else CHAR=$CROSS; fi
else
if npm i -g --unsafe-perm --no-progress node-red@latest 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null; then CHAR=$TICK; else CHAR=$CROSS; fi
fi
if sudo npm i -g --unsafe-perm --no-progress node-red@latest 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null; then CHAR=$TICK; else CHAR=$CROSS; fi
nrv=$(npm --no-progress -g ls node-red | grep node-red | cut -d '@' -f 2 | sudo tee -a /var/log/nodered-install.log) >>/dev/null 2>&1
echo -ne " Install Node-RED core $CHAR $nrv\r\n"

echo "Now create basic package.json for the user" | sudo tee -a /var/log/nodered-install.log >>/dev/null
mkdir -p "$NODERED_HOME/.node-red/node_modules"
sudo chown -Rf $NODERED_USER:$NODERED_GROUP $NODERED_HOME/.node-red/ 2>&1 >>/dev/null
pushd "$NODERED_HOME/.node-red" 2>&1 >>/dev/null
npm config set update-notifier false 2>&1 >>/dev/null
if [ ! -f "package.json" ]; then
sudo lastlog -u $NODERED_USER -C >>/dev/null 2>&1
sudo su - $NODERED_USER <<'EOF'
cd
mkdir -p ".node-red/node_modules"
cd .node-red
npm config set update-notifier false #2>&1 >>/dev/null
if [ ! -f "package.json" ]; then
echo '{' > package.json
echo ' "name": "node-red-project",' >> package.json
echo ' "description": "initially created for you by Node-RED '$nrv'",' >> package.json
Expand All @@ -151,12 +179,11 @@ case $yn in
echo ' }' >> package.json
echo '}' >> package.json
fi
popd 2>&1 >>/dev/null
sudo chown -Rf $NODERED_USER:$NODERED_GROUP $NODERED_HOME/.npm 2>&1 >>/dev/null

EOF
echo "Now add start/stop/reload/log scripts" | sudo tee -a /var/log/nodered-install.log >>/dev/null
sudo mkdir -p /usr/bin
if curl -f https://raw.githubusercontent.com/node-red/linux-installers/master/resources/node-red-icon.svg >/dev/null 2>&1; then
if curl -f https://raw.githubusercontent.com/node-red/linux-installers/master/resources/node-red-icon.svg >/dev/null 2>&1
then
sudo curl -sL -o /usr/bin/node-red-start https://raw.githubusercontent.com/node-red/linux-installers/master/resources/node-red-start.rpm 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null
sudo curl -sL -o /usr/bin/node-red-stop https://raw.githubusercontent.com/node-red/linux-installers/master/resources/node-red-stop 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null
sudo curl -sL -o /usr/bin/node-red-restart https://raw.githubusercontent.com/node-red/linux-installers/master/resources/node-red-restart 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null
Expand All @@ -180,11 +207,14 @@ case $yn in
SYSTEMDFILE="/etc/systemd/system/nodered.service"

if sudo curl -sL -o ${SYSTEMDFILE}.temp https://raw.githubusercontent.com/node-red/linux-installers/master/resources/nodered.service 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null; then CHAR=$TICK; else CHAR=$CROSS; fi
# set the User Group and WorkingDirectory in nodered.service
sudo sed -i 's#^User=pi#User='$NODERED_USER'#;s#^Group=pi#Group='$NODERED_GROUP'#;s#^WorkingDirectory=/home/pi#WorkingDirectory='$NODERED_HOME'#;' ${SYSTEMDFILE}.temp
# set the User,Group,EnvironmentFile and WorkingDirectory in nodered.service
sudo sed -i 's#^User=pi#User='$NODERED_USER'#;
s#^Group=pi#Group='$NODERED_GROUP'#;
s#^WorkingDirectory=/home/pi#WorkingDirectory='$NODERED_HOME'#;
s#^EnvironmentFile=-/home/pi/.node-red#EnvironmentFile=-/etc/node-red#;
s!^Environment="NODE_OPTIONS=--max_old_space_size=512"!#Environment="NODE_OPTIONS=--max_old_space_size=512"!' ${SYSTEMDFILE}.temp

if test -f "$SYSTEMDFILE"; then

# there's already a systemd script
EXISTING_FILE=$(md5sum $SYSTEMDFILE | awk '$1 "${SYSTEMDFILE}" {print $1}');
TEMP_FILE=$(md5sum ${SYSTEMDFILE}.temp | awk '$1 "${SYSTEMDFILE}.temp" {print $1}');
Expand All @@ -201,7 +231,27 @@ case $yn in
else
sudo mv ${SYSTEMDFILE}.temp $SYSTEMDFILE
fi
# make environment file for systemd
sudo mkdir -p /etc/node-red
if test ! -f /etc/node-red/environment; then
echo "# Node-RED EnvironmentFile for Systemd Service" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "# after edit this file `sudo systemctl restart nodered` to reload Node-red with the new options" | sudo tee -a /etc/node-red/environment >>/dev/null

echo "" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "# uncomment and edit if running on low memory resource hardware" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "#NODE_OPTIONS=--max_old_space_size=512" | sudo tee -a /etc/node-red/environment >>/dev/null

echo "" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "# uncomment next line and edit if you need an http proxy" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "#HTTP_PROXY=my.httpproxy.server.address" | sudo tee -a /etc/node-red/environment >>/dev/null

echo "" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "# uncomment the next line for a more verbose log output" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "#NODE_RED_OPTIONS=-v" | sudo tee -a /etc/node-red/environment >>/dev/null
echo "" | sudo tee -a /etc/node-red/environment >>/dev/null

echo "Created /etc/node-red/environment for systed service." | sudo tee -a /var/log/nodered-install.log >>/dev/null
fi
sudo systemctl daemon-reload 2>&1 | sudo tee -a /var/log/nodered-install.log >>/dev/null
echo -ne " Update systemd script $CHAR\r\n"

Expand All @@ -219,11 +269,7 @@ case $yn in
fi
echo -ne "\r\n\r\n\r\n"
echo -ne "All done.\r\n"
if [[ $GLOBAL == "true" ]]; then
echo -ne " You can now start Node-RED with the command \033[0;36mnode-red-start\033[0m\r\n"
else
echo -ne " You can now start Node-RED with the command \033[0;36m./node-red\033[0m\r\n"
fi
echo -ne " You can now start Node-RED with the command \033[0;36mnode-red-start\033[0m\r\n"
echo -ne " Then point your browser to \033[0;36mlocalhost:1880\033[0m or \033[0;36mhttp://{your_ip-address}:1880\033[0m\r\n"
echo -ne "\r\nStarted $time1 - Finished $(date)\r\n\r\n"
echo "Finished : "$time1 | sudo tee -a /var/log/nodered-install.log >>/dev/null
Expand Down

0 comments on commit 3be6ae0

Please sign in to comment.