Skip to content

Commit

Permalink
isort, black and ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
winstonsmith1897 committed Aug 21, 2023
1 parent f3bb5ef commit 96e7816
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 100 deletions.
98 changes: 63 additions & 35 deletions app_dht.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import docker

import websocket

import json

import docker
import requests


client = docker.from_env()

api_url = "http://localhost:3000/"


def publish(ws, topic_uuid, requestor_id, request_id, containers):
ws_req = {
"RequestPostTopicUUID": {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:container_list",
"topic_uuid": topic_uuid,
"value": {
"requestor_id": requestor_id,
"request_id": request_id,
"containers": containers
}
}
"containers": containers,
},
}
}
ws.send(json.dumps(ws_req))
print("[!] Container List has been updated")
return


def request_list(ws, message, image_name):
json_message = json.loads(message)
# Handle messages
Expand All @@ -41,8 +38,7 @@ 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("Active Containers: " + str(containers))


def update_dht_list(ws, image_name):
Expand All @@ -58,31 +54,46 @@ def update_dht_list(ws, 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):
if image_name:
try:
client.images.pull(image_name)
update_dht_list(ws,image_name)
update_dht_list(ws, image_name)
topic_name = "SIFIS:application_manager_pull_image"
pulling_data = {
"requestor_id": requestor_id,
"request_id": request_id,
"pulled_image": image_name,
"operation" : "pull image",
"result" : "successfull",
"pulled_image": image_name,
"operation": "pull image",
"result": "successfull",
}
requests.post(api_url + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid, json=pulling_data)
requests.post(
api_url
+ "topic_name/"
+ topic_name
+ "/topic_uuid/"
+ topic_uuid,
json=pulling_data,
)
return f"Image {image_name} pulled successfully!"
except docker.errors.APIError as e:
pulling_data = {
"requestor_id": requestor_id,
"request_id": request_id,
"pulled_image": image_name,
"operation" : "pull image",
"result" : "Error while pulling image {image_name}: {e}",
"pulled_image": image_name,
"operation": "pull image",
"result": "Error while pulling image {image_name}: {e}",
}
requests.post(api_url + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid, json=pulling_data)
requests.post(
api_url
+ "topic_name/"
+ topic_name
+ "/topic_uuid/"
+ topic_uuid,
json=pulling_data,
)
return f"Error while pulling image {image_name}: {e}", 500
else:
return "Missing 'image_name' parameter", 400
Expand Down Expand Up @@ -113,11 +124,18 @@ def stop_container(container_id, topic_uuid, request_id, requestor_id):
container_info = {
"requestor_id": requestor_id,
"request_id": request_id,
"container_id": container_id,
"operation" : "stop container",
"result" : "successfull",
"container_id": container_id,
"operation": "stop container",
"result": "successfull",
}
requests.post(api_url + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid, json=container_info)
requests.post(
api_url
+ "topic_name/"
+ topic_name
+ "/topic_uuid/"
+ topic_uuid,
json=container_info,
)
return f"Container {container_id} stopped successfully!"
except docker.errors.NotFound as e:
return f"Container {container_id} not found: {e}", 404
Expand Down Expand Up @@ -151,11 +169,14 @@ def remove_image(image_name, topic_uuid, request_id, requestor_id):
removing_info = {
"requestor_id": requestor_id,
"request_id": request_id,
"image": image_name,
"operation" : "remove image",
"result" : "successfull",
"image": image_name,
"operation": "remove image",
"result": "successfull",
}
requests.post(api_url + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid, json=removing_info)
requests.post(
api_url + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid,
json=removing_info,
)
list_containers(topic_uuid, requestor_id, request_id, image_name)
return f"Image {image_name} removed successfully!"
except docker.errors.ImageNotFound as e:
Expand All @@ -168,7 +189,7 @@ def list_containers(topic_uuid, requestor_id, request_id, image_name):
topic_name = "SIFIS:container_list"
response = requests.get(api_url + "topic_name/" + topic_name)
message = str(response.json()[0])
message = message.replace("'", '"')
message = message.replace("'", '"')
if "value" in str(message):
json_message = json.loads(message)
topic_value = json_message["value"]
Expand All @@ -178,10 +199,17 @@ def list_containers(topic_uuid, requestor_id, request_id, image_name):
_list = {
"requestor_id": requestor_id,
"request_id": request_id,
"containers": containers
}
local_response = requests.post(api_url + "topic_name/" + topic_name + "/topic_uuid/" + topic_uuid, json=_list)
"containers": containers,
}
local_response = requests.post(
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')
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
37 changes: 16 additions & 21 deletions trials/list_applications.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@

import websocket
import time
import json
import os
import subprocess
import _thread

import rel
import argparse
from argparse import ArgumentParser
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 = 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": {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:app_manager",
"topic_uuid": "application_manager_uuid",
"value": {
"operation": "list_containers",
"requestor_id": "1",
"request_id": "1",
}
}
},
}
}
ws.send(json.dumps(ws_req))

publish()



publish()
38 changes: 16 additions & 22 deletions trials/pull_image.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@


import websocket
import time
import json
import os
import subprocess
import _thread

import rel
import argparse
from argparse import ArgumentParser
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 = 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": {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:app_manager",
"topic_uuid": "application_manager_uuid",
"value": {
"operation": "pull_image",
"requestor_id": "1",
"request_id": "1",
"image_name": "ghcr.io/sifis-home/application-manager:latest",
}
}
},
}
}
ws.send(json.dumps(ws_req))

publish()


publish()
38 changes: 16 additions & 22 deletions trials/remove_image.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@


import websocket
import time
import json
import os
import subprocess
import _thread

import rel
import argparse
from argparse import ArgumentParser
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 = 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": {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:app_manager",
"topic_uuid": "application_manager_uuid",
"value": {
"operation": "remove_image",
"requestor_id": "1",
"request_id": "1",
"image_name": "ghcr.io/sifis-home/application-manager:latest",
}
}
},
}
}
ws.send(json.dumps(ws_req))

publish()


publish()

0 comments on commit 96e7816

Please sign in to comment.