96 lines
3.6 KiB
Python
96 lines
3.6 KiB
Python
"""
|
|
Flask API routes for command execution, device status, and auto-update
|
|
"""
|
|
|
|
from flask import Flask, request, jsonify
|
|
import logging
|
|
from commands_module import execute_system_command
|
|
from autoupdate_module import perform_auto_update
|
|
from logger_module import log_with_server, read_masa_name
|
|
import subprocess
|
|
|
|
|
|
def create_api_routes(app, hostname, device_ip, local_app_path, local_repo_path):
|
|
"""
|
|
Create and register API routes on the Flask app
|
|
|
|
Args:
|
|
app: Flask application instance
|
|
hostname: Device hostname
|
|
device_ip: Device IP
|
|
local_app_path: Path to local app.py file
|
|
local_repo_path: Path to local repository
|
|
"""
|
|
|
|
@app.route('/execute_command', methods=['POST'])
|
|
def handle_command_execution():
|
|
"""
|
|
Endpoint to receive and execute system commands
|
|
"""
|
|
try:
|
|
data = request.json
|
|
if not data or 'command' not in data:
|
|
return jsonify({"error": "Invalid request. 'command' field is required"}), 400
|
|
|
|
command = data.get('command')
|
|
result = execute_system_command(command, hostname, device_ip)
|
|
|
|
return jsonify(result), 200 if result['status'] == 'success' else 400
|
|
|
|
except Exception as e:
|
|
log_with_server(f"Error handling command execution request: {str(e)}", hostname, device_ip)
|
|
return jsonify({"error": f"Server error: {str(e)}"}), 500
|
|
|
|
@app.route('/status', methods=['GET'])
|
|
def get_device_status():
|
|
"""
|
|
Endpoint to get device status information
|
|
"""
|
|
try:
|
|
n_masa = read_masa_name()
|
|
|
|
# Get system information
|
|
uptime_result = subprocess.run(['uptime'], capture_output=True, text=True)
|
|
df_result = subprocess.run(['df', '-h', '/'], capture_output=True, text=True)
|
|
free_result = subprocess.run(['free', '-m'], capture_output=True, text=True)
|
|
|
|
from datetime import datetime
|
|
|
|
status_info = {
|
|
"hostname": hostname,
|
|
"device_ip": device_ip,
|
|
"nume_masa": n_masa,
|
|
"timestamp": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
|
"uptime": uptime_result.stdout.strip() if uptime_result.returncode == 0 else "N/A",
|
|
"disk_usage": df_result.stdout.strip() if df_result.returncode == 0 else "N/A",
|
|
"memory_usage": free_result.stdout.strip() if free_result.returncode == 0 else "N/A"
|
|
}
|
|
|
|
return jsonify(status_info), 200
|
|
|
|
except Exception as e:
|
|
log_with_server(f"Error getting device status: {str(e)}", hostname, device_ip)
|
|
return jsonify({"error": f"Error getting status: {str(e)}"}), 500
|
|
|
|
@app.route('/auto_update', methods=['POST'])
|
|
def auto_update_app():
|
|
"""
|
|
Auto-update the application from the central server
|
|
Checks version, downloads newer files if available, and restarts the device
|
|
"""
|
|
try:
|
|
result = perform_auto_update(local_app_path, local_repo_path, hostname, device_ip)
|
|
|
|
if result.get('status') == 'success':
|
|
return jsonify(result), 200
|
|
elif result.get('status') == 'no_update_needed':
|
|
return jsonify(result), 200
|
|
else:
|
|
return jsonify(result), 500
|
|
|
|
except Exception as e:
|
|
log_with_server(f"Auto-update endpoint error: {str(e)}", hostname, device_ip)
|
|
return jsonify({"error": f"Auto-update failed: {str(e)}"}), 500
|
|
|
|
return app
|