Files
homelabstack/apply-compose.ps1
T
ginnoir 57cd6ed270 refactor: split monolith into per-domain stacks (Phase A: build)
Add stacks/<domain>/ compose + env for the 11 target stacks (proxy, media,
foundry, owncloud, resume, famapp, authentik, notify, monitoring, remote, dev).
Each app stack joins a shared external `edge` network for Caddy and keeps its
DB/cache co-located (no shared backing services). All named volumes convert to
tiered bind mounts: DBs/configs -> /config (SSD), blobs/repos/registry ->
/storage1/labdata (ZFS). Gitea repos+LFS split to ZFS.

Move Vault into the management plane (portainer-compose.yml) and add the shared
`edge` network there. apply-compose.ps1 -Portainer now also pushes vault.hcl.

Additive only: root docker-compose.yml/.env/Caddyfile untouched, so the live
monolith is unchanged. Live cutover (Phase B) is next.
2026-06-04 16:11:31 -05:00

101 lines
4.3 KiB
PowerShell

# Push .env, Caddyfile, and docker-compose.yml to valhalla, then apply them
# to the live Docker stack.
#
# On valhalla, `dc` is aliased (in ~/.bashrc) to:
# docker compose -f ~/valhalla-lab/docker-compose.yml --env-file ~/valhalla-lab/.env
# It's a shell alias, so we invoke it over SSH with `bash -ic` to load it.
#
# Usage (flags combine; no flags = push & apply all three):
# powershell -File apply-compose.ps1 # .env + Caddyfile + compose
# powershell -File apply-compose.ps1 -EnvFile # .env only
# powershell -File apply-compose.ps1 -Caddy # Caddyfile only + hot-reload Caddy
# powershell -File apply-compose.ps1 -Compose # docker-compose.yml only + pull + up -d
# powershell -File apply-compose.ps1 -Portainer # portainer-compose.yml only + up -d
# powershell -File apply-compose.ps1 -DevStack # dev-compose.yml + vault.hcl + up -d
# powershell -File apply-compose.ps1 -VaultUnseal # unseal vault after restart (uses keys from .env)
param(
[switch]$Compose,
[switch]$Caddy,
[switch]$EnvFile,
[switch]$Portainer,
[switch]$DevStack,
[switch]$VaultUnseal
)
$ErrorActionPreference = "Stop"
# Make a non-zero exit from scp/ssh abort the script instead of silently continuing.
$PSNativeCommandUseErrorActionPreference = $true
$server = "ginnoir@valhalla"
$composeLocal = Join-Path $PSScriptRoot "docker-compose.yml"
$caddyLocal = Join-Path $PSScriptRoot "Caddyfile"
$envLocal = Join-Path $PSScriptRoot ".env"
$portainerLocal = Join-Path $PSScriptRoot "portainer-compose.yml"
$devComposeLocal = Join-Path $PSScriptRoot "dev-compose.yml"
$vaultConfigLocal = Join-Path $PSScriptRoot "vault.hcl"
# Default (no flags): push everything.
if (-not $Compose -and -not $Caddy -and -not $EnvFile -and -not $Portainer -and -not $DevStack -and -not $VaultUnseal) {
$EnvFile = $true; $Caddy = $true; $Compose = $true
}
if ($EnvFile) {
Write-Host "Pushing .env ..."
scp $envLocal "${server}:~/valhalla-lab/.env"
}
if ($Caddy) {
Write-Host "Pushing Caddyfile ..."
scp $caddyLocal "${server}:/config/caddy/Caddyfile"
Write-Host "Reloading Caddy ..."
ssh $server "bash -ic 'dc exec caddy caddy reload --config /etc/caddy/Caddyfile'"
}
if ($Compose) {
Write-Host "Pushing docker-compose.yml ..."
scp $composeLocal "${server}:~/valhalla-lab/docker-compose.yml"
Write-Host "Pulling latest images ..."
ssh $server "bash -ic 'dc pull'"
Write-Host "Applying compose changes (recreates only changed containers) ..."
ssh $server "bash -ic 'dc up -d'"
}
if ($Portainer) {
Write-Host "Pushing portainer-compose.yml ..."
scp $portainerLocal "${server}:~/valhalla-lab/portainer-compose.yml"
Write-Host "Pushing vault.hcl (Vault now lives in the management plane) ..."
ssh $server "mkdir -p /config/vault/config /config/vault/data /config/vault/logs"
scp $vaultConfigLocal "${server}:/config/vault/config/vault.hcl"
Write-Host "Applying management plane (portainer, vault, github-runner, watchtower) ..."
ssh $server "docker compose -f ~/valhalla-lab/portainer-compose.yml --env-file ~/valhalla-lab/.env up -d"
}
if ($DevStack) {
Write-Host "Pushing dev-compose.yml ..."
scp $devComposeLocal "${server}:~/valhalla-lab/dev-compose.yml"
Write-Host "Pushing vault.hcl ..."
ssh $server "mkdir -p /config/vault/config /config/vault/data /config/vault/logs"
scp $vaultConfigLocal "${server}:/config/vault/config/vault.hcl"
Write-Host "Pulling latest dev images ..."
ssh $server "docker compose -f ~/valhalla-lab/dev-compose.yml --env-file ~/valhalla-lab/.env pull"
Write-Host "Applying dev stack (recreates only changed containers) ..."
ssh $server "docker compose -f ~/valhalla-lab/dev-compose.yml --env-file ~/valhalla-lab/.env up -d"
}
if ($VaultUnseal) {
# Load the three unseal keys from .env
$envContent = Get-Content $envLocal | Where-Object { $_ -match "^VAULT_UNSEAL_KEY_[123]=" }
$keys = $envContent | ForEach-Object { ($_ -split "=", 2)[1] }
if ($keys.Count -lt 3) {
Write-Error "Could not find VAULT_UNSEAL_KEY_1/2/3 in .env"
exit 1
}
Write-Host "Unsealing Vault (3 of 5 keys) ..."
foreach ($key in $keys) {
ssh $server "docker exec vault vault operator unseal $key 2>&1 | grep -E 'Sealed|Error'"
}
}
Write-Host "Done."