-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabfile.py
226 lines (193 loc) · 5.74 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
from fabric.api import *
"""
Base configuration
"""
env.project_name = 'hacktyler_crime'
env.database_password = 'qw8ndyHprt'
env.path = '/home/ubuntu/src/%(project_name)s' % env
env.log_path = '/var/log/src/%(project_name)s' % env
env.env_path = '/home/ubuntu/.virtualenvs/%(project_name)s' % env
env.repo_path = '%(path)s' % env
env.server_config_path = '/etc/nginx/sites-enabled/%(project_name)s' % env
env.python = 'python2.7'
env.repository_url = "[email protected]:hacktyler/hacktyler_crime.git"
env.user = 'ubuntu'
env.hosts = ['hasufel.hacktyler.com']
"""
Branches
"""
def stable():
"""
Work on stable branch.
"""
env.branch = 'stable'
def master():
"""
Work on development branch.
"""
env.branch = 'master'
def branch(branch_name):
"""
Work on any specified branch.
"""
env.branch = branch_name
"""
Commands - setup
"""
def setup():
"""
Setup a fresh virtualenv, install everything we need, and fire up the database.
"""
require('branch', provided_by=[stable, master, branch])
setup_directories()
setup_virtualenv()
clone_repo()
checkout_latest()
install_requirements()
destroy_database()
create_database()
syncdb()
install_server_conf()
collect_static_files()
reload_app();
def setup_directories():
"""
Create directories necessary for deployment.
"""
run('mkdir -p %(path)s' % env)
sudo('mkdir -p /var/log/sites/%(project_name)s/' % env, user='uwsgi')
sudo('touch /var/log/sites/hacktyler_crime/hacktyler_crime.log', user='uwsgi')
def setup_virtualenv():
"""
Setup a fresh virtualenv.
"""
run('virtualenv -p %(python)s --no-site-packages %(env_path)s;' % env)
run('source %(env_path)s/bin/activate;' % env)
def clone_repo():
"""
Do initial clone of the git repository.
"""
run('git clone %(repository_url)s %(repo_path)s' % env)
def checkout_latest():
"""
Pull the latest code on the specified branch.
"""
run('cd %(repo_path)s; git checkout %(branch)s; git pull origin %(branch)s' % env)
def install_requirements():
"""
Install the required packages using pip.
"""
run('source %(env_path)s/bin/activate; pip install -r %(repo_path)s/requirements.txt' % env)
def install_server_conf():
"""
Install the server config file.
"""
sudo('ln -s %(repo_path)s/config/deployed/nginx %(server_config_path)s' % env)
sudo('ln -s %(repo_path)s/config/deployed/uwsgi.conf /etc/init/%(project_name)s.conf' % env)
sudo('initctl reload-configuration' % env)
"""
Commands - deployment
"""
def deploy():
"""
Deploy the latest version of the site to the server and restart the web server.
Does not perform the functions of load_new_data().
"""
require('branch', provided_by=[stable, master, branch])
checkout_latest()
collect_static_files()
reload_app()
def collect_static_files():
"""
Collect static files on the server.
"""
sudo('cd %(repo_path)s; %(env_path)s/bin/python manage.py collectstatic --noinput' % env, user="uwsgi")
def reload_app():
"""
Restart the uwsgi server.
"""
sudo('service %(project_name)s restart' % env)
def update_requirements():
"""
Update the installed dependencies the server.
"""
run('source %(env_path)s/bin/activate; pip install -q -U -r %(repo_path)s/requirements.txt' % env)
"""
Commands - data
"""
def reset_database():
"""
Drop and recreate the project database.
"""
pgpool_down()
destroy_database()
create_database()
syncdb()
pgpool_up()
def create_database():
"""
Creates the user and database for this project.
"""
sudo('echo "CREATE USER %(project_name)s WITH PASSWORD \'%(database_password)s\';" | psql postgres' % env, user='postgres')
sudo('createdb -T template_postgis -O %(project_name)s %(project_name)s' % env, user='postgres')
def destroy_database():
"""
Destroys the user and database for this project.
Will not cause the fab to fail if they do not exist.
"""
pgpool_down()
with settings(warn_only=True):
sudo('dropdb %(project_name)s' % env, user='postgres')
sudo('dropuser %(project_name)s' % env, user='postgres')
pgpool_up()
def syncdb():
"""
Sync the Django models to the database.
"""
sudo('cd %(repo_path)s; %(env_path)s/bin/python manage.py syncdb --noinput' % env, user="uwsgi")
def pgpool_down():
"""
Stop pgpool so that it won't prevent the database from being rebuilt.
"""
sudo('service pgpool2 stop')
def pgpool_up():
"""
Start pgpool.
"""
sudo('service pgpool2 start')
"""
Commands - local
"""
def local_reset():
"""
Reset the local database and Solr instance.
"""
local_reset_database()
def local_reset_database():
"""
Reset the local database.
"""
with settings(warn_only=True):
local('dropdb %(project_name)s' % env)
#local('dropuser %(project_name)s' % env)
#local('echo "CREATE USER %(project_name)s WITH PASSWORD \'%(database_password)s\';" | psql postgres' % env)
local('createdb -O %(project_name)s %(project_name)s -T template_postgis' % env)
local('python manage.py syncdb --noinput' % env)
"""
Deaths, destroyers of worlds
"""
def shiva_the_destroyer():
"""
Remove all directories, databases, etc. associated with the application.
"""
with settings(warn_only=True):
run('rm -Rf %(path)s' % env)
run('rm -Rf %(log_path)s' % env)
run('rm -Rf %(env_path)s' % env)
pgpool_down()
run('dropdb %(project_name)s' % env)
run('dropuser %(project_name)s' % env)
pgpool_up()
sudo('rm %(server_config_path)s' % env)
reload_app()