Last active
August 29, 2015 14:24
-
-
Save melwil/0569cfdefe7ef89b9971 to your computer and use it in GitHub Desktop.
Script for managing production versions of online.ntnu.no
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 | |
# | |
# Written by Håvard Slettvold | |
PROJECT_ROOT="/srv/www/onlineweb4" | |
ENV="$PROJECT_ROOT/env" | |
GIT="$PROJECT_ROOT/onlineweb4" | |
NUM_SHOW=10 | |
# Move to git checkout repo | |
cd "$GIT" | |
# Fetch all tags from origin | |
echo - Fetching tags | |
git fetch --tags > /dev/null | |
TAGS=(`git for-each-ref --format='%(*committerdate:raw)%(committerdate:raw) %(refname) %(*objectname) %(objectname)' refs/tags | sort -n | awk '{ print $3; }' | awk -F"/" '{ print $3; }'`) | |
# Find out which versions to show | |
START=`expr ${#TAGS[*]} - $NUM_SHOW` | |
# Make an array with only the X latest | |
LATEST="${TAGS[*]:$START}" | |
LATEST=($LATEST) | |
# Show versions | |
for index in ${!LATEST[*]}; do | |
printf "%s: %s\n" `expr $NUM_SHOW - $index` ${LATEST[$index]} | |
done | tac | |
# 'tac' is an oposite of 'cat', just reverses lines in input | |
# Take user input for which version they want | |
echo - Which version of onlineweb4 do you wish to set in production? | |
echo - "'Enter' for latest (${LATEST[`expr $NUM_SHOW - 1`]})." | |
read CHOICE | |
if [ -z "$CHOICE" ]; then | |
CHOICE=1 | |
fi | |
CHOICE=`expr $NUM_SHOW - $CHOICE` | |
TAG=${LATEST[$CHOICE]} | |
echo "- You have chosen '$TAG'. Preparing production staging." | |
# Enabling virtualenv for project | |
source "$ENV/bin/activate" | |
# Delete old .pyc files | |
find . -name '*.pyc' -delete | |
# Fetch selected version | |
`git checkout $TAG` | |
# Install requirements if any | |
echo - Updating requirements | |
pip freeze | grep -v -f requirements.txt - | xargs pip uninstall -y | |
pip install -r requirements.txt | |
# Update and migrate database | |
python manage.py migrate --noinput | |
# Update static files | |
python manage.py collectstatic --noinput | |
# Restart services | |
supervisorctl restart onlineweb4 | |
supervisorctl restart mommy | |
# Restart memcached | |
service memcached restart | |
service nginx restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment