-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
154 lines (127 loc) · 4.28 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
153
154
###############
### imports ###
###############
from fabric.api import cd, env, lcd, put, prompt, local, sudo
from fabric.contrib.files import exists
##############
### config ###
##############
local_app_dir = './flask_project'
local_config_dir = './config'
remote_app_dir = '/home/www/flask-deploy'
remote_git_dir = '/home/git'
remote_flask_dir = remote_app_dir + '/flask_project'
remote_nginx_dir = '/etc/nginx/sites-enabled'
remote_supervisor_dir = '/etc/supervisor/conf.d'
env.hosts = ['YOUR_IP_ADDRESS_OR_HOST_HERE'] # replace with IP address or hostname
env.user = 'linux-system-user'
env.password = 'linux-system-user-password'
#############
### tasks ###
#############
def install_requirements():
""" Install required packages. """
sudo('apt-get update')
sudo('apt-get install -y python')
sudo('apt-get install -y python-pip')
sudo('apt-get install -y python-virtualenv')
sudo('apt-get install -y nginx')
sudo('apt-get install -y gunicorn')
sudo('apt-get install -y supervisor')
sudo('apt-get install -y git')
def install_flask():
"""
1. Create project directories
2. Create and activate a virtualenv
3. Copy Flask files to remote host
"""
if exists(remote_app_dir) is False:
sudo('mkdir ' + remote_app_dir)
if exists(remote_flask_dir) is False:
sudo('mkdir ' + remote_flask_dir)
with lcd(local_app_dir):
with cd(remote_app_dir):
sudo('virtualenv env')
sudo('source env/bin/activate')
sudo('pip install Flask==0.10.1')
with cd(remote_flask_dir):
put('*', './', use_sudo=True)
def configure_nginx():
"""
1. Remove default nginx config file
2. Create new config file
3. Setup new symbolic link
4. Copy local config to remote config
5. 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/flask_project') is False:
sudo('touch /etc/nginx/sites-available/flask_project')
sudo('ln -s /etc/nginx/sites-available/flask_project' +
' /etc/nginx/sites-enabled/flask_project')
with lcd(local_config_dir):
with cd(remote_nginx_dir):
put('./flask_project', './', use_sudo=True)
sudo('/etc/init.d/nginx restart')
def configure_supervisor():
"""
1. Create new supervisor config file
2. Copy local config to remote config
3. Register new command
"""
if exists('/etc/supervisor/conf.d/flask_project.conf') is False:
with lcd(local_config_dir):
with cd(remote_supervisor_dir):
put('./flask_project.conf', './', use_sudo=True)
sudo('supervisorctl reread')
sudo('supervisorctl update')
def configure_git():
"""
1. Setup bare Git repo
2. Create post-receive hook
"""
if exists(remote_git_dir) is False:
sudo('mkdir ' + remote_git_dir)
with cd(remote_git_dir):
sudo('mkdir flask_project.git')
with cd('flask_project.git'):
sudo('git init --bare')
with lcd(local_config_dir):
with cd('hooks'):
put('./post-receive', './', use_sudo=True)
sudo('chmod +x post-receive')
def run_app():
""" Run the app! """
with cd(remote_flask_dir):
sudo('supervisorctl start flask_project')
def deploy():
"""
1. Copy new Flask files
2. Restart gunicorn via supervisor
"""
with lcd(local_app_dir):
local('git add -A')
commit_message = prompt("Commit message?")
local('git commit -am "{0}"'.format(commit_message))
local('git push production master')
sudo('supervisorctl restart flask_project')
def rollback():
"""
1. Quick rollback in case of error
2. Restart gunicorn via supervisor
"""
with lcd(local_app_dir):
local('git revert master --no-edit')
local('git push production master')
sudo('supervisorctl restart flask_project')
def status():
""" Is our app live? """
sudo('supervisorctl status')
def create():
install_requirements()
install_flask()
configure_nginx()
configure_supervisor()
configure_git()