Files

111 lines
6.1 KiB
PowerShell

<#
.SYNOPSIS
Create a Coolify application directly in the database (bypasses the 404 API and the web UI).
.DESCRIPTION
For instances where POST /applications/* returns 404 and no UI credentials are available.
Requires SSH access to the Proxmox host and the Coolify DB container.
Creates:
1. applications row (Dockerfile build pack, GitHub source)
2. application_settings row (prevents "disable_build_cache on null" crash)
Then triggers a deploy via the API.
See deploy_skill/references/coolify-4.1.2-notes.md §8 for the full backstory.
.PARAMETER AppName
Display name (also used in the generated UUID suffix).
.PARAMETER GitRepo
GitHub repo in owner/repo format (e.g. urieljarethbusiness-cpu/agendamax).
.PARAMETER Fqdn
Full domain (e.g. https://agendamax.urieljareth.org).
.PARAMETER Port
Internal port the app listens on. Default: 3000.
.PARAMETER EnvironmentId
Numeric ID of the Coolify environment. Find with:
SELECT id FROM environments WHERE uuid = '<env_uuid>';
.PARAMETER GithubRepoId
Numeric GitHub repo ID. Get from: GET https://api.github.com/repos/<owner>/<repo> → .id
.EXAMPLE
. .\.env.local.ps1
.\deploy_skill\scripts\New-CoolifyAppViaDB.ps1 `
-AppName agendamax `
-GitRepo urieljarethbusiness-cpu/agendamax `
-Fqdn https://agendamax.urieljareth.org `
-Port 3000 -EnvironmentId 9 -GithubRepoId 1314049704
#>
param(
[Parameter(Mandatory)] [string]$AppName,
[Parameter(Mandatory)] [string]$GitRepo,
[Parameter(Mandatory)] [string]$Fqdn,
[int]$Port = 3000,
[Parameter(Mandatory)] [int]$EnvironmentId,
[Parameter(Mandatory)] [int64]$GithubRepoId
)
$ErrorActionPreference = "Stop"
. "$PSScriptRoot\..\..\scripts\ProxmoxAgent.ps1"
# Generate a 24-char lowercase UUID (Coolify style)
$uuid = -join ((1..24) | ForEach-Object { '{0:x}' -f (Get-Random -Max 16) })
Write-Host "Generated UUID: $uuid" -ForegroundColor Cyan
# Build the SQL (single-line to avoid heredoc issues through SSH)
$sql = @"
INSERT INTO applications (uuid, name, git_repository, git_branch, git_commit_sha, build_pack, dockerfile_location, ports_exposes, health_check_path, health_check_port, health_check_host, health_check_method, health_check_return_code, health_check_scheme, health_check_interval, health_check_timeout, health_check_retries, health_check_start_period, health_check_enabled, limits_memory, limits_memory_swap, limits_memory_swappiness, limits_memory_reservation, limits_cpus, limits_cpu_shares, status, preview_url_template, fqdn, repository_project_id, source_type, source_id, destination_type, destination_id, environment_id, base_directory, static_image, created_at, updated_at) VALUES ('${uuid}', '${AppName}:main-${uuid}', '${GitRepo}', 'main', 'HEAD', 'dockerfile', '/Dockerfile', '${Port}', '/', '${Port}', 'localhost', 'GET', 200, 'http', 5, 5, 10, 5, false, '0', '0', 60, '0', '0', 1024, 'exited', '{{pr_id}}.{{domain}}', '${Fqdn}', ${GithubRepoId}, 'App\Models\GithubApp', 3, 'App\Models\StandaloneDocker', 0, ${EnvironmentId}, '/', 'nginx:alpine', NOW(), NOW()) RETURNING id;
"@
# Write the SQL to a temp .sh script, scp it, execute it
$tmpSh = [IO.Path]::GetTempFileName() + ".sh"
@"
pct exec 102 -- docker exec coolify-db psql -U coolify -d coolify -t -A -c "$($sql -replace '"', '\"' -replace "'", "'\''")"
"@ | Set-Content -Path $tmpSh -Encoding ASCII
Write-Host "`n>>> Step 1: INSERT application row" -ForegroundColor Magenta
$remoteSh = "/tmp/coolify_create_$(Get-Random).sh"
scp -o BatchMode=yes -o StrictHostKeyChecking=no -i $env:PROXMOX_SSH_KEY $tmpSh "root@$($env:PROXMOX_HOST):$remoteSh" 2>&1 | Out-Null
$appId = Invoke-ProxmoxSshCommand -Command "sh $remoteSh; rm $remoteSh"
$appId = $appId.Trim()
Remove-Item $tmpSh -ErrorAction SilentlyContinue
Write-Host "Application ID: $appId" -ForegroundColor Green
if (-not $appId -or $appId -notmatch '^\d+$') {
throw "INSERT failed or didn't return an ID. Output: $appId"
}
# Step 2: application_settings
Write-Host "`n>>> Step 2: INSERT application_settings row" -ForegroundColor Magenta
$sqlSettings = @"
INSERT INTO application_settings (application_id, is_static, is_git_submodules_enabled, is_git_lfs_enabled, is_auto_deploy_enabled, is_force_https_enabled, is_debug_enabled, is_preview_deployments_enabled, is_log_drain_enabled, is_gpu_enabled, is_swarm_only_worker_nodes, is_raw_compose_deployment_enabled, is_build_server_enabled, is_consistent_container_name_enabled, is_gzip_enabled, is_stripprefix_enabled, is_container_label_escape_enabled, is_container_label_readonly_enabled, disable_build_cache, is_spa, is_git_shallow_clone_enabled, is_pr_deployments_public_enabled, use_build_secrets, inject_build_args_to_dockerfile, docker_images_to_keep, created_at, updated_at) VALUES (${appId}, false, true, true, true, true, false, false, false, false, true, false, false, false, true, true, true, true, false, false, true, false, false, true, 2, NOW(), NOW());
"@
$tmpSh2 = [IO.Path]::GetTempFileName() + ".sh"
@"
pct exec 102 -- docker exec coolify-db psql -U coolify -d coolify -c "$($sqlSettings -replace '"', '\"' -replace "'", "'\''")"
"@ | Set-Content -Path $tmpSh2 -Encoding ASCII
$remoteSh2 = "/tmp/coolify_settings_$(Get-Random).sh"
scp -o BatchMode=yes -o StrictHostKeyChecking=no -i $env:PROXMOX_SSH_KEY $tmpSh2 "root@$($env:PROXMOX_HOST):$remoteSh2" 2>&1 | Out-Null
Invoke-ProxmoxSshCommand -Command "sh $remoteSh2; rm $remoteSh2" | Write-Host
Remove-Item $tmpSh2 -ErrorAction SilentlyContinue
Write-Host "Settings created for app_id=$appId" -ForegroundColor Green
# Step 3: Deploy via API
Write-Host "`n>>> Step 3: Trigger deploy" -ForegroundColor Magenta
$headers = @{ Authorization = "Bearer $($env:COOLIFY_TOKEN)" }
$deploy = Invoke-RestMethod "$($env:COOLIFY_API_URL)/deploy?uuid=${uuid}&force=true" -Headers $headers
$deployUuid = $deploy.deployments[0].deployment_uuid
Write-Host "Deployment queued: $deployUuid" -ForegroundColor Green
Write-Host "`n=== DONE ===" -ForegroundColor Green
Write-Host "App UUID: $uuid"
Write-Host "App ID: $appId"
Write-Host "FQDN: $Fqdn"
Write-Host "Deploy: $deployUuid"
Write-Host "Monitor: GET $($env:COOLIFY_API_URL)/deployments/$deployUuid"