From 42ab40d3e05b80a4dc13169141770c2416a27c6b Mon Sep 17 00:00:00 2001 From: winstonsmith1897 Date: Tue, 22 Aug 2023 12:55:06 +0200 Subject: [PATCH] Automate starting container --- application_manager/app_dht.py | 60 ++++++++++++++++++++++++++-------- trials/stop_container.py | 45 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 trials/stop_container.py diff --git a/application_manager/app_dht.py b/application_manager/app_dht.py index 00c9ab5..d1a7875 100644 --- a/application_manager/app_dht.py +++ b/application_manager/app_dht.py @@ -21,7 +21,7 @@ def publish(ws, topic_uuid, requestor_id, request_id, containers): } } ws.send(json.dumps(ws_req)) - print("[!] Container List has been updated") + print("\n[!] Container List has been updated") return @@ -38,22 +38,23 @@ def request_list(ws, message, image_name): containers = topic_value["containers"] containers.append(image_name) publish(ws, topic_uuid, requestor_id, request_id, containers) - print("Active Containers: " + str(containers)) + print("\n[!] Active Containers: " + str(containers) + "\n") def update_dht_list(ws, image_name): topic_name = "SIFIS:container_list" - response = requests.get(api_url + "topic_name/" + topic_name) - message = str(response.json()[0]) - message = message.replace("'", '"') - if message is None: + try: + response = requests.get(api_url + "topic_name/" + topic_name) + message = str(response.json()[0]) + message = message.replace("'", '"') + request_list(ws, message, image_name) + except IndexError: + print("\n[!] Update List, the list is empty") topic_uuid = "Pippo" request_id = "1" requestor_id = "1" containers = [image_name] publish(ws, topic_uuid, requestor_id, request_id, containers) - else: - request_list(ws, message, image_name) def pull_image(ws, image_name, topic_uuid, requestor_id, request_id): @@ -79,7 +80,10 @@ def pull_image(ws, image_name, topic_uuid, requestor_id, request_id): + topic_uuid, json=pulling_data, ) - return f"Image {image_name} pulled successfully!" + + print(f"[!] Image {image_name} pulled successfully!") + start_container(image_name, topic_uuid, requestor_id, request_id) + return "\n Pulling and Starting operation completed ..." except docker.errors.APIError as e: pulling_data = { "requestor_id": requestor_id, @@ -101,11 +105,40 @@ def pull_image(ws, image_name, topic_uuid, requestor_id, request_id): return "Missing 'image_name' parameter", 400 -def start_container(image_name): +def start_container(image_name, topic_uuid, requestor_id, request_id): if image_name: try: - container = client.containers.run(image_name, detach=True) - return f"Container {container.id} started successfully!" + print("\n[!] Starting: " + image_name) + topic_name = "SIFIS:application_manager_starting_image" + container = client.containers.run( + image_name, + detach=True, + network_mode="host", # --net=host + privileged=True, # --privileged + volumes={ + "/var/run/docker.sock": { + "bind": "/var/run/docker.sock", + "mode": "rw", + } + }, # volume + ) + print(f"\n[!] Container {container.id} started successfully!") + data = { + "requestor_id": requestor_id, + "request_id": request_id, + "image": image_name, + "operation": "starting image", + "container_id": container.id, + } + local_response = requests.post( + api_url + + "topic_name/" + + topic_name + + "/topic_uuid/" + + topic_uuid, + json=data, + ) + return except docker.errors.ImageNotFound as e: return f"Image {image_name} not found: {e}", 404 except docker.errors.APIError as e: @@ -207,11 +240,12 @@ def list_containers(topic_uuid, requestor_id, request_id, image_name): api_url + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid, json=_list, ) + """ remote_host = "https://yggio.sifis-home.eu:3000/dht-insecure/" yggio_response = requests.post( remote_host + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid, json=_list, verify=False, ) - print("YGGIO Response: " + str(yggio_response.content) + "\n\n") + """ return _list diff --git a/trials/stop_container.py b/trials/stop_container.py new file mode 100644 index 0000000..a8f0a1e --- /dev/null +++ b/trials/stop_container.py @@ -0,0 +1,45 @@ +import json + +import rel +import websocket + + +def on_error(ws, error): + print(error) + + +def on_close(ws, close_status_code, close_msg): + print("### Connection closed ###") + + +def on_open(ws): + print("### Connection established ###") + + +def publish(): + ws = websocket.WebSocketApp( + "ws://localhost:3000/ws", + on_open=on_open, + on_error=on_error, + on_close=on_close, + ) + + ws.run_forever(dispatcher=rel) # Set dispatcher to automatic reconnection + rel.signal(2, rel.abort) # Keyboard Interrupt + + ws_req = { + "RequestPostTopicUUID": { + "topic_name": "SIFIS:app_manager", + "topic_uuid": "application_manager_uuid", + "value": { + "operation": "stop_container", + "requestor_id": "1", + "request_id": "1", + "container_id": "5bf37840ff414d26598da5a9608047aefa53cae876b001728c4de27ec46e6342", + }, + } + } + ws.send(json.dumps(ws_req)) + + +publish()