forked from flazzarini/blocklister
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
282 lines (247 loc) · 7.78 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from StringIO import StringIO
from os import path, environ
import fabric.api as fab
import fabric.colors as clr
PYREPO_DIR = "/var/www/gefoo.org/pyrepo"
PYREPO_URL = "https://pyrepo.gefoo.org"
DEPLOY_DIR = "/var/www/gefoo.org/blocklister"
PACKAGE_NAME = 'blocklister'
USER = 'blocklister'
fab.env.roledefs = {
'pyrepo': ['pyrepo.gefoo.org'],
'prod': ['spoon.gefoo.org'],
'staging': ['foodtaster.lchome.net'],
}
@fab.task
def develop():
"""
Prepares a development environment
"""
dev_packages = [
'pytest', 'pytest-xdist', 'pytest-pep8', 'tox', 'httpie'
]
if not path.exists("env"):
fab.local("virtualenv -p /usr/bin/python3 env")
fab.local("env/bin/pip install --upgrade pip setuptools")
fab.local("env/bin/python setup.py develop")
fab.local("env/bin/pip install {}".format(" ".join(dev_packages)))
@fab.task
def test():
"""
Run Py.test
"""
fab.local("env/bin/py.test -f --color yes blocklister")
@fab.roles('pyrepo')
@fab.task
def publish():
"""
Publish package to pyrepo.gefoo.org
"""
fab.local("env/bin/python setup.py sdist")
tar_filename = fab.local(
"env/bin/python setup.py --fullname", capture=True
)
dist_filename = "dist/{}.tar.gz".format(tar_filename)
fab.put(dist_filename, PYREPO_DIR)
@fab.task
def deploy():
"""
Deploy package
"""
branch = fab.local('git rev-parse --abbrev-ref HEAD', capture=True)
if branch == "develop":
fab.local("env/bin/python setup.py sdist")
tar_filename = "{}.tar.gz".format(
fab.local(
"env/bin/python setup.py --fullname", capture=True
)
)
dist_filename = "dist/{}".format(tar_filename)
dest_filename = "/tmp/{}".format(tar_filename)
fab.put(dist_filename, dest_filename)
fab.env.user = USER
with fab.cd(DEPLOY_DIR):
fab.run(
"env/bin/pip uninstall --trusted-host pyrepo.gefoo.org -y {}"
.format(PACKAGE_NAME))
fab.run(
"env/bin/pip install --trusted-host pyrepo.gefoo.org "
"--upgrade {}"
.format(dest_filename))
else:
fab.execute("publish")
fab.env.user = USER
with fab.cd(DEPLOY_DIR):
fab.run("env/bin/pip install --upgrade pip")
fab.run("env/bin/pip install --upgrade setuptools")
fab.run(
"env/bin/pip install --trusted-host pyrepo.gefoo.org "
"--upgrade -f {} {}"
.format(PYREPO_URL, PACKAGE_NAME)
)
@fab.task
def bootstrap():
"""
Bootstrap environment
"""
# Install system dependencies
deps = [
'apache2',
'libapache2-mod-wsgi',
'curl',
'gzip',
'python-setuptools',
]
fab.sudo("aptitude install -q -y {0}".format(" ".join(deps)))
fab.sudo("easy_install virtualenv")
# Create Application User on the node
fab.env.warn_only = True
if not fab.run("cat /etc/passwd | grep \"^{}:.*$\"".format(USER)):
fab.sudo(
"useradd -d {0} -r -s /bin/sh {1}".format(DEPLOY_DIR, USER)
)
fab.sudo(
"install -o {0} -g {0} -d {1}".format(USER, DEPLOY_DIR)
)
home = environ['HOME']
publickeyfile = "{0}/.ssh/id_rsa.pub".format(home)
if not path.exists(publickeyfile):
print clr.red(
"Could not continue! You need a public key on the target "
"machine!"
)
publickey = readfile(publickeyfile)
with fab.cd(DEPLOY_DIR):
fab.sudo("[ -d .ssh ] || mkdir .ssh")
fab.sudo("echo {0} > .ssh/authorized_keys".format(publickey))
fab.sudo("chown {0}.{0} .ssh -R".format(USER))
fab.sudo("install -o {0} -g {1} -d log".format("www-data", USER))
fab.sudo("install -o {0} -g {0} -d conf".format(USER, USER))
fab.sudo("install -o {0} -g {0} -d wsgi".format(USER, USER))
# Create Apache config files
apache_content = apache_template(
PACKAGE_NAME,
DEPLOY_DIR,
USER,
servername="{}.gefoo.org".format(PACKAGE_NAME)
)
apache_filename = (
"/etc/apache2/sites-available/{}.conf".format(PACKAGE_NAME)
)
fab.put(StringIO(apache_content), apache_filename, use_sudo=True)
fab.env.user = USER
with fab.cd(DEPLOY_DIR):
fab.run("virtualenv env")
# Create config files
apache_content = apache_template(
PACKAGE_NAME,
DEPLOY_DIR,
USER,
servername="{}.gefoo.org".format(PACKAGE_NAME)
)
apache_filename = (
"/etc/apache2/sites-available/{}.conf".format(PACKAGE_NAME)
)
wsgi_content = wsgi_template(
PACKAGE_NAME,
DEPLOY_DIR,
)
wsgi_filename = "{0}/wsgi/{1}.wsgi".format(DEPLOY_DIR, PACKAGE_NAME)
logging_content = logging_template()
logging_filename = "{0}/logging.ini".format(DEPLOY_DIR)
fab.put(StringIO(wsgi_content), wsgi_filename)
fab.put(StringIO(logging_content), logging_filename)
@fab.task
def unbootstrap():
"""
Remove bootstrap
"""
fab.sudo("rm -Rf {0}".format(DEPLOY_DIR))
fab.sudo("userdel {0}".format(USER))
def readfile(filename):
if path.exists(filename):
return open(filename, "r").read().strip()
def wsgi_template(appname, path, logging_level="INFO"):
"""
Returns a wsgi configuration based on this template
"""
template = (
'import os\n'
'import logging\n'
'import logging.config\n'
'\n'
'activate_this = "{path}/env/bin/activate_this.py"\n'
'execfile(activate_this, dict(__file__=activate_this))\n'
'\n'
'logging.basicConfig(level=logging.{logging_level})\n'
'logging.config.fileConfig("{path}/logging.ini")\n'
'\n'
'from {app}.main import app as application\n'
.format(
app=appname,
app_up=appname.upper(),
path=path,
logging_level=logging_level
)
)
return template
def logging_template():
"""
Returns a logging configuration based on this template
"""
template = (
'[loggers]\n'
'keys=root\n'
'\n'
'[handlers]\n'
'keys=consoleHandler\n'
'\n'
'[formatters]\n'
'keys=simpleFormatter\n'
'\n'
'[logger_root]\n'
'level=DEBUG\n'
'handlers=consoleHandler\n'
'\n'
'[handler_consoleHandler]\n'
'class=StreamHandler\n'
'level=DEBUG\n'
'formatter=simpleFormatter\n'
'args=(sys.stdout,)\n'
'\n'
'[formatter_simpleFormatter]\n'
'format=%(asctime)s - %(name)s - %(levelname)s - %(message)s\n'
'datefmt=\n')
return template
def apache_template(app, path, user, servername=None):
template = (
'<VirtualHost *:80>\n'
' ServerName {servername}\n'
' \n'
' WSGIDaemonProcess {app} user={user} group={user} threads=5\n'
' WSGIScriptAlias / {path}/wsgi/{app}.wsgi\n'
' \n'
' <Directory {path}>\n'
' WSGIProcessGroup {app}\n'
' WSGIApplicationGroup %{{GLOBAL}}\n'
' Order deny,allow\n'
' Allow from all\n'
' </Directory>\n'
' \n'
' # Log Files\n'
' LogLevel warn\n'
' CustomLog {path}/log/access.log combined\n'
' ErrorLog {path}/log/error.log\n'
'</VirtualHost>\n'
)
if not servername:
servername = fab.prompt(
"What will be the apache servername?",
default="app.local.net"
)
return template.format(
app=app,
path=path,
servername=servername,
user=user
)