forked from CloudScale-Project/Response-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
152 lines (120 loc) · 4.49 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
148
149
150
151
152
from fabric.context_managers import cd, settings, prefix
from fabric.contrib.files import exists
from fabric.decorators import task
from fabric.operations import require, sudo, run, put, local
from fabric.state import env
"""
Base configuration
"""
env.user = 'cloudscale'
env.path = '/home/%(user)s' % env
env.virtualenvwrapper = '/usr/local/bin/virtualenvwrapper.sh'
env.branch = 'master'
env.hosts = ['127.0.0.1']
env.project = 'response_generator'
@task
def setup():
setup_os()
setup_directories()
setup_virtualenv()
@task
def deploy():
env.path = '%(path)s/%(project)s' % env
package()
upload()
symlink()
permissions()
install_requirements()
# staticfiles()
# django_settings()
config()
try:
for callback in env.callbacks:
callback()
except:
pass
def permissions():
with cd('%(path)s/releases/current/' % env):
if not exists('debug.log'):
run('touch debug.log')
sudo('chown %(user)s:%(user)s debug.log' % env)
sudo('chmod 777 debug.log')
def setup_os():
sudo('apt-get install -y python-setuptools')
sudo('easy_install pip')
sudo('apt-get install supervisor')
sudo('apt-get install python-dev')
sudo('pip install virtualenv')
sudo('apt-get install libmysqlclient-dev')
sudo('apt-get install -y git')
sudo('apt-get install -y nginx')
sudo('aptitude install -y build-essential')
sudo('apt-get install -y openjdk-7-jdk')
sudo('apt-get install -y maven')
def setup_directories():
run("mkdir -p %(path)s/%(project)s" % env)
with cd('%(path)s/%(project)s' % env):
run("mkdir -p logs" % env)
run("mkdir -p releases" % env)
run("mkdir -p packages" % env)
def setup_virtualenv():
with cd('%(path)s/%(project)s' % env):
run('virtualenv --no-site-packages env')
def symlink():
require('release')
with cd('%(path)s/releases' % env):
if exists('previous'):
run('rm previous')
if exists('current'):
run('mv current previous')
run('ln -s %(release)s current' % env)
def upload():
require('archive_name')
require('release')
run('mkdir -p %(path)s/releases/%(release)s/' % env)
put(('%(archive_name)s' % env), ('%(path)s/packages/' % env))
with cd('%(path)s/releases/%(release)s/' % env):
run('tar zxf ../../packages/%(archive_name)s' % env)
local('/bin/rm %(archive_name)s' % env)
def package():
import time
env.release = time.strftime('%Y%m%d%H%M%S')
env.archive_name = '%(release)s.tar.gz' % env
local('/usr/bin/git archive --format=tar %(branch)s | /bin/gzip > %(archive_name)s' % env)
def install_requirements():
require('release')
require('project')
with settings(virtualhost_path = '%(path)s/env' % env):
with cd('%(virtualhost_path)s' % env):
with prefix('source %(virtualhost_path)s/bin/activate' % env):
run('%(virtualhost_path)s/bin/pip install -r %(path)s/releases/%(release)s/requirements.txt' % env)
def staticfiles():
with cd('%(path)s/releases/current/' % env):
with prefix('source %(path)s/env/bin/activate' % env):
sudo('%(path)s/env/bin/python manage.py collectstatic --noinput' % env)
def django_settings():
require('project')
sudo('rm %(path)s/releases/current/webservice/settings_local.py*' % env)
sudo('rm %(path)s/releases/current/webservice/settings.pyc*' % env)
def config():
require('project')
config_flask()
config_supervisor()
# config_nginx()
def config_flask():
with cd('%(path)s/releases/current/' % env):
run('cp config.production.py config.py')
def config_supervisor():
require('project')
supervisor_conf_path = '%(path)s/releases/current/configs/supervisor.conf' % env
#supervisor_celery_conf_path = '%(path)s/releases/current/configs/supervisor_celery.conf' % env
sudo('cp %s /etc/supervisor/conf.d/%s.conf' % (supervisor_conf_path, env.project))
#sudo('cp %s /etc/supervisor/conf.d/%s_celery.conf' % (supervisor_celery_conf_path, env.project))
sudo('supervisorctl -c /etc/supervisor/supervisord.conf restart %(project)s' % env)
sudo('supervisorctl update')
def config_nginx():
require('project')
nginx_conf_path = '%(path)s/releases/current/configs/nginx.conf' % env
sudo('cp %s /etc/nginx/sites-available/%s.conf' % (nginx_conf_path, env.project))
sudo('ln -sf /etc/nginx/sites-available/%(project)s.conf /etc/nginx/sites-enabled/' % env)
sudo('service nginx restart')