Explorar o código

fix shutdown script

tuanchris hai 3 meses
pai
achega
fa7fa806f9
Modificáronse 3 ficheiros con 25 adicións e 40 borrados
  1. 4 0
      docker-compose.yml
  2. 10 40
      main.py
  3. 11 0
      shutdown_host.sh

+ 4 - 0
docker-compose.yml

@@ -10,6 +10,10 @@ services:
       - .:/app
       # Mount timezone file from host for Still Sands scheduling
       - /etc/timezone:/etc/host-timezone:ro
+      # Mount Docker socket to allow host shutdown
+      - /var/run/docker.sock:/var/run/docker.sock:ro
+      # Mount shutdown script
+      - ./shutdown_host.sh:/usr/local/bin/shutdown_host.sh:ro
     devices:
       - "/dev/ttyACM0:/dev/ttyACM0"
     privileged: true

+ 10 - 40
main.py

@@ -1661,57 +1661,27 @@ async def trigger_update():
             status_code=500
         )
 
-@app.get("/api/system/check_pi")
-async def check_pi():
-    """Check if the system is a Raspberry Pi"""
-    try:
-        # Check if running on ARM architecture (Raspberry Pi indicator)
-        is_arm = platform.machine().startswith('arm') or platform.machine().startswith('aarch')
-
-        # Additional check: look for Raspberry Pi specific files
-        is_pi_file = os.path.exists('/proc/device-tree/model')
-        if is_pi_file:
-            with open('/proc/device-tree/model', 'r') as f:
-                model = f.read()
-                is_raspberry_pi = 'Raspberry Pi' in model
-        else:
-            is_raspberry_pi = False
-
-        # System is considered Pi if either check passes
-        is_pi = is_arm or is_raspberry_pi
-
-        return {"is_pi": is_pi}
-    except Exception as e:
-        logger.error(f"Error checking if system is Pi: {e}")
-        return {"is_pi": False}
-
 @app.post("/api/system/shutdown")
 async def shutdown_system():
     """Shutdown the system"""
     try:
         logger.warning("Shutdown initiated via API")
 
-        # Run docker compose down in background
-        try:
-            # Get the directory where main.py is located
-            app_dir = os.path.dirname(os.path.abspath(__file__))
-            subprocess.Popen(
-                ["docker", "compose", "down"],
-                cwd=app_dir,
-                stdout=subprocess.DEVNULL,
-                stderr=subprocess.DEVNULL
-            )
-            logger.info("Docker compose down command issued")
-        except Exception as e:
-            logger.error(f"Error running docker compose down: {e}")
-
         # Schedule shutdown command after a short delay to allow response to be sent
         def delayed_shutdown():
             time.sleep(2)  # Give time for response to be sent
             try:
-                subprocess.run(["sudo", "shutdown", "-h", "now"], check=True)
+                # Use nsenter to escape container and run shutdown on host
+                # This works because we're running with privileged: true
+                subprocess.run([
+                    "nsenter", "--target", "1", "--mount", "--uts", "--ipc", "--net", "--pid",
+                    "--", "shutdown", "-h", "now"
+                ], check=True)
+                logger.info("Host shutdown command executed successfully")
+            except FileNotFoundError:
+                logger.error("nsenter command not found - privileged container required for host shutdown")
             except Exception as e:
-                logger.error(f"Error executing shutdown command: {e}")
+                logger.error(f"Error executing host shutdown command: {e}")
 
         import threading
         shutdown_thread = threading.Thread(target=delayed_shutdown)

+ 11 - 0
shutdown_host.sh

@@ -0,0 +1,11 @@
+#!/bin/bash
+# Script to shutdown the host system from inside Docker container
+# This must be placed on the host and mounted into the container
+
+echo "$(date): Shutdown requested from Dune Weaver" >> /tmp/dune-weaver-shutdown.log
+
+# Stop Docker containers gracefully
+docker compose -f /app/docker-compose.yml down 2>/dev/null || true
+
+# Shutdown the host system
+shutdown -h now