201 lines
6.3 KiB
Python
201 lines
6.3 KiB
Python
from database import Session
|
|
from models.models import Client, ClientGroup
|
|
from flask import Blueprint, request, jsonify
|
|
import sys
|
|
sys.path.append('/workspace')
|
|
|
|
clients_bp = Blueprint("clients", __name__, url_prefix="/api/clients")
|
|
|
|
|
|
@clients_bp.route("/without_description", methods=["GET"])
|
|
def get_clients_without_description():
|
|
session = Session()
|
|
clients = session.query(Client).filter(
|
|
(Client.description == None) | (Client.description == "")
|
|
).all()
|
|
result = [
|
|
{
|
|
"uuid": c.uuid,
|
|
"hardware_token": c.hardware_token,
|
|
"ip": c.ip,
|
|
"type": c.type,
|
|
"hostname": c.hostname,
|
|
"os_version": c.os_version,
|
|
"software_version": c.software_version,
|
|
"macs": c.macs,
|
|
"model": c.model,
|
|
"registration_time": c.registration_time.isoformat() if c.registration_time else None,
|
|
"last_alive": c.last_alive.isoformat() if c.last_alive else None,
|
|
"is_active": c.is_active,
|
|
"group_id": c.group_id,
|
|
}
|
|
for c in clients
|
|
]
|
|
session.close()
|
|
return jsonify(result)
|
|
|
|
|
|
@clients_bp.route("/<uuid>/description", methods=["PUT"])
|
|
def set_client_description(uuid):
|
|
data = request.get_json()
|
|
description = data.get("description", "").strip()
|
|
if not description:
|
|
return jsonify({"error": "Beschreibung darf nicht leer sein"}), 400
|
|
session = Session()
|
|
client = session.query(Client).filter_by(uuid=uuid).first()
|
|
if not client:
|
|
session.close()
|
|
return jsonify({"error": "Client nicht gefunden"}), 404
|
|
client.description = description
|
|
session.commit()
|
|
session.close()
|
|
return jsonify({"success": True})
|
|
|
|
|
|
@clients_bp.route("", methods=["GET"])
|
|
def get_clients():
|
|
session = Session()
|
|
clients = session.query(Client).all()
|
|
result = [
|
|
{
|
|
"uuid": c.uuid,
|
|
"hardware_token": c.hardware_token,
|
|
"ip": c.ip,
|
|
"type": c.type,
|
|
"hostname": c.hostname,
|
|
"os_version": c.os_version,
|
|
"software_version": c.software_version,
|
|
"macs": c.macs,
|
|
"model": c.model,
|
|
"description": c.description,
|
|
"registration_time": c.registration_time.isoformat() if c.registration_time else None,
|
|
"last_alive": c.last_alive.isoformat() if c.last_alive else None,
|
|
"is_active": c.is_active,
|
|
"group_id": c.group_id,
|
|
}
|
|
for c in clients
|
|
]
|
|
session.close()
|
|
return jsonify(result)
|
|
|
|
|
|
@clients_bp.route("/group", methods=["PUT"])
|
|
def update_clients_group():
|
|
data = request.get_json()
|
|
client_ids = data.get("client_ids", [])
|
|
group_name = data.get("group_name")
|
|
session = Session()
|
|
group = session.query(ClientGroup).filter_by(name=group_name).first()
|
|
if not group:
|
|
session.close()
|
|
return jsonify({"error": "Gruppe nicht gefunden"}), 404
|
|
session.query(Client).filter(Client.uuid.in_(client_ids)).update(
|
|
{Client.group_id: group.id}, synchronize_session=False
|
|
)
|
|
session.commit()
|
|
session.close()
|
|
return jsonify({"success": True})
|
|
|
|
|
|
@clients_bp.route("/<uuid>", methods=["PATCH"])
|
|
def update_client(uuid):
|
|
data = request.get_json()
|
|
session = Session()
|
|
client = session.query(Client).filter_by(uuid=uuid).first()
|
|
if not client:
|
|
session.close()
|
|
return jsonify({"error": "Client nicht gefunden"}), 404
|
|
allowed_fields = ["description", "model"]
|
|
updated = False
|
|
for field in allowed_fields:
|
|
if field in data:
|
|
setattr(client, field, data[field])
|
|
updated = True
|
|
if updated:
|
|
session.commit()
|
|
result = {"success": True}
|
|
else:
|
|
result = {"error": "Keine gültigen Felder zum Aktualisieren übergeben"}
|
|
session.close()
|
|
return jsonify(result)
|
|
|
|
|
|
# Neue Route: Liefert die aktuelle group_id für einen Client
|
|
@clients_bp.route("/<uuid>/group", methods=["GET"])
|
|
def get_client_group(uuid):
|
|
session = Session()
|
|
client = session.query(Client).filter_by(uuid=uuid).first()
|
|
if not client:
|
|
session.close()
|
|
return jsonify({"error": "Client nicht gefunden"}), 404
|
|
group_id = client.group_id
|
|
session.close()
|
|
return jsonify({"group_id": group_id})
|
|
|
|
# Neue Route: Liefert alle Clients mit Alive-Status
|
|
|
|
|
|
@clients_bp.route("/with_alive_status", methods=["GET"])
|
|
def get_clients_with_alive_status():
|
|
session = Session()
|
|
clients = session.query(Client).all()
|
|
result = []
|
|
for c in clients:
|
|
result.append({
|
|
"uuid": c.uuid,
|
|
"description": c.description,
|
|
"ip": c.ip,
|
|
"last_alive": c.last_alive.isoformat() if c.last_alive else None,
|
|
"is_active": c.is_active,
|
|
"is_alive": bool(c.last_alive and c.is_active),
|
|
})
|
|
session.close()
|
|
return jsonify(result)
|
|
|
|
|
|
@clients_bp.route("/<uuid>/restart", methods=["POST"])
|
|
def restart_client(uuid):
|
|
"""
|
|
Route to restart a specific client by UUID.
|
|
Sends an MQTT message to the broker to trigger the restart.
|
|
"""
|
|
import paho.mqtt.client as mqtt
|
|
import json
|
|
|
|
# MQTT broker configuration
|
|
MQTT_BROKER = "mqtt"
|
|
MQTT_PORT = 1883
|
|
MQTT_TOPIC = f"clients/{uuid}/restart"
|
|
|
|
# Connect to the database to check if the client exists
|
|
session = Session()
|
|
client = session.query(Client).filter_by(uuid=uuid).first()
|
|
if not client:
|
|
session.close()
|
|
return jsonify({"error": "Client nicht gefunden"}), 404
|
|
session.close()
|
|
|
|
# Send MQTT message
|
|
try:
|
|
mqtt_client = mqtt.Client()
|
|
mqtt_client.connect(MQTT_BROKER, MQTT_PORT)
|
|
payload = {"action": "restart"}
|
|
mqtt_client.publish(MQTT_TOPIC, json.dumps(payload))
|
|
mqtt_client.disconnect()
|
|
return jsonify({"success": True, "message": f"Restart signal sent to client {uuid}"}), 200
|
|
except Exception as e:
|
|
return jsonify({"error": f"Failed to send MQTT message: {str(e)}"}), 500
|
|
|
|
|
|
@clients_bp.route("/<uuid>", methods=["DELETE"])
|
|
def delete_client(uuid):
|
|
session = Session()
|
|
client = session.query(Client).filter_by(uuid=uuid).first()
|
|
if not client:
|
|
session.close()
|
|
return jsonify({"error": "Client nicht gefunden"}), 404
|
|
session.delete(client)
|
|
session.commit()
|
|
session.close()
|
|
return jsonify({"success": True})
|