-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
375 lines (341 loc) · 13.6 KB
/
install.ps1
File metadata and controls
375 lines (341 loc) · 13.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
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
param(
[string]$Version = $(if ($env:SDM_CLICKHOUSE_VERSION) { $env:SDM_CLICKHOUSE_VERSION } else { "latest" }),
[string]$Repo = $(if ($env:SDM_CLICKHOUSE_REPO) { $env:SDM_CLICKHOUSE_REPO } else { "lord007tn/sdm-clickhouse" }),
[string]$GitHubToken = $(if ($env:GITHUB_TOKEN) { $env:GITHUB_TOKEN } elseif ($env:GH_TOKEN) { $env:GH_TOKEN } else { "" }),
[switch]$SystemInstall,
[switch]$Portable,
[switch]$CheckOnly
)
$ErrorActionPreference = "Stop"
function Get-OsName {
$platform = [System.Runtime.InteropServices.OSPlatform]
if ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($platform::Windows)) { return "windows" }
if ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($platform::Linux)) { return "linux" }
if ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($platform::OSX)) { return "macos" }
return "unknown"
}
function Get-ArchName {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant()
if ($arch -eq "x64") { return "x64" }
if ($arch -eq "arm64") { return "arm64" }
return "unknown"
}
function Test-Truthy {
param([string]$Value)
if ([string]::IsNullOrWhiteSpace($Value)) { return $false }
$normalized = $Value.Trim().ToLowerInvariant()
return $normalized -in @("1", "true", "yes", "on")
}
function Select-Asset {
param(
[array]$Assets,
[string]$OsName,
[string]$ArchName,
[bool]$PreferPortable = $false
)
$withLower = $Assets | ForEach-Object {
[PSCustomObject]@{ Asset = $_; Name = $_.name.ToLowerInvariant() }
}
function Find-Asset([scriptblock]$Predicate) {
$hit = $withLower | Where-Object { & $Predicate $_.Name } | Select-Object -First 1
if ($null -ne $hit) { return $hit.Asset }
return $null
}
function First-Asset([array]$Candidates) {
foreach ($candidate in $Candidates) {
if ($null -ne $candidate) { return $candidate }
}
return $null
}
if ($OsName -eq "windows") {
if ($PreferPortable) {
if ($ArchName -eq "arm64") {
$portableAsset = First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".zip") -and $n.Contains("portable") -and ($n.Contains("arm64") -or $n.Contains("aarch64")) }),
(Find-Asset { param($n) $n.EndsWith(".zip") -and $n.Contains("portable") })
)
} else {
$portableAsset = First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".zip") -and $n.Contains("portable") -and ($n.Contains("x64") -or $n.Contains("amd64")) }),
(Find-Asset { param($n) $n.EndsWith(".zip") -and $n.Contains("portable") })
)
}
if ($null -ne $portableAsset) { return $portableAsset }
}
if ($ArchName -eq "arm64") {
return First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".exe") -and $n.Contains("arm64") }),
(Find-Asset { param($n) $n.EndsWith(".msi") -and $n.Contains("arm64") })
)
}
return First-Asset @(
(Find-Asset { param($n) $n.EndsWith("-setup.exe") -and $n.Contains("x64") }),
(Find-Asset { param($n) $n.EndsWith(".exe") }),
(Find-Asset { param($n) $n.EndsWith(".msi") -and $n.Contains("x64") }),
(Find-Asset { param($n) $n.EndsWith(".msi") -and $n.Contains("amd64") }),
(Find-Asset { param($n) $n.EndsWith(".msi") })
)
}
if ($OsName -eq "linux") {
if ($PreferPortable) {
if ($ArchName -eq "arm64") {
$portableAsset = First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".appimage") -and $n.Contains("aarch64") }),
(Find-Asset { param($n) $n.EndsWith(".appimage") -and $n.Contains("arm64") }),
(Find-Asset { param($n) $n.EndsWith(".appimage") })
)
} else {
$portableAsset = First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".appimage") -and $n.Contains("amd64") }),
(Find-Asset { param($n) $n.EndsWith(".appimage") -and $n.Contains("x64") }),
(Find-Asset { param($n) $n.EndsWith(".appimage") })
)
}
if ($null -ne $portableAsset) { return $portableAsset }
}
if ($ArchName -eq "arm64") {
return First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".appimage") -and $n.Contains("aarch64") }),
(Find-Asset { param($n) $n.EndsWith(".appimage") }),
(Find-Asset { param($n) $n.EndsWith("_arm64.deb") }),
(Find-Asset { param($n) $n.EndsWith("_aarch64.deb") })
)
}
return First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".appimage") -and $n.Contains("amd64") }),
(Find-Asset { param($n) $n.EndsWith(".appimage") }),
(Find-Asset { param($n) $n.EndsWith("_amd64.deb") }),
(Find-Asset { param($n) $n.EndsWith("_x64.deb") })
)
}
if ($OsName -eq "macos") {
if ($PreferPortable) {
if ($ArchName -eq "arm64") {
$portableAsset = First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".app.tar.gz") -and ($n.Contains("aarch64") -or $n.Contains("arm64")) }),
(Find-Asset { param($n) $n.EndsWith(".app.tar.gz") })
)
} else {
$portableAsset = First-Asset @(
(Find-Asset { param($n) $n.EndsWith(".app.tar.gz") -and ($n.Contains("x64") -or $n.Contains("amd64")) }),
(Find-Asset { param($n) $n.EndsWith(".app.tar.gz") })
)
}
if ($null -ne $portableAsset) { return $portableAsset }
}
if ($ArchName -eq "arm64") {
return Find-Asset { param($n) $n.EndsWith("_aarch64.dmg") }
}
return First-Asset @(
(Find-Asset { param($n) $n.EndsWith("_x64.dmg") }),
(Find-Asset { param($n) $n.EndsWith("_amd64.dmg") }),
(Find-Asset { param($n) $n.EndsWith(".dmg") })
)
}
return $null
}
function Assert-Sha256 {
param(
[string]$Path,
[string]$Expected
)
$hash = (Get-FileHash -Path $Path -Algorithm SHA256).Hash.ToLowerInvariant()
$exp = $Expected.ToLowerInvariant()
if ($hash -ne $exp) {
throw "SHA256 mismatch. Expected $exp but got $hash"
}
}
function Invoke-GitHubApi {
param(
[string]$Url,
[hashtable]$Headers
)
try {
return Invoke-RestMethod -Uri $Url -Headers $Headers
} catch {
$message = $_.Exception.Message
if ($message -match "404") {
throw "GitHub API returned 404 for '$Url'. Check repository name/visibility and set GITHUB_TOKEN (or GH_TOKEN) for private repositories."
}
throw
}
}
function Install-Asset {
param(
[string]$Path,
[string]$AssetName,
[string]$OsName,
[bool]$SystemInstallMode = $false,
[bool]$PortableMode = $false
)
$lower = $AssetName.ToLowerInvariant()
if ($OsName -eq "windows") {
if ($lower.EndsWith(".zip") -and ($PortableMode -or $lower.Contains("portable"))) {
$targetDir = if ($env:SDM_CLICKHOUSE_PORTABLE_DIR) {
$env:SDM_CLICKHOUSE_PORTABLE_DIR
} else {
Join-Path $HOME "sdm-clickhouse-portable"
}
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
Expand-Archive -LiteralPath $Path -DestinationPath $targetDir -Force
$portableExe = Get-ChildItem -Path $targetDir -Filter *.exe -File -Recurse |
Where-Object { $_.Name.ToLowerInvariant().Contains("sdm") -and $_.Name.ToLowerInvariant().Contains("clickhouse") } |
Select-Object -First 1
if ($null -eq $portableExe) {
$portableExe = Get-ChildItem -Path $targetDir -Filter *.exe -File -Recurse | Select-Object -First 1
}
Write-Host "Extracted portable package to $targetDir"
if ($null -ne $portableExe) {
Write-Host "Portable executable: $($portableExe.FullName)"
} else {
Write-Host "Portable executable not found automatically. Check extracted files in $targetDir"
}
return
}
if ($lower.EndsWith(".msi")) {
$args = if ($SystemInstallMode) {
"/i `"$Path`" /passive /norestart"
} else {
"/i `"$Path`" MSIINSTALLPERUSER=1 ALLUSERS=2 /qb /norestart"
}
Start-Process -FilePath "msiexec.exe" -ArgumentList $args -Wait
return
}
if ($lower.EndsWith(".exe")) {
if ($SystemInstallMode) {
Start-Process -FilePath $Path -Wait
} else {
Start-Process -FilePath $Path -ArgumentList "/CURRENTUSER" -Wait
}
return
}
throw "Unsupported Windows asset: $AssetName"
}
if ($OsName -eq "linux") {
if ($lower.EndsWith(".deb")) {
if (-not $SystemInstallMode) {
throw "Refusing .deb install without -SystemInstall. Use AppImage for user-space install."
}
if (Get-Command sudo -ErrorAction SilentlyContinue) {
& sudo dpkg -i $Path
} else {
& dpkg -i $Path
}
return
}
if ($lower.EndsWith(".appimage")) {
$targetDir = if ($env:SDM_CLICKHOUSE_APPIMAGE_DIR) { $env:SDM_CLICKHOUSE_APPIMAGE_DIR } else { Join-Path $HOME ".local/bin" }
New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
$targetPath = Join-Path $targetDir "sdm-clickhouse.AppImage"
Copy-Item -Force -Path $Path -Destination $targetPath
& chmod +x $targetPath
Write-Host "Installed AppImage to $targetPath"
return
}
throw "Unsupported Linux asset: $AssetName"
}
if ($OsName -eq "macos") {
if ($lower.EndsWith(".app.tar.gz")) {
$extractDir = Join-Path ([System.IO.Path]::GetTempPath()) ("sdm-clickhouse-portable-" + [guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $extractDir -Force | Out-Null
& tar -xzf $Path -C $extractDir
if ($LASTEXITCODE -ne 0) {
throw "Failed to extract portable macOS app archive."
}
$app = Get-ChildItem -Path $extractDir -Filter *.app -Directory -Recurse | Select-Object -First 1
if ($null -eq $app) {
throw "No .app found in portable archive."
}
$destination = if ($SystemInstallMode) { "/Applications/" } else { (Join-Path $HOME "Applications") }
if (-not (Test-Path $destination)) {
New-Item -ItemType Directory -Path $destination -Force | Out-Null
}
if ($SystemInstallMode -and (Get-Command sudo -ErrorAction SilentlyContinue)) {
& sudo cp -R $app.FullName $destination
} else {
Copy-Item -Recurse -Force -Path $app.FullName -Destination $destination
}
Write-Host "Installed $($app.Name) into $destination"
return
}
if (-not $lower.EndsWith(".dmg")) {
throw "Unsupported macOS asset: $AssetName"
}
$mountLine = & hdiutil attach $Path -nobrowse -quiet | Select-String "/Volumes/" | Select-Object -First 1
if ($null -eq $mountLine) {
throw "Failed to mount DMG."
}
$mountPoint = ($mountLine.ToString() -split "\s+")[-1]
$app = Get-ChildItem -Path $mountPoint -Filter *.app -Directory | Select-Object -First 1
if ($null -eq $app) {
& hdiutil detach $mountPoint -quiet | Out-Null
throw "No .app found in DMG."
}
$destination = if ($SystemInstallMode) { "/Applications/" } else { (Join-Path $HOME "Applications") }
if (-not (Test-Path $destination)) {
New-Item -ItemType Directory -Path $destination -Force | Out-Null
}
Copy-Item -Recurse -Force -Path $app.FullName -Destination $destination
& hdiutil detach $mountPoint -quiet | Out-Null
return
}
throw "Unsupported OS: $OsName"
}
$osName = Get-OsName
$archName = Get-ArchName
if ($osName -eq "unknown") {
throw "Unsupported operating system."
}
$portableMode = $Portable.IsPresent -or (Test-Truthy $env:SDM_CLICKHOUSE_PORTABLE)
$headers = @{ Accept = "application/vnd.github+json" }
if ($GitHubToken) {
$headers.Authorization = "Bearer $GitHubToken"
}
$release = $null
$asset = $null
if ($Version -eq "latest") {
$releaseUrl = "https://api.github.com/repos/$Repo/releases?per_page=20"
Write-Host "Resolving release metadata from $releaseUrl"
$candidates = Invoke-GitHubApi -Url $releaseUrl -Headers $headers
foreach ($candidate in $candidates) {
if ($candidate.draft -or $candidate.prerelease) { continue }
$candidateAsset = Select-Asset -Assets $candidate.assets -OsName $osName -ArchName $archName -PreferPortable $portableMode
if ($null -eq $candidateAsset) { continue }
$candidateDigest = [string]$candidateAsset.digest
if (-not $candidateDigest.StartsWith("sha256:")) { continue }
$release = $candidate
$asset = $candidateAsset
break
}
if ($null -eq $release -or $null -eq $asset) {
throw "No compatible release asset found for $osName/$archName."
}
} else {
$tag = if ($Version.StartsWith("v")) { $Version } else { "v$Version" }
$releaseUrl = "https://api.github.com/repos/$Repo/releases/tags/$tag"
Write-Host "Resolving release metadata from $releaseUrl"
$release = Invoke-GitHubApi -Url $releaseUrl -Headers $headers
$asset = Select-Asset -Assets $release.assets -OsName $osName -ArchName $archName -PreferPortable $portableMode
if ($null -eq $asset) {
throw "No compatible release asset found for $osName/$archName in tag $tag."
}
}
$digest = [string]$asset.digest
if (-not $digest.StartsWith("sha256:")) {
throw "Release asset digest is missing SHA256 metadata."
}
$expectedSha256 = $digest.Substring(7)
$latestVersion = ([string]$release.tag_name).TrimStart("v")
Write-Host "Target release: v$latestVersion"
Write-Host "Selected asset: $($asset.name) ($osName/$archName)"
if ($CheckOnly) {
Write-Host "Check only mode: update metadata resolved."
exit 0
}
$tmpPath = Join-Path ([System.IO.Path]::GetTempPath()) $asset.name
Write-Host "Downloading asset..."
Invoke-WebRequest -Uri $asset.browser_download_url -Headers $headers -OutFile $tmpPath
Assert-Sha256 -Path $tmpPath -Expected $expectedSha256
Write-Host "SHA256 verified."
Install-Asset -Path $tmpPath -AssetName $asset.name -OsName $osName -SystemInstallMode $SystemInstall.IsPresent -PortableMode $portableMode
Write-Host "SDM ClickHouse installation complete."