1 from __future__ import with_statement # needed for python 2.5
2 from fabric.api import *
11 env.project_name = 'platforma'
16 """Use staging server"""
17 env.hosts = ['stigma.nowoczesnapolska.org.pl:2222']
19 env.path = '/var/services/platforma'
20 env.python = '/usr/bin/python'
21 env.virtualenv = '/usr/bin/virtualenv'
22 env.pip = '/usr/bin/pip'
25 """Use production server"""
26 env.hosts = ['wolnelektury.pl:22123']
28 env.path = '/opt/lektury/platforma'
29 env.python = '/opt/lektury/basevirtualenv/bin/python'
30 env.virtualenv = '/opt/lektury/basevirtualenv/bin/virtualenv'
31 env.pip = '/opt/lektury/basevirtualenv/bin/pip'
38 "Run the test suite and bail out if it fails"
39 require('hosts', 'path', provided_by=[staging, production])
40 result = run('cd %(path)s/%(project_name)s; %(python)s manage.py test' % env)
44 Setup a fresh virtualenv as well as a few useful directories, then run
45 a full deployment. virtualenv and pip should be already installed.
47 require('hosts', 'path', provided_by=[staging, production])
49 run('mkdir -p %(path)s; cd %(path)s; %(virtualenv)s --no-site-packages .;' % env, pty=True)
50 run('cd %(path)s; mkdir releases; mkdir shared; mkdir packages;' % env, pty=True)
55 Deploy the latest version of the site to the servers,
56 install any required third party modules,
57 install the virtual host and then restart the webserver
59 require('hosts', 'path', provided_by=[staging, production])
62 env.release = time.strftime('%Y-%m-%dT%H%M')
66 install_requirements()
67 symlink_current_release()
71 def deploy_version(version):
72 "Specify a specific version to be made live"
73 require('hosts', 'path', provided_by=[localhost,webserver])
76 run('rm releases/previous; mv releases/current releases/previous;', pty=True)
77 run('ln -s %(version)s releases/current' % env, pty=True)
82 Limited rollback capability. Simple loads the previously current
83 version of the code. Rolling back again will swap between the two.
85 require('hosts', provided_by=[staging, production])
88 run('mv releases/current releases/_previous;', pty=True)
89 run('mv releases/previous releases/current;', pty=True)
90 run('mv releases/_previous releases/previous;', pty=True)
94 # =====================================================================
95 # = Helpers. These are called by other functions rather than directly =
96 # =====================================================================
97 def upload_tar_from_git():
98 "Create an archive from the current Git master branch and upload it"
99 print '>>> upload tar from git'
100 require('release', provided_by=[deploy])
101 local('git archive --format=tar master | gzip > %(release)s.tar.gz' % env)
102 run('mkdir -p %(path)s/releases/%(release)s' % env, pty=True)
103 run('mkdir -p %(path)s/packages' % env, pty=True)
104 put('%(release)s.tar.gz' % env, '%(path)s/packages/' % env)
105 run('cd %(path)s/releases/%(release)s && tar zxf ../../packages/%(release)s.tar.gz' % env, pty=True)
106 local('rm %(release)s.tar.gz' % env)
108 def install_requirements():
109 "Install the required packages from the requirements file using pip"
110 print '>>> install requirements'
111 require('release', provided_by=[deploy])
112 run('cd %(path)s; %(pip)s install -E . requirements.pybundle' % env)
114 def upload_pybundle():
115 "Create pybundle with required libraries and upload it"
116 print ">>> upload pybundle"
117 require('release', provided_by=[deploy])
118 with settings(warn_only=True):
119 pip_options = run('cat %(path)s/releases/%(release)s/pip-options.txt' % env)
120 if pip_options.failed:
123 env.pip_options = pip_options
125 requirements_mtime = os.path.getmtime('requirements.txt')
128 bundle_mtime = os.path.getmtime('requirements.pybundle')
132 if requirements_mtime > bundle_mtime:
133 local('pip bundle requirements.pybundle %(pip_options)s -r requirements.txt' % env)
134 put('requirements.pybundle', '%(path)s' % env)
136 def symlink_current_release():
137 "Symlink our current release"
138 print '>>> symlink current release'
139 require('release', provided_by=[deploy])
140 require('path', provided_by=[staging, production])
142 run('rm releases/previous; mv releases/current releases/previous')
143 run('ln -s %(release)s releases/current' % env)
146 "Update the database"
148 require('project_name', provided_by=[staging, production])
149 with cd('%(path)s/releases/current/%(project_name)s' % env):
150 run('../../../bin/python manage.py syncdb --noinput' % env, pty=True)
152 run('../../../bin/python manage.py migrate' % env, pty=True)
154 def restart_webserver():
155 "Restart the web server"
156 print '>>> restart webserver'
157 run('touch %(path)s/releases/current/%(project_name)s/%(project_name)s.wsgi' % env)