120 lines
5.2 KiB
Python
120 lines
5.2 KiB
Python
# websocket_server/events/docker.py
|
|
|
|
from flask_socketio import SocketIO
|
|
from datetime import datetime
|
|
import logging
|
|
import requests
|
|
import json
|
|
|
|
from websocket_server.events import base
|
|
|
|
logger = logging.getLogger("websocket_server")
|
|
socketio = base.socketio
|
|
|
|
api_server_url = "http://127.0.0.1:5000/api"
|
|
|
|
def register_socketio_handlers(socketio):
|
|
|
|
|
|
@socketio.on("docker_event")
|
|
def handle_docker_event(data):
|
|
"""
|
|
Handle Docker events received from the worker.
|
|
Extract relevant data and log it to the console.
|
|
"""
|
|
try:
|
|
# Extract relevant data from the event
|
|
worker_id = data.get("worker_id")
|
|
event_type = data.get("type")
|
|
details = data.get("details", {})
|
|
|
|
container_id = details.get("container_id")
|
|
system_container_id = details.get("system_container_id")
|
|
container_name = details.get("container_name")
|
|
unix_timestamp = details.get("timestamp")
|
|
timestamp=datetime.utcfromtimestamp(unix_timestamp).isoformat()
|
|
status = details.get("details", {}).get("status")
|
|
image = details.get("details", {}).get("from")
|
|
action = details.get("details", {}).get("Action")
|
|
|
|
# Log the extracted data
|
|
logger.info(f"Received Docker event from worker {worker_id}:")
|
|
logger.info(f" Event Type: {event_type}")
|
|
logger.info(f" Docker Container ID: {container_id}")
|
|
logger.info(f" System Container ID: {system_container_id}")
|
|
logger.info(f" Container Name: {container_name}")
|
|
logger.info(f" Timestamp: {timestamp}")
|
|
logger.info(f" Status: {status}")
|
|
logger.info(f" Image: {image}")
|
|
logger.info(f" Action: {action}")
|
|
|
|
# Optionally, log the full details for debugging purposes
|
|
logger.debug(f"Full event details: {data}")
|
|
|
|
if event_type=="docker_start":
|
|
#Send the update to the API Server
|
|
# system_container_id
|
|
# worker_id
|
|
# state="started"
|
|
# timestamp
|
|
action_URL=f"workloads/containers/status_update/{system_container_id}"
|
|
logger.debug(f"Sending task update payload to API server for Container ID {system_container_id}")
|
|
|
|
payload = {
|
|
"new_status": "running",
|
|
"system_container_id": system_container_id,
|
|
"worker_id": worker_id,
|
|
"timestamp": timestamp
|
|
}
|
|
|
|
headers = {"Content-Type": "application/json"}
|
|
websocket_server_response = requests.put(f"{api_server_url}/{action_URL}", data=json.dumps(payload), headers=headers)
|
|
logger.debug( websocket_server_response)
|
|
logger.info("Update complete")
|
|
|
|
if event_type=="docker_destroy":
|
|
#Send the update to the API Server
|
|
# system_container_id
|
|
# worker_id
|
|
# state="started"
|
|
# timestamp
|
|
action_URL=f"workloads/containers/status_update/{system_container_id}"
|
|
logger.debug(f"Sending task update payload to API server for Container ID {system_container_id}")
|
|
|
|
payload = {
|
|
"new_status": "deleted",
|
|
"system_container_id": system_container_id,
|
|
"worker_id": worker_id,
|
|
"timestamp": timestamp
|
|
}
|
|
|
|
headers = {"Content-Type": "application/json"}
|
|
websocket_server_response = requests.put(f"{api_server_url}/{action_URL}", data=json.dumps(payload), headers=headers)
|
|
logger.debug( websocket_server_response)
|
|
logger.info("Update complete")
|
|
|
|
if event_type=="docker_die":
|
|
#Send the update to the API Server
|
|
# system_container_id
|
|
# worker_id
|
|
# state="started"
|
|
# timestamp
|
|
action_URL=f"workloads/containers/status_update/{system_container_id}"
|
|
logger.debug(f"Sending task update payload to API server for Container ID {system_container_id}")
|
|
|
|
payload = {
|
|
"new_status": "dead",
|
|
"system_container_id": system_container_id,
|
|
"worker_id": worker_id,
|
|
"timestamp": timestamp
|
|
}
|
|
|
|
headers = {"Content-Type": "application/json"}
|
|
websocket_server_response = requests.put(f"{api_server_url}/{action_URL}", data=json.dumps(payload), headers=headers)
|
|
logger.debug( websocket_server_response)
|
|
logger.info("Update complete")
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error processing Docker event: {e}")
|
|
|