-
Notifications
You must be signed in to change notification settings - Fork 6
/
cleanup.py
75 lines (61 loc) · 2.8 KB
/
cleanup.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
"""
cleanup docker container/images and mock root for smoke testing
"""
import logging
import subprocess
import re
log = logging.getLogger('avocado.test')
def cleanup_docker_and_mock(mockcfg, img_name):
# Clean-up old test artifacts (docker containers, image, mock root)
docker_containerlist_cmdline = 'docker ps --filter=ancestor=%s -a -q' % img_name
try:
containerlist = subprocess.check_output(docker_containerlist_cmdline,
stderr = subprocess.STDOUT, shell = True)
except subprocess.CalledProcessError as e:
log.error("command '%s' returned exit status %d; output:\n%s" %
(e.cmd, e.returncode, e.output))
raise
else:
log.info("docker container list with '%s' succeeded with output:\n%s" %
(docker_containerlist_cmdline, containerlist))
if containerlist:
containers = re.sub('[\r\n]+', ' ', containerlist)
log.info("docker containers using image %s need to be removed: %s\n" %
(img_name, containers));
docker_teardown_cmdline = 'docker rm -f %s' % containers
try:
docker_teardown_output = subprocess.check_output(docker_teardown_cmdline,
stderr = subprocess.STDOUT, shell = True)
except subprocess.CalledProcessError as e:
log.error("command '%s' returned exit status %d; output:\n%s" %
(e.cmd, e.returncode, e.output))
raise
else:
log.info("docker container teardown with '%s' succeeded with output:\n%s" %
(docker_teardown_cmdline, docker_teardown_output))
else:
log.info("no docker containers are using image %s\n" % img_name)
docker_teardown_cmdline = 'docker rmi %s' % img_name
try:
docker_teardown_output = subprocess.check_output(docker_teardown_cmdline,
stderr = subprocess.STDOUT, shell = True)
except subprocess.CalledProcessError as e:
if "No such image" not in e.output:
log.error("command '%s' returned exit status %d; output:\n%s" %
(e.cmd, e.returncode, e.output))
raise
else:
log.info("No existing docker image named %s" % img_name)
else:
log.info("docker teardown with '%s' succeeded with output:\n%s" %
(docker_teardown_cmdline, docker_teardown_output))
mock_teardown_cmdline = ['mock', '-r', mockcfg, '--scrub=all']
try:
mock_teardown_output = subprocess.check_output(mock_teardown_cmdline,
stderr = subprocess.STDOUT)
except subprocess.CalledProcessError as e:
log.error("command '%s' returned exit status %d; output:\n%s" %
(e.cmd, e.returncode, e.output))
raise
log.info("mock teardown with '%s' succeeded with output:\n%s" %
(mock_teardown_cmdline, mock_teardown_output))