From d391e9b4433ff369b41a1572c023778fe99b56da Mon Sep 17 00:00:00 2001 From: A R R R Associates Date: Sun, 12 Jul 2026 23:47:33 +0530 Subject: [PATCH] Fix generated Windows storage agent background installation --- app/modules/documents/agent_package.py | 64 +++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/app/modules/documents/agent_package.py b/app/modules/documents/agent_package.py index d708812..50bfd81 100644 --- a/app/modules/documents/agent_package.py +++ b/app/modules/documents/agent_package.py @@ -476,6 +476,13 @@ anV4CwABBAAAAAAEAAAAAFBLBQYAAAAAJQAlALsOAABnkwAAAAA= def build_agent_env(*, erp_base_url: str, node_code: str, node_secret: str, storage_root: str, tenant_id: int | str | None = None, branch_id: int | str | None = None, sync_interval_seconds: int = 30, request_timeout_seconds: int = 60, tunnel_enabled: bool = True, tunnel_reconnect_seconds: int = 10) -> str: erp_base_url = (erp_base_url or "").strip().rstrip("/") + # Coolify/Traefik may forward the application request internally as HTTP even + # when the public ERP URL is HTTPS. Public agent packages must use HTTPS so + # tunnel.py derives a valid wss:// endpoint. Preserve explicit local test URLs. + if erp_base_url.startswith("http://"): + host = erp_base_url[7:].split("/", 1)[0].split(":", 1)[0].lower() + if host not in {"localhost", "127.0.0.1", "::1"}: + erp_base_url = "https://" + erp_base_url[7:] storage_root = (storage_root or r"D:\AuditFirmStorage").strip() return ( f"ERP_BASE_URL={erp_base_url}\n" @@ -503,6 +510,61 @@ def build_preconfigured_agent_zip(*, env_text: str, include_admin_readme: bool = src = zipfile.ZipFile(io.BytesIO(base), "r") buffer = io.BytesIO() with zipfile.ZipFile(buffer, "w", compression=zipfile.ZIP_DEFLATED) as dst: + replacements = { + "run_agent.bat": ( + "@echo off\r\n" + "setlocal\r\n" + "cd /d \"%~dp0\"\r\n" + "if not exist \".env\" (\r\n" + " echo ERROR: .env file is missing. Generate/download the configured agent package from ERP.\r\n" + " exit /b 1\r\n" + ")\r\n" + "if not exist \".venv\\Scripts\\python.exe\" (\r\n" + " echo ERROR: Python virtual environment is missing. Run install_task_scheduler.bat first.\r\n" + " exit /b 1\r\n" + ")\r\n" + "\".venv\\Scripts\\python.exe\" -m audit_storage_agent.main\r\n" + ).encode("utf-8"), + "install_task_scheduler.bat": ( + "@echo off\r\n" + "setlocal\r\n" + "cd /d \"%~dp0\"\r\n" + "if not exist \".env\" (\r\n" + " echo ERROR: .env file is missing. Generate/download the configured agent package from ERP.\r\n" + " pause\r\n" + " exit /b 1\r\n" + ")\r\n" + "if not exist \".venv\\Scripts\\python.exe\" (\r\n" + " echo Creating virtual environment...\r\n" + " py -3 -m venv .venv\r\n" + " if errorlevel 1 (\r\n" + " echo ERROR: Unable to create the Python virtual environment.\r\n" + " pause\r\n" + " exit /b 1\r\n" + " )\r\n" + ")\r\n" + "\".venv\\Scripts\\python.exe\" -m pip install --upgrade pip\r\n" + "if errorlevel 1 goto :install_failed\r\n" + "\".venv\\Scripts\\python.exe\" -m pip install -r requirements.txt\r\n" + "if errorlevel 1 goto :install_failed\r\n" + "set \"AGENT_DIR=%~dp0\"\r\n" + "set \"AGENT_PYTHON=%~dp0.venv\\Scripts\\python.exe\"\r\n" + "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command \"$ErrorActionPreference='Stop'; $taskName='AuditFirmStorageAgent'; $agentDir=$env:AGENT_DIR.TrimEnd('\\'); $python=$env:AGENT_PYTHON; $action=New-ScheduledTaskAction -Execute $python -Argument '-m audit_storage_agent.main' -WorkingDirectory $agentDir; $trigger=New-ScheduledTaskTrigger -AtStartup; $principal=New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest; $settings=New-ScheduledTaskSettingsSet -RestartCount 999 -RestartInterval (New-TimeSpan -Minutes 1) -ExecutionTimeLimit ([TimeSpan]::Zero) -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries; Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force | Out-Null; Start-ScheduledTask -TaskName $taskName\"\r\n" + "if errorlevel 1 (\r\n" + " echo ERROR: Task Scheduler installation failed. Run this file as Administrator.\r\n" + " pause\r\n" + " exit /b 1\r\n" + ")\r\n" + "echo Audit Firm Storage Agent scheduled task installed and started successfully.\r\n" + "echo The agent now runs in the background; no command window needs to remain open.\r\n" + "pause\r\n" + "exit /b 0\r\n" + ":install_failed\r\n" + "echo ERROR: Python dependency installation failed.\r\n" + "pause\r\n" + "exit /b 1\r\n" + ).encode("utf-8"), + } for info in src.infolist(): name = info.filename.replace("\\", "/") lower = name.lower() @@ -510,7 +572,7 @@ def build_preconfigured_agent_zip(*, env_text: str, include_admin_readme: bool = continue if (not include_admin_readme) and lower.endswith(".txt") and lower != "requirements.txt": continue - dst.writestr(info, src.read(info.filename)) + dst.writestr(info, replacements.get(name, src.read(info.filename))) dst.writestr(".env", env_text) if include_admin_readme: dst.writestr(