forked from dcflachs/compose_plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeploy.ps1
More file actions
335 lines (277 loc) · 10.6 KB
/
deploy.ps1
File metadata and controls
335 lines (277 loc) · 10.6 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
#Requires -Version 7.0
<#!
.SYNOPSIS
Build and deploy compose.manager package to a remote Unraid host.
.DESCRIPTION
Builds (or reuses) a package, uploads it via SCP, installs it with installpkg,
and removes the temporary package file from the remote host.
.PARAMETER Version
Package version to build. If omitted, build.ps1 resolves from compose.manager.plg.
.PARAMETER Dev
Generate a development build with timestamp: YYYY.MM.DD.HHmm
.PARAMETER RemoteHost
Remote hostname(s) or IP(s). Accepts a single value or multiple values using PowerShell array syntax (e.g. "saturn","jupiter").
.PARAMETER User
SSH username.
.PARAMETER RemoteDir
Remote directory used for upload/install.
.PARAMETER PackagePath
Existing package path to deploy. If not set, script builds by default.
.PARAMETER SkipBuild
Skip build and deploy latest package from archive if PackagePath is not specified.
.PARAMETER ComposeVersion
Docker Compose version to pass through to build.ps1.
.PARAMETER AceVersion
Ace Editor version to pass through to build.ps1.
.PARAMETER Quick
Skip package build/install and deploy tracked staged+unstaged file changes
from source/compose.manager directly to the remote live emhttp plugin folder.
.EXAMPLE
./deploy.ps1 -Version "2026.03.07" -RemoteHost "saturn"
.EXAMPLE
./deploy.ps1 -Dev -RemoteHost "saturn"
.EXAMPLE
./deploy.ps1 -Dev -RemoteHost "saturn","jupiter"
.EXAMPLE
./deploy.ps1 -SkipBuild -RemoteHost "saturn"
.EXAMPLE
./deploy.ps1 -PackagePath ".\archive\compose.manager-2026.03.07-noarch-1234.txz" -RemoteHost "saturn"
.EXAMPLE
./deploy.ps1 -Quick -RemoteHost "saturn"
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param(
[string]$Version,
[switch]$Dev,
[string[]]$RemoteHost = @(),
[string]$User = "root",
[string]$RemoteDir = "/tmp",
[string]$PackagePath,
[switch]$SkipBuild,
[string]$ComposeVersion = "5.0.2",
[string]$AceVersion = "1.43.5",
[switch]$Quick
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$scriptDir = $PSScriptRoot
$archiveDir = Join-Path $scriptDir "archive"
if ($Quick) {
if (-not $RemoteHost -or $RemoteHost.Count -eq 0) {
throw "RemoteHost is required when using -Quick"
}
if ($Version -or $Dev -or $PackagePath -or $SkipBuild) {
Write-Host "Quick mode ignores -Version, -Dev, -PackagePath, and -SkipBuild." -ForegroundColor DarkYellow
}
$repoRoot = (& git -C $scriptDir rev-parse --show-toplevel 2>$null)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($repoRoot)) {
throw "Unable to resolve git repository root from $scriptDir"
}
$repoRoot = $repoRoot.Trim()
$quickPrefix = "source/compose.manager/"
$quickRemoteRoot = "/usr/local/emhttp/plugins/compose.manager"
$statUnstaged = (& git -C $repoRoot diff --stat -- source/compose.manager)
$statStaged = (& git -C $repoRoot diff --cached --stat -- source/compose.manager)
if ($statUnstaged) {
Write-Host "Unstaged tracked diff:" -ForegroundColor Cyan
$statUnstaged | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
}
if ($statStaged) {
Write-Host "Staged tracked diff:" -ForegroundColor Cyan
$statStaged | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
}
$unstagedTracked = @(& git -C $repoRoot diff --name-only --diff-filter=ACMR -- source/compose.manager)
$stagedTracked = @(& git -C $repoRoot diff --cached --name-only --diff-filter=ACMR -- source/compose.manager)
$changedFiles = @($unstagedTracked + $stagedTracked |
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
Sort-Object -Unique)
if (-not $changedFiles -or $changedFiles.Count -eq 0) {
Write-Host "No tracked staged/unstaged file changes found under source/compose.manager." -ForegroundColor Yellow
return @{
Hosts = $RemoteHost
User = $User
Quick = $true
FileCount = 0
Files = @()
WhatIf = [bool]$WhatIfPreference
}
}
Write-Host "Files queued for quick sync ($($changedFiles.Count)):" -ForegroundColor Green
$changedFiles | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
$allResults = @()
foreach ($host_ in $RemoteHost) {
$remoteTarget = "$User@$host_"
Write-Host "`nQuick deploy to $remoteTarget :" -ForegroundColor Green
$syncedFiles = @()
foreach ($relativePath in $changedFiles) {
if (-not $relativePath.StartsWith($quickPrefix, [System.StringComparison]::Ordinal)) {
continue
}
$subPath = $relativePath.Substring($quickPrefix.Length)
if ([string]::IsNullOrWhiteSpace($subPath)) {
continue
}
$localPath = Join-Path $repoRoot ($relativePath -replace '/', [IO.Path]::DirectorySeparatorChar)
if (-not (Test-Path -Path $localPath -PathType Leaf)) {
Write-Host "Skipping missing local file: $relativePath" -ForegroundColor DarkYellow
continue
}
$remoteFile = "$quickRemoteRoot/$subPath"
$remoteParent = ($remoteFile -replace '/[^/]+$','')
$syncAction = "Upload changed file via SCP"
if ($PSCmdlet.ShouldProcess("$remoteTarget`:$remoteFile", $syncAction)) {
ssh -- "$remoteTarget" "mkdir -p '$remoteParent'"
if ($LASTEXITCODE -ne 0) {
throw "Failed to create remote directory $remoteParent on $host_ (exit code $LASTEXITCODE)"
}
scp -- "$localPath" "$remoteTarget`:$remoteFile"
if ($LASTEXITCODE -ne 0) {
throw "Failed to upload $relativePath to $remoteFile on $host_ (exit code $LASTEXITCODE)"
}
}
$syncedFiles += $relativePath
}
$allResults += @{
Host = $host_
User = $User
Quick = $true
FileCount = $syncedFiles.Count
Files = $syncedFiles
WhatIf = [bool]$WhatIfPreference
}
}
if ($WhatIfPreference) {
Write-Host "WhatIf simulation complete (quick mode)." -ForegroundColor Green
} else {
Write-Host "`nQuick deployment complete to $($RemoteHost.Count) host(s)." -ForegroundColor Green
}
return $allResults
}
# Generate dev version with timestamp if -Dev flag is used
if ($Dev) {
$now = Get-Date
$Version = $now.ToString("yyyy.MM.dd.HHmm")
Write-Host "Generated dev version: $Version" -ForegroundColor Cyan
}
function Get-LatestPackagePath {
param([string]$Path)
$latest = Get-ChildItem -Path $Path -Filter 'compose.manager-*-noarch-*.txz' -File |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if (-not $latest) {
throw "No package found in $Path"
}
return $latest.FullName
}
if (-not $PackagePath) {
if ($SkipBuild) {
Write-Host "Skipping build; using latest package from archive..." -ForegroundColor Yellow
$PackagePath = Get-LatestPackagePath -Path $archiveDir
} else {
$buildTarget = "local package"
$buildAction = "Build package via build.ps1"
if ($PSCmdlet.ShouldProcess($buildTarget, $buildAction)) {
Write-Host "Building package..." -ForegroundColor Yellow
$buildParams = @{
ComposeVersion = $ComposeVersion
AceVersion = $AceVersion
}
if ($Dev) {
$buildParams.Dev = $true
} elseif ($Version) {
$buildParams.Version = $Version
}
$buildResult = & (Join-Path $scriptDir "build.ps1") @buildParams
$buildInfo = if ($buildResult -is [array]) { $buildResult | Where-Object { $_ -is [hashtable] } | Select-Object -Last 1 } else { $buildResult }
$PackagePath = $buildInfo.PackagePath
} else {
if ($Dev) {
$now = Get-Date
$simulatedVersion = $now.ToString("yyyy.MM.dd.HHmm")
} elseif ($Version) {
$simulatedVersion = $Version
} else {
$simulatedVersion = "<version-from-plg>"
}
$PackagePath = Join-Path $archiveDir "compose.manager-package-$simulatedVersion.txz"
Write-Host "WhatIf: Simulating build output package path: $PackagePath" -ForegroundColor DarkYellow
}
}
} else {
if (-not (Test-Path -Path $PackagePath -PathType Leaf)) {
if ($WhatIfPreference) {
Write-Host "WhatIf: PackagePath does not exist locally, continuing with simulated deploy target: $PackagePath" -ForegroundColor DarkYellow
} else {
throw "PackagePath does not exist: $PackagePath"
}
} else {
$PackagePath = (Resolve-Path $PackagePath).Path
}
}
$packageName = Split-Path -Leaf $PackagePath
if (-not $RemoteHost -or $RemoteHost.Count -eq 0) {
Write-Host "No RemoteHost specified — build only, skipping deploy." -ForegroundColor Yellow
return @{
Hosts = @()
User = $User
PackagePath = $PackagePath
WhatIf = [bool]$WhatIfPreference
}
}
# Prefer plugin manifest generated by build.ps1 for this exact package; fallback to repository source .plg
if ($buildInfo -and $buildInfo.PluginPath -and (Test-Path -Path $buildInfo.PluginPath -PathType Leaf)) {
$pluginPath = $buildInfo.PluginPath
} else {
$pluginPath = Join-Path $scriptDir "compose.manager.plg"
}
if (-not (Test-Path -Path $pluginPath -PathType Leaf)) {
throw "Plugin file not found: $pluginPath"
}
$pluginName = Split-Path -Leaf $pluginPath
$installScriptLocal = Join-Path $scriptDir "install.sh"
if (-not (Test-Path -Path $installScriptLocal -PathType Leaf)) {
throw "Install script not found: $installScriptLocal"
}
$allResults = @()
foreach ($host_ in $RemoteHost) {
$remoteTarget = "$User@$host_"
$remotePackage = "$RemoteDir/$packageName"
$remotePlugin = "$RemoteDir/$pluginName"
$remoteInstallScript = "$RemoteDir/install.sh"
Write-Host "`nDeploying to $remoteTarget :" -ForegroundColor Green
Write-Host " Local package : $PackagePath" -ForegroundColor Gray
Write-Host " Local .plg : $pluginPath" -ForegroundColor Gray
Write-Host " Local install : $installScriptLocal" -ForegroundColor Gray
Write-Host " Remote target : ${remoteTarget}:$RemoteDir" -ForegroundColor Gray
$uploadAction = "Upload package + .plg + install.sh via SCP"
if ($PSCmdlet.ShouldProcess("${remoteTarget}:$RemoteDir/", $uploadAction)) {
Write-Host "Uploading package, .plg and install.sh via SCP..." -ForegroundColor Yellow
scp -- "$PackagePath" "$remoteTarget`:$RemoteDir/"
scp -- "$pluginPath" "$remoteTarget`:$RemoteDir/"
scp -- "$installScriptLocal" "$remoteTarget`:$remoteInstallScript"
if ($LASTEXITCODE -ne 0) {
throw "SCP upload to $host_ failed with exit code $LASTEXITCODE"
}
}
$installAction = "Execute remote install script"
if ($PSCmdlet.ShouldProcess($remoteTarget, $installAction)) {
Write-Host "Executing remote install script..." -ForegroundColor Yellow
ssh -- "$remoteTarget" "bash '$remoteInstallScript' '$remotePackage' '$remotePlugin' && rm -f '$remoteInstallScript'"
if ($LASTEXITCODE -ne 0) {
throw "Remote install script on $host_ failed with exit code $LASTEXITCODE"
}
}
$allResults += @{
Host = $host_
User = $User
PackagePath = $PackagePath
RemotePackage = $remotePackage
WhatIf = [bool]$WhatIfPreference
}
}
if ($WhatIfPreference) {
Write-Host "WhatIf simulation complete." -ForegroundColor Green
} else {
Write-Host "`nDeployment complete to $($RemoteHost.Count) host(s)." -ForegroundColor Green
}
return $allResults