-
Notifications
You must be signed in to change notification settings - Fork 1
/
DockerManager.py
153 lines (127 loc) · 5.27 KB
/
DockerManager.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
import docker
import os
class DockerManager(object):
_simulation_server_command = 'python /Simulation/SimulationServer.py'
_optimization_server_command = 'python /Optimization/OptimizationManager.py'
def __init__(self, configuration):
self.configuration = configuration
self.client = docker.from_env()
self.optimization_containers = []
self.simulation_containers = []
self.optimization_image = self.configuration['OPTIMIZATION_IMAGE']
self.simulation_image = self.configuration['SIMULATION_IMAGE']
self.volumes = {
os.path.realpath(self.configuration['HOST_TEMP_FOLDER']): {'bind': self.configuration['DOCKER_TEMP_FOLDER'], 'mode': 'rw'},
os.path.realpath('./Optimization'): {'bind': '/Optimization', 'mode': 'rw'},
os.path.realpath('./Simulation'): {'bind': '/Simulation', 'mode': 'rw'}
}
def get_containers(self, container_type, status='running'):
if container_type == 'optimization':
image = self.optimization_image
elif container_type == 'simulation':
image = self.simulation_image
else:
print('Invald container type - {}'.format(container_type))
return []
containers = self.client.containers.list(
filters={
'ancestor': image,
'status': status
}
)
return containers
def count_simulation_containers(self, status='running'):
containers = self.get_containers('simulation', status)
return len(containers)
def count_optimization_containers(self, status='running'):
containers = self.get_containers('optimization', status)
return len(containers)
def run_optimization_container(self, number):
for _ in range(number):
container = self.client.containers.run(
self.optimization_image,
command=self._optimization_server_command,
environment=self.configuration,
volumes=self.volumes,
detach=True
)
self.optimization_containers.append(container)
return
def run_simulation_container(self, number):
for _ in range(number):
container = self.client.containers.run(
self.simulation_image,
command=self._simulation_server_command,
environment=self.configuration,
volumes=self.volumes,
detach=True
)
self.simulation_containers.append(container)
def stop_all_simulation_containers(self, remove=True):
containers = self.get_containers('simulation', 'running')
print('Total {} Simulation containers running'.format(len(containers)))
for container in containers:
self.stop_container(container)
if remove:
self.remove_container(container)
return
def stop_all_optimization_containers(self, remove=True):
containers = self.get_containers('optimization', 'running')
print('Total {} Optimization containers running'.format(len(containers)))
for container in containers:
self.stop_container(container)
if remove:
self.remove_container(container)
return
def stop_own_simulation_containers(self, remove=True):
containers = self.simulation_containers
print('{} own Simulation containers running'.format(len(containers)))
for container in containers:
self.stop_container(container)
if remove:
self.remove_container(container)
return
def stop_own_optimization_containers(self, remove=True):
containers = self.optimization_containers
print('{} own Optimization containers running'.format(len(containers)))
for container in containers:
self.stop_container(container)
if remove:
self.remove_container(container)
return
def remove_exited_containers(self):
containers = self.client.containers.list(
filters={
'status': 'exited'
}
)
for container in containers:
container.remove()
def clean(self):
self.stop_own_optimization_containers(remove=True)
self.stop_own_simulation_containers(remove=True)
self.optimization_containers = []
self.simulation_containers = []
def clean_all(self):
self.stop_all_optimization_containers(remove=True)
self.stop_all_simulation_containers(remove=True)
self.optimization_containers = []
self.simulation_containers = []
@staticmethod
def stop_container(container):
try:
container.stop()
print('Stoped container {}'.format(container.id))
except:
print('Could not stop container {}'.format(container.id))
pass
return
@staticmethod
def remove_container(container):
try:
container.remove()
print('Removed container {}'.format(container.id))
except:
print('Could not remove container {}'.format(container.id))
pass
return