1 from __future__ import with_statement # needed for python 2.5
2 from fabric.api import *
3 from fabric.contrib import files
12 env.project_name = 'wolnelektury'
17 """Use staging server"""
18 env.hosts = ['stigma.nowoczesnapolska.org.pl:2222']
19 env.user = 'platforma'
20 env.path = '/var/services/wolnelektury'
21 env.python = '/usr/bin/python'
22 env.virtualenv = '/usr/bin/virtualenv'
23 env.pip = '/usr/bin/pip'
26 """Use production server"""
27 env.hosts = ['wolnelektury.pl:22123']
29 env.path = '/opt/lektury/wolnelektury'
30 env.python = '/opt/lektury/basevirtualenv/bin/python'
31 env.virtualenv = '/opt/lektury/basevirtualenv/bin/virtualenv'
32 env.pip = '/opt/lektury/basevirtualenv/bin/pip'
35 """Use giewont server"""
36 env.hosts = ['giewont.icm.edu.pl']
38 env.path = '/srv/wolnelektury.pl'
39 env.python = '/usr/bin/python'
40 env.virtualenv = '/usr/bin/virtualenv'
41 env.pip = '/usr/bin/pip'
47 "Run the test suite and bail out if it fails"
48 require('hosts', 'path', provided_by=[staging, production])
49 result = run('cd %(path)s/%(project_name)s; %(python)s manage.py test' % env)
53 Setup a fresh virtualenv as well as a few useful directories, then run
54 a full deployment. virtualenv and pip should be already installed.
56 require('hosts', 'path', provided_by=[staging, production])
58 run('mkdir -p %(path)s; cd %(path)s; %(virtualenv)s .;' % env, pty=True)
59 run('cd %(path)s; mkdir releases; mkdir shared; mkdir packages;' % env, pty=True)
60 run('cd %(path)s/releases; ln -s . current; ln -s . previous' % env, pty=True)
65 Deploy the latest version of the site to the servers,
66 install any required third party modules,
67 install the virtual host and then restart the webserver
69 require('hosts', 'path', provided_by=[staging, production])
72 env.release = time.strftime('%Y-%m-%dT%H%M')
77 install_requirements()
79 symlink_current_release()
83 def deploy_version(version):
84 "Specify a specific version to be made live"
85 require('hosts', 'path', provided_by=[localhost,webserver])
88 run('rm releases/previous; mv releases/current releases/previous;', pty=True)
89 run('ln -s %(version)s releases/current' % env, pty=True)
94 Limited rollback capability. Simple loads the previously current
95 version of the code. Rolling back again will swap between the two.
97 require('hosts', provided_by=[staging, production])
100 run('mv releases/current releases/_previous;', pty=True)
101 run('mv releases/previous releases/current;', pty=True)
102 run('mv releases/_previous releases/previous;', pty=True)
106 # =====================================================================
107 # = Helpers. These are called by other functions rather than directly =
108 # =====================================================================
109 def upload_tar_from_git():
110 "Create an archive from the current Git branch and upload it"
111 print '>>> upload tar from git'
112 require('release', provided_by=[deploy])
113 local('git-archive-all.sh --format tar %(release)s.tar' % env)
114 local('gzip %(release)s.tar' % env)
115 run('mkdir -p %(path)s/releases/%(release)s' % env, pty=True)
116 run('mkdir -p %(path)s/packages' % env, pty=True)
117 put('%(release)s.tar.gz' % env, '%(path)s/packages/' % env)
118 run('cd %(path)s/releases/%(release)s && tar zxf ../../packages/%(release)s.tar.gz' % env, pty=True)
119 local('rm %(release)s.tar.gz' % env)
121 def upload_vhost_sample():
122 "Create and upload Apache virtual host configuration sample"
123 print ">>> upload vhost sample"
124 files.upload_template('%(project_name)s.vhost.template' % env, '%(path)s/%(project_name)s.vhost.sample' % env, context=env)
126 def upload_wsgi_script():
127 "Create and upload a wsgi script sample"
128 print ">>> upload wsgi script sample"
129 files.upload_template('%(project_name)s.wsgi.template' % env, '%(path)s/%(project_name)s.wsgi' % env, context=env)
130 run('chmod ug+x %(path)s/%(project_name)s.wsgi' % env)
132 def install_requirements():
133 "Install the required packages from the requirements file using pip"
134 print '>>> install requirements'
135 require('release', provided_by=[deploy])
136 run('cd %(path)s; %(pip)s install -E . -r %(path)s/releases/%(release)s/requirements.txt' % env, pty=True)
138 def copy_localsettings():
139 "Copy localsettings.py from root directory to release directory (if this file exists)"
140 print ">>> copy localsettings"
141 require('release', provided_by=[deploy])
142 require('path', provided_by=[staging, production])
144 with settings(warn_only=True):
145 run('cp %(path)s/localsettings.py %(path)s/releases/%(release)s/%(project_name)s' % env)
147 def symlink_current_release():
148 "Symlink our current release"
149 print '>>> symlink current release'
150 require('release', provided_by=[deploy])
151 require('path', provided_by=[staging, production])
153 run('rm releases/previous; mv releases/current releases/previous')
154 run('ln -s %(release)s releases/current' % env)
157 "Update the database"
159 require('project_name', provided_by=[staging, production])
160 with cd('%(path)s/releases/current/%(project_name)s' % env):
161 run('../../../bin/python manage.py syncdb --noinput' % env, pty=True)
163 run('../../../bin/python manage.py migrate' % env, pty=True)
165 def restart_webserver():
166 "Restart the web server"
167 print '>>> restart webserver'
168 run('touch %(path)s/%(project_name)s.wsgi' % env)