Last active
February 24, 2018 16:52
-
-
Save Znuff/d9c09eb83cfea490d67fda1f873f67c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
RELEASEFILE="https://github.com/Znuff/qbic/releases/download/v1.1.1/qbicd-linux64-v1.1.1.tar.gz" | |
daemon="qbicd" | |
cli="qbic-cli" | |
stopcli="stop" | |
# environment setup, make it pretty | |
tred=$(tput setaf 1) | |
tgreen=$(tput setaf 2) | |
tyellow=$(tput setaf 3) | |
tblue=$(tput setaf 4) | |
tmagenta=$(tput setaf 5) | |
tcyan=$(tput setaf 6) | |
treset=$(tput sgr0) | |
tclear=$(tput clear) | |
# starting blank | |
echo $tclear | |
echo "------------------------------" | |
echo $tblue"Masternode Upgrade Script$treset" | |
echo "------------------------------" | |
# checking for current user id, we should be running as root | |
if (( $(id -u) != 0 )); then | |
echo $tred"I need to be root to run this. Please run this with $(tput setab 7)sudo$treset$tred or log in directly as root.$treset" | |
echo -e "\n" | |
exit 1 | |
fi | |
if (( $(lsb_release -s -i) != "Ubuntu")); then | |
echo $tred"This doesn't seem to be Ubuntu. Distribution is unsuported."$treset | |
echo "Please upgrade manually using: $tmagenta$RELEASEFILE$treset" | |
exit 1 | |
fi | |
# grabbing the details of the currently running daemon | |
BINPID=$(pidof $daemon) | |
if (( $BINPID )); then | |
echo "Found $tcyan$daemon$treset running with:" | |
echo " * PID: $tyellow$BINPID$treset." | |
BINPATH=$(readlink -f /proc/$BINPID/exe) | |
echo " * Path: $tyellow$BINPATH$treset." | |
BINUID=$(awk '/^Uid:/{print $2}' /proc/$BINPID/status) | |
BINUSER=$(getent passwd $BINUID | awk -F: '{print $1}') | |
echo " * Running as: $tyellow$BINUSER$treset." | |
DISTRO=$(lsb_release -s -c) | |
echo " * Running under: $tyellow$DISTRO$treset." | |
echo "------------------------------" | |
else | |
echo $tred"No $daemon found running. Exiting.$treset" | |
exit 1 | |
fi | |
# stopping currently running daemon | |
BINDIR=$(dirname $BINPATH) | |
chmod +x $BINDIR/$cli | |
su - $BINUSER -c "$BINDIR/$cli $stopcli" | |
# just in case... | |
killall -9 $daemon 2> /dev/null | |
# deleting old binaries | |
rm -rf $BINDIR/{$cli,$daemon} | |
rm -ff $HOME/{$cli,$daemon} | |
# grabbing the new release | |
FILENAME=$(basename $RELEASEFILE) | |
echo "Downloading upgrade:" | |
curl -LJO -k $RELEASEFILE -o $FILENAME | |
echo -e "\n" | |
# unpacking in /usr/local/bin | |
tar -C /usr/local/bin/ -xzf $FILENAME | |
# making sure the files have the correct permission, maybe someone cross-compiled and moved to a non-posix filesystem | |
chmod +x /usr/local/bin/{$daemon,$cli} | |
# we should be done, let's check if we're running on a systemd compatible distro (only xenial tested!) | |
if [[ $DISTRO == "xenial" ]]; then | |
echo "Installing a systemd service file to keep our daemon running & auto-restarting." | |
cat <<- EOF > /lib/systemd/system/$daemon.service | |
[Unit] | |
Description=$daemon's masternode daemon | |
After=network.target | |
[Service] | |
User=$BINUSER | |
Group=$BINUSER | |
Type=forking | |
ExecStart=/usr/local/bin/$daemon -daemon -disablewallet | |
ExecStop=/usr/local/bin/$cli $stopcli | |
Restart=always | |
TimeoutStopSec=60s | |
TimeoutStartSec=30s | |
StartLimitInterval=120s | |
StartLimitBurst=5 | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
systemctl daemon-reload | |
systemctl enable $daemon | |
systemctl start $daemon | |
echo -e "\n" | |
echo "From now on, $tcyan$daemon$treset will always start on boot automatically. It will also be restarted automatically in case of failure." | |
echo "To manually stop/start $tcyan$daemon$treset, please use:" | |
echo " Start:$tyellow systemctl start $daemon$treset." | |
echo " Stop:$tyellow systemctl stop $daemon$treset." | |
echo -e "\n" | |
else | |
echo "You are running Ubuntu $DISTRO. We will set up a task to start $tcyan$daemon$treset on boot." | |
crontab -l -u $BINUSER | { cat; echo "@reboot /usr/local/bin/$daemon -reindex" ;} | crontab -u $BINUSER - | |
/usr/local/bin/$daemon -reindex | |
echo -e "\n" | |
fi | |
rm -rf $daemon* | |
echo "Please keep in mind that $tcyan$daemon$treset and $tcyan$cli$treset have been moved to /usr/local/bin" | |
echo "As a consequence, now you can just type $tcyan$daemon$treset instead of $tcyan./$daemon$treset (same for $tcyan$cli$treset)" | |
echo -e "\n" | |
echo "Upgrade procedure$tgreen COMPLETE$treset. Happy masternode-ing." | |
echo -e "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment