forked from cirlabs/rainmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
215 lines (180 loc) · 6.44 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
# CIR news applications fabfile for Heroku
# Modified from work by the Chicago Tribune's News Applications team
# We encourage you to copy this floppy (http://www.youtube.com/watch?v=up863eQKGUI)
import os
from fabric.api import *
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
"""
Base configuration
"""
# CHANGE THESE TO FIT YOUR PROJECT
env.project_name = 'rainmaker'
env.db_name = 'rainmaker'
env.s3_name = 'rainmaker'
env.database_password = ''
env.site_media_prefix = "site_media"
env.admin_media_prefix = "admin_media"
env.dbserver_path = '/home/projects' % env
env.localpath = BASE_DIR
env.python = 'python2.7'
"""
Environments
"""
def production():
"""
Work on production environment
"""
# CHANGE THESE TOO
env.settings = 'production'
env.hosts = [''] # Database host URL
env.user = '' # DB user with permission to create roles, DBs
env.s3_bucket = env.s3_name
env.singlehost = ''
if len(env.hosts) == 1:
env.singlehost = env.hosts[0]
"""
Running OSX?
"""
def install_homebrew():
"""
Installs homebrew -- the sane OSX package manager.
"""
local('/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"')
def setup_osx():
"""
OSX is going to throw a fit if you try to bootstrap a virtualenv from the
requirements file without doing the following. Requires homebrew. You can
either run install_homebrew above or follow the instructions here:
https://github.com/mxcl/homebrew/wiki/installation
"""
local('brew install libmemcached')
local('brew install libevent')
"""
Local bootstrap
"""
def bootstrap():
"""
Local development bootstrap: you should only run this once.
"""
# Install requirements
local("pip install -r ./requirements.txt")
# Create database
create_database(local)
local("python ./%(project_name)s/manage.py syncdb --noinput" % env)
# Set virtualenv vars for local dev
local('echo "export PROJECT_NAME=\"%(project_name)s\"" >> $WORKON_HOME/%(project_name)s/bin/postactivate' % env)
local('echo "export DJANGO_SETTINGS_MODULE=\"%(project_name)s.settings\"" >> $WORKON_HOME/%(project_name)s/bin/postactivate' % env)
local('echo "export PYTHONPATH=%(localpath)s:%(localpath)s/%(project_name)s" >> $WORKON_HOME/%(project_name)s/bin/postactivate' % env)
local('echo "export PATH=$PATH:%s" >> $WORKON_HOME/%s/bin/postactivate' % (BASE_DIR, env.project_name))
local('echo -e "*.pyc\ndata\\n%(project_name)s/gzip" > .gitignore' % env)
"""
Heroku
"""
def setup_heroku():
"""
Performs initial setup on heroku.
"""
local("cd %(localpath)s" % env)
local("git init")
local("git add .")
local("git commit -m 'Initial commit'")
local("heroku create -s cedar" % env)
def deploy_to_heroku():
local("pip freeze > requirements.txt")
local("git add .")
prompt("Type your commit message here:", key='commitmessage')
local("git commit -m '%(commitmessage)s';" % env)
local("git push heroku master")
def heroku_shell():
local("heroku run python %(project_name)s/manage.py shell --settings=%(project_name)s.settings.production")
def heroku_clear_cache():
local("heroku run python %(project_name)s/manage.py clearcache --settings=%(project_name)s.settings.production" % env)
"""
Database setup and deploy
"""
def dump_database(func=local):
func('pg_dump --no-owner %(db_name)s | gzip -c > %(localpath)s/data/dump.sql.gz' % env)
def create_database(func=run):
"""
Creates the user and database for this project.
"""
func('echo "CREATE USER %(db_name)s WITH PASSWORD \'%(database_password)s\';" | psql postgres' % env)
func('createdb -O %(db_name)s %(db_name)s -T template_postgis' % env)
func('echo "GRANT ALL PRIVILEGES ON %(db_name)s to %(db_name)s;" | psql postgres' % env)
func('psql -c "ALTER TABLE public.spatial_ref_sys OWNER TO %(db_name)s" %(db_name)s;' % env)
func('psql -c "ALTER TABLE public.geometry_columns OWNER TO %(db_name)s" %(db_name)s;'% env )
def destroy_database(func=run):
"""
Destroys the user and database for this project.
Will not cause the fab to fail if they do not exist.
"""
with settings(warn_only=True):
func('dropdb %(db_name)s' % env)
func('dropuser %(db_name)s' % env)
def load_data():
"""
Loads data from the repository into PostgreSQL.
"""
with settings(warn_only=True):
run("mkdir %(dbserver_path)s/data/%(db_name)s" % env)
local("scp %(localpath)s/data/dump.sql.gz %(user)s@%(singlehost)s:data/%(db_name)s" % env)
run("gunzip %(dbserver_path)s/data/%(db_name)s/dump.sql.gz" % env)
run('psql -U %(db_name)s -q %(db_name)s < %(dbserver_path)s/data/%(db_name)s/dump.sql' % env)
def pgbouncer_down():
"""
Stop pgpool so that it won't prevent the database from being rebuilt.
"""
sudo('/etc/init.d/pgbouncer stop')
def pgbouncer_up():
"""
Start pgpool.
"""
sudo('/etc/init.d/pgbouncer start')
def deploy_data():
dump_database()
destroy_database()
create_database()
load_data()
"""
Static media
"""
def gzip_assets():
"""
GZips every file in the assets directory and places the new file
in the gzip directory with the same filename.
"""
local('cd %s; python ./gzip_assets.py' % BASE_DIR)
def deploy_to_s3():
"""
Deploy the latest project site media to S3.
"""
env.gzip_path = '%(localpath)s/%(project_name)s/gzip/static/' % env
local(('s3cmd -P --add-header=Content-encoding:gzip --guess-mime-type --rexclude-from=%(localpath)s/s3exclude sync %(gzip_path)s s3://%(s3_bucket)s/%(s3_name)s/%(site_media_prefix)s/') % env)
def deploy_static():
local("python ./%(project_name)s/manage.py collectstatic" % env)
gzip_assets()
deploy_to_s3()
"""
Deaths, destroyers of worlds
"""
def shiva_the_destroyer():
"""
Remove all directories, databases, etc. associated with the application.
"""
with settings(warn_only=True):
prompt("What's the name of your app on Heroku? (ex. strong-sword-3895):", key='appname')
local('heroku apps:destroy --app %(appname)s' % env)
destroy_database()
run('s3cmd del --recursive s3://%(s3_bucket)s/%(s3_name)s' % env)
def shiva_local():
"""
Undo any local setup. This will *destroy* your local database, so use with caution.
"""
destroy_database(local)
"""
3 ... 2 ... 1 ... blastoff
"""
def blastoff():
deploy_data()
deploy_static()
deploy_to_heroku()