Skip to content

Instantly share code, notes, and snippets.

@soundstorm
Last active January 24, 2017 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soundstorm/5612b817ba3b9b738c0b131ea504a851 to your computer and use it in GitHub Desktop.
Save soundstorm/5612b817ba3b9b738c0b131ea504a851 to your computer and use it in GitHub Desktop.
Rocket.Chat auto-updater
#!/bin/bash
#configure an incoming webhook
SERVER="https://demo.rocket.chat/hooks/TOKEN"
CHANNEL="#rocketchat"
cd /opt
#get most recent version
curl -s -L https://rocket.chat/releases/latest/download -o rocket.chat.tgz
#compare files, you can use it in a cronjob
cmp --silent rocket.chat.tgz.cur rocket.chat.tgz && echo "Rocket.Chat is already up-to-date!" && exit 0
tar zxvf rocket.chat.tgz
mv -f rocket.chat.tgz rocket.chat.tgz.cur
#optional: announce update half an hour before shutting down system - look at milestone of update to check if there are any incompatibilities (like different node version)
#MINS=30
#SECS=(($MINS*60))
#curl -s -X POST --data-urlencode 'payload={"text": "A more recent version is available. The update will run in '$MINS' minutes, service will be unavailable for a short time." , "channel": "'$CHANNEL'", "username": "Rocket.Chat Updater", "icon_emoji": ":no_entry:"}' "$SERVER"
#
#while [ $SECS -gt 0 ]; do
# echo -ne "$SECS seconds remaining till update\033[0K\r"
# sleep 1
# : $((SECS--))
#done
#echo
#echo "Updating now."
curl -s -X POST --data-urlencode 'payload={"text": "I downloaded the most recent version of Rocket.Chat and will update now. Due to the update, service will be offline shortly." , "channel": "'$CHANNEL'", "username": "Rocket.Chat Updater", "icon_emoji": ":no_entry:"}' "$SERVER"
echo "Shutting down service"
systemctl stop rocketchat.service
#clear old backup and create new backup
#you may like to backup MongoDB too?
echo "Starting backup"
rm -rf Rocket.Chat.bak
mv Rocket.Chat Rocket.Chat.bak
mv bundle Rocket.Chat
cd Rocket.Chat/programs/server
echo "Installing new version:"
npm install
echo "Starting new version..."
systemctl start rocketchat.service
#check if system is back up
#hopefully startup log output won't change in the future
VERS=`systemctl status rocketchat | grep Version`
while [[ "$VERS" != *"Version"* ]]
do
#system is still booting, wait a second...
sleep 1
#check again
VERS=`systemctl status rocketchat | grep Version`
done
#extract running version from log
VERSION=`echo "$VERS" | sed -n "s/^.*\s*:\s*\(\S*\).*$/\1/p"`
echo "Version $VERSION is up and running!"
#notify users
curl -s -X POST --data-urlencode 'payload={"text": "Service is back up running version '$VERSION'.", "channel": "'$CHANNEL'", "username": "Rocket.Chat Updater", "icon_emoji": ":white_check_mark:"}' "$SERVER"
#You may copy ../../rocket.chat.tgz to rocket.chat.$VERSION.tgz to have a backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment