diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2021-10-21 09:56:22 +0200 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2021-10-21 09:56:22 +0200 |
commit | 6b87d58cbd9501eb549fb3a164c61ac520b4e469 (patch) | |
tree | 619c5cdce096da290bf85b5f2c3152fa4755cdad | |
parent | 4aaf4a843f204252c7e91537151d25c175f7c20a (diff) | |
download | Luxcena-Neo-6b87d58cbd9501eb549fb3a164c61ac520b4e469.tar.gz Luxcena-Neo-6b87d58cbd9501eb549fb3a164c61ac520b4e469.zip |
:boom: Remove CLI, and add seperate install/uninstall scripts
-rwxr-xr-x | bin/install.sh | 170 | ||||
-rwxr-xr-x | bin/luxcena-neo-cli.sh | 278 | ||||
-rwxr-xr-x | bin/post-update.sh | 7 | ||||
-rw-r--r-- | bin/uninstall.sh | 150 |
4 files changed, 286 insertions, 319 deletions
diff --git a/bin/install.sh b/bin/install.sh index a7b811b..cabd6bf 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -1,9 +1,7 @@ #!/bin/bash -tput rev -printf '%s\n' "Luxcena-neo Installer" -tput sgr0 -printf '\e[93m%s\e[0m\n\n' "---------------------" +printf "\e[37mLuxcena-\e[31mn\e[32me\e[34mo\e[0m\n" +printf '\e[93m%s\e[0m' "-----------" if [ "$EUID" -ne 0 ]; then echo "You need to run this script as root." @@ -18,51 +16,155 @@ function die() { exit 1 } + +function TPUT() { + if [ -t 1 ]; then + if [ "$1" = "tput" ]; then + shift + fi + tput $@ + fi +} + +function header() { + TPUT setaf 3 + if [ -t 1 ]; then + printf "\n[ ] $1" + else + printf "\n- $1" + fi + TPUT sgr0 +} + +function commandError() { + trap - 1 + cat /tmp/luxcena-neo-update.log + + TPUT setaf 1 + printf "\n\nInstall failed.\n" + TPUT sgr0 + TPUT cnorm + exit 1 +} + +function spinner() { + i=1 + sp="/-\|" + while ps a | awk '{print $1}' | grep -q "$1"; do + TPUT cub $(tput cols) + TPUT cuf 1 + printf "${sp:i++%${#sp}:1}" + TPUT cuf $(tput cols) + sleep 0.09 + done + + TPUT cub $(tput cols) + TPUT cuf 1 +} + +function execCommand() { + TPUT sc + TPUT setaf 4 + if [ -t 1 ]; then + printf " ($1)" + else + printf "\n>> $1 " + fi + TPUT sgr0 + bash -c "$1 > /tmp/luxcena-neo-update.log 2>&1" & + + PID=$! + + if [ -t 1 ]; then + spinner $PID + fi + + wait $PID + commandSucc=$? + if [ $commandSucc -eq 0 ]; then + TPUT setaf 2 + printf "โ" + TPUT sgr0 + TPUT rc + TPUT el + else + TPUT setaf 1 + printf "x" + TPUT sgr0 + TPUT cuf $(tput cols) + printf "\n" + if [ $# -eq 1 ] || [ $2 -eq "0" ]; then + commandError + fi + fi +} + +TPUT civis + # Create user 'luxcena-neo' -tput setaf 8 -printf '%s\n' "- Creating user 'lux-neo'..." -tput sgr0 +header "Creating user 'lux-neo'" username="lux-neo" egrep "^$username" /etc/passwd >/dev/null if [ $? -eq 0 ]; then - echo "User already exists, continuing..." + printf '\n%s\n' "User already exists, continuing..." else - useradd -m $username || die + execCommand "useradd -m $username" fi -usermod -a -G gpio $username -usermod -a -G spi $username +header "Add user to groups" +execCommand "usermod -a -G gpio $username" +execCommand "usermod -a -G spi $username" # First we make our directories -tput setaf 8 -printf '%s\n' "- Making directories..." -tput sgr0 -[ -d "/opt/luxcena-neo/" ] && echo "Seems like luxcena-neo is already installed, please do update instead" && die -mkdir -p "/opt/luxcena-neo" || die -chown $username:$username "/opt/luxcena-neo" || die -mkdir -p "/var/luxcena-neo" || die -chown $username:$username "/var/luxcena-neo" || die -mkdir -p "/etc/luxcena-neo" || die -chown $username:$username "/etc/luxcena-neo" || die -mkdir -p "/var/log/luxcena-neo" || die -chown $username:$username "/var/log/luxcena-neo" || die - -printf '%s' "Which branch do you want to install (default: master)? " +header "Making directories" +[ -d "/opt/luxcena-neo/" ] && echo "\nSeems like luxcena-neo is already installed, please do update instead" && die +execCommand "mkdir -p \"/opt/luxcena-neo\"" +execCommand "chown $username:$username \"/opt/luxcena-neo\"" +execCommand "mkdir -p \"/var/luxcena-neo\"" +execCommand "chown $username:$username \"/var/luxcena-neo\"" +execCommand "mkdir -p \"/etc/luxcena-neo\"" +execCommand "chown $username:$username \"/etc/luxcena-neo\"" +execCommand "mkdir -p \"/var/log/luxcena-neo\"" +execCommand "chown $username:$username \"/var/log/luxcena-neo\"" + +# Choose branch to install +printf '\n%s' "Which branch do you want to install (default: master)? " read BRANCH if [ -z "$BRANCH" ]; then BRANCH="master" fi # Get source code -tput setaf 8 -printf '%s\n' "- Fetch source code..." -tput sgr0 -runuser -l $username -c "git clone -b $BRANCH https://github.com/jakobst1n/luxcena-neo /opt/luxcena-neo/" || die +header "Fetch source code" +execCommand "runuser -l $username -c \"git clone -b $BRANCH https://github.com/jakobst1n/luxcena-neo /opt/luxcena-neo/\"" +execCommand "chown -R lux-neo:lux-neo /opt/luxcena-neo" + +# Install dependencies +header "Install dependencies" +execCommand "wget -qO- https://deb.nodesource.com/setup_14.x | bash -" +execCommand "apt -q update" +execCommand "apt -qy install nodejs python3-pip" +execCommand "pip3 install virtualenv" +execCommand "runuser -l 'lux-neo' -c \"export NODE_ENV=development; npm --prefix /opt/luxcena-neo install /opt/luxcena-neo\"" + +# Create virtualenv +header "Create python virtualenv and install dependencies" +execCommand "rm -rf /opt/luxcena-neo/NeoRuntime/Runtime/venv" +execCommand "virtualenv -p /usr/bin/python3 /opt/luxcena-neo/NeoRuntime/Runtime/venv" +execCommand "source /opt/luxcena-neo/NeoRuntime/Runtime/venv/bin/activate && pip install rpi_ws281x" + +# Build the source code +header "Build source code" +execCommand "runuser -l 'lux-neo' -c \"npm --prefix /opt/luxcena-neo run build:frontend\"" +execCommand "runuser -l 'lux-neo' -c \"npm --prefix /opt/luxcena-neo run build:fontawesome\"" +execCommand "runuser -l 'lux-neo' -c \"npm --prefix /opt/luxcena-neo run build:dialog-polyfill\"" -# Install all packages, build the app, and prepare everything -tput setaf 8 -printf '%s\n' "- Running installer (updater) from newly fetched source code..." -tput sgr0 -/opt/luxcena-neo/bin/luxcena-neo-cli.sh update || die +# Install systemd service +header "Install new systemd service" +execCommand "cp /opt/luxcena-neo/bin/luxcena-neo.service /etc/systemd/system/luxcena-neo.service" +execCommand "systemctl daemon-reload" +execCommand "systemctl enable luxcena-neo" +execCommand "systemctl start luxcena-neo" # Installation is done! printf '\n\e[5m%s\e[0m\n' "๐Luxcena-Neo is now installed๐" +TPUT cnorm diff --git a/bin/luxcena-neo-cli.sh b/bin/luxcena-neo-cli.sh deleted file mode 100755 index 7346b3a..0000000 --- a/bin/luxcena-neo-cli.sh +++ /dev/null @@ -1,278 +0,0 @@ -#!/bin/bash - -usage() { - printf "Usage: $0 update/uninstall/start/stop\n" 1>&2 - exit 1 -} - -function TPUT() { - if [ -t 1 ]; then - shift - tput $@ - fi -} - -function startLuxcenaNeo() { - header "Start Luxcena NEO" - execCommand "systemctl start luxcena-neo" 1 -} - -function header() { - TPUT setaf 3 - if [ -t 1 ]; then - printf "\n[ ] $1" - else - printf "\n- $1" - fi - TPUT sgr0 -} - -function commandError() { - trap - 1 - cat /tmp/luxcena-neo-update.log - - TPUT setaf 1 - printf "\n\nInstall failed.\n" - TPUT sgr0 - - startLuxcenaNeo - exit 1 -} - -function spinner() { - i=1 - sp="/-\|" - while ps a | awk '{print $1}' | grep -q "$1"; do - TPUT cub $(TPUT cols) - TPUT cuf 1 - printf "${sp:i++%${#sp}:1}" - TPUT cuf $(TPUT cols) - sleep 0.09 - done - - TPUT cub $(TPUT cols) - TPUT cuf 1 -} - -function execCommand() { - TPUT sc - TPUT setaf 4 - if [ -t 1 ]; then - printf " ($1)" - else - printf "\n>> $1 " - fi - TPUT sgr0 - bash -c "$1 > /tmp/luxcena-neo-update.log 2>&1" & - - PID=$! - - if [ -t 1 ]; then - spinner $PID - fi - - wait $PID - commandSucc=$? - if [ $commandSucc -eq 0 ]; then - TPUT setaf 2 - printf "โ" - TPUT sgr0 - TPUT rc - TPUT el - else - TPUT setaf 1 - printf "x" - TPUT sgr0 - TPUT cuf $(TPUT cols) - printf "\n" - if [ $# -eq 1 ] || [ $2 -eq "0" ]; then - commandError - fi - fi -} - -function dlgYN() { - TPUT sc - TPUT setaf 4 - printf "$1 (y/n)? " - while : - do - read -n 1 -p "" YNQuestionAnswer - if [[ $YNQuestionAnswer == "y" ]]; then - TPUT rc; TPUT el - printf ". $1?: \e[0;32mYes\e[0m\n" - TPUT sc - eval $2=1 # Set parameter 2 of input to the return value - break - elif [[ $YNQuestionAnswer == "n" ]]; then - TPUT rc; TPUT el - printf ". $1?: \e[0;31mNo\e[0m\n" - eval $2=0 # Set parameter 2 of input to the return value - break - fi - done -} - -while getopts ":a:" o; do - case "${o}" in - a ) - p=${OPTARG} - ;; - *) - usage - ;; - esac -done -shift $((OPTIND-1)) - -printf "\e[37mLuxcena-\e[31mn\e[32me\e[34mo\e[37m-cli \e[90m[args: '$*']\n\n\e[0m" - -action=$1 -if [ "$action" == "update" ]; then - PATH=/bin:$PATH - PATH=/sbin:$PATH - PATH=/usr/local/sbin:$PATH - PATH=/usr/local/bin:$PATH - PATH=/usr/sbin:$PATH - PATH=/usr/bin:$PATH - PATH=/usr/sbin:$PATH - - TPUT rev - printf '%s\n' "Luxcena-neo Updater" - TPUT sgr0 - printf '\e[93m%s\e[0m\n' "-------------------" - - if [ "$EUID" -ne 0 ]; then - echo "You need to run this script as root." - echo "Try running with 'sudo ./bin/install.sh'" - exit 1 - fi - - trap commandError 1 - - WDIR="/opt/luxcena-neo" - - header "Download update" - UPDATEDIR=$(mktemp -d -p /tmp luxcena-neo-update.XXXXX) - repoUrl=$(git -C $WDIR remote get-url origin) - repoBranch=$(git -C $WDIR rev-parse --abbrev-ref HEAD) - execCommand "git clone -b $repoBranch $repoUrl $UPDATEDIR" - - header "Create backup" - execCommand "mkdir -p /var/luxcena-neo/backup" - BACKUPDIR=$(mktemp -d -p /var/luxcena-neo/backup backup.XXXXXX) - execCommand "cp -R /opt/luxcena-neo/ $BACKUPDIR" - - header "Stop the running luxcena-neo" - execCommand "systemctl stop luxcena-neo" - - header "Install update" - execCommand "cp -Rf $UPDATEDIR/* $WDIR" - execCommand "chown -R lux-neo:lux-neo $WDIR" - - header "Install dependencies" - execCommand "wget -qO- https://deb.nodesource.com/setup_14.x | bash -" - execCommand "apt -qy install nodejs python3-pip " - execCommand "pip3 install virtualenv" - execCommand "runuser -l 'lux-neo' -c \"export NODE_ENV=development; npm --prefix $WDIR install $WDIR\"" - - header "Create python virtualenv and install dependencies" - execCommand "rm -rf $WDIR/NeoRuntime/Runtime/venv" - execCommand "virtualenv -p /usr/bin/python3 $WDIR/NeoRuntime/Runtime/venv" - execCommand "source $WDIR/NeoRuntime/Runtime/venv/bin/activate && pip install rpi_ws281x" - - header "Build source code" - execCommand "runuser -l 'lux-neo' -c \"npm --prefix \"$WDIR\" run build:frontend\"" - execCommand "runuser -l 'lux-neo' -c \"npm --prefix \"$WDIR\" run build:fontawesome\"" - execCommand "runuser -l 'lux-neo' -c \"npm --prefix \"$WDIR\" run build:dialog-polyfill\"" - - header "Install new CLI script" - execCommand "cp /opt/luxcena-neo/bin/luxcena-neo-cli.sh /usr/bin/luxcena-neo-cli.sh" - - header "Install new systemd service" - execCommand "cp /opt/luxcena-neo/bin/luxcena-neo.service /etc/systemd/system/luxcena-neo.service" - execCommand "systemctl daemon-reload" - execCommand "systemctl enable luxcena-neo" - execCommand "systemctl start luxcena-neo" - - printf "\nUpdate complete.\n" - exit 0 - -elif [ "$action" == "uninstall" ]; then - TPUT setab 1 - printf '%s\n' "Luxcena Neo Uninstaller..." - TPUT sgr0 - printf '\e[93m%s\e[0m' "--------------------------" - TPUT setaf 8 - printf "By uninstalling Luxcena-Neo you might loose all data, including your scripts.\n\n" - - dlgYN "Are you sure you want to uninstall?" res - if [ $res -eq 1 ]; then - header "Remove systemd service" - execCommand "systemctl stop luxcena-neo" - - header "Delete lux-neo user" - execCommand "deluser lux-neo" - - header "Uninstall luxcena-neo" - execCommand "rm -rf /opt/luxcena-neo" - execCommand "rm -f /etc/systemd/system/luxcena-neo.service" - execCommand "rm -f /usr/bin/luxcena-neo.sh" - execCommand "rm -f /usr/bin/lux-neo" - - TPUT setaf 2 - printf "\nEverything should now be gone.\n" - printf "/etc/luxcena-neo and /var/log/luxcena-neo is not removed.\n" - TPUT sgr0 - TPUT setaf 8 - printf "Well, some dependencies still exists. Those are:\n" - printf " - packages (nodejs python3 python3-pip)\n" - TPUT sgr0 - fi - -elif [ "$action" == "start" ]; then - header "Start luxcena-neo" - execCommand "systemctl start luxcena-neo" - -elif [ "$action" == "stop" ]; then - header "Stop luxcena-neo" - execCommand "systemctl stop luxcena-neo" - -elif [ "$action" == "status" ]; then - printf "โญโโโโโโโโโโโโโโโโโโโโโโฎ\n" - printf "โ Service active: " - [[ "$(systemctl is-active luxcena-neo)" == *"active"* ]] && printf '\e[32m%s\e[0m โ\n' "yes" || printf '\e[31m%s\e[0m โ\n' "no" - printf "โ Starts on boot: " - [[ "$(systemctl is-enabled luxcena-neo)" == *"enabled"* ]] && printf '\e[32m%s\e[0m โ\n' "yes" || printf '\e[31m%s\e[0m โ\n' "no" - printf "โ Has failed: " - [[ "$(systemctl is-failed luxcena-neo)" == *"failed"* ]] && printf '\e[32m%s\e[0m โ\n' "yes" || printf '\e[31m%s\e[0m โ\n' "no" - printf "โฐโโโโโโโโโโโโโโโโโโโโโโฏ\n\n" - - printf '\e[93m%s\e[0m\n' "โโโService statusโโโโโโโโโโโโโโโโโโ" - systemctl status luxcena-neo - printf '\e[93m%s\e[0m\n' "โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ" - -elif [ "$action" == "log" ]; then - if [ "$2" == "service" ]; then - printf '\e[93m%s\e[0m\n' "โโโService log (press ctrl+c to exit)โโโโโโโโโโโโโโโโโโ" - tail -F -n 20 /var/log/luxcena-neo/service.log - fi - if [ "$2" == "app" ]; then - printf '\e[93m%s\e[0m\n' "โโโApp log (press ctrl+c to exit)โโโโโโโโโโโโโโโโโโ" - tail -F -n 20 /var/log/luxcena-neo/logger.log - fi - -elif [ "$action" == "version" ] || [ "$action" == "v" ]; then - printf "โญโโโโโโโโโโโโโโโโโโโโโโฎ\n" - printf "โ Version: Unknown โ\n" - printf "โ branch : $(git -C /opt/luxcena-neo branch | grep \* | cut -d ' ' -f2) โ\n" - printf "โฐโโโโโโโโโโโโโโโโโโโโโโฏ\n\n" - -elif [ "$action" == "selectBranch" ]; then - printf "Current $(git -C /opt/luxcena-neo branch | grep \* | cut -d ' ' -f2)Branch \n" - runuser -l 'lux-neo' -c "git -C /opt/luxcena-neo stash" - runuser -l 'lux-neo' -c "git -C /opt/luxcena-neo checkout $2" || printf "\e[91mYou should now run \e[90m'sudo lux-neo update'\e[91m!\n" - -else - usage -fi diff --git a/bin/post-update.sh b/bin/post-update.sh deleted file mode 100755 index 01b0fee..0000000 --- a/bin/post-update.sh +++ /dev/null @@ -1,7 +0,0 @@ -oldDir=$PWD -cd /home/lux-neo/src - -cp bin/luxcena-neo-cli.sh /usr/bin/luxcena-neo-cli.sh - -cd $oldDir -echo "Post-update done..." diff --git a/bin/uninstall.sh b/bin/uninstall.sh new file mode 100644 index 0000000..a9014f6 --- /dev/null +++ b/bin/uninstall.sh @@ -0,0 +1,150 @@ +#!/bin/bash + +printf "\e[37mLuxcena-\e[31mn\e[32me\e[34mo\e[0m\n" +printf '\e[93m%s\e[0m' "-----------" + +if [ "$EUID" -ne 0 ]; then + echo "You need to run this script as root." + echo "Try running with 'sudo ./bin/install.sh'" + exit 1 +fi + +function die() { + tput setaf 1 + printf "\n\nInstall failed, successfull steps not reversed.\n" + tput sgr0 + exit 1 +} + + +function TPUT() { + if [ -t 1 ]; then + if [ "$1" = "tput" ]; then + shift + fi + tput $@ + fi +} + +function header() { + TPUT setaf 3 + if [ -t 1 ]; then + printf "\n[ ] $1" + else + printf "\n- $1" + fi + TPUT sgr0 +} + +function commandError() { + trap - 1 + cat /tmp/luxcena-neo-update.log + + TPUT setaf 1 + printf "\n\nInstall failed.\n" + TPUT sgr0 + TPUT cnorm + exit 1 +} + +function spinner() { + i=1 + sp="/-\|" + while ps a | awk '{print $1}' | grep -q "$1"; do + TPUT cub $(tput cols) + TPUT cuf 1 + printf "${sp:i++%${#sp}:1}" + TPUT cuf $(tput cols) + sleep 0.09 + done + + TPUT cub $(tput cols) + TPUT cuf 1 +} + +function execCommand() { + TPUT sc + TPUT setaf 4 + if [ -t 1 ]; then + printf " ($1)" + else + printf "\n>> $1 " + fi + TPUT sgr0 + bash -c "$1 > /tmp/luxcena-neo-update.log 2>&1" & + + PID=$! + + if [ -t 1 ]; then + spinner $PID + fi + + wait $PID + commandSucc=$? + if [ $commandSucc -eq 0 ]; then + TPUT setaf 2 + printf "โ" + TPUT sgr0 + TPUT rc + TPUT el + else + TPUT setaf 1 + printf "x" + TPUT sgr0 + TPUT cuf $(tput cols) + printf "\n" + if [ $# -eq 1 ] || [ $2 -eq "0" ]; then + commandError + fi + fi +} + +function dlgYN() { + TPUT sc + TPUT setaf 4 + printf "$1 (y/n)? " + while : + do + read -n 1 -p "" YNQuestionAnswer + if [[ $YNQuestionAnswer == "y" ]]; then + TPUT rc; TPUT el + printf ". $1?: \e[0;32mYes\e[0m\n" + TPUT sc + eval $2=1 # Set parameter 2 of input to the return value + break + elif [[ $YNQuestionAnswer == "n" ]]; then + TPUT rc; TPUT el + printf ". $1?: \e[0;31mNo\e[0m\n" + eval $2=0 # Set parameter 2 of input to the return value + break + fi + done +} + +TPUT civis +TPUT setaf 8 +printf "By uninstalling Luxcena-Neo you might loose all data, including your scripts.\n\n" + +dlgYN "Are you sure you want to uninstall?" res +if [ $res -eq 1 ]; then + header "Remove systemd service" + execCommand "systemctl stop luxcena-neo" + + header "Delete lux-neo user" + execCommand "deluser lux-neo" + + header "Uninstall luxcena-neo" + execCommand "rm -rf /opt/luxcena-neo" + execCommand "rm -f /etc/systemd/system/luxcena-neo.service" + execCommand "rm -f /usr/bin/luxcena-neo.sh" + execCommand "rm -f /usr/bin/lux-neo" + + TPUT setaf 2 + printf "\nEverything should now be gone.\n" + printf "/etc/luxcena-neo and /var/log/luxcena-neo is not removed.\n" + TPUT sgr0 + TPUT setaf 8 + printf "Well, some dependencies still exists. Those are:\n" + printf " - packages (nodejs python3 python3-pip)\n" + TPUT sgr0 +fi |