Skip to content

Commit

Permalink
support for starting multiple container on docker side
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-20 committed Apr 8, 2022
1 parent 3c94f43 commit 53a4000
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 62 deletions.
17 changes: 10 additions & 7 deletions docker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ def is_invalid_status(status: str) -> bool:

@app.post('/start')
async def start_container(num_experiments: int, config: Request) -> JSONResponse:
container_info = manager.start(config=await config.json())
for _ in range(num_experiments):
container_info += manager.start(config=await config.json())
if (is_invalid_status(container_info.status) or container_info.data is False):
return JSONResponse(status_code=404, content=vars(container_info))
else:
return JSONResponse(vars(container_info))
final_infos = []
for number in range(num_experiments):
final_infos += [manager.start(config=await config.json())]
if (is_invalid_status(final_infos[number].status) or final_infos[number].data is False):
return JSONResponse(status_code=404, content=vars(final_infos[0]))
return_dict = {}
for index in range(num_experiments):
return_dict[index] = vars(final_infos[index])
print(f'successfully started {num_experiments} container')
return JSONResponse(return_dict)


@app.get('/health/')
Expand Down
20 changes: 3 additions & 17 deletions docker/docker_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@ def __init__(self, id: str, status: str, data=None, stream: GeneratorType = None
self.data = data
self.stream = stream

def __add__(self, other_docker_info):
resulting_docker_info = DockerInfo(id='not relevant', status='not relavant')
resulting_docker_info.id = self._add_to_list(self.id, other_docker_info.id)
resulting_docker_info.status = self._add_to_list(self.status, other_docker_info.status)
resulting_docker_info.data = self._add_to_list(self.data, other_docker_info.data)
resulting_docker_info.stream = self._add_to_list(self.stream, other_docker_info.stream)
return resulting_docker_info

def _add_to_list(self, var1, var2) -> list:
# var1 could be list
if type(var1) == list:
return var1 + [var2]
else:
return [var1, var2]


class DockerManager():
"""
Expand Down Expand Up @@ -78,15 +63,16 @@ def __new__(cls):
cls._update_port_mapping()
return cls._instance

def start(self, config: dict) -> DockerInfo:
def start(self, config: dict, count: int) -> DockerInfo:
"""
To be called by the REST API. Create and start a new docker container from the image of the specified command.
Args:
config (dict): The combined hyperparameter_config and environment_config_command dictionaries that should be sent to the container.
count (int): number of containers that should be started
Returns:
DockerInfo: A JSON serializable object containing the id and the status of the new container.
DockerInfo: A JSON serializable object containing the id and the status of the new container(s).
"""
if 'hyperparameter' not in config:
return DockerInfo(id='No container was started', status='The config is missing the "hyperparameter"-field')
Expand Down
38 changes: 0 additions & 38 deletions docker/test_docker_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,41 +94,3 @@ def test_port_mapping_initialization():
def test_allowed_commands_is_up_to_date():
assert set(manager._allowed_commands) == {'training', 'exampleprinter', 'agent_monitoring'}, \
'The set of allowed commands has changed, please update this and all the other tests!'


def test_docker_info_add():
docker_info1 = docker_manager.DockerInfo(id='test1', status='healthy', data='some test data', stream=None)
docker_info2 = docker_manager.DockerInfo(id='test2', status='not so healthy', data=False, stream=None)

expected_docker_info = docker_manager.DockerInfo(id='not relevant', status='not relavant')
expected_docker_info.id = ['test1', 'test2']
expected_docker_info.status = ['healthy', 'not so healthy']
expected_docker_info.data = ['some test data', False]
expected_docker_info.stream = [None, None]

resulting_docker_info = docker_info1 + docker_info2
assert expected_docker_info.id == resulting_docker_info.id
assert expected_docker_info.status == resulting_docker_info.status
assert expected_docker_info.data == resulting_docker_info.data
assert expected_docker_info.stream == resulting_docker_info.stream


def test_docker_info_add_multiple():
docker_info1 = docker_manager.DockerInfo(id='test1', status='healthy', data='some test data', stream=None)
docker_info2 = docker_manager.DockerInfo(id='test2', status='not so healthy', data=False, stream=None)
docker_info3 = docker_manager.DockerInfo(id='test3', status='so lala', data=42, stream=None)

resulting_docker_info = docker_info1
for docker_info in [docker_info2, docker_info3]:
resulting_docker_info += docker_info

expected_docker_info = docker_manager.DockerInfo(id='not relevant', status='not relavant')
expected_docker_info.id = ['test1', 'test2', 'test3']
expected_docker_info.status = ['healthy', 'not so healthy', 'so lala']
expected_docker_info.data = ['some test data', False, 42]
expected_docker_info.stream = [None, None, None]

assert expected_docker_info.id == resulting_docker_info.id
assert expected_docker_info.status == resulting_docker_info.status
assert expected_docker_info.data == resulting_docker_info.data
assert expected_docker_info.stream == resulting_docker_info.stream

0 comments on commit 53a4000

Please sign in to comment.