diff options
author | jakob.stendahl <jakob.stendahl@infomedia.dk> | 2022-12-17 21:31:41 +0100 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2022-12-17 21:31:41 +0100 |
commit | 1e588718a855ae2871a8841f6c6e621f49795454 (patch) | |
tree | 6599b3959554b307a571a73373114cb2d34a98ef /bin | |
parent | 6c37c28d7044a813fcde9ef80bf8852529b8305f (diff) | |
download | Luxcena-Neo-1e588718a855ae2871a8841f6c6e621f49795454.tar.gz Luxcena-Neo-1e588718a855ae2871a8841f6c6e621f49795454.zip |
Start moving to esm, work on updater
Diffstat (limited to 'bin')
-rw-r--r-- | bin/bashfuncs.sh | 96 | ||||
-rw-r--r-- | bin/build.sh | 12 | ||||
-rwxr-xr-x | bin/install.sh | 77 | ||||
-rw-r--r-- | bin/luxcena-neo.service | 4 | ||||
-rwxr-xr-x | bin/luxcena-neo.sh | 32 | ||||
-rw-r--r-- | bin/postinstall.sh | 13 | ||||
-rwxr-xr-x | bin/uninstall.sh | 7 |
7 files changed, 174 insertions, 67 deletions
diff --git a/bin/bashfuncs.sh b/bin/bashfuncs.sh new file mode 100644 index 0000000..a1559a2 --- /dev/null +++ b/bin/bashfuncs.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +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 $1 + rm $1 + + 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 + log=$(mktemp) + bash -c "$1 > $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 $log + fi + fi + rm $log +} + +TPUT civis diff --git a/bin/build.sh b/bin/build.sh deleted file mode 100644 index 6c10225..0000000 --- a/bin/build.sh +++ /dev/null @@ -1,12 +0,0 @@ -echo "> Install node modules needed for build" -NODE_ENV="development" -npm i - -echo "> Make sure all python dependencies for build is installed" -pip3 install mkdocs mkdocs-gitbook pygments pymdown-extensions - -echo "> Compile es6 and sass to bundles" -npx webpack -p - -echo "> Generate html docs using sphinx" -mkdocs build diff --git a/bin/install.sh b/bin/install.sh index d27c324..e9bd1aa 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -4,8 +4,8 @@ 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'" + echo "\nYou need to run this script as root." + echo "Try running with 'sudo $0/install.sh'" exit 1 fi @@ -38,7 +38,8 @@ function header() { function commandError() { trap - 1 - cat /tmp/luxcena-neo-update.log + cat $1 + rm $1 TPUT setaf 1 printf "\n\nInstall failed.\n" @@ -71,7 +72,8 @@ function execCommand() { printf "\n>> $1 " fi TPUT sgr0 - bash -c "$1 > /tmp/luxcena-neo-update.log 2>&1" & + log=$(mktemp) + bash -c "$1 > $log 2>&1" & PID=$! @@ -94,9 +96,10 @@ function execCommand() { TPUT cuf $(tput cols) printf "\n" if [ $# -eq 1 ] || [ $2 -eq "0" ]; then - commandError + commandError $1 fi fi + rm $log } TPUT civis @@ -116,9 +119,6 @@ execCommand "usermod -a -G spi $username" # First we make our directories 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\"" @@ -126,56 +126,37 @@ 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 -TPUT cnorm -printf '\n%s' "Which branch do you want to install (default: master)? " -read BRANCH -if [ -z "$BRANCH" ]; then - BRANCH="master" -fi -TPUT civis - -# Get source code -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" if [ "$(uname -m)" = "armv6l" ]; then - execCommand "wget https://unofficial-builds.nodejs.org/download/release/v14.10.0/node-v14.10.0-linux-armv6l.tar.gz" - execCommand "tar -xzf node-v14.10.0-linux-armv6l.tar.gz" - execCommand "sudo cp -r node-v14.10.0-linux-armv6l/* /usr/local" - execCommand "rm -r node-v14.10.0-linux-armv6l" - execCommand "rm node-v14.10.0-linux-armv6l.tar.gz" + execCommand "wget https://unofficial-builds.nodejs.org/download/release/v18.9.1/node-v18.9.1-linux-armv6l.tar.gz" + execCommand "tar -xzf node-v18.9.1-linux-armv6l.tar.gz" + execCommand "sudo cp -r node-v18.9.1-linux-armv6l/* /usr/local" + execCommand "rm -r node-v18.9.1-linux-armv6l" + execCommand "rm node-v18.9.1-linux-armv6l.tar.gz" else - execCommand "wget -qO- https://deb.nodesource.com/setup_14.x | bash -" + execCommand "wget -qO- https://deb.nodesource.com/setup_18.x | bash -" execCommand "apt -q update" execCommand "apt -qy install nodejs" fi +execCommand "apt -qy install jq curl" execCommand "apt -qy install 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 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" + +# Get package +header "Download luxcena-neo" +INSTALLDIR=$(getent passwd "$username" | cut -d: -f6) +APIURL="https://api.github.com/repos/JakobST1n/luxcena-neo" +REPOINFO=$(curl -s "$APIURL/releases/86402456" -H "Accept: application/vnd.github+json") +TARBALL_NAME=$(echo "$REPOINFO" | jq '.assets[0].name') +TARBALL_URL=$(echo "$REPOINFO" | jq '.assets[0].browser_download_url') +execCommand "runuser -l $username -c \"curl -s -L -o $INSTALLDIR/$TARBALL_NAME $TARBALL_URL\"" + +header "Install luxcena-neo" +execCommand "runuser -l $username -c \"export NODE_ENV=production; npm --prefix $INSTALLDIR/luxcena-neo/ install $INSTALLDIR/$TARBALL_NAME \"" +execCommand "runuser -l $username -c \"rm $INSTALLDIR/$TARBALL_NAME\"" # Installation is done! printf '\n\e[5m%s\e[0m\n' "🎉Luxcena-Neo is now installed🎉" +echo "Run 'sudo $INSTALLDIR/luxcena-neo/node_modules/luxcena-neo/bin/luxcena-neo.sh'" TPUT cnorm diff --git a/bin/luxcena-neo.service b/bin/luxcena-neo.service index b4115be..42d5e64 100644 --- a/bin/luxcena-neo.service +++ b/bin/luxcena-neo.service @@ -2,13 +2,13 @@ Description=Luxcena Neo [Service] -ExecStart=/opt/luxcena-neo/bin/luxcena-neo.sh +ExecStart={{WD}}/bin/luxcena-neo.sh Restart=always RestartSec=10 Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=development -WorkingDirectory=/opt/luxcena-neo/ +WorkingDirectory={{WD}} [Install] WantedBy=multi-user.target diff --git a/bin/luxcena-neo.sh b/bin/luxcena-neo.sh index c9a6d97..9653d20 100755 --- a/bin/luxcena-neo.sh +++ b/bin/luxcena-neo.sh @@ -5,5 +5,33 @@ # the server needs root as well. #runuser -l pi -c "export NODE_ENV=production; node ~/luxcena-neo-install/src/app.js" -export NODE_ENV=development -node /opt/luxcena-neo/app.js >> /var/log/luxcena-neo/service.log 2>&1 + +set -o pipefail + +# Root directory of the installation +BASEDIR=$(dirname $(dirname "$0")) +SYSTEMD_SRC_FILE="$BASEDIR/bin/luxcena-neo.service" +SYSTEMD_DEST_FILE="/etc/systemd/system/luxcena-neo.service" + +echo "Verifying that we are running the newest systemd service file" +SYSTEMD_TMP=$(mktemp) +sed "s|{{WD}}|$BASEDIR|" "$SYSTEMD_SRC_FILE" > "$SYSTEMD_TMP" + +if [[ -f "$SYSTEMD_DEST_FILE" ]] && cmp -s "$SYSTEMD_TMP" "$SYSTEMD_DEST_FILE"; then + echo "Newest service file installed." + rm "$SYSTEMD_TMP" +else + echo "Serice file not up to date, attempting to update." + cp "$SYSTEMD_TMP" "$SYSTEMD_DEST_FILE" + rm "$SYSTEMD_TMP" + systemctl daemon-reload + systemctl enable luxcena-neo + systemctl restart luxcena-neo + echo "Service file updated, exiting with the hopes that the new file will automatically restart luxcena-neo." + exit 0 +fi + +echo "Starting luxcena-neo" +export NODE_ENV=production +node "$BASEDIR/app.js" >> /var/log/luxcena-neo/service.log 2>&1 +echo "Luxcena neo exited $?" diff --git a/bin/postinstall.sh b/bin/postinstall.sh new file mode 100644 index 0000000..85a77e7 --- /dev/null +++ b/bin/postinstall.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +CWD=$PWD +. "$CWD/bin/bashfuncs.sh" + +# Create virtualenv +header "Create python virtualenv and install dependencies" +execCommand "rm -rf $CWD/NeoRuntime/Runtime/venv" +execCommand "pip3 install virtualenv" +execCommand "virtualenv -p /usr/bin/python3 \"$CWD/NeoRuntime/Runtime/venv\"" +header "Attempting to install the rpi_ws281x library, if you want to actually control some leds you need this. Don't worry if not." +execCommand "source \"$CWD/NeoRuntime/Runtime/venv/bin/activate\" && pip install rpi_ws281r" 1 + diff --git a/bin/uninstall.sh b/bin/uninstall.sh index ee8a5a2..c05d2f8 100755 --- a/bin/uninstall.sh +++ b/bin/uninstall.sh @@ -137,15 +137,16 @@ if [ $res -eq 1 ]; then header "Uninstall luxcena-neo" execCommand "rm -f /etc/systemd/system/luxcena-neo.service" - execCommand "rm -rf /opt/luxcena-neo" + execCommand "systemctl reload-daemon" + execCommand "rm -rf /var/log/luxcena-neo" TPUT setaf 2 printf "\nEverything should now be gone.\n" - printf "/etc/luxcena-neo and /var/log/luxcena-neo is not removed.\n" + printf "/etc/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 -TPUT cnorm
\ No newline at end of file +TPUT cnorm |