-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocker_py_wrapper.py
113 lines (94 loc) · 4.07 KB
/
docker_py_wrapper.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
import logging
import docker
class DockerPyWrapper:
RECOGNISED_FIXTURE = 'dockerpy'
def __init__(self):
self._client = docker.from_env()
def ls_images(self):
image_tags = []
for image in self._client.images.list():
for tag in image.tags:
image_tags.append(tag)
return image_tags
def rm_images(self, images_to_rm):
for image_to_rm in images_to_rm:
self._rm_image(image_to_rm)
def _rm_image(self, image_to_rm):
if image_to_rm in self.ls_images():
logging.debug(image_to_rm)
self._client.images.remove(image_to_rm, force=True)
def pull(self, images_to_pull):
for image_to_pull in images_to_pull:
if image_to_pull not in self.ls_images():
logging.debug(image_to_pull)
try:
self._client.images.pull(image_to_pull)
except docker.errors.ImageNotFound as exception:
logging.error(str(exception))
return False
else:
logging.debug('skipped: %s', image_to_pull)
return True
def ls_containers(self, images_from_config):
containers = []
for container in self._client.containers.list():
for tag in container.image.tags:
for image_from_config in images_from_config:
if image_from_config == tag:
containers.append({'image': tag,
'id': container.id,
'container': container})
return containers
def start_containers(self, config):
self.rm_containers(config.images())
for container in config.containers():
self._start_container(config, container)
def _start_container(self, config, container):
try:
logging.debug(container['image'])
try:
self._start_network(config, container['network'])
except KeyError:
pass
detached_container = self._client.containers.run(container['image'],
** config.container_kwargs(container),
detach=True)
logging.debug(detached_container.attrs['Id'])
except KeyError:
logging.error('missing: image')
def rm_containers(self, containers_to_stop):
for docker_container in self.ls_containers(containers_to_stop):
for container_to_stop in containers_to_stop:
if docker_container['image'] == container_to_stop:
self._rm_container_artefacts(docker_container)
self._client.containers.prune()
self._client.networks.prune()
def _rm_container_artefacts(self, docker_container):
logging.debug(docker_container['id'])
try:
docker_container['container'].stop(timeout=1)
docker_container['container'].remove(force=True)
except docker.errors.NotFound:
pass
self._client.volumes.prune()
def ls_networks(self, config_networks):
networks = []
for network in self._client.networks.list():
for config_network in config_networks:
if config_network == network.name:
logging.debug(network.name)
networks.append(network.name)
return networks
def _start_network(self, config, network_to_start):
if network_to_start not in self.ls_networks(config.networks()):
logging.debug('%s', network_to_start)
self._client.networks.create(network_to_start)
def ls_volumes(self, config_volumes):
self._client.volumes.prune()
volumes = []
for volume in self._client.volumes.list():
for config_volume in config_volumes:
if volume.name in config_volume:
logging.debug(config_volume)
volumes.append(config_volume)
return sorted(volumes)