diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2019-09-22 13:12:44 +0200 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2019-09-22 13:12:44 +0200 |
commit | e6880cd8ccf82d993f222cb14b4860581654acb8 (patch) | |
tree | 45f318f4ece2f0d6ca73fc3f775b9ba277ccdb77 /bin | |
parent | e911f8029ca612d3b17ced300cdf5b6f13e20972 (diff) | |
parent | 2e60b25ae368b8c19ce5e982aa2672a6c56edf90 (diff) | |
download | Luxcena-Neo-e6880cd8ccf82d993f222cb14b4860581654acb8.tar.gz Luxcena-Neo-e6880cd8ccf82d993f222cb14b4860581654acb8.zip |
Merge branch 'master' of https://github.com/JakobST1n/Luxcena-Neo
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/install.sh | 242 | ||||
-rwxr-xr-x | bin/luxcena-neo-cli.sh | 164 | ||||
-rw-r--r-- | bin/luxcena-neo.service | 6 | ||||
-rwxr-xr-x | bin/luxcena-neo.sh | 3 | ||||
-rwxr-xr-x | bin/post-update.sh | 7 |
5 files changed, 240 insertions, 182 deletions
diff --git a/bin/install.sh b/bin/install.sh index c72949b..cd4f07b 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -5,178 +5,150 @@ printf '%s\n' "Luxcena-neo Installer" tput sgr0 printf '\e[93m%s\e[0m\n\n' "---------------------" -if [ $USER != "pi" ]; then - printf "Install failed...\nOther user than 'pi' detected. If you want to use another user, you have to install manually." +LOG="/tmp/luxcena-neo.install.log" +echo "Starting Luxcena-neo installer..." > $LOG + +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.\n" + printf "Check the logfile at '/tmp/lucxena-neo.install.log'.\n" + printf "Use this command to see the last 30 lines of the file;\n" + printf " tail -n 30 /tmp/luxcena-neo.install.log" + tput sgr0 + exit 1 +} + +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 +} + # Update system -tput sc -tput setaf 4 -printf ". Update your system (y/n)? " -while : -do - read -n 1 -p "" YNQuestionAnswer - if [[ $YNQuestionAnswer == "y" ]]; then - tput rc; tput el - printf ". Update your system?: \e[0;32mYes\e[0m\n" - tput sc - sudo apt-get -y -qq update || { printf "\n\nInstall failed.\n"; exit 1; } - sudo apt-get -y -qq upgrade || { printf "\n\nInstall failed.\n"; exit 1; } - tput rc; tput ed - break - elif [[ $YNQuestionAnswer == "n" ]]; then - tput rc; tput el - printf ". Update your system?: \e[0;31mNo\e[0m\n" - break - fi -done +dlgYN ". Update your system" res +if [ $res -eq 1 ]; then + tput sc + apt-get -y update &>> $LOG || die + apt-get -y upgrade &>> $LOG || die + tput rc; tput ed +fi # Install packages -tput sc -tput setaf 4 -printf ". Install required packages (y/n)? " -while : -do - read -n 1 -p "" YNQuestionAnswer - if [[ $YNQuestionAnswer == "y" ]]; then - tput rc; tput el - printf ". Install required packages?: \e[0;32mYes\e[0m\n" - tput sc - sudo apt-get -y -qq install nodejs scons python-dev swig || { printf "\n\nInstall failed.\n"; exit 1; } - if [ $? -eq 0 ]; then - tput rc; tput ed - printf "✓" - else - printf "\nInstall failed.\n" - exit 1 - fi - break - elif [[ $YNQuestionAnswer == "n" ]]; then - tput rc; tput el - printf ". Install required packages?: \e[0;31mNo\e[0m\n" - tput setaf 2 - printf " We are now assuming that all the following packages exists on your system:\n" - printf " nodejs scons python-dev swig\n" - tput sgr0 - break +dlgYN ". Install required packages" res +if [ $res -eq 1 ]; then + tput sc + apt-get -y install nodejs scons python-dev swig &>> $LOG || die + if [ $? -eq 0 ]; then + tput rc; tput ed + printf "✓" + else + printf "\nInstall failed.\n" + exit 1 fi -done +else + tput setaf 2 + printf " We are now assuming that all the following packages exists on your system:\n" + printf " nodejs scons python-dev swig\n" + tput sgr0 +fi # Install led-library -tput sc -tput setaf 4 -printf ". Install jgarff's rpi_ws281x library (y/n)? " -while : -do - read -n 1 -p "" YNQuestionAnswer - if [[ $YNQuestionAnswer == "y" ]]; then - tput rc; tput el - printf ". Install jgarff's rpi_ws281x library?: \e[0;32mYes\e[0m\n" - tput sc - git clone https://github.com/jgarff/rpi_ws281x /tmp/rpi_ws281x # TODO CHANGE PATH - python /tmp/rpi_ws281x/python/setup.py install # TODO CHANGE PAHT - if [ $? -eq 0 ]; then - tput rc; tput ed - printf "✓" - else - printf "\nInstall failed.\n" - exit 1 - fi - break - elif [[ $YNQuestionAnswer == "n" ]]; then - tput rc; tput el - printf ". Install jgarff's rpi_ws281x library?: \e[0;31mNo\e[0m\n" - break - fi -done - - -tput setaf 4 -printf ". Which rPi is this? (j:↓, k:↑, ↩:↩)\n" -tput sgr0 -tput sc -tput bel - -piModel=0 -printf "\n\n\n" # So that the menu just erases things it has alredy written -while : -do - tput cuu 3 - tput ed +dlgYN ". Install jgarff's rpi_ws281x library" res +if [ $res -eq 1 ]; then tput sc - tput setaf 8 - - if [[ piModel -eq 0 ]]; then - tput sgr0; fi - printf '%s\n' "- Raspberry Pi B" - tput setaf 8 - if [[ piModel -eq 1 ]]; then - tput sgr0; fi - printf '%s\n' "- Raspberry Pi B+" - tput setaf 8 - if [[ piModel -eq 2 ]]; then - tput sgr0; fi - printf '%s\n' "- Raspberry Pi Model 2" - tput setaf 8 - - read -sn1 key - if [ "$key" == "j" ]; then - piModel=$((piModel+1)) - if [[ piModel -gt 2 ]]; then - piModel=2 - fi - fi - if [ "$key" == "k" ]; then - piModel=$((piModel-1)) - if [[ piModel -lt 0 ]]; then - piModel=0 - fi - fi - if [ "$key" == "" ]; then - tput cuu 4 - tput ed - tput sgr0 - printf "%s\e[0;34m%s\e[0m\n" ". Which rPi is this? " "ListItem #$piModel" - break + git clone https://github.com/jgarff/rpi_ws281x /tmp/rpi_ws281x # TODO CHANGE PATH + python /tmp/rpi_ws281x/python/setup.py install # TODO CHANGE PAHT + if [ $? -eq 0 ]; then + tput rc; tput ed + printf "✓" + else + printf "\nInstall failed.\n" + exit 1 fi - -done - +fi tput setaf 4 printf ". Installing the app itself...\n" tput sgr0 +# Create user 'luxcena-neo' +tput setaf 8 +printf '%s\n' " - Creating user 'lux-neo'..." +tput sgr0 +username="lux-neo" +egrep "^$username" /etc/passwd >/dev/null +if [ $? -eq 0 ]; then + echo "User already exists, continuing..." +else + #pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) + useradd -m $username &>> $LOG || die +fi + # First we make our directories tput setaf 8 printf '%s\n' " - Making app-dir (/bin/luxcena-neo)..." tput sgr0 -mkdir ~/luxcena-neo-install || { printf "\n\nInstall failed.\n"; exit 1; } -mkdir ~/luxcena-neo-install/src || { printf "\n\nInstall failed.\n"; exit 1; } -mkdir ~/luxcena-neo-install/userdata || { printf "\n\nInstall failed.\n"; exit 1; } +userDir=$(eval echo "~$username") +#mkdir -p "$userDir/install" &>> $LOG || die +#chown $username:$username "$userDir/install" &>> $LOG || die +mkdir -p "$userDir/src" &>> $LOG || die +chown $username:$username "$userDir/src" &>> $LOG || die +mkdir -p "$userDir/userdata" &>> $LOG || die +chown $username:$username "$userDir/userdata" &>> $LOG || die # Third we copy the source into the correct swap-folder tput setaf 8 printf '%s\n' " - Copying sourceCode to app-dir..." tput sgr0 -cp -r . ~/luxcena-neo-install/src || { printf "\n\nInstall failed.\n"; exit 1; } +cp -r . "$userDir/src" &>> $LOG || die +chown -R $username:$username "$userDir/src" &>> $LOG || die # fourth we run npm i tput setaf 8 printf '%s\n' " - Running npm i..." tput sgr0 tput sc -export NODE_ENV=production || { printf "\n\nInstall failed.\n"; exit 1; } -npm --prefix ~/luxcena-neo-install/src install ~/luxcena-neo-install/src --only=production || { printf "\n\nInstall failed.\n"; exit 1; } # This is probably a bit overkill to have --only=... but better safe than sorry? +export NODE_ENV=production &>> $LOG || die +runuser -l $username -c 'npm --prefix ~/src install ~/src --only=production' &>> $LOG || die # This is probably a bit overkill to have --only=... but better safe than sorry? +tput rc; tput ed + +# fourth we copy the cli to our bin folder +tput setaf 8 +printf '%s\n' " - Adding cli-script..." +tput sgr0 +cp bin/luxcena-neo-cli.sh /usr/bin/luxcena-neo-cli.sh &>> $LOG || die +ln -sf /usr/bin/luxcena-neo-cli.sh /usr/bin/lux-neo &>> $LOG || die tput rc; tput ed # Fifth we add the service files tput setaf 8 printf '%s\n' " - Adding service-file to systemd..." tput sgr0 -sudo cp bin/luxcena-neo.service /etc/systemd/system/luxcena-neo.service -sudo systemctl daemon-reload +cp bin/luxcena-neo.service /etc/systemd/system/luxcena-neo.service &>> $LOG || die +systemctl daemon-reload &>> $LOG || die # Installation is done! printf '\n\e[5m%s\e[0m\n' "🎉Luxcena-Neo is now installed🎉" +printf 'You can now delete this folder' diff --git a/bin/luxcena-neo-cli.sh b/bin/luxcena-neo-cli.sh index 034d713..defb766 100755 --- a/bin/luxcena-neo-cli.sh +++ b/bin/luxcena-neo-cli.sh @@ -5,6 +5,38 @@ usage() { exit 1 } +function die() { + tput setaf 1 + printf "\n\nInstall failed.\n" + printf "Check the logfile at '/tmp/lucxena-neo.install.log'.\n" + printf "Use this command to see the last 30 lines of the file;\n" + printf " tail -n 30 /tmp/luxcena-neo.install.log" + tput sgr0 + exit 1 +} + +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 ) @@ -17,6 +49,8 @@ while getopts ":a:" o; do 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 @@ -25,14 +59,23 @@ if [ "$action" == "update" ]; then tput sgr0 printf '\e[93m%s\e[0m\n\n' "-------------------" - sudo systemctl stop lxucena-neo - oldDir=$PWD - cd ~/luxcena-neo-install - git pull - export NODE_ENV=production - npm i --only=production - cd $oldDir - sudo systemctl start luxcena-neo + 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 + + systemctl stop luxcena-neo + runuser -l 'lux-neo' -c 'git -C ~/src pull' + + if [ "$2" != "skipNode" ]; then + runuser -l 'lux-neo' -c 'export NODE_ENV=production; npm --prefix ~/src install ~/src --only=production' + fi + + cp /home/lux-neo/src/bin/luxcena-neo-cli.sh /usr/bin/luxcena-neo-cli.sh + printf "Update complete.\n" + systemctl start luxcena-neo + exit 0 elif [ "$action" == "uninstall" ]; then tput setab 1 @@ -42,43 +85,80 @@ elif [ "$action" == "uninstall" ]; then tput setaf 8 printf "By uninstalling Luxcena-Neo you will loose all you data, including your scripts.\n\n" - tput sc - tput setaf 4 - printf ". Are you sure you want to uninstall (y/n)? " - while : - do - read -n 1 -p "" YNQuestionAnswer - if [[ $YNQuestionAnswer == "y" ]]; then - tput rc; tput el - printf ". Are you sure you want to uninstall? \e[0;32mYes\e[0m\n" - tput sc - sudo systemctl stop luxcena-neo || { printf "\n\nUninstall failed.\n"; exit 1; } - rm -rf ~/luxcena-neo-install || { printf "\n\nUninstall failed.\n"; exit 1; } - sudo rm /etc/systemd/system/luxcena-neo.service || { printf "\n\nUninstall failed.\n"; exit 1; } - tput rc; tput ed - - tput setaf 2 - printf "\nEverything should now be gone. To remove the last piece, enter this command:\n" - tput sgr0 - tput smso - printf "sudo rm /bin/luxcena-neo\n\n" - tput sgr0 - tput setaf 8 - printf "Well, some dependencies still exists. Those are:\n" - printf " - rpi_ws281x-library\n" - printf " - packages (nodejs scons python-dev swig)\n" - break - elif [[ $YNQuestionAnswer == "n" ]]; then - tput rc; tput el - printf ". Are you sure you want to uninstall? \e[0;31mNo\e[0m\n" - break - fi - done + dlgYN "Are you sure you want to uninstall?" res + if [ $res -eq 1 ]; then + systemctl stop luxcena-neo + deluser lux-neo + rm -rf /home/lux-neo + rm /etc/systemd/system/luxcena-neo.service + rm /usr/bin/luxcena-neo.sh + rm /usr/bin/lux-neo + + + tput setaf 2 + printf "\nEverything should now be gone.\n" + tput sgr0 + tput setaf 8 + printf "Well, some dependencies still exists. Those are:\n" + printf " - rpi_ws281x-library\n" + printf " - packages (nodejs scons python-dev swig)\n" + tput sgr0 + fi + +elif [ "$action" == "conf" ]; then + nano /home/lux-neo/userdata/config/strip.json elif [ "$action" == "start" ]; then - sudo systemctl start luxcena-neo + systemctl start luxcena-neo + if [ "$2" == "boot" ]; then + systemctl enable luxcena-neo + printf "Now starting on boot...\n" + fi + printf "Luxcena-neo service started...\n" + elif [ "$action" == "stop" ]; then - sudo systemctl stop luxcena-neo + systemctl stop luxcena-neo + if [ "$2" == "boot" ]; then + systemctl disable luxcena-neo + printf "Not longer active on boot...\n" + fi + printf "Luxcena-neo service stopped...\n" + +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 /home/lux-neo/logs/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 /home/lux-neo/logs/logger.log + fi + +elif [ "$action" == "version" ] || [ "$action" == "v" ]; then + printf "╭─────────────────────╮\n" + printf "│ Version: Unknown │\n" + printf "│ branch : $(git -C /home/lux-neo/src branch | grep \* | cut -d ' ' -f2) │\n" + printf "╰─────────────────────╯\n\n" + +elif [ "$action" == "selectBranch" ]; then + printf "Current $(git -C /home/lux-neo/src branch | grep \* | cut -d ' ' -f2)Branch \n" + runuser -l 'lux-neo' -c "git -C ~/src stash" + runuser -l 'lux-neo' -c "git -C ~/src checkout $2" || printf "\e[91mYou should now run \e[90m'sudo lux-neo update'\e[91m!\n" + else usage fi diff --git a/bin/luxcena-neo.service b/bin/luxcena-neo.service index 62111a6..efea1ad 100644 --- a/bin/luxcena-neo.service +++ b/bin/luxcena-neo.service @@ -2,15 +2,13 @@ Description=Luxcena Neo [Service] -ExecStart=/home/pi/luxcena-neo-install/src/bin/luxcena-neo.sh +ExecStart=/home/lux-neo/src/bin/luxcena-neo.sh Restart=always RestartSec=10 -User=nobody -Group=nogroup Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=production -WorkingDirectory=/home/pi/luxcena-neo-install/src/ +WorkingDirectory=/home/lux-neo/src/ [Install] WantedBy=multi-user.target diff --git a/bin/luxcena-neo.sh b/bin/luxcena-neo.sh index 9bcf125..fc41f75 100755 --- a/bin/luxcena-neo.sh +++ b/bin/luxcena-neo.sh @@ -5,4 +5,5 @@ # the server needs root as well. #runuser -l pi -c "export NODE_ENV=production; node ~/luxcena-neo-install/src/app.js" -export NODE_ENV=production; node /home/pi/luxcena-neo-install/src/app.js +export NODE_ENV=production +node /home/lux-neo/src/app.js >> /home/lux-neo/logs/service.log diff --git a/bin/post-update.sh b/bin/post-update.sh new file mode 100755 index 0000000..01b0fee --- /dev/null +++ b/bin/post-update.sh @@ -0,0 +1,7 @@ +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..." |