diff --git a/scripts/deploy.py b/scripts/deploy.py index 57aea24..9a5d7a7 100644 --- a/scripts/deploy.py +++ b/scripts/deploy.py @@ -281,58 +281,34 @@ def verify_db() -> DeployResult: def ensure_volume() -> DeployResult: - """Ensure persistent volume is mounted on the app container. + """Ensure persistent volume is configured in Coolify DB. - Coolify regenerates docker-compose.yaml on each deploy, overwriting manual - volume config. This function patches the generated compose file to add the - volume, then restarts the container. + Creates a persistent volume entry in Coolify's local_persistent_volumes table + if it doesn't exist. Coolify will then automatically mount the volume in + every generated docker-compose.yaml — no post-deploy patching needed. + + This works on any Coolify instance and with multiple apps per server. """ - print(" Ensuring persistent volume...") + print(" Ensuring persistent volume in Coolify DB...") - # Ensure volume exists - code, output = ssh_run("docker volume inspect leocrm-storage >/dev/null 2>&1 || docker volume create leocrm-storage") + # Get app ID from Coolify DB + code, output = ssh_run(f"docker exec coolify-db psql -U coolify -d coolify -t -c \"SELECT id FROM applications WHERE uuid = '{APP_UUID}'\" 2>/dev/null") + if code != 0 or not output.strip(): + return DeployResult(False, "Could not find app in Coolify DB") + app_id = output.strip() + + # Check if volume entry already exists + vol_name = f"{APP_UUID}_storage" + code, output = ssh_run(f"docker exec coolify-db psql -U coolify -d coolify -t -c \"SELECT id FROM local_persistent_volumes WHERE resource_id = {app_id} AND name = '{vol_name}'\" 2>/dev/null") + if code == 0 and output.strip(): + return DeployResult(True, "Volume already configured in Coolify DB") + + # Create volume entry in Coolify DB + code, output = ssh_run(f"docker exec coolify-db psql -U coolify -d coolify -c \"INSERT INTO local_persistent_volumes (name, mount_path, resource_type, resource_id, created_at, updated_at, is_preview_suffix_enabled, uuid) VALUES ('{vol_name}', '/data/storage', 'App\\\\\\\\Models\\\\\\\\Application', {app_id}, NOW(), NOW(), false, gen_random_uuid())\" 2>&1") if code != 0: - return DeployResult(False, f"Failed to ensure volume: {output}") + return DeployResult(False, f"Failed to create volume entry: {output}") - # Patch the Coolify-generated docker-compose.yaml to add volume - patch_script = '''python3 -c " -import sys -path = '/data/coolify/applications/stvabl4vaqru7jclx4ittzr3/docker-compose.yaml' -with open(path) as f: - content = f.read() - -# Add volume to service section (after env_file) -old = ' env_file:\\n - .env\\n' -new = ' env_file:\\n - .env\\n volumes:\\n - leocrm-storage:/data/storage\\n' - -if 'leocrm-storage:/data/storage' not in content: - content = content.replace(old, new, 1) - -# Add top-level volumes section if missing -if 'volumes:' not in content.split('networks:')[0] if 'networks:' in content else True: - if 'volumes:\\n leocrm-storage:' not in content: - content += '\\nvolumes:\\n leocrm-storage:\\n external: true\\n' - -with open(path, 'w') as f: - f.write(content) -print('Volume patched') -"''' - code, output = ssh_run(patch_script) - if code != 0: - return DeployResult(False, f"Failed to patch compose file: {output}") - - # Restart container with volume - code, output = ssh_run("cd /data/coolify/applications/stvabl4vaqru7jclx4ittzr3/ && docker compose down 2>&1 && docker compose up -d 2>&1") - if code != 0: - return DeployResult(False, f"Failed to restart with volume: {output}") - - # Verify volume is mounted - import time as _time - _time.sleep(5) - code, output = ssh_run('docker inspect $(docker ps --format "{{.Names}}" | grep stvabl4 | head -1) --format "{{json .Mounts}}" 2>/dev/null') - if code == 0 and "leocrm-storage" in output: - return DeployResult(True, "Persistent volume mounted") - return DeployResult(False, f"Volume not found in mounts: {output}") + return DeployResult(True, "Volume entry created in Coolify DB") def ensure_rls() -> DeployResult: