Deploy: portable volume config via Coolify DB, works on any instance

This commit is contained in:
Agent Zero
2026-07-25 23:50:06 +02:00
parent 745b634e7c
commit 12220cc640
+23 -47
View File
@@ -281,58 +281,34 @@ def verify_db() -> DeployResult:
def ensure_volume() -> 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 Creates a persistent volume entry in Coolify's local_persistent_volumes table
volume config. This function patches the generated compose file to add the if it doesn't exist. Coolify will then automatically mount the volume in
volume, then restarts the container. 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 # Get app ID from Coolify DB
code, output = ssh_run("docker volume inspect leocrm-storage >/dev/null 2>&1 || docker volume create leocrm-storage") 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: 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 return DeployResult(True, "Volume entry created in Coolify DB")
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}")
def ensure_rls() -> DeployResult: def ensure_rls() -> DeployResult: