Skip to content

Commit

Permalink
Merge pull request nightscout#7 from Navid200/patch-2
Browse files Browse the repository at this point in the history
Create update_nightscout.sh
  • Loading branch information
jamorham authored Oct 14, 2022
2 parents 6abf893 + ef2de3c commit 1b2c7cf
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions update_nightscout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/bin/bash

echo
echo "Install Nightscout again, from the official repository or from a fork"
echo


if [ "`id -u`" != "0" ]
then
echo "Script needs root - execute bootstrap.sh or use sudo bash installation.sh"
echo "Cannot continue.."
exit 5
fi

echo "Installing dialog"
sudo apt-get update
sudo apt-get -y install dialog

# Setting the defaults to correspond to the official Nightscout repository.
user="nightscout"
repo="cgm-remote-monitor"
brnch="master"

clear # Clear the screen before placing the next dialog on.
dialog --yesno "Reinstall official Nightscout?\n\n
Choose Yes to install the latest version of official Nightscout.\n\n
Choose No to install from a fork (advanced).\n\n
Or, press escape to cancel." 14 50
ans=$?
if [ $ans = 255 ] # Exit if escape is pressed.
then
clear # Clear the screen before exiting.
echo "Escape"
echo "Cannot continue."
exit 5
elif [ $ans = 1 ] # We need Github details
then
# Clear fork parameters so that we can detect user not entering them all.
user=""
repo=""
brnch=""

# open fd
exec 3>&1

# Ask for the fork details.
clear # Clear the screen before placing the next dialog on.
VALUES=$(dialog --ok-label "Submit" --form "Enter the GitHub details for the Nightscout version you want to install.\n" 12 50 0 "User ID:" 1 1 "$user" 1 14 25 0 "Repository:" 2 1 "$repo" 2 14 25 0 "Branch:" 3 1 "$brnch" 3 14 25 0 2>&1 1>&3)
ans2=$?
if [ $ans2 = 255 ] || [ $ans2 = 1 ] # Exit if escaped or cancelled
then
clear # Clear dialog.
echo "Escape or Cancel"
echo "Cannot continue."
exit 5
fi

# close fd
exec 3>&-

# Assign the entered values to corresponding parameters
user=$(echo "$VALUES" | sed -n 1p)
repo=$(echo "$VALUES" | sed -n 2p)
brnch=$(echo "$VALUES" | sed -n 3p)
if [ "$user" = "" ] || [ "$repo" = "" ] || [ "$brnch" = "" ] # Abort if either paramter was left blank.
then
clear # clear before exiting
echo "Missing fork parameters"
echo "Cannot continue."
exit 5
fi
fi
clear # Clear the last dialog

cd /
if [ -s ./nightscout_start ] # Delete the previous install directory if it exists.
then
sudo rm -r nightscout_start
fi
sudo mkdir nightscout_start # Create a directory where the GitHub fork will be extracted to.
cd /nightscout_start

combined="https://github.com/$user/$repo.git" # This is the path to the repository we are installing from.
sudo git clone $combined

# Kill Nightscout to speed up the install.
sudo pkill -f SCREEN
cd $repo
sudo git checkout $brnch
sudo git pull

sudo npm install
sudo npm run postinstall # Complete the install.


# Create the first section of the Nightscout start script replacing the $repo variable
# with its value, the entered repository title.
cat> /etc/nightscout-start.sh<<EOF
#!/bin/sh
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
. /etc/nsconfig
export MONGO_COLLECTION="entries"
export MONGO_CONNECTION="mongodb://username:password@localhost:27017/Nightscout"
export INSECURE_USE_HTTP=true
export HOSTNAME="127.0.0.1"
export PORT="1337"
cd /nightscout_start/$repo
EOF

# Create the last section of the Nightscout start script
# not replacing the argument of while with its value.
cat>> /etc/nightscout-start.sh<< "EOF"
while [ "`netstat -lnt | grep 27017 | grep -v grep`" = "" ]
do
echo "Waiting for mongo to start"
sleep 5
done
sleep 5
while [ 1 ]
do
node server.js
sleep 30
done
EOF

sudo reboot # Reboot so that Nightscout starts.

0 comments on commit 1b2c7cf

Please sign in to comment.