-
Notifications
You must be signed in to change notification settings - Fork 4
/
fabfile.py
122 lines (100 loc) · 4 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
import os
import pwd
from fabric.api import cd, env, task, require, sudo, prefix, shell_env, run
from fabric.contrib.files import exists, upload_template
from fabric.context_managers import shell_env
VIRTUALENV_DIR = 'siyazana'
CODE_DIR = 'connectedafrica'
PROD_HOSTS = ['ec2-54-76-245-198.eu-west-1.compute.amazonaws.com']
PACKAGES = (
'git-core',
'memcached',
'nginx',
'npm',
'python-dev',
'python-virtualenv',
'supervisor',
)
# Nginx & Supervisor constants
SERVER_NAMES = 'siyazana.co.za'
PROXY_PORT = 5001
PROXY_HOST = '127.0.0.1'
LOG_DIR = 'siyazana_logs'
@task
def prod():
env.deploy_user = 'ubuntu'
env.deploy_dir = '/home/ubuntu/siyazana'
env.branch = 'master'
env.nginx_bind = '127.0.0.1:80'
env.hosts = PROD_HOSTS
@task
def provision():
require('deploy_user', 'deploy_dir', provided_by=[prod])
commands = (
'apt-get install %s --no-upgrade' % ' '.join(PACKAGES),
'apt-get build-dep lxml --no-upgrade',
'mkdir -p %s' % env.deploy_dir,
'chown -R %s:%s %s' % (env.deploy_user, env.deploy_user, env.deploy_dir),
)
sudo('; '.join(commands))
@task
def deploy():
require('deploy_user', 'deploy_dir', 'branch',
provided_by=[prod])
repo_dir = os.path.join(env.deploy_dir, CODE_DIR)
ve_dir = os.path.join(env.deploy_dir, VIRTUALENV_DIR)
if not exists(ve_dir):
sudo('virtualenv -p python2.7 %s' % ve_dir, user=env.deploy_user)
if not exists(repo_dir):
with cd(env.deploy_dir):
sudo('git clone -b %s https://github.com/ANCIR/siyazana.co.za.git %s'
% (env.branch, repo_dir), user=env.deploy_user)
else:
with cd(repo_dir):
sudo('git checkout -B %s' % env.branch, user=env.deploy_user)
sudo('git pull origin %s' % env.branch, user=env.deploy_user)
with cd(repo_dir), prefix('. ../%s/bin/activate' % VIRTUALENV_DIR):
sudo('pip install -e ./', user=env.deploy_user)
sudo('make install', user=env.deploy_user)
# render and upload templates
upload_template(os.path.join(os.path.dirname(__file__), 'deploy/nginx.template'),
'/etc/nginx/sites-enabled/siyazana.conf',
get_nginx_template_context(), use_sudo=True, backup=False)
upload_template(os.path.join(os.path.dirname(__file__), 'deploy/supervisor.template'),
'/etc/supervisor/conf.d/siyazana.conf',
get_supervisor_template_context(), use_sudo=True, backup=False)
# make sure logging dir exists and update processes
log_dir = os.path.join(env.deploy_dir, LOG_DIR)
sudo('mkdir -p %s' % log_dir, user=env.deploy_user)
sudo('supervisorctl update')
sudo('/etc/init.d/nginx reload')
@task
def make(rule):
require('deploy_dir', provided_by=[prod])
repo_dir = os.path.join(env.deploy_dir, CODE_DIR)
with cd(repo_dir), prefix('. ../%s/bin/activate' % VIRTUALENV_DIR):
with shell_env(GRANO_HOST='http://backend.connectedafrica.org',
GRANO_PROJECT='southafrica',
COAF_SETTINGS='settings'):
run('make %s' % rule)
def get_nginx_template_context():
return {
'server-name': SERVER_NAMES,
'server-port': env.nginx_bind,
'static-path': os.path.join(env.deploy_dir, 'connectedafrica/connectedafrica/static/'),
'log': os.path.join(env.deploy_dir, LOG_DIR, 'nginx.log'),
'err-log': os.path.join(env.deploy_dir, LOG_DIR, 'nginx.err'),
'proxy-host': PROXY_HOST,
'proxy-port': PROXY_PORT,
}
def get_supervisor_template_context():
return {
'user': env.deploy_user,
'project-dir': os.path.join(env.deploy_dir, CODE_DIR),
've-dir': os.path.join(env.deploy_dir, VIRTUALENV_DIR),
'log': os.path.join(env.deploy_dir, LOG_DIR, 'gunicorn.log'),
'err-log': os.path.join(env.deploy_dir, LOG_DIR, 'gunicorn.err'),
'host': PROXY_HOST,
'port': PROXY_PORT,
'process-name': '%(program_name)s_%(process_num)s'
}