109 lines
4.2 KiB
Python
109 lines
4.2 KiB
Python
# websocket_server/events/libvirt.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://172.17.0.1:5000/api"
|
|
|
|
def register_socketio_handlers(socketio):
|
|
|
|
@socketio.on("libvirt_event")
|
|
def handle_libvirt_event(data):
|
|
"""
|
|
Handle libvirt 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", {})
|
|
|
|
libvirt_vm_id = details.get("libvirt_vm_id")
|
|
system_vm_id = details.get("system_vm_id")
|
|
unix_timestamp = details.get("timestamp")
|
|
timestamp=datetime.utcfromtimestamp(unix_timestamp).isoformat()
|
|
|
|
|
|
# Log the extracted data
|
|
logger.info(f"Received libvirt event from worker {worker_id}:")
|
|
logger.info(f" Event Type: {event_type}")
|
|
logger.info(f" Libvirt VM ID: {libvirt_vm_id}")
|
|
logger.info(f" System VM ID: {system_vm_id}")
|
|
logger.info(f" Timestamp: {timestamp}")
|
|
|
|
# Optionally, log the full details for debugging purposes
|
|
logger.debug(f"Full event details: {data}")
|
|
|
|
if event_type=="libvirt_started":
|
|
#Send the update to the API Server
|
|
# system_vm_id
|
|
# worker_id
|
|
# state="started"
|
|
# timestamp
|
|
action_URL=f"workloads/virtual_machines/status_update/{system_vm_id}"
|
|
logger.debug(f"Sending task update payload to API server for VM ID {system_vm_id}")
|
|
|
|
payload = {
|
|
"new_status": "running",
|
|
"system_vm_id": system_vm_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.info("Status update sent to API server")
|
|
|
|
if event_type=="libvirt_stopped":
|
|
#Send the update to the API Server
|
|
# system_vm_id
|
|
# worker_id
|
|
# state="started"
|
|
# timestamp
|
|
action_URL=f"workloads/virtual_machines/status_update/{system_vm_id}"
|
|
logger.debug(f"Sending task update payload to API server for VM ID {system_vm_id}")
|
|
|
|
payload = {
|
|
"new_status": "stopped",
|
|
"system_vm_id": system_vm_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.info("Status update sent to API server")
|
|
|
|
|
|
if event_type=="libvirt_undefined":
|
|
#Send the update to the API Server
|
|
# system_vm_id
|
|
# worker_id
|
|
# state="started"
|
|
# timestamp
|
|
action_URL=f"workloads/virtual_machines/status_update/{system_vm_id}"
|
|
logger.debug(f"Sending task update payload to API server for VM ID {system_vm_id}")
|
|
|
|
payload = {
|
|
"new_status": "deleted",
|
|
"system_vm_id": system_vm_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.info("Status update sent to API server")
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error processing libvirt event: {e}")
|