-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
288 lines (223 loc) · 8.29 KB
/
utils.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
"""
Generic utility classes for Fabric.
These should act as standalone functions and not modify Fabric's "env"
directly.
"""
import context_managers
import os
import shutil
import tempfile
import json
from string import replace, Template
from xml.dom import minidom
from fabric.api import env, prompt, runs_once, sudo, local, puts, lcd, abort
from fabric.context_managers import hide, cd, prefix
#from fabric.contrib.files import append
def django_manage_run(virtualenv, path, command, user, interactive=False):
"""
Run a Django management command from a Python virtual environment.
- virtualenv: Absolute path of Python virtualenv.
- path: Absolute path of Django project.
- command: Management command to run.
- user: User to sudo as.
- interactive: Whether to honour interactive prompts.
"""
manage_py = os.path.join(path, "manage.py")
cmd = "python %s %s" % (manage_py, command)
if not interactive:
cmd = "%s --noinput" % cmd
with context_managers.virtualenv(virtualenv):
sudo(cmd, user=user)
def play_run(path, command, user):
"""
Run the command of a Play application.
Always uses the environment `--%console`.
"""
cmd = "%s %s %s --%%console" % (env.python_bin, env.play_bin, command)
with cd(path):
# Make absolutely sure resulting directories are readable by the
# the Play process which runs as a different user.
with prefix('umask 0002'):
return sudo(cmd, user=user)
def supervisorctl(command, name):
"""
Run a supervisorctl action against a given project.
Abort if the output contains ERROR.
"""
cmd = "supervisorctl %s %s" % (command, name)
out = sudo(cmd, shell=False)
if command == "restart":
# We only care about the last item which is start.
out = out.split("\n")[-1]
# Supervisorctl doesn't support exit codes.
if "ERROR" in out:
abort(out)
@runs_once
def scm_get_ref(scm_type, use_default=False):
if scm_type.lower() == "svn":
if not use_default:
puts("SCM reference must be a path " \
"relative to the project's root URL.")
default = "trunk"
elif scm_type.lower() == "git":
if not use_default:
puts("SCM reference must be a named " \
"'branch', 'tag' or 'revision'.")
default = "master"
if use_default:
ref = default
else:
ref = prompt("SCM ref", default=default)
return ref
@runs_once
def scm_get_info(scm_type, scm_ref=None, directory=False):
scm_info = None
if not scm_ref:
scm_ref = scm_get_ref(scm_type, True)
if not directory:
directory = '.'
if scm_type.lower() == "svn":
with lcd(directory):
with hide("running"):
xml = local(
"svn info --xml",
capture=True,
)
dom = minidom.parseString(xml)
scm_info = {
"type": scm_type,
"rev": dom.getElementsByTagName("entry")[0] \
.getAttribute("revision"),
"url": dom.getElementsByTagName("url")[0] \
.firstChild.wholeText,
"branch": scm_ref,
}
elif scm_type.lower() == "git":
with lcd(directory):
with hide("running"):
revision = local(
"git describe --always",
capture=True,
)
repo = local(
"git remote -v | grep fetch",
capture=True,
)
scm_info = {
"type": scm_type,
"rev": revision,
"url": repo,
"branch": scm_ref,
}
return scm_info
@runs_once
def fetch_source(scm_type, scm_url, scm_ref=None, dirty=False):
if dirty:
tempdir = os.path.abspath(os.getcwd())
else:
tempdir = tempfile.mkdtemp()
os.chmod(tempdir, 0755)
if not scm_ref:
scm_ref = scm_get_ref(scm_type)
if scm_type.lower() == "svn":
local(
"svn checkout --quiet --config-option " \
"config:miscellany:use-commit-times=yes %s/%s %s"
% (
env.scm_url,
scm_ref,
tempdir,
),
)
elif scm_type.lower() == "git":
# should only pull latest revision off a tag/branch
git_branch = scm_ref if scm_ref else 'master'
local("git clone --depth 1 --branch %s %s %s" % (git_branch, env.scm_url, tempdir))
#
# Write out the version info
#
with lcd(tempdir):
scm_info = scm_get_info(scm_type, scm_ref, tempdir)
json_info = json.dumps(scm_info)
# remove (fetch) string
json_info = json_info.replace(' (fetch)', '')
# remove tab character after origin
json_info = json_info.replace('\\t', ' ')
version_file = open(os.path.join(tempdir,"version"), "w")
version_file.write(json_info)
if "scm_path" in env:
tempdir = os.path.join(tempdir, env.scm_path)
return tempdir
def delete_source_conditional(tempdir, dirty=False):
if dirty:
return
if env.host != env.hosts[-1]:
return
shutil.rmtree(tempdir)
def render_settings_template(source, target, settings, debug):
"""
Render a settings file from a template in a local checkout.
"""
context = template_context(settings)
# Treat as a string even though it's going to be rendered as unquoted.
# Clobbers anything from env in the project's own fabfile because the
# default should always be False.
if "%s" % debug in ["True", "False"]:
context["DEBUG"] = debug
else:
abort("local_settings.DEBUG may only be True or False")
template_to_file(source, target, context)
def render_custom_templates(tempdir, settings, debug):
"""
env.custom_config_files can be optionally used to specify key-value pairs
of config templates to be processed. The structure looks like:
env.custom_config_files = [
{ "source": "conf/foo.conf.template", "dest": "conf/foo.conf" },
{ "source": "conf/bar.conf.template", "dest": "conf/bar.conf" }
]
"""
# Additional config templates are optional.
if "custom_config_files" in env:
for custom_config_file in env.custom_config_files:
try:
config_source = os.path.join(tempdir, custom_config_file['source'])
config_target = os.path.join(tempdir, custom_config_file['dest'])
render_settings_template(config_source, config_target, settings, debug)
except KeyError:
# Blow up if the structure isn't as expected.
import sys, traceback
traceback.print_exc()
abort("The structure of env.custom_config_files is invalid")
@runs_once
def template_context(var_names):
"""
Compiles a list of variables and their values from Fabric's env into a
dictionary which can be used to render a template. Any values that aren't
present in env are prompted for.
"""
context = {}
for var_name in var_names:
var = env.get(var_name, None)
if var == None:
var = prompt('Enter settings var for %r:' % var_name)
context[var_name] = var
return context
def template_to_file(source, target, context):
"""
Populate templated local_settings and place it in the tempdir to be
rsynced. If `env.template_key = '$'` then it will use string.Template.
Otherwise it will fallback to Python `%()s` string interpolation.
"""
# make sure directory exists
dir = os.path.dirname(target)
if not os.path.exists(dir):
os.makedirs(dir)
with open(target, "w") as target_file:
with open(source) as source_file:
if env.get('template_key') == '$':
text = DottedIdAllowedTemplate(source_file.read()).substitute(context)
else:
text = source_file.read() % context
target_file.write(text)
class DottedIdAllowedTemplate(Template):
idpattern = r'[a-z][\._a-z0-9]*'