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 = 'platforma'
18 """Use staging server"""
19 env.hosts = ['stigma.nowoczesnapolska.org.pl:2222']
20 env.user = 'platforma'
21 env.path = '/var/services/platforma'
22 env.python = '/usr/bin/python'
23 env.virtualenv = '/usr/bin/virtualenv'
24 env.pip = '/usr/bin/pip'
28 """Use production server"""
29 env.hosts = ['szo.nowoczesnapolska.org.pl:2225']
30 env.user = 'librarian'
31 env.path = '/opt/lektury/platforma'
32 env.python = '/srv/library-in-a-box/sandbox/env/redakcja/bin/python'
33 env.virtualenv = '/opt/lektury/basevirtualenv/bin/virtualenv'
34 env.pip = '/opt/lektury/basevirtualenv/bin/pip'
41 "Run the test suite and bail out if it fails"
42 require('hosts', 'path', provided_by=[staging, production])
43 result = run('cd %(path)s/%(project_name)s; %(python)s manage.py test' % env)
48 Setup a fresh virtualenv as well as a few useful directories, then run
49 a full deployment. virtualenv and pip should be already installed.
51 require('hosts', 'path', provided_by=[staging, production])
53 run('mkdir -p %(path)s; cd %(path)s; %(virtualenv)s --no-site-packages .;' % env, pty=True)
54 run('cd %(path)s; mkdir releases; mkdir shared; mkdir packages;' % env, pty=True)
55 run('cd %(path)s/releases; ln -s . current; ln -s . previous' % env, pty=True)
61 Deploy the latest version of the site to the servers,
62 install any required third party modules,
63 install the virtual host and then restart the webserver
65 require('hosts', 'path', provided_by=[staging, production])
68 env.release = time.strftime('%Y-%m-%dT%H%M')
73 install_requirements()
75 symlink_current_release()
81 def deploy_version(version):
82 "Specify a specific version to be made live"
83 require('hosts', 'path', provided_by=[localhost, webserver])
86 run('rm releases/previous; mv releases/current releases/previous;', pty=True)
87 run('ln -s %(version)s releases/current' % env, pty=True)
93 Limited rollback capability. Simple loads the previously current
94 version of the code. Rolling back again will swap between the two.
96 require('hosts', provided_by=[staging, production])
99 run('mv releases/current releases/_previous;', pty=True)
100 run('mv releases/previous releases/current;', pty=True)
101 run('mv releases/_previous releases/previous;', pty=True)
105 # =====================================================================
106 # = Helpers. These are called by other functions rather than directly =
107 # =====================================================================
108 def upload_tar_from_git():
109 "Create an archive from the current Git master branch and upload it"
110 print '>>> upload tar from git'
111 require('release', provided_by=[deploy])
112 local('git archive --format=tar master | gzip > %(release)s.tar.gz' % env)
113 run('mkdir -p %(path)s/releases/%(release)s' % env, pty=True)
114 run('mkdir -p %(path)s/packages' % env, pty=True)
115 put('%(release)s.tar.gz' % env, '%(path)s/packages/' % env)
116 run('cd %(path)s/releases/%(release)s && tar zxf ../../packages/%(release)s.tar.gz' % env, pty=True)
117 local('rm %(release)s.tar.gz' % env)
120 def upload_vhost_sample():
121 "Create and upload Apache virtual host configuration sample"
122 print ">>> upload vhost sample"
123 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)
133 def install_requirements():
134 "Install the required packages from the requirements file using pip"
135 print '>>> install requirements'
136 require('release', provided_by=[deploy])
137 run('cd %(path)s; %(pip)s install -E . -r %(path)s/releases/%(release)s/requirements.txt' % env, pty=True)
140 def copy_localsettings():
141 "Copy localsettings.py from root directory to release directory (if this file exists)"
142 print ">>> copy localsettings"
143 require('release', provided_by=[deploy])
144 require('path', provided_by=[staging, production])
146 with settings(warn_only=True):
147 run('cp %(path)s/localsettings.py %(path)s/releases/%(release)s/%(project_name)s' % env)
150 def symlink_current_release():
151 "Symlink our current release"
152 print '>>> symlink current release'
153 require('release', provided_by=[deploy])
154 require('path', provided_by=[staging, production])
156 run('rm releases/previous; mv releases/current releases/previous')
157 run('ln -s %(release)s releases/current' % env)
161 "Update the database"
163 require('project_name', provided_by=[staging, production])
164 with cd('%(path)s/releases/current/%(project_name)s' % env):
165 run('../../../bin/python manage.py syncdb --noinput' % env, pty=True)
167 run('../../../bin/python manage.py migrate' % env, pty=True)
170 def django_compress():
171 "Update static files"
173 require('project_name', provided_by=[staging, production])
174 with cd('%(path)s/releases/current/%(project_name)s' % env):
175 run('../../../bin/python manage.py synccompress --force' % env, pty=True)
178 def restart_webserver():
179 "Restart the web server"
180 print '>>> restart webserver'
181 run('touch %(path)s/releases/current/%(project_name)s/%(project_name)s.wsgi' % env)