This repository has been archived by the owner on Sep 22, 2019. It is now read-only.
forked from osuosl/ganeti_webmgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
370 lines (299 loc) · 10.6 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import os
import pkg_resources
from fabric.api import env, abort, task
from fabric.context_managers import settings, hide, lcd
from fabric.contrib.console import confirm
from fabric.contrib.files import exists
from fabric.operations import local, prompt
# Required dependencies
#
# PIP_INSTALL - install packages from pip: name:version
#
# GIT_INSTALL - install packages from git:
#
# url - git url for checkouts
# development - head to checkout for dev.
# Defaults to master
# production - head to checkout for prod.
# Defaults to master.
# symlink - directory to symlink into project.
# Uses project name if blank
# Packages from git are given preference for dev environment. PIP is given
# preference for production environments
PIP_INSTALL = dict((r.project_name, str(r)) for r in
pkg_resources.parse_requirements(
open("requirements/prod.txt").read()
))
GIT_INSTALL = {
'django-object-permissions': {
'url': 'git://git.osuosl.org/gitolite/django/'
'django_object_permissions',
'development': 'develop',
},
'django-object-log': {
'url': 'git://git.osuosl.org/gitolite/django/'
'django_object_log',
'development': 'develop',
},
'twisted_vncauthproxy': {
'url': 'git://git.osuosl.org/gitolite/ganeti/'
'twisted_vncauthproxy',
'development': 'develop',
},
}
DEV = 'development'
PROD = 'production'
# default environment settings - override these in environment methods if you
# wish to have an environment that functions differently
env.doc_root = '.'
env.remote = False
env.environment = PROD
env.verbose = False
# List of stuff to include in the tarball, recursive.
env.MANIFEST = [
# Directories
"deprecated",
"django_test_tools",
"docs/source",
"docs/Makefile",
"ganeti_web",
"locale",
"muddle",
"muddle_users",
# Files
"AUTHORS",
"CHANGELOG",
"COPYING",
"LICENSE",
"LICENSE.muddle",
"README.rst",
"UPGRADING",
"__init__.py",
"fabfile.py",
"manage.py",
"requirements",
"search_sites.py",
"settings.py.dist",
"ldap_settings.py.dist",
"urls.py",
]
@task
def dev():
"""
Configure development deployment.
"""
env.environment = DEV
@task
def deploy():
"""
Install all dependencies from git and pip.
"""
install_dependencies_pip()
install_dependencies_git()
novnc()
@task
def clean():
"""
In a development environment, remove all installed packages and symlinks.
"""
with lcd('%(doc_root)s' % env):
gitcmd = 'git clean -%sdX -e \!settings.py'
print('Files to be removed:')
local(gitcmd % 'n')
if confirm('Are you certain you would like to remove these files?'):
local(gitcmd % 'f')
else:
abort('Aborting clean.')
@task
def update():
"""
In a development environment, update all develop branches.
"""
if env.environment != DEV:
raise Exception('must be in a development environment in order to'
'update develop branches.')
else:
with lcd('%(doc_root)s/dependencies' % env):
for git_dir, opts in GIT_INSTALL.items():
env.git_repo = git_dir
if (_exists('%(doc_root)s/dependencies/%(git_repo)s' % env) and
'development' in opts and 'checkout' not in opts):
with lcd(git_dir):
print 'Updating git repo: %(git_repo)s' % env
local('git pull --ff')
def _exists(path):
"""
A helper function to determine whether a path exists.
This function does the right thing in both remote and local environments.
"""
if env.remote:
return exists(path)
else:
return os.path.exists(path)
def create_virtualenv(virtualenv='venv', force=False):
"""
Create a virtualenv for pip installations.
By default, the environment will be placed in the document root. Pass a
path to override the location.
If ``force`` is False, then the environment will not be recreated if it
already exists.
"""
env.virtualenv = virtualenv if virtualenv else env.doc_root
with lcd(env.doc_root):
if force or not _exists('%(virtualenv)s/lib' % env):
# XXX does this actually create a new environment if one already
# exists there?
local('virtualenv %(virtualenv)s --distribute --no-site-packages'
% env)
# now lets make sure the virtual env has the the newest pip
local(str(verbose_check()+'--upgrade pip') % env)
def create_env():
"""
Setup environment for git dependencies.
"""
with lcd(env.doc_root):
if _exists('dependencies'):
print 'dependencies directory exists already'
else:
local('mkdir dependencies')
def verbose_check():
"""
Default to quiet install when env.verbose is false
"""
install_str = '%(virtualenv)s/bin/pip install '
if not env.verbose:
install_str += '-q '
return install_str
def install_dependencies_pip():
"""
Install all dependencies available from pip.
"""
create_virtualenv()
with lcd(env.doc_root):
# Run the installation with pip, passing in our
# requirements/prod.txt.
local(str(verbose_check()+'-r requirements/prod.txt') % env)
def install_dependencies_git():
"""
Install all dependencies available from git.
"""
if env.environment != DEV:
# If we can satisfy all of our dependencies from pip alone, then don't
# bother running the git installation.
if all(p in PIP_INSTALL for p in GIT_INSTALL):
print 'No git repos to install! Yay!'
return
create_env()
for name in (set(GIT_INSTALL) - set(PIP_INSTALL)):
opts = GIT_INSTALL[name]
# check for required values
if 'url' not in opts:
raise Exception('missing required argument "url" '
'for git repo: %s' % name)
# set git head to check out
if env.environment in opts:
opts['head'] = opts[env.environment]
elif env.environment == DEV and 'production' in opts:
opts['head'] = opts['production']
else:
opts['head'] = 'master'
# clone repo
with lcd('%(doc_root)s/dependencies' % env):
env.git_repo = name
env.git_url = opts['url']
if not _exists('%(doc_root)s/dependencies/%(git_repo)s' % env):
local('git clone %(git_url)s %(git_repo)s' % env)
# create branch if not using master
if opts['head'] != 'master':
with lcd(name):
local('git fetch')
# Attempt to create a tracked branch and update it.
with settings(hide('warnings', 'stderr'), warn_only=True):
local('git checkout -t origin/%(head)s' % opts)
local('git pull')
# install to virtualenv using setup.py if it exists. Some repos might
# not have it and will need to be symlinked into the project
if _exists('%(doc_root)s/dependencies/%(git_repo)s/setup.py' % env):
with lcd(env.doc_root):
local(
str(verbose_check()+'-e dependencies/%(git_repo)s') % env
)
else:
# else, configure and create symlink to git repo
with lcd(env.doc_root):
if 'symlink' in opts:
env.symlink = opts['symlink']
env.symlink_path = '%(doc_root)s/dependencies/' \
'%(git_repo)s/%(symlink)s' % env
else:
env.symlink = name
env.symlink_path = '%(doc_root)s/dependencies/' \
'%(git_repo)s' % env
with settings(hide('warnings', 'stderr'), warn_only=True):
local('ln -sf %(symlink_path)s %(doc_root)s' % env)
def novnc():
"""
Grab noVNC.
"""
if _exists("%(doc_root)s/noVNC" % env):
return
# Grab the tarball, pass it through filters. Heavy abuse of the fact that
# shell=True in local().
with lcd(env.doc_root):
# -L follows redirects.
local("curl https://github.com/kanaka/noVNC/tarball/v0.3 -L | tar xz")
# The glob replaces a git revision.
local("mv kanaka-noVNC-*/ noVNC")
@task
def tarball():
"""
Package a release tarball.
"""
tarball = prompt('tarball name', default='ganeti-webmgr.tar.gz')
files = ['ganeti_webmgr/%s' % file for file in env.MANIFEST]
files = ' '.join(files)
with lcd('..'):
data = dict(
tarball=tarball,
files=files
)
local('tar zcf %(tarball)s %(files)s --exclude=*.pyc' % data)
local('mv %(tarball)s ./ganeti_webmgr/' % data)
def _uncomment(filen, com):
args = dict(filen=filen, com=com)
local('sed -i.bak -r -e "/%(com)s/ '
's/^([[:space:]]*)#[[:space:]]?/\\1/g" %(filen)s' % args)
def _comment(filen, com):
args = dict(filen=filen, com=com)
local('sed -i.bak -r -e "s/(%(com)s)/#\\1/g" %(filen)s' % args)
@task
def ldap(state="enable", virtualenv='venv'):
"""
Enable LDAP settings, and install packages
Depends on: libldap2-dev, libsasl2-dev
"""
filename = 'settings.py'
env.virtualenv = virtualenv if virtualenv else env.doc_root
with lcd(env.doc_root):
if state == "enable":
# Install and enable LDAP settings
if not _exists('/usr/include/lber.h'):
abort("Make sure libldap-dev is installed before continuing")
if not _exists('/usr/include/sasl'):
abort("Make sure libsasl2-dev is"
" installed before continuing")
local(str(verbose_check()+'-r requirements/ldap.txt') % env)
_uncomment(filename, 'from ldap_settings')
_uncomment(filename, "'django_auth_ldap")
elif state == "disable":
# Disable LDAP settings and uninstall requirments
local('%(virtualenv)s/bin/pip uninstall '
'-r requirements/ldap.txt' % env)
_comment(filename, 'from ldap_settings')
_comment(filename, "'django_auth_ldap")
@task
def v():
"""
Enable verbose output in some commands
"""
env.verbose = True