From d76d9d1b0a627ee95120d029bc0d54369c39ca43 Mon Sep 17 00:00:00 2001 From: Judah Paul Date: Mon, 20 May 2024 09:40:38 -0400 Subject: [PATCH] =?UTF-8?q?Bug=20Fixes=20=F0=9F=90=9B=20(#35)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed `No such file or directory: 'sudo'` - Docker runs as root by default, removing sudo in `backend.py` * Reinitialize app with environment vars when restarted - Fixes `No Default Input Device Available` after `supervisorctl restart app` --- Dockerfile | 6 ++++++ src/backend.py | 26 +++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index f0aed20..5eb6d89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -89,6 +89,12 @@ RUN mkdir -p /var/log/supervisor && \ echo 'stdout_logfile=/dev/null'; \ echo 'stdout_logfile_maxbytes=0'; \ echo 'redirect_stderr=true'; \ + echo 'startsecs=0'; \ + echo 'autorestart=true'; \ + echo 'stopsignal=INT'; \ + echo 'stopasgroup=true'; \ + echo 'killasgroup=true'; \ + echo 'environment=HOME="/root",USER="root"'; \ echo ''; \ echo '[program:web-interface]'; \ echo 'command=bash -c "source /env/bin/activate && cd src && uvicorn backend:app --host 0.0.0.0 --port 8000"'; \ diff --git a/src/backend.py b/src/backend.py index 8afe380..2a37d54 100644 --- a/src/backend.py +++ b/src/backend.py @@ -129,32 +129,32 @@ async def settings(request: Request): new_settings = incoming_data['data'] with settings_path.open("w") as f: json.dump(new_settings, f) - subprocess.run(["sudo", "systemctl", "restart", "gpt-home.service"]) + subprocess.run(["supervisorctl", "restart", "app"]) return JSONResponse(content=new_settings) else: return HTTPException(status_code=400, detail="Invalid action") @app.post("/gptRestart") async def gpt_restart(request: Request): - subprocess.run(["sudo", "systemctl", "restart", "gpt-home.service"]) + subprocess.run(["supervisorctl", "restart", "app"]) return JSONResponse(content={"success": True}) @app.post("/spotifyRestart") async def spotify_restart(request: Request): loop = asyncio.get_event_loop() - subprocess.run(["sudo", "systemctl", "stop", "spotifyd"]) + subprocess.run(["supervisorctl", "stop", "spotifyd"]) await loop.run_in_executor(None, time.sleep, 10) - subprocess.run(["sudo", "systemctl", "start", "spotifyd"]) + subprocess.run(["supervisorctl", "start", "spotifyd"]) return JSONResponse(content={"success": True}) @app.post("/reboot") async def reboot(request: Request): - subprocess.run(["sudo", "reboot"]) + subprocess.run(["reboot"]) return JSONResponse(content={"success": True}) @app.post("/shutdown") async def shutdown(request: Request): - subprocess.run(["sudo", "shutdown", "now"]) + subprocess.run(["shutdown", "now"]) return JSONResponse(content={"success": True}) @app.post("/availableModels") @@ -195,7 +195,7 @@ async def update_model(request: Request): json.dump(settings, f) # Restart service - subprocess.run(["sudo", "systemctl", "restart", "gpt-home.service"]) + subprocess.run(["supervisorctl", "restart", "app"]) return JSONResponse(content={"model": model_id}) else: @@ -343,7 +343,7 @@ async def connect_service(request: Request): file.write("discovery = false\n") # Restart spotifyd to apply changes - subprocess.run(["sudo", "systemctl", "restart", "spotifyd"], check=True) + subprocess.run(["supervisorctl", "restart", "spotifyd"], check=True) # Setting REDIRECT URI explicitly to gpt-home.local redirect_uri = "http://gpt-home.local/api/callback" @@ -381,7 +381,7 @@ async def connect_service(request: Request): auth_url = sp_oauth.get_authorize_url() # Restarting the service after setting up configurations for any of the services - subprocess.run(["sudo", "systemctl", "restart", "gpt-home.service"]) + subprocess.run(["supervisorctl", "restart", "app"]) logger.success(f"Successfully connected to {name}.") content = {"success": True} @@ -411,7 +411,7 @@ async def disconnect_service(request: Request): unset_key(ENV_FILE_PATH, "PHILIPS_HUE_BRIDGE_IP") unset_key(ENV_FILE_PATH, "PHILIPS_HUE_USERNAME") - subprocess.run(["sudo", "systemctl", "restart", "gpt-home.service"]) + subprocess.run(["supervisorctl", "restart", "app"]) return JSONResponse(content={"success": True}) except Exception as e: return JSONResponse(content={"error": str(e), "traceback": traceback.format_exc()}) @@ -478,7 +478,7 @@ async def handle_callback(request: Request): store_token(token_info) - subprocess.run(["sudo", "systemctl", "restart", "gpt-home.service"]) + subprocess.run(["supervisorctl", "restart", "app"]) return RedirectResponse(url="/", status_code=302) else: @@ -579,9 +579,9 @@ async def spotify_control(request: Request): if not device_id: # restart spotifyd and try again - subprocess.run(["sudo", "systemctl", "stop", "spotifyd"]) + subprocess.run(["supervisorctl", "stop", "spotifyd"]) time.sleep(3) - subprocess.run(["sudo", "systemctl", "start", "spotifyd"]) + subprocess.run(["supervisorctl", "start", "spotifyd"]) time.sleep(3) devices = sp.devices() logger.debug(f"Devices: {devices}")