-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
116 lines (92 loc) · 2.97 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
from fabric.api import cd, sudo, run, prefix
from fabric.contrib.files import exists
from fabric.state import env
from contextlib import contextmanager as _contextmanager
##############
### config ###
##############
git_repo = 'https://github.com/b-cube/restparql.git'
app_dir = '~/restparql'
virtualenv_dir = app_dir + '/env'
flask_dir = app_dir + '/app'
nginx_dir = '/etc/nginx/sites-enabled'
env.directory = virtualenv_dir
env.activate = 'source {0}/bin/activate'.format(virtualenv_dir)
@_contextmanager
def virtualenv():
with cd(env.directory):
with prefix(env.activate):
yield
#############
### tasks ###
#############
def install_os_requirements_1():
""" Install required packages. """
sudo('apt-get update')
sudo('apt-get install -y build-essential')
sudo('apt-get install -y python-dev')
sudo('apt-get install -y libxslt1-dev')
sudo('apt-get install -y libxml2-dev')
sudo('apt-get install -y python3.4-dev')
sudo('apt-get install -y python-pip')
sudo('apt-get install -y nginx')
sudo('apt-get install -y supervisor')
sudo('apt-get install -y git')
sudo('apt-get install -y zlib1g-dev')
sudo('sudo pip install virtualenv')
def checkout_project_2():
"""
1. Create project directories
2. checkout the latest code
"""
if exists(app_dir) is False:
run('mkdir ' + app_dir)
if exists(flask_dir) is False:
with cd(app_dir):
run('git clone ' + git_repo + ' ' + flask_dir)
else:
with cd(flask_dir):
run('git pull --rebase')
def install_virtualenv_packages_3():
"""
1. Create a new virtualenv
2. Install the requirements
"""
if exists(virtualenv_dir) is False:
run('virtualenv -p python3 ' + virtualenv_dir)
with virtualenv():
run('pip install --upgrade pip')
run('pip install -r ' + flask_dir + '/requirements.txt')
def configure_nginx_4():
"""
1. Remove default nginx config file
2. Create new config file
3. Setup new symbolic link
4. Restart nginx
"""
sudo('/etc/init.d/nginx start')
if exists('/etc/nginx/sites-enabled/default'):
sudo('rm /etc/nginx/sites-enabled/default')
if exists('/etc/nginx/sites-enabled/nginx_flask') is False:
sudo('touch /etc/nginx/sites-available/nginx_flask')
sudo('ln -s /etc/nginx/sites-available/nginx_flask' +
' /etc/nginx/sites-enabled/nginx_flask')
sudo('cp ' + flask_dir + '/config/nginx_flask ' + nginx_dir)
sudo('/etc/init.d/nginx restart')
def run_app():
""" Run the app! """
with virtualenv():
with cd(flask_dir):
run('gunicorn app:app -b localhost:5000 -w4 --daemon', pty=False)
def stop_app():
sudo('pkill gunicorn.*')
def status():
""" Is our app live? """
run('ps -aux | grep gunicorn')
def provision():
install_os_requirements_1()
checkout_project_2()
stop_app()
install_virtualenv_packages_3()
run_app()
configure_nginx_4()