Autodeployment av branches

Denne informasjonen er utdatert etter UKA-13.

Se eget git-repo README for autodeploy. Hint: Vi bruker Fabric for deployments og kommandoen er 'fabric deploy'

Git repoet ssh://dev.uka.no/srv/uka.git er satt opp med et post-receive script som automagisk tar seg av deployment av nye branches. I praksis betyr dette at du bare trenger å gjøre følgende for å få opp branchen din på dev.uka.no/branch/$navn

Første gangs oppsett
git add remote dev ssh://dev.uka.no/srv/uka.git
Deploying av en branch
git push dev $navn
Last opp branch under annet navn
git push dev $lokalt-navn:$nytt-navn-på-dev
Sletting av branches
git push dev :$branch-som-skal-slettes

Siden /branch på dev.uka.no lister forøvrig alle branchene som er tilgjengelige på uka.no

Oppsett av autodeployment

Oppsett av git repoet

sudo mkdir /srv/uka.git
cd /srv/uka.git
sudo chgrp uka11-admin-it-utv .
sudo chmod g+w .
git init –bare –shared=group
sudo chgrp -R uka11-admin-it-utv .

Oppsett av mapper til scriptet

sudo mkdir /var/www/uka.no/dev/{branch,checkout,htdocs,media}
virtualenv /var/www/uka.no/dev/env
sudo chmod u+rwx,g+rwxs,o+rx-w /var/www/uka.no/dev/{branch,checkout,htdocs,media,env}
sudo chgrp uka11-admin-it-utv /var/www/uka.no/dev/{branch,checkout,htdocs,media,env}
source /var/www/uka.no/dev/env/bin/activate
pip install /path/til/en/checkout/requirements.txt
cp local.py wsgi.py /var/www/uka.no/dev
sudo chgrp uka-web /var/www/uka.no/dev/local
sudo chmod 750 /var/www/uka.no/dev/local.py
sudo mkdir /var/www/uka.no/dev/media/cache
sudo chgrp uka-web /var/www/uka.no/dev/media/cache
sudo chmod 775 /var/www/uka.no/dev/media/cache

Apache oppsett

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/branch/([^/]+)
RewriteRule . - [E=APPLICATION_GROUP:%1,S=1]
RewriteRule . - [E=APPLICATION_GROUP:master]
WSGIApplicationGroup %{ENV:APPLICATION_GROUP}

RewriteRule ^/branch/([^/]+)$ /branch/$1/ R,L

AliasMatch ^/branch/([^/]+)/media/admin(.*)$ /usr/share/pyshared/django/contrib/admin/media$2
AliasMatch ^/branch/([^/]+)/media(.*)$ /var/www/uka.no/dev/branch/$1/ukeweb/media$2
AliasMatch ^/branch/?$ <basedir>/htdocs/index.html
WSGIScriptAliasMatch ^/branch/([^/]+) /var/www/uka.no/dev/branch/$1/wsgi.py

# + En kopi av alle Location(Match) innslagene man ville hatt for en
# vanlig install, bare modifisert til <LocationMatch "^/[^/]+/…">

post-recieve

Denne filen installeres i /srv/uka.git/hooks/post-receive

# Settings:
basedir="/var/www/uka.no/dev/"
media="${basedir}media/"
html="${basedir}htdocs/index.html"
# ==

read oldrev newrev refname

branch="${refname#refs/heads/}"
checkout="${basedir}checkout/$newrev/"
empty="0000000000000000000000000000000000000000"

umask u=rwx,g=rwx,o=rx

echo –

if [[ "$newrev" != "$empty" ]]; then
  echo "Exporting $newrev to checkout directory."
  mkdir $checkout
  git archive $newrev | tar -x -C $checkout

  echo "Linking in settings and wsgi file."
  ln -sf ${basedir}local.py ${checkout}ukeweb/settings/local.py
  echo "BRANCH = '$branch'"    > ${checkout}ukeweb/settings/branch.py
  echo "REVISION = '$newrev'" >> ${checkout}ukeweb/settings/branch.py
  ln -sf  ${basedir}wsgi.py ${checkout}wsgi.py

  echo "Replacing media/{images,upload} with symlinks to shared media folder."
  rm -rf ${checkout}ukeweb/media/images
  rm -rf ${checkout}ukeweb/media/upload
  ln -s ${media}images ${checkout}ukeweb/media/images
  ln -s ${media}upload ${checkout}ukeweb/media/upload
 
  echo "Replacing media/cache with symlinks to shared media folder."
  rm -rf ${checkout}ukeweb/media/cache
  ln -s ${media}cache ${checkout}ukeweb/media/cache

  echo "Replacing branch symlink for '$branch' and touching wsgi file."
  ln -sfn $checkout ${basedir}branch/$branch
  touch ${basedir}branch/$branch/wsgi.py
else
  echo "Branch is being deleted."
fi

if [[ "$oldrev" != "$newrev" ]]; then
  if [[ "$oldrev" != "$empty" ]]; then
    echo "Removing old checkout ${oldrev}."
    rm -rf ${basedir}checkout/$oldrev
  fi
fi
if [[ "$newrev" == "$empty" ]]; then
  echo "Removing '$branch' symlink."
  rm -f ${basedir}branch/$branch
fi

echo "Generatiing '$html'."

cat <<EOF > $html
<!doctype html>
<html>
<head>
  <title>UKA.no branches</title>
</head>
<body>
  <h1>UKA.no branches</h1>
  <ul>
EOF

for a in `git branch | perl -pe 's/[ *]*([^*]+)/$1/'`; do
  echo "    <li><a href='/branch/$a'>$a</a></li>" >> $html
done

cat <<EOF >> $html
  </ul>
</body>
</html>
EOF

echo –

wsgi.py

import os, sys, locale

# Set Norwegian locale, to get correct dates et al.
locale.setlocale(locale.LC_ALL, 'en_DK.UTF-8')

# This needs to be dynamic to support multiple branches
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# Switch to the directory of your project. (Optional.)
os.chdir("/var/www/uka.no/dev/htdocs")

# Activate virtualenv
activation_file = "/var/www/uka.no/dev/env/bin/activate_this.py"
execfile(activation_file, dict(__file__=activation_file))

if 'DJANGO_SETTINGS_MODULE' not in os.environ:
    os.environ['DJANGO_SETTINGS_MODULE'] = 'ukeweb.settings'

import django.core.handlers.wsgi
from django.conf import settings

handler = django.core.handlers.wsgi.WSGIHandler()

def application(environ, original_start_response):
    extra_headers = []

    if hasattr(settings, 'REVISION'):
        environ['REVISION'] = settings.REVISION
        extra_headers.append(('X-Revision', settings.REVISION))

    def start_response(status, headers, exc_info=None):
        headers.extend(extra_headers)
        return original_start_response(status, headers, exc_info)

    return handler(environ, start_response)

settings.py

(Dette er bare minimale settings, man trenger strengt tatt db, billig, payex innstillinger++)

from ukeweb.settings.branch import BRANCH, REVISION

DEBUG = False
TEMPLATE_DEBUG = DEBUG
COMPRESS_ENABLED = True

MEDIA_URL = '/branch/%s/media/' % BRANCH
ADMIN_MEDIA_PREFIX = MEDIA_URL + 'admin/'

EMAIL_SUBJECT_PREFIX = '[TEST-%s] ' % BRANCH

Lenker: Start

Mail: itk@samfundet.no | Telefon: 992 15 925 | Sist endret: 2013-09-26 22:05 | Revisjon: 2 (historie, blame) | Totalt: 1468 kB | Rediger