Skip to content

Instantly share code, notes, and snippets.

@Sryther
Created June 6, 2019 10:06
Show Gist options
  • Save Sryther/a41e20f309d84718d0f2cb59cf8655c0 to your computer and use it in GitHub Desktop.
Save Sryther/a41e20f309d84718d0f2cb59cf8655c0 to your computer and use it in GitHub Desktop.
Upgrade Mattermost with minimum unavailability
#!/bin/bash
SERVICE_NAME="mattermostdocker_app" # Change this with the service name you're using for the Mattermost service
SHORT_SERVICE_NAME="app"
VERBOSE_SERVICE_NAME="Mattermost"
DIRECTORY="/root/projects/mattermost-docker" # Change this with the path of your mattermost-docker project directory
PREVIOUS_CONTAINER=$(docker ps --format "table {{.ID}} {{.Names}} {{.CreatedAt}}" --filter "name=$SERVICE_NAME" | grep -v "ID" | awk -F " " '{print $1}')
cd $DIRECTORY
# Backup your docker-compose.yml in case
cp docker-compose.yml docker-compose.yml.old
git stash
git pull
if [ "$?" != "0" ]; then
echo "The repository couldn't be pulled. Please check manually"
exit 1
fi
git stash pop
if [ "$?" != "0" ]; then
echo "The repository needs a merge, the update must be done manually."
exit 1
fi
docker-compose build
docker-compose up -d --scale $SHORT_SERVICE_NAME=2 --no-recreate
SHOULD_CONTINUE="false"
echo "Waiting 10 seconds to ensure $VERBOSE_SERVICE_NAME is started."
sleep 10
for i in 2 3 4 5 6 7; do
STATUS=$(docker ps --format "table {{.Status}}" --filter "name=${SERVICE_NAME}" | grep -v STATUS | head -n 1)
if [[ "$STATUS" != *"Up"* ]] && [[ "$STATUS" != *"healthy"* ]]; then
echo "Retry $i: service $VERBOSE_SERVICE_NAME not up yet, waiting another 10 seconds"
sleep 10
else
SHOULD_CONTINUE="true"
echo "Service $VERBOSE_SERVICE_NAME up after $(expr $i \* 10) seconds"
break;
fi
done
if [ "$SHOULD_CONTINUE" == "true" ]; then
docker rm -f $PREVIOUS_CONTAINER
docker-compose up -d --scale $SHORT_SERVICE_NAME=1 --no-recreate
echo "Migration done!"
echo "You can verify if all is ok by doing \`cd ${DIRECTORY} && docker-compose logs -f ${SHORT_SERVICE_NAME}\`"
exit 0
fi
echo "Migration failed!"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment