-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
452 lines (324 loc) · 10.5 KB
/
tasks.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# coding: utf-8
from __future__ import unicode_literals
from __future__ import absolute_import
from invoke import run
from invoke import task
import io
import json
import os
import re
PROJECT_ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
ANSIBLE_CFG = '''
[ssh_connection]
control_path = %(directory)s/%%h-%%p-%%r
scp_if_ssh = True
[defaults]
inventory = ./inventory
gathering = smart
fact_caching_timeout = 300
host_key_checking = False
retry_files_enabled = False
fact_caching = jsonfile
ansible_managed = Ansible managed: Do NOT edit this file manually!
nocows = 1
pipelining = True
fact_caching_connection = tmp/ansible-facts
hash_behaviour = merge
'''
def rm_f(path):
if os.path.isfile(path):
os.unlink(path)
return True
def touch(filename):
"""Simulate touch
"""
with io.open(filename, mode='a', encoding='utf-8'):
pass
def vagrant_status():
"""Get status of vagrant box
Returns:
`generator` - (box `str`, status `str`, provider `str`
"""
result = run('vagrant status', hide=True)
out = result.stdout
assert result.ok, 'Unable to get list of vagrant environments, please invoke `vagrant status`'
for x in out.splitlines():
regex = re.compile(r'\([a-zA-Z0-9]+\)')
x = x.encode().decode()
if regex.search(x):
x = x.replace(')', '')
env, status, provider = re.split(r' \(| +', x)
yield (env, status, provider)
def vagrant_halt(box='default', **kwargs):
"""Poweroff a vagrant box
"""
return run(
'vagrant halt --force {box}'.format(box=box),
**kwargs
)
def vagrant_up(box='default', **kwargs):
"""Starts a vagrant box
"""
# remove state file
rm_f_built_state(box=box)
result = None
if is_active(box=box) is False:
result = run(
'vagrant up {box}'.format(box=box),
**kwargs
)
# create the built state file
touch_built_state(box=box)
return result
def vagrant_destroy(box='default', **kwargs):
"""Destroys a vagrant box
"""
return run(
'vagrant destroy --force {box}'.format(box=box),
**kwargs
)
def vagrant_provision(box='default', **kwargs):
"""Run the vagrant provisioner
"""
return run(
'vagrant provision {box}'.format(box=box),
**kwargs
)
def vagrant_snapshot_list(name, box='default', **kwargs):
"""Return a list of vagrant box snapshots
"""
result = run(
'vagrant snapshot list {box}'.format(box=box),
hide=True,
**kwargs
)
out = result.stdout
result = [line.replace('==>', '').replace('default:', '').strip() for line in out.splitlines()]
return result
def vagrant_snapshot(name, box='default', **kwargs):
"""Creates a new snapshot, if name does not already exist
Checks to see if the snapshot name for the box already exists?
- if no snapshot name exists
- create a new snapshot called {name} for {box}
- if snapshot name exists
- do nothing
"""
if has_snapshot(box=box, name=name, **kwargs) is False:
return run(
'vagrant snapshot save {box} {snapshot}'.format(box=box, snapshot=name),
hide=True,
**kwargs
)
def vagrant_ssh_config(box='default', filename=None, *args, **kwargs):
"""Returns the ssh-config for box
"""
args = ' '.join(args)
if is_active(box=box) is False:
return None
# otherwise the box is active so we can get at the ssh config
cmd = 'vagrant ssh-config {box} {args}'.format(box=box, args=args)
result = run(cmd, **kwargs)
assert result.return_code == 0, 'Expected command: `{}` to return exit code: 0 got: {}'.format(cmd, result.return_code)
ssh_config = result.stdout.decode('utf-8').strip() or result.stderr.decode('utf-8').strip()
if filename:
with io.open(filename, encoding='utf-8', mode='w') as fd:
fd.write(ssh_config)
fd.write('\n')
return ssh_config
def run_converger(box='default', **kwargs):
"""Converge the vagrant environment
"""
# remove converged state file
rm_f_converged_state(box=box)
result = run(
'ansible-playbook converge.yml -vv -l {box}'.format(box=box),
**kwargs
)
# create the converged state file
touch_converged_state(box=box)
return result
def run_verifier(*args, **kwargs):
"""Runs the verifier
"""
default_args = (
'tox',
)
cmd = default_args + args
return run(' '.join(cmd), **kwargs)
def has_snapshot(name, box='default', **kwargs):
"""Returns `True` if snapshot name exists, otherwise returns `False`
"""
# we remove pty becuase we don't want the color codes mixed into the output
kwargs['pty'] = False
out = vagrant_snapshot_list(name=name, box=box, **kwargs)
return name in out
def is_built(box):
"""Returns `True` if box is already built, otherwise returns `False`
"""
return os.path.isfile('.{box}.built'.format(box=box))
def touch_built_state(box):
"""Create the box built state file
"""
filename = os.path.join(PROJECT_ROOT_DIR, '.{box}.built'.format(box=box))
return touch(filename)
def rm_f_built_state(box):
"""Remove the box built state file
"""
filename = os.path.join(PROJECT_ROOT_DIR, '.{box}.built'.format(box=box))
return rm_f(filename)
def is_converged(box):
"""Returns `True` if box is already converged, otherwise returns `False`
"""
return os.path.isfile('.{box}.converged'.format(box=box))
def touch_converged_state(box):
"""Create the box converged state file
"""
filename = os.path.join(PROJECT_ROOT_DIR, '.{box}.converged'.format(box=box))
return touch(filename)
def rm_f_converged_state(box):
"""Remove the box converged state file
"""
filename = os.path.join(PROJECT_ROOT_DIR, '.{box}.converged'.format(box=box))
return rm_f(filename)
def is_active(box, provider='virtualbox', **kwargs):
"""Returns `True` if box is active, otherwise returns `False`
"""
off_states = (
'aborted',
'frozen',
'not created',
'poweroff',
'shutoff',
'paused',
'saved',
)
for env, status, provider in vagrant_status():
if provider == provider and env == box and status in off_states:
return False
return True
return False
def get_vagrant_suts():
"""Return a `generator` of defined vagrant test environments
"""
for sut, status, provider in vagrant_status():
yield dict(sut=sut,
status=status,
provider=provider,
built=is_built(box=sut),
converged=is_converged(box=sut),
)
@task
def clean(c, echo=False):
"""Cleans all compiled artifacts recursively
"""
file_patterns = [
'*.pyc',
'*.retry',
'.*.built',
'.*.converged',
'.coverage',
]
dir_patterns = [
'build',
'dist',
'*.egg-info',
'.tox',
'__pycache__',
'tmp',
]
for pattern in file_patterns:
c.run('find . -name "{}" -type f | xargs rm -f'.format(pattern), echo=echo)
for pattern in dir_patterns:
c.run('find . -name "{}" -type d | xargs rm -rf'.format(pattern), echo=echo)
@task
def coverage(c, extra=None, pty=True):
"""Run code coverage
"""
if os.path.isfile('.coverage'):
os.unlink('.coverage')
default_args = [
'coverage',
'run',
'-m',
'pytest',
'-v',
'-rs',
'-s',
]
cmd = ' '.join(default_args)
if extra:
cmd += ' -- {}'.format(extra)
c.run(cmd, pty=pty)
c.run('coverage report -m', pty=pty)
@task
def prep(c, pty=True, box='default', skip_if_built=False):
"""Create the VM, Power it on, run the prep provisioner
"""
snapshot = 'prep'
if skip_if_built and is_built(box=box):
return None
vagrant_up(box=box, pty=pty)
vagrant_provision(box=box, pty=pty)
vagrant_snapshot(name=snapshot, box=box, pty=pty)
@task
def list(c, pty=True):
"""Lists test environments
"""
envs = [x for x in get_vagrant_suts()]
sut_json = json.dumps(envs, sort_keys=True, indent=4, ensure_ascii=False)
print(sut_json)
@task
def converge(c, box='default', pty=True, skip_if_built=True, skip_if_converged=False):
"""Runs the custom code that puts the VM into the desired state
"""
snapshot = 'converged'
if skip_if_converged and is_converged(box=box):
return None
prep(c, box=box, pty=pty, skip_if_built=skip_if_built)
run_converger(box=box, pty=pty)
vagrant_snapshot(name=snapshot, box=box, pty=pty)
@task
def test(c, box='default', pty=True, skip_if_built=True, skip_if_converged=True, verify=True, destroy_box=False, tox_args=[]):
"""Starts the VM if not running, converge, runs tests, shuts the VM down
"""
prep(c, box=box, pty=pty, skip_if_built=skip_if_built)
converge(c, box=box, pty=pty, skip_if_built=skip_if_built, skip_if_converged=skip_if_converged)
if verify:
run_verifier(*tox_args, pty=pty)
if destroy_box:
destroy(c, box=box, pty=pty)
@task
def destroy(c, box='default', pty=True):
"""Destroys a vagrant box
"""
rm_f_built_state(box=box)
rm_f_converged_state(box=box)
vagrant_destroy(box=box, pty=pty)
@task
def ssh_config(c, box='default', filename=None, pty=True):
"""Returns the ssh-config for the box if it is running
"""
vagrant_ssh_config(box=box, filename=filename, pty=pty)
@task
def ansible_inventory(c, box='default', pty=True):
"""Creates the inventory file used for ansible
"""
filename = os.path.join(PROJECT_ROOT_DIR, 'inventory')
ssh_config = vagrant_ssh_config(box=box, filename='ssh-config.{}'.format(box), hide=True)
ssh_config = dict([re.sub('\s\s+', '', x).split(' ', 1) for x in ssh_config.splitlines()])
inventory = (
'{box} '
'ansible_host={ansible_host} '
'ansible_port={ansible_port} '
'ansible_user={ansible_user} '
'ansible_ssh_private_key_file={ansible_ssh_private_key_file}'
).format(
box=box,
ansible_host=ssh_config['HostName'],
ansible_port=ssh_config['Port'],
ansible_user=ssh_config['User'],
ansible_ssh_private_key_file=ssh_config['IdentityFile']
)
with io.open(filename, encoding='utf-8', mode='w') as fd:
fd.write(inventory)
fd.write("\n")