/
fabfile.py
126 lines (82 loc) · 2.64 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
from fabric.api import env, run, cd, sudo, local, get
env.hosts = ['wraithan.net', ]
deploy_dir = '/srv/wsgi/reciblog/'
dumpdata_apps = 'admin blog auth messages recipes sites south tagging webdesign'
json_fixture = 'live_data.json'
def virtualenv_run(cmd):
run('workon reciblog && ' + cmd)
def virtualenv_local(cmd):
local('workon reciblog && ' + cmd)
def deploy():
git_pull()
install_requirements()
sync_db()
reload_code()
def install():
make_deploy_dir()
git_clone()
make_virtualenv()
install_requirements()
start_gunicorn()
install_nginx_conf()
enable_nginx_conf()
reload_nginx_conf()
create_db()
sync_db()
def load_data_from_live():
reset_data()
dump_live_data()
load_live_data()
def full_restart_gunicorn():
stop_gunicorn()
start_gunicorn()
def make_deploy_dir():
sudo('mkdir ' + deploy_dir)
sudo('chown wraithan:users ' + deploy_dir)
def git_clone():
with cd('/srv/wsgi/'):
run('git clone git@github.com:wraithan/reciblog.git')
def git_pull():
with cd(deploy_dir):
run('git pull')
def make_virtualenv():
run('mkvirtualenv --no-site-packages reciblog')
def install_requirements():
with cd(deploy_dir):
virtualenv_run('pip install -r deploy-requirements.txt')
def start_gunicorn():
with cd(deploy_dir):
virtualenv_run('gunicorn_django --pid=' + deploy_dir +
'/gunicorn.pid --workers=8 -b 127.0.0.1:8002 --daemon deploy_settings.py')
def stop_gunicorn():
with cd(deploy_dir):
sudo('kill `cat gunicorn.pid`')
def install_nginx_conf():
sudo('cp ' + deploy_dir +
'/conf/reciblog /etc/nginx/sites-available/reciblog')
def enable_nginx_conf():
sudo('ln -s /etc/nginx/sites-available/reciblog'
'/etc/nginx/sites-enabled/reciblog')
def reload_nginx_conf():
sudo('/etc/rc.d/nginx check')
sudo('/etc/rc.d/nginx reload')
def create_db():
run('createdb reciblog')
def sync_db():
with cd(deploy_dir):
virtualenv_run('./manage.py migrate')
def reload_code():
with cd(deploy_dir):
sudo('kill -HUP `cat gunicorn.pid`')
def reset_data():
local('./manage.py flush', capture=False)
def dump_live_data():
with cd(deploy_dir):
virtualenv_run('./manage.py dumpdata %s > %s' % (dumpdata_apps, json_fixture))
get('%s/%s' % (deploy_dir, json_fixture), '/home/wraithan/devel/reciblog/')
def load_live_data():
with cd('/home/wraithan/devel/reciblog'):
local('./manage.py loaddata %s' % json_fixture)
local('rm %s' % json_fixture)
with cd(deploy_dir):
virtualenv_run('rm %s' % json_fixture)