-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfabfile.py
148 lines (114 loc) · 4.42 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.files import exists
import sys
import time
env.project_name = 'bugle_project'
env.roledefs = {
'localhost': ['127.0.0.1',],
'live': ['[email protected]',],
}
######################################
# Environment
######################################
def localhost():
"""
Set up a development environment locally.
"""
env.hosts = env.roledefs['localhost']
env.path = '/'.join(sys.modules[__name__].__file__.split('/')[:-1])
env.version_path = env.path
def live():
env.hosts = env.roledefs['live']
env.path = '/home/bugle'
######################################
# Tasks
######################################
def management_command(c):
require('path', 'version_path')
with cd(env.version_path):
with cd(env.project_name):
run('./manage.py %s --settings=configs.live.settings' % c)
def test():
"Run the test suite and bail out if it fails"
management_command('test --noinput')
def syncdb_migrate():
management_command('syncdb --noinput')
management_command('migrate --noinput')
@runs_once
def setup():
require('hosts', 'path')
put('dependencies/pip-0.8.1.tar.gz', env.path)
put('dependencies/virtualenv-1.5.1.tar.gz', env.path)
with cd(env.path):
sudo('easy_install pip-0.8.1.tar.gz')
sudo('pip install virtualenv-1.5.1.tar.gz')
run('mkdir -p packages')
run('mkdir -p releases')
run('mkdir -p uploads')
sudo('chown -R www-data:www-data uploads')
def version(version):
env.version = version
env.version_path = '%s/releases/%s' % (env.path, env.version)
def create_version():
require('hosts', 'path')
version(time.strftime('%Y%m%d%H%M%S'))
with cd(env.path):
run('mkdir -p releases/%s' % env.version)
upload_tar_from_git()
setup_virtualenv()
install_requirements()
def deploy():
"Specify a specific version to be made live"
require('hosts', 'path', 'version')
with cd(env.path):
if exists('releases/previous'):
run('rm releases/previous')
if exists('releases/current'):
run('mv releases/current releases/previous')
run('ln -s %s releases/current' % env.version_path)
# Install virtualenv
sudo('cp %(version_path)s/%(project_name)s/configs/live/virtualhosts/bugle /etc/apache2/sites-available/')
sudo('a2ensite bugle')
run('rm -f %(version_path)s/%(project_name)s/static/admin' % env)
run('ln -s %(version_path)s/%(project_name)s_ve/lib/python2.5/site-packages/django/contrib/admin/media %(version_path)s/%(project_name)s/static/admin' % env)
run('rm -f %(version_path)s/%(project_name)s/uploads' % env)
run('ln -s %(path)s/uploads %(version_path)s/%(project_name)s/uploads' % env)
restart_apache()
def setup_virtualenv():
require('version_path')
with cd(env.version_path):
run('mkdir -p %s_ve' % env.project_name)
run('virtualenv %s_ve/' % env.project_name)
def upload_tar_from_git():
"Create an archive from the current Git master branch and upload it"
require('version', 'path')
local('git archive --format=tar master | gzip > %s.tar.gz' % env.version)
with cd(env.path):
run('mkdir -p releases/%s' % env.version)
put('%s.tar.gz' % env.version, '%s/packages/' % env.path)
with cd('releases/%s' % env.version):
run('tar zxf ../../packages/%s.tar.gz' % env.version)
local('rm %s.tar.gz' % env.version)
def install_requirements():
"Install the required packages from the requirements file using pip"
require('version_path')
with cd(env.version_path):
run('pip install --upgrade -E %s_ve/ -r requirements.txt' % env.project_name)
def restart_apache():
sudo('/etc/init.d/apache2 force-reload')
def setup_dev():
require('version_path')
with cd(env.path):
sudo('easy_install dependencies/pip-0.8.1.tar.gz')
sudo('pip install dependencies/virtualenv-1.5.1.tar.gz')
setup_virtualenv()
install_requirements()
def clean():
local("find ./%s/ -name '*.pyc' -exec rm -rf {} \;" % env.project_name)
######################################
# Utils
######################################
def use_virtualenv():
# This can use prefix when it's available!
return 'source %s/%s_ve/bin/activate && ' % (env.version_path, env.project_name)