refs #437 Adds uninstall option to daemon

pull/3/head
Luis Gerardo Romero Garcia 2024-06-24 16:24:00 +02:00
parent 636c6fb2c5
commit 361c892acb
1 changed files with 24 additions and 18 deletions

View File

@ -1,8 +1,9 @@
import os
import socket
import json
import subprocess
import logging
import os
import stat
# Configuración de logging
logging.basicConfig(level=logging.INFO, filename='/var/log/oglive_daemon.log', filemode='a', format='%(asctime)s - %(levelname)s - %(message)s')
@ -10,31 +11,33 @@ logging.basicConfig(level=logging.INFO, filename='/var/log/oglive_daemon.log', f
def handle_command(command):
action = command.get('action')
args = command.get('args', [])
cleaned_args = [arg.strip('\'"') for arg in args]
if action in ['install', 'download', 'uninstall', 'show', 'check']:
try:
logging.info(f'Handling command: {action} with args: {args}')
cleaned_args = [arg.strip('\'"') for arg in args] # Limpiar comillas
logging.info(f'Handling command: {action} with args: {cleaned_args}')
try:
if action in ['config', 'install', 'download', 'show', 'check', 'uninstall']:
command_to_run = ['sudo', '/opt/ogboot/bin/oglivecli', action] + cleaned_args
logging.info(f'Running command: {" ".join(command_to_run)}')
process = subprocess.Popen(command_to_run, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
logging.info(f'Command output: {stdout}')
if process.returncode == 0:
logging.info(f'Command executed successfully: {stdout}')
return {"success": True, "output": stdout}
return {"success": True, "output": stdout.strip()}
else:
logging.error(f'Command failed with error: {stderr}')
return {"success": False, "error": stderr.strip()}
except Exception as e:
logging.error(f'Failed to handle command {action}: {e}')
return {"success": False, "error": str(e)}
else:
logging.error(f'Unknown command action: {action}')
return {"success": False, "error": "Unknown command action"}
else:
return {"success": False, "error": "Unknown command"}
except Exception as e:
logging.error(f'Error handling command {action}: {e}')
return {"success": False, "error": str(e)}
def main():
socket_path = '/tmp/oglive_daemon.sock'
# Crea el directorio si no existe
if not os.path.exists('/var/run/oglive'):
os.makedirs('/var/run/oglive', exist_ok=True)
socket_path = '/var/run/oglive/oglive_daemon.sock'
# Elimina el socket si existe
if os.path.exists(socket_path):
@ -42,6 +45,10 @@ def main():
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(socket_path)
# Establece los permisos del socket
os.chmod(socket_path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # Permisos para todos los usuarios
server.listen()
try:
@ -53,7 +60,6 @@ def main():
data = conn.recv(1024)
if not data:
continue
try:
command = json.loads(data.decode('utf-8'))
logging.info(f'Received command: {command}')