-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdispatch.ps1
More file actions
698 lines (582 loc) · 21.7 KB
/
dispatch.ps1
File metadata and controls
698 lines (582 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
<#
.SYNOPSIS
Dispatch production launcher.
.DESCRIPTION
Docker-only commands for running Dispatch without npm.
#>
param(
[Parameter(Position = 0)]
[ValidateSet("setup", "start", "stop", "restart", "logs", "status", "down", "pull", "pullpreprod", "pulllatest", "freshstart", "updateself", "help", "version", "")]
[string]$Command = ""
)
$ErrorActionPreference = "Stop"
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$ScriptRoot = $PSScriptRoot
$ScriptFilePath = Join-Path $ScriptRoot "dispatch.ps1"
$EnvFilePath = Join-Path $ScriptRoot ".env.prod"
$RepoOwner = "nkasco"
$RepoName = "DispatchTodoApp"
$RepoApiUrl = "https://api.github.com/repos/$RepoOwner/$RepoName"
$ScriptRepoPath = "dispatch.ps1"
$PackageJson = Get-Content -Raw -Path (Join-Path $ScriptRoot "package.json") | ConvertFrom-Json
$Version = $PackageJson.version
$RawAppName = if ($PackageJson.name) { [string]$PackageJson.name } else { "dispatch" }
$AppName = (Get-Culture).TextInfo.ToTitleCase(($RawAppName -replace "[-_]+", " ").ToLowerInvariant())
$VersionMoniker = "$AppName v$Version"
function Write-CyanLn { param([string]$Text) Write-Host $Text -ForegroundColor Cyan }
function Write-DimLn { param([string]$Text) Write-Host $Text -ForegroundColor DarkGray }
function Write-GreenLn { param([string]$Text) Write-Host $Text -ForegroundColor Green }
function Write-YellowLn { param([string]$Text) Write-Host $Text -ForegroundColor Yellow }
function Write-RedLn { param([string]$Text) Write-Host $Text -ForegroundColor Red }
function Show-Logo {
$logo = @(
@{ Color = "Cyan"; Text = " ██████╗ ██╗███████╗██████╗ █████╗ ████████╗ ██████╗██╗ ██╗" }
@{ Color = "Cyan"; Text = " ██╔══██╗██║██╔════╝██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██║ ██║" }
@{ Color = "DarkCyan"; Text = " ██║ ██║██║███████╗██████╔╝███████║ ██║ ██║ ███████║" }
@{ Color = "Blue"; Text = " ██║ ██║██║╚════██║██╔═══╝ ██╔══██║ ██║ ██║ ██╔══██║" }
@{ Color = "Blue"; Text = " ██████╔╝██║███████║██║ ██║ ██║ ██║ ╚██████╗██║ ██║" }
@{ Color = "DarkBlue"; Text = " ╚═════╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝" }
)
Write-Host ""
foreach ($line in $logo) {
Write-Host $line.Text -ForegroundColor $line.Color
}
Write-Host ""
Write-DimLn " $VersionMoniker - Docker production launcher"
Write-Host ""
}
function Show-Help {
Show-Logo
Write-Host " USAGE" -ForegroundColor White
Write-Host " .\dispatch.ps1 <command>"
Write-Host ""
Write-Host " COMMANDS" -ForegroundColor White
Write-Host " setup Interactive production setup (.env.prod + optional start)"
Write-Host " start Start Dispatch with Docker Compose (.env.prod)"
Write-Host " stop Stop running Dispatch containers"
Write-Host " restart Restart Dispatch containers"
Write-Host " logs Follow Dispatch logs"
Write-Host " status Show container status"
Write-Host " pull Pull latest image and restart"
Write-Host " pullpreprod Pull preprod image tag and restart using it"
Write-Host " pulllatest Pull latest image tag and restart using it"
Write-Host " freshstart Remove containers and volumes, then start fresh"
Write-Host " down Stop and remove containers/network"
Write-Host " updateself Download the latest version of this launcher from GitHub"
Write-Host " version Show version number"
Write-Host " help Show this help message"
Write-Host ""
Write-DimLn " Production config is stored in .env.prod"
Write-DimLn " Image selection is architecture-aware (amd64/arm64)."
Write-DimLn " Developer workflow (npm build/test/dev): .\scripts\launchers\dispatch-dev.ps1"
Write-Host ""
}
function Assert-Docker {
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
Write-RedLn "Docker is not installed or not on PATH."
exit 1
}
}
function Assert-EnvFile {
if (-not (Test-Path $EnvFilePath)) {
Write-RedLn "Missing .env.prod. Run '.\dispatch.ps1 setup' first."
exit 1
}
}
function New-AuthSecret {
$bytes = New-Object byte[] 32
[System.Security.Cryptography.RandomNumberGenerator]::Fill($bytes)
return ([Convert]::ToBase64String($bytes)).TrimEnd("=") -replace "\+", "-" -replace "/", "_"
}
function Get-EnvMap {
param([string]$Path)
$map = [ordered]@{}
if (-not (Test-Path $Path)) {
return $map
}
foreach ($line in Get-Content -Path $Path) {
$trimmed = $line.Trim()
if ($trimmed.Length -eq 0 -or $trimmed.StartsWith("#")) {
continue
}
$parts = $trimmed -split "=", 2
if ($parts.Length -ne 2) {
continue
}
$key = $parts[0].Trim()
$value = $parts[1]
if ($key -match "^[A-Za-z_][A-Za-z0-9_]*$") {
$map[$key] = $value
}
}
return $map
}
function Get-ConfiguredDispatchImage {
param([hashtable]$EnvMap)
if ($env:DISPATCH_IMAGE -and $env:DISPATCH_IMAGE.Trim().Length -gt 0) {
return $env:DISPATCH_IMAGE.Trim()
}
if ($EnvMap -and $EnvMap.Contains("DISPATCH_IMAGE") -and $EnvMap.DISPATCH_IMAGE) {
return [string]$EnvMap.DISPATCH_IMAGE
}
return "ghcr.io/nkasco/dispatchtodoapp:latest"
}
function Get-DockerArchitecture {
$candidates = @()
$versionArch = (docker version --format "{{.Server.Arch}}" 2>$null)
if ($LASTEXITCODE -eq 0 -and $versionArch -and $versionArch.Trim().Length -gt 0) {
$candidates += $versionArch.Trim()
}
if ($candidates.Count -eq 0) {
$infoArch = (docker info --format "{{.Architecture}}" 2>$null)
if ($LASTEXITCODE -eq 0 -and $infoArch -and $infoArch.Trim().Length -gt 0) {
$candidates += $infoArch.Trim()
}
}
$candidates += [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()
foreach ($candidate in $candidates) {
switch ($candidate.Trim().ToLowerInvariant()) {
{ $_ -in @("amd64", "x86_64", "x64") } { return "amd64" }
{ $_ -in @("arm64", "aarch64") } { return "arm64" }
}
}
return "unknown"
}
function Test-HasExplicitArchTag {
param([string]$Image)
if (-not $Image -or $Image.Contains("@")) {
return $false
}
$lastSlash = $Image.LastIndexOf("/")
$lastColon = $Image.LastIndexOf(":")
if ($lastColon -le $lastSlash) {
return $false
}
$tag = $Image.Substring($lastColon + 1).ToLowerInvariant()
return $tag.EndsWith("-arm64") -or $tag.EndsWith("-amd64")
}
function Test-IsDispatchImage {
param([string]$Image)
if (-not $Image -or $Image.Trim().Length -eq 0) {
return $false
}
return $Image.Trim().ToLowerInvariant() -match "(^|/)dispatchtodoapp(?::|@|$)"
}
function Convert-ToArmImageTag {
param([string]$Image)
$lastSlash = $Image.LastIndexOf("/")
$lastColon = $Image.LastIndexOf(":")
if ($lastColon -gt $lastSlash) {
$repo = $Image.Substring(0, $lastColon)
$tag = $Image.Substring($lastColon + 1)
} else {
$repo = $Image
$tag = "latest"
}
return "${repo}:$tag-arm64"
}
function Convert-ToPreprodImageTag {
param([string]$Image)
if (-not $Image -or $Image.Trim().Length -eq 0) {
return "ghcr.io/nkasco/dispatchtodoapp:preprod"
}
$trimmedImage = $Image.Trim()
if ($trimmedImage.Contains("@")) {
return "ghcr.io/nkasco/dispatchtodoapp:preprod"
}
$lastSlash = $trimmedImage.LastIndexOf("/")
$lastColon = $trimmedImage.LastIndexOf(":")
if ($lastColon -gt $lastSlash) {
$repo = $trimmedImage.Substring(0, $lastColon)
} else {
$repo = $trimmedImage
}
return "${repo}:preprod"
}
function Get-PreprodDispatchImage {
param([hashtable]$EnvMap)
if ($env:DISPATCH_PREPROD_IMAGE -and $env:DISPATCH_PREPROD_IMAGE.Trim().Length -gt 0) {
return $env:DISPATCH_PREPROD_IMAGE.Trim()
}
if ($EnvMap -and $EnvMap.Contains("DISPATCH_PREPROD_IMAGE") -and $EnvMap.DISPATCH_PREPROD_IMAGE) {
return [string]$EnvMap.DISPATCH_PREPROD_IMAGE
}
$configuredImage = Get-ConfiguredDispatchImage -EnvMap $EnvMap
return Convert-ToPreprodImageTag -Image $configuredImage
}
function Resolve-DispatchImageForHost {
param([string]$Image)
if (-not $Image -or $Image.Trim().Length -eq 0) {
return "ghcr.io/nkasco/dispatchtodoapp:latest"
}
$trimmedImage = $Image.Trim()
if ($trimmedImage.Contains("@") -or (Test-HasExplicitArchTag -Image $trimmedImage)) {
return $trimmedImage
}
if (-not (Test-IsDispatchImage -Image $trimmedImage)) {
return $trimmedImage
}
$arch = Get-DockerArchitecture
if ($arch -eq "arm64") {
return Convert-ToArmImageTag -Image $trimmedImage
}
return $trimmedImage
}
function Prompt-Value {
param(
[string]$Message,
[string]$Default = "",
[bool]$AllowEmpty = $false
)
while ($true) {
$prompt = if ($Default) { "$Message (default: $Default)" } else { $Message }
$value = Read-Host $prompt
if ([string]::IsNullOrWhiteSpace($value)) {
if ($Default) {
return $Default
}
if ($AllowEmpty) {
return ""
}
Write-YellowLn "Value is required."
continue
}
return $value.Trim()
}
}
function Prompt-Port {
param([string]$Default = "3000")
while ($true) {
$raw = Prompt-Value -Message "Port to run Dispatch on" -Default $Default
$portNumber = 0
if (-not [int]::TryParse($raw, [ref]$portNumber)) {
Write-YellowLn "Port must be a number."
continue
}
if ($portNumber -lt 1 -or $portNumber -gt 65535) {
Write-YellowLn "Port must be between 1 and 65535."
continue
}
return [string]$portNumber
}
}
function Prompt-YesNo {
param(
[string]$Message,
[bool]$Default = $true
)
while ($true) {
$defaultLabel = if ($Default) { "Y" } else { "N" }
$raw = Read-Host "$Message [y/n] (default: $defaultLabel)"
if ([string]::IsNullOrWhiteSpace($raw)) {
return $Default
}
switch ($raw.Trim().ToLowerInvariant()) {
"y" { return $true }
"yes" { return $true }
"n" { return $false }
"no" { return $false }
default {
Write-YellowLn "Enter y or n."
}
}
}
}
function Write-ProdEnvFile {
param([hashtable]$Map)
$lines = @(
"# Production runtime",
"AUTH_SECRET=$($Map.AUTH_SECRET)",
"NEXTAUTH_URL=$($Map.NEXTAUTH_URL)",
"AUTH_TRUST_HOST=true",
"AUTH_GITHUB_ID=$($Map.AUTH_GITHUB_ID)",
"AUTH_GITHUB_SECRET=$($Map.AUTH_GITHUB_SECRET)",
"DISPATCH_PORT=$($Map.DISPATCH_PORT)",
"DISPATCH_IMAGE=$($Map.DISPATCH_IMAGE)",
""
)
Set-Content -Path $EnvFilePath -Value $lines -Encoding UTF8
}
function Set-EnvValueInFile {
param(
[string]$Path,
[string]$Key,
[string]$Value
)
$lines = @()
if (Test-Path $Path) {
$lines = Get-Content -Path $Path
}
$updated = $false
for ($i = 0; $i -lt $lines.Count; $i++) {
$line = $lines[$i]
if ($line -match "^\s*#") {
continue
}
$parts = $line -split "=", 2
if ($parts.Length -eq 2 -and $parts[0].Trim() -eq $Key) {
$lines[$i] = "$Key=$Value"
$updated = $true
}
}
if (-not $updated) {
if ($lines.Count -eq 0) {
$lines = @("$Key=$Value")
} else {
$lines += "$Key=$Value"
}
}
Set-Content -Path $Path -Value $lines -Encoding UTF8
}
function Run-Compose {
param([string[]]$ComposeArgs)
Set-Location $ScriptRoot
$envMap = Get-EnvMap -Path $EnvFilePath
$configuredImage = Get-ConfiguredDispatchImage -EnvMap $envMap
$resolvedImage = Resolve-DispatchImageForHost -Image $configuredImage
$hadPreviousDispatchImage = Test-Path Env:DISPATCH_IMAGE
$previousDispatchImage = $env:DISPATCH_IMAGE
$env:DISPATCH_IMAGE = $resolvedImage
try {
docker compose --env-file .env.prod @ComposeArgs
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
} finally {
if ($hadPreviousDispatchImage) {
$env:DISPATCH_IMAGE = $previousDispatchImage
} else {
Remove-Item Env:DISPATCH_IMAGE -ErrorAction SilentlyContinue
}
}
}
function Get-RepoDefaultBranch {
try {
$repo = Invoke-RestMethod -Method Get -Uri $RepoApiUrl -Headers @{ "User-Agent" = "DispatchLauncher" }
if ($repo -and $repo.default_branch) {
return [string]$repo.default_branch
}
} catch {
# Fallback handled below.
}
return "main"
}
function Get-ComposeProjectName {
if ($env:COMPOSE_PROJECT_NAME -and $env:COMPOSE_PROJECT_NAME.Trim().Length -gt 0) {
return $env:COMPOSE_PROJECT_NAME.Trim().ToLowerInvariant()
}
return (Split-Path -Path $ScriptRoot -Leaf).ToLowerInvariant()
}
function Remove-AssociatedComposeVolumes {
$projectName = Get-ComposeProjectName
$volumeNames = @(docker volume ls --filter "label=com.docker.compose.project=$projectName" --format "{{.Name}}" |
Where-Object { $_ -and $_.Trim().Length -gt 0 })
if ($volumeNames.Count -gt 0) {
Write-DimLn "Removing associated volumes..."
docker volume rm @volumeNames | Out-Null
}
}
function Invoke-Setup {
Show-Logo
Assert-Docker
$existing = Get-EnvMap -Path $EnvFilePath
$defaultPort = if ($existing.Contains("DISPATCH_PORT")) { $existing.DISPATCH_PORT } else { "3000" }
$port = Prompt-Port -Default $defaultPort
$defaultUrl = if ($existing.Contains("NEXTAUTH_URL")) {
$existing.NEXTAUTH_URL
} else {
"http://localhost:$port"
}
$nextAuthUrl = Prompt-Value -Message "Public URL for Dispatch (NEXTAUTH_URL)" -Default $defaultUrl
$defaultImage = if ($existing.Contains("DISPATCH_IMAGE") -and $existing.DISPATCH_IMAGE) {
$existing.DISPATCH_IMAGE
} elseif ($env:DISPATCH_IMAGE) {
$env:DISPATCH_IMAGE
} else {
"ghcr.io/nkasco/dispatchtodoapp:latest"
}
$defaultImage = Resolve-DispatchImageForHost -Image $defaultImage
$dispatchImage = Prompt-Value -Message "Container image to run (DISPATCH_IMAGE)" -Default $defaultImage
$hasGitHub = ($existing.Contains("AUTH_GITHUB_ID") -and $existing.AUTH_GITHUB_ID) -and ($existing.Contains("AUTH_GITHUB_SECRET") -and $existing.AUTH_GITHUB_SECRET)
$useGitHub = Prompt-YesNo -Message "Enable GitHub OAuth sign-in?" -Default ([bool]$hasGitHub)
$githubId = ""
$githubSecret = ""
if ($useGitHub) {
Write-Host ""
Write-CyanLn "GitHub OAuth setup:"
Write-DimLn " 1) Open: https://github.com/settings/developers"
Write-DimLn " 2) OAuth callback URL: $nextAuthUrl/api/auth/callback/github"
Write-Host ""
$defaultGithubId = if ($existing.Contains("AUTH_GITHUB_ID")) { $existing.AUTH_GITHUB_ID } else { "" }
$defaultGithubSecret = if ($existing.Contains("AUTH_GITHUB_SECRET")) { $existing.AUTH_GITHUB_SECRET } else { "" }
$githubId = Prompt-Value -Message "GitHub OAuth Client ID (AUTH_GITHUB_ID)" -Default $defaultGithubId
$githubSecret = Prompt-Value -Message "GitHub OAuth Client Secret (AUTH_GITHUB_SECRET)" -Default $defaultGithubSecret
}
$authSecret = if ($existing.Contains("AUTH_SECRET") -and $existing.AUTH_SECRET) { $existing.AUTH_SECRET } else { New-AuthSecret }
$finalMap = [ordered]@{
AUTH_SECRET = $authSecret
NEXTAUTH_URL = $nextAuthUrl
AUTH_GITHUB_ID = $githubId
AUTH_GITHUB_SECRET = $githubSecret
DISPATCH_PORT = $port
DISPATCH_IMAGE = $dispatchImage
}
Write-ProdEnvFile -Map $finalMap
Write-GreenLn "Wrote .env.prod"
Write-DimLn "Image: $dispatchImage"
Write-DimLn "URL: $nextAuthUrl"
Write-Host ""
if (Prompt-YesNo -Message "Start Dispatch now?" -Default $true) {
Run-Compose -ComposeArgs @("up", "-d")
Write-GreenLn "Dispatch is running."
}
}
function Invoke-Start {
Show-Logo
Assert-Docker
Assert-EnvFile
Run-Compose -ComposeArgs @("up", "-d")
Write-GreenLn "Dispatch is running."
}
function Invoke-Stop {
Show-Logo
Assert-Docker
Assert-EnvFile
Run-Compose -ComposeArgs @("stop")
}
function Invoke-Restart {
Show-Logo
Assert-Docker
Assert-EnvFile
Run-Compose -ComposeArgs @("restart")
}
function Invoke-Logs {
Show-Logo
Assert-Docker
Assert-EnvFile
Run-Compose -ComposeArgs @("logs", "-f", "dispatch")
}
function Invoke-Status {
Show-Logo
Assert-Docker
Assert-EnvFile
Run-Compose -ComposeArgs @("ps")
}
function Invoke-Down {
Show-Logo
Assert-Docker
Assert-EnvFile
Run-Compose -ComposeArgs @("down")
}
function Invoke-UpdateSelf {
Show-Logo
$defaultBranch = Get-RepoDefaultBranch
$candidateUrls = @(
"https://raw.githubusercontent.com/$RepoOwner/$RepoName/$defaultBranch/$ScriptRepoPath"
)
if ($defaultBranch -ne "main") {
$candidateUrls += "https://raw.githubusercontent.com/$RepoOwner/$RepoName/main/$ScriptRepoPath"
}
if ($defaultBranch -ne "master") {
$candidateUrls += "https://raw.githubusercontent.com/$RepoOwner/$RepoName/master/$ScriptRepoPath"
}
$candidateUrls = $candidateUrls | Select-Object -Unique
$tempPath = [System.IO.Path]::GetTempFileName()
$downloadedFrom = $null
try {
foreach ($url in $candidateUrls) {
try {
Invoke-WebRequest -Uri $url -Headers @{ "User-Agent" = "DispatchLauncher" } -OutFile $tempPath
if ((Get-Item $tempPath).Length -gt 0) {
$downloadedFrom = $url
break
}
} catch {
# Try next candidate URL.
}
}
if (-not $downloadedFrom) {
Write-RedLn "Failed to download latest script from GitHub."
exit 1
}
Move-Item -Path $tempPath -Destination $ScriptFilePath -Force
$tempPath = $null
Write-GreenLn "Updated launcher from: $downloadedFrom"
Write-DimLn "Saved to: $ScriptFilePath"
} finally {
if ($tempPath -and (Test-Path $tempPath)) {
Remove-Item -Path $tempPath -Force -ErrorAction SilentlyContinue
}
}
}
function Invoke-Pull {
Show-Logo
Assert-Docker
Assert-EnvFile
Run-Compose -ComposeArgs @("pull")
Write-DimLn "Cleaning up old Dispatch containers..."
Run-Compose -ComposeArgs @("down", "--remove-orphans")
Run-Compose -ComposeArgs @("up", "-d", "--remove-orphans")
}
function Invoke-PullPreprod {
Show-Logo
Assert-Docker
Assert-EnvFile
$arch = Get-DockerArchitecture
if ($arch -eq "arm64") {
Write-RedLn "pullpreprod is amd64-only. This host is arm64 and no arm64 preprod image is published."
exit 1
}
$envMap = Get-EnvMap -Path $EnvFilePath
$preprodImage = Get-PreprodDispatchImage -EnvMap $envMap
Set-EnvValueInFile -Path $EnvFilePath -Key "DISPATCH_IMAGE" -Value $preprodImage
Write-DimLn "Using preprod image: $preprodImage"
Run-Compose -ComposeArgs @("pull")
Write-DimLn "Cleaning up old Dispatch containers..."
Run-Compose -ComposeArgs @("down", "--remove-orphans")
Run-Compose -ComposeArgs @("up", "-d", "--remove-orphans")
}
function Invoke-PullLatest {
Show-Logo
Assert-Docker
Assert-EnvFile
$latestImage = "ghcr.io/nkasco/dispatchtodoapp:latest"
Set-EnvValueInFile -Path $EnvFilePath -Key "DISPATCH_IMAGE" -Value $latestImage
Write-DimLn "Using latest image: $latestImage"
Run-Compose -ComposeArgs @("pull")
Write-DimLn "Cleaning up old Dispatch containers..."
Run-Compose -ComposeArgs @("down", "--remove-orphans")
Run-Compose -ComposeArgs @("up", "-d", "--remove-orphans")
}
function Invoke-FreshStart {
Show-Logo
Assert-Docker
Assert-EnvFile
$confirmed = Prompt-YesNo -Message "This will permanently remove Dispatch containers and volumes. Continue?" -Default $false
if (-not $confirmed) {
Write-YellowLn "Fresh start cancelled."
return
}
Write-YellowLn "Removing containers and volumes for a clean start..."
Run-Compose -ComposeArgs @("down", "-v", "--remove-orphans")
Remove-AssociatedComposeVolumes
Run-Compose -ComposeArgs @("up", "-d", "--remove-orphans", "--force-recreate")
Write-GreenLn "Dispatch fresh start complete."
}
switch ($Command) {
"setup" { Invoke-Setup }
"start" { Invoke-Start }
"stop" { Invoke-Stop }
"restart" { Invoke-Restart }
"logs" { Invoke-Logs }
"status" { Invoke-Status }
"down" { Invoke-Down }
"updateself" { Invoke-UpdateSelf }
"pull" { Invoke-Pull }
"pullpreprod" { Invoke-PullPreprod }
"pulllatest" { Invoke-PullLatest }
"freshstart" { Invoke-FreshStart }
"version" { Write-Host $VersionMoniker }
"help" { Show-Help }
default { Show-Help }
}