forked from chrislawlor/django-project-skel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
224 lines (186 loc) · 7.57 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
from __future__ import with_statement
import os
from os.path import abspath, basename, dirname, join
from fabric.api import env, local, run, require, prompt, cd, prefix, execute, settings
from fabric.colors import yellow as _yellow
from fabric.contrib import django
import site
from django.utils.crypto import get_random_string
import inspect
from datetime import datetime
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
# GLOBALS
#########
env['projectname'] = basename(dirname(abspath(__file__)))
env["repo"] = "[email protected]:dries/%(projectname)s" % env
env["less"] = True
env["db_engine"] = "postgres" # Use 'postgres', 'mysql' or 'sqlite'
def _set_alwaysdata_env():
if env.settings == "staging":
env['suffix'] = "-%(settings)s" % env
else:
env['suffix'] = ""
env['run'] = run
env['accountname'] = "urga"
env['hosts']=['%(accountname)[email protected]' % env, ]
env['venv'] = "%(projectname)s%(suffix)s" % env
env['requirementsfile'] = "requirements.txt"
env['homedir']=join("/home", "%(accountname)s" % env )
env['basedir']= join(env.homedir, "www")
env['projectdir'] = join(env.basedir, env.projectname + env.suffix)
env['staticdir'] = join(env.basedir, env.projectname + env.suffix + "-static")
env['db_user'] = "%(accountname)s_%(projectname)s" % env
env["db_host"] = "adpostgresql.urga.be"
env['db_name'] = "%(accountname)s_%(projectname)s%(suffix)s" % env
# ENVIRONMENTS
##############
def localhost():
"""
Use development server settings
"""
env['settings'] = "dev"
env['run'] = local
env['venv'] = "%(projectname)s" % env
env['requirementsfile'] = "requirements_%(settings)s.txt" % env
env['projectdir'] = dirname(abspath( __file__ ))
env['db_user'] = "%(projectname)s" % env
env["db_host"] = "localhost"
env['db_name'] = env.db_user
def staging():
"""
Use staging server settings
"""
env['settings'] = "staging"
env['branch'] = 'develop'
_set_alwaysdata_env()
def production():
"""
Use production server settings
"""
env['settings'] = "prod"
env['branch'] = 'master'
_set_alwaysdata_env()
# HELPERS
#########
def _generate_key():
"""
Generate a random key just like django-startproject does it.
"""
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*-_=+()'
return get_random_string(50, chars)
def _generate_db_url():
print "Setting up Database for %s" % env.settings
print "Engine: %s" % env.db_engine
print "User: %s" % env.db_user
print "Hostname: %s" % env.db_host
print "Database name: %s" % env.db_name
if not env.settings=="dev":
db_password = prompt(_yellow('DB Password: '))
else:
db_password = env.projectname
return "%s://%s:%s@%s/%s" % (env.db_engine, env.db_user, db_password, env.db_host, env.db_name)
def virtualenv(command):
"""
Run command in virtualenv.
"""
with prefix('source virtualenvwrapper.sh && workon %(venv)s' % env):
return env.run(command)
def _fn():
"""
Returns current function name
"""
return inspect.stack()[1][3]
def _db_getdict():
if not env.settings == "dev":
db_url = virtualenv("echo $DATABASE_URL")
else:
db_url = os.environ['DATABASE_URL']
parsed_url = urlparse.urlparse(db_url)
db_dict = {
'PASSWORD' : parsed_url.password,
'HOST' : parsed_url.hostname,
'USER' : parsed_url.username,
'NAME' : parsed_url.path[1:],
}
return db_dict
def _db_exists():
d = _db_getdict()
shellreturn = local("PGPASSWORD=%(PASSWORD)s psql -l -h %(HOST)s -U %(USER)s| grep %(NAME)s | wc -l" % d, capture=True)
return int(shellreturn)
# COMMANDS
##########
def bootstrap():
"""
Initialize a virtual environment and configure variables
"""
require("settings", provided_by=[localhost, staging, production])
with cd("%(projectdir)s" % env):
env.run("source virtualenvwrapper.sh && mkvirtualenv %(venv)s && setvirtualenvproject" % env)
virtualenv("pip install -r %(requirementsfile)s" % env)
virtualenv("echo 'export DJANGO_SETTINGS_MODULE=%(projectname)s.settings.%(settings)s'>>$WORKON_HOME/%(venv)s/bin/postactivate" % env)
virtualenv("echo 'unset DJANGO_SETTINGS_MODULE'>>$WORKON_HOME/%(venv)s/bin/postdeactivate" % env)
virtualenv("""echo "export DJANGO_SECRET_KEY='%s'">>$WORKON_HOME/%s/bin/postactivate""" % (_generate_key(), env.venv))
virtualenv("echo 'unset DJANGO_SECRET_KEY'>>$WORKON_HOME/%(venv)s/bin/postdeactivate " % env)
virtualenv("""echo "export DATABASE_URL='%s'">>$WORKON_HOME/%s/bin/postactivate""" % (_generate_db_url(), env.venv))
virtualenv("echo 'unset DATABASE_URL'>>$WORKON_HOME/%(venv)s/bin/postdeactivate" % env)
virtualenv("chmod +x ./manage.py")
def update_requirements():
with prefix("source virtualenvwrapper.sh && workon %(venv)s" % env):
env.run('pip install -r requirements.txt --upgrade')
def update(skipreq=True):
"""
Update the remote target. By default it skips checking requirements. Use 'update:skipreq=False' to force updating requirements.
"""
with prefix('workon %(venv)s' % env):
env.run('git pull')
if env.less:
run('lessc -x %(projectdir)s/static/less/theme-default/style.less > %(projectdir)s/static/css/theme-default/style.css' % env)
if skipreq in ("False", "false"): # Have to test against a string, because the skipreq parameter is not a boolean, but a string.
execute(update_requirements)
env.run('./manage.py collectstatic --noinput --no-default-ignore')
env.run('cp fcgi/django_%(settings)s.fcgi fcgi/django.fcgi' % env)
def deploy():
"""
Delete and initialize deployment on a remote server
"""
require("settings", provided_by=[staging, production])
env.run("rm -rf %(projectdir)s" % env)
env.run("git clone -b %(branch)s %(repo)s %(projectdir)s" % env )
execute(bootstrap)
update(skipreq=False)
def getmedia():
require("settings", provided_by=["staging", "production"])
local('rsync -r -L --delete -vv %(host_string)s:%(staticdir)s/media/ public/media' % env)
def putmedia():
require("settings", provided_by=["staging", "production"])
local('rsync -r -L --delete -vv public/media/ %(host_string)s:%(staticdir)s/media/' % env)
# Database operations
#####################
def db_dump(dumpfile="sql/dumpdata.sql"):
print(_yellow('>>> starting %s()' % _fn()))
require("settings", provided_by=[localhost, staging, production])
context = _db_getdict()
context['DUMPFILE'] = dumpfile
local("PGPASSWORD=%(PASSWORD)s pg_dump -O -x -h %(HOST)s -U %(USER)s %(NAME)s>%(DUMPFILE)s" % context )
def db_drop():
print(_yellow('>>> starting %s()' % _fn()))
local("PGPASSWORD=%(PASSWORD)s dropdb -h %(HOST)s -U %(USER)s %(NAME)s" % _db_getdict())
def db_create():
print(_yellow('>>> starting %s()' % _fn()))
local("PGPASSWORD=%(PASSWORD)s createdb -h %(HOST)s -O %(USER)s %(NAME)s " % _db_getdict())
def db_restore(dumpfile="sql/dumpdata.sql"):
print(_yellow('>>> starting %s()' % _fn()))
require("settings", provided_by=[localhost, staging, production])
if _db_exists():
filename = "sql/%s" % env.settings + datetime.now().strftime('_%H_%M_%d_%m_%Y.sql')
db_dump(dumpfile=filename)
with settings(warn_only=True):
db_drop()
with settings(warn_only=True):
db_create()
context = _db_getdict()
context['DUMPFILE'] = dumpfile
local("PGPASSWORD=%(PASSWORD)s psql -1 -h %(HOST)s -U %(USER)s %(NAME)s < %(DUMPFILE)s" % context )