Files

184 lines
7.3 KiB
PowerShell
Raw Permalink Normal View History

2026-05-04 22:21:07 +12:00
<#
.SYNOPSIS
Deploy the Lean 101 Clients app to a Digital Ocean droplet over SSH.
2026-05-04 22:21:07 +12:00
.DESCRIPTION
Tars the local source tree, uploads it to the droplet, and runs
docker compose up --build. No git required on the server.
2026-05-04 22:21:07 +12:00
The same script handles first-time setup and subsequent updates.
2026-05-04 22:21:07 +12:00
.PARAMETER RemoteHost
Hostname or IP of the Digital Ocean droplet. Required.
.PARAMETER RemoteUser
SSH user. Defaults to 'root'.
2026-05-04 22:21:07 +12:00
.PARAMETER RemotePath
Absolute path on the droplet. Defaults to '/srv/lean101-clients'.
2026-05-04 22:21:07 +12:00
.PARAMETER EnvFile
Local path to the production env file. Defaults to '.env.production'.
2026-05-04 22:21:07 +12:00
.PARAMETER SshKey
Optional path to an SSH private key.
2026-05-04 22:21:07 +12:00
.PARAMETER ComposeFile
Compose file name on the remote host. Defaults to 'docker-compose.production.yml'.
2026-05-04 22:21:07 +12:00
.PARAMETER Seed
Run 'python -m app.seed' inside the backend container after the stack is up.
2026-05-04 22:21:07 +12:00
.PARAMETER Logs
Tail logs for ~60 lines after deploy to verify the stack came up.
.PARAMETER SkipBuild
Pass --no-build to docker compose (use when only env changed).
2026-05-04 22:21:07 +12:00
.EXAMPLE
./deploy/Deploy.ps1 -RemoteHost 209.38.24.231
2026-05-04 22:21:07 +12:00
.EXAMPLE
./deploy/Deploy.ps1 -RemoteHost 209.38.24.231 -Seed -Logs
2026-05-04 22:21:07 +12:00
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)] [string] $RemoteHost,
[string] $RemoteUser = "root",
[string] $RemotePath = "/srv/lean101-clients",
[string] $EnvFile = ".env.production",
2026-05-04 22:21:07 +12:00
[string] $SshKey,
[string] $ComposeFile = "docker-compose.production.yml",
2026-05-04 22:21:07 +12:00
[switch] $Seed,
[switch] $Logs,
[switch] $SkipBuild
2026-05-04 22:21:07 +12:00
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
# ── Helpers ───────────────────────────────────────────────────────────────────
function Write-Step($msg) { Write-Host "==> $msg" -ForegroundColor Cyan }
function Write-Warn($msg) { Write-Host "!! $msg" -ForegroundColor Yellow }
function Get-RepoRoot {
$dir = Split-Path -Parent $PSScriptRoot
if (-not $dir) { $dir = (Get-Location).Path }
return $dir
2026-05-04 22:21:07 +12:00
}
$RepoRoot = Get-RepoRoot
$SshTarget = "$RemoteUser@$RemoteHost"
$SshOpts = @("-o", "StrictHostKeyChecking=accept-new", "-o", "BatchMode=no")
if ($SshKey) { $SshOpts += @("-i", $SshKey) }
function Invoke-Ssh([string] $cmd) {
& ssh @SshOpts $SshTarget $cmd
if ($LASTEXITCODE -ne 0) { throw "Remote command failed (exit $LASTEXITCODE): $cmd" }
2026-05-04 22:21:07 +12:00
}
function Invoke-Scp([string] $local, [string] $remote) {
& scp @SshOpts $local "${SshTarget}:${remote}"
if ($LASTEXITCODE -ne 0) { throw "scp failed: $local -> $remote" }
2026-05-04 22:21:07 +12:00
}
# ── Resolve paths ─────────────────────────────────────────────────────────────
2026-05-04 22:21:07 +12:00
Push-Location $RepoRoot
try {
$EnvPath = if ([System.IO.Path]::IsPathRooted($EnvFile)) { $EnvFile } else { Join-Path $RepoRoot $EnvFile }
if (-not (Test-Path $EnvPath)) {
throw "Env file not found at '$EnvPath'. Copy .env.production.example and fill in secrets."
2026-05-04 22:21:07 +12:00
}
# ── Connectivity check ──────────────────────────────────────────────────────
Write-Step "Checking SSH connectivity to $SshTarget"
2026-05-04 22:21:07 +12:00
Invoke-Ssh "echo connected as `$(whoami) on `$(hostname)"
# ── Package source files ────────────────────────────────────────────────────
Write-Step "Packaging source files (excluding node_modules, caches, etc.)"
$TarFile = Join-Path $env:TEMP "lean101-deploy-$(Get-Date -Format 'yyyyMMdd-HHmmss').tar.gz"
$excludes = @(
"--exclude=./node_modules",
"--exclude=./frontend/node_modules",
"--exclude=./frontend/.svelte-kit",
"--exclude=./frontend/build",
"--exclude=./.git",
"--exclude=./__pycache__",
"--exclude=./backend/__pycache__",
"--exclude=./backend/app/__pycache__",
"--exclude=./**/__pycache__",
"--exclude=./*.pyc",
"--exclude=./.env",
"--exclude=./.env.production",
"--exclude=./.env.alpha",
"--exclude=./data_entry_app.db",
"--exclude=./*.db"
)
& tar -czf $TarFile @excludes -C $RepoRoot .
if ($LASTEXITCODE -ne 0) { throw "tar failed" }
$TarSize = [math]::Round((Get-Item $TarFile).Length / 1MB, 1)
Write-Host " Archive: $TarFile ($TarSize MB)"
# ── Upload env file ─────────────────────────────────────────────────────────
Write-Step "Uploading env file"
Invoke-Scp $EnvPath "$RemotePath/.env.production"
2026-05-04 22:21:07 +12:00
Invoke-Ssh "chmod 600 '$RemotePath/.env.production'"
# ── Upload and extract source ────────────────────────────────────────────────
Write-Step "Uploading source archive"
Invoke-Scp $TarFile "/tmp/lean101-deploy.tar.gz"
Remove-Item $TarFile -Force
2026-05-04 22:21:07 +12:00
Write-Step "Extracting on server"
Invoke-Ssh "mkdir -p '$RemotePath' && tar -xzf /tmp/lean101-deploy.tar.gz -C '$RemotePath' && rm /tmp/lean101-deploy.tar.gz"
2026-05-04 22:21:07 +12:00
# ── Docker compose up ───────────────────────────────────────────────────────
$ComposeArgs = "--env-file .env.production -f $ComposeFile"
$BuildFlag = if ($SkipBuild) { "--no-build" } else { "--build" }
2026-05-04 22:21:07 +12:00
Write-Step "Bringing stack up (build=$(-not $SkipBuild))"
Invoke-Ssh "cd '$RemotePath' && docker compose $ComposeArgs up -d $BuildFlag --remove-orphans"
2026-05-04 22:21:07 +12:00
# ── Health check ────────────────────────────────────────────────────────────
Write-Step "Waiting for backend health check"
2026-05-04 22:21:07 +12:00
$healthScript = @"
set -e
cd '$RemotePath'
for i in `$(seq 1 30); do
2026-05-04 22:21:07 +12:00
status=`$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' lean101-clients-backend 2>/dev/null || echo missing)
case "`$status" in
healthy|running) echo "backend is `$status"; exit 0 ;;
*) printf '.'; sleep 4 ;;
2026-05-04 22:21:07 +12:00
esac
done
echo; echo 'backend did not become healthy in time' >&2; exit 1
2026-05-04 22:21:07 +12:00
"@
Invoke-Ssh $healthScript
# ── Optional seed ───────────────────────────────────────────────────────────
2026-05-04 22:21:07 +12:00
if ($Seed) {
Write-Step "Seeding reference data"
Invoke-Ssh "cd '$RemotePath' && docker compose $ComposeArgs exec -T backend python -m app.seed"
2026-05-04 22:21:07 +12:00
}
# ── Final status ────────────────────────────────────────────────────────────
Write-Step "Stack status"
Invoke-Ssh "cd '$RemotePath' && docker compose $ComposeArgs ps"
2026-05-04 22:21:07 +12:00
if ($Logs) {
Write-Step "Recent logs (last 60 lines)"
Invoke-Ssh "cd '$RemotePath' && docker compose $ComposeArgs logs --tail=60"
2026-05-04 22:21:07 +12:00
}
Write-Host ""
Write-Host "Deployment complete -> https://clients.lean-101.com.au" -ForegroundColor Green
2026-05-04 22:21:07 +12:00
}
finally {
Pop-Location
}