-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
479 lines (396 loc) · 14 KB
/
Copy pathinstall.ps1
File metadata and controls
479 lines (396 loc) · 14 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
#!/usr/bin/env pwsh
# flextunnel installer for Windows
# Downloads latest binary from: https://github.com/flexaccessdev/flextunnel/releases
# Installs system-wide to C:\Program Files\flextunnel — requires an elevated
# (Administrator) PowerShell session.
#
# Usage: .\install.ps1 [RELEASE_TAG] [-PreRelease]
# Or set $env:RELEASE_TAG environment variable
param(
[Parameter(Position = 0)]
[string]$ReleaseTag,
[Parameter()]
[switch]$PreRelease,
[Parameter()]
[switch]$DownloadOnly
)
$ErrorActionPreference = "Stop"
$REPO_OWNER = "flexaccessdev"
$REPO_NAME = "flextunnel"
# Function to print colored messages
function Print-Info {
param([string]$Message)
Write-Host "[INFO] $Message" -ForegroundColor Green
}
function Print-Warn {
param([string]$Message)
Write-Host "[WARN] $Message" -ForegroundColor Yellow
}
function Print-Error {
param([string]$Message)
Write-Host "[ERROR] $Message" -ForegroundColor Red
}
# Fetch the latest stable release tag (non-prerelease)
function Get-LatestReleaseTag {
$apiUrl = "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest"
try {
$release = Invoke-RestMethod -Uri $apiUrl -Method Get
}
catch {
Print-Error "Failed to fetch latest release from GitHub: $_"
exit 1
}
if (-not $release.tag_name) {
Print-Error "Could not find a latest release on GitHub"
exit 1
}
return $release.tag_name
}
# Fetch the latest prerelease tag
function Get-LatestPrereleaseTag {
$apiUrl = "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases?per_page=30"
try {
$releases = Invoke-RestMethod -Uri $apiUrl -Method Get
}
catch {
Print-Error "Failed to fetch releases from GitHub: $_"
exit 1
}
# Only consider prereleases that actually ship the Windows binary, so the
# tag we return can be installed (matches the -PreRelease help text).
$binaryName = Get-BinaryName -Arch (Get-Architecture)
$latestPrerelease = $releases |
Where-Object { $_.prerelease -eq $true -and ($_.assets | Where-Object { $_.name -eq $binaryName }) } |
Select-Object -First 1 -ExpandProperty tag_name
if (-not $latestPrerelease) {
Print-Error "Could not find a prerelease on GitHub that includes $binaryName"
exit 1
}
return $latestPrerelease
}
# Fetch full release info (including asset checksums) from GitHub API
function Get-ReleaseInfo {
param([string]$Tag)
$apiUrl = "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$Tag"
try {
$release = Invoke-RestMethod -Uri $apiUrl -Method Get
return $release
}
catch {
Print-Warn "Could not fetch release info: $_"
return $null
}
}
# Extract SHA-256 checksum from release JSON for a specific binary
function Get-ExpectedChecksum {
param(
[object]$ReleaseInfo,
[string]$BinaryName
)
if (-not $ReleaseInfo -or -not $ReleaseInfo.assets) {
return $null
}
# Find the asset matching the binary name
$asset = $ReleaseInfo.assets | Where-Object { $_.name -eq $BinaryName } | Select-Object -First 1
if (-not $asset) {
return $null
}
# Extract sha256 hash from digest field
if ($asset.digest -match 'sha256:([a-f0-9]+)') {
return $matches[1]
}
return $null
}
# Compute SHA-256 checksum of a file
function Get-FileChecksum {
param([string]$FilePath)
try {
$hash = Get-FileHash -Path $FilePath -Algorithm SHA256
return $hash.Hash.ToLower()
}
catch {
Print-Error "Failed to compute checksum: $_"
return $null
}
}
# Verify file checksum against expected value
function Test-Checksum {
param(
[string]$FilePath,
[string]$ExpectedChecksum
)
Print-Info "Verifying checksum..."
$actualChecksum = Get-FileChecksum -FilePath $FilePath
if (-not $actualChecksum) {
return $false
}
if ($ExpectedChecksum -eq $actualChecksum) {
$shortHash = $actualChecksum.Substring(0, 16)
Print-Info "Checksum verified: $shortHash..."
return $true
}
else {
Print-Error "Checksum verification FAILED!"
Print-Error "Expected: $ExpectedChecksum"
Print-Error "Actual: $actualChecksum"
return $false
}
}
# Detect architecture
function Get-Architecture {
$arch = [System.Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
if ($arch -ne "AMD64") {
Print-Error "Unsupported architecture: $arch"
Print-Error "Only AMD64 (x86_64) is supported for Windows"
exit 1
}
return "amd64"
}
# Get binary name based on architecture
function Get-BinaryName {
param([string]$Arch)
if ($Arch -ne "amd64") {
Print-Error "Unsupported architecture: $Arch"
Print-Error "Only amd64 is supported for Windows"
exit 1
}
return "flextunnel-windows-amd64.exe"
}
# Download binary and verify checksum
function Download-Binary {
param(
[string]$Url,
[string]$OutputPath,
[string]$ExpectedChecksum
)
Print-Info "Downloading from $Url"
# Download the binary
try {
Invoke-WebRequest -Uri $Url -OutFile $OutputPath -UseBasicParsing
}
catch {
Print-Error "Failed to download binary: $_"
exit 1
}
# Verify checksum
if (-not $ExpectedChecksum) {
Print-Error "No checksum available. Aborting."
Remove-Item -Path $OutputPath -Force -ErrorAction SilentlyContinue
exit 1
}
if (-not (Test-Checksum -FilePath $OutputPath -ExpectedChecksum $ExpectedChecksum)) {
Print-Error "Binary integrity check failed. Aborting."
Remove-Item -Path $OutputPath -Force -ErrorAction SilentlyContinue
exit 1
}
}
# Download only - save to current directory
function Download-Only {
param(
[string]$BaseUrl,
[string]$BinaryName,
[string]$ExpectedChecksum
)
$url = "$BaseUrl/$BinaryName"
$outputFile = Join-Path (Get-Location) $BinaryName
Download-Binary -Url $url -OutputPath $outputFile -ExpectedChecksum $ExpectedChecksum
# Test the binary
Print-Info "Testing downloaded binary..."
try {
$versionInfo = & $outputFile --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Binary returned non-zero exit code"
}
Print-Info "Binary test successful: $versionInfo"
}
catch {
Print-Error "Binary test failed. The downloaded file may be corrupted or incompatible."
Print-Error "Output: $_"
Remove-Item -Path $outputFile -Force -ErrorAction SilentlyContinue
exit 1
}
Print-Info "Binary saved to: $outputFile"
}
# Download binary to temporary location, test it, and install
function Install-Binary {
param(
[string]$BaseUrl,
[string]$BinaryName,
[string]$ExpectedChecksum
)
$url = "$BaseUrl/$BinaryName"
$tempDir = Join-Path $env:TEMP "flextunnel-install-$(Get-Random)"
$tempBinary = Join-Path $tempDir $BinaryName
$installDir = Join-Path $env:ProgramFiles "flextunnel"
$finalPath = Join-Path $installDir "flextunnel.exe"
try {
# Create temp directory
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
Download-Binary -Url $url -OutputPath $tempBinary -ExpectedChecksum $ExpectedChecksum
# Test the binary
Print-Info "Testing downloaded binary..."
try {
$versionInfo = & $tempBinary --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Binary returned non-zero exit code"
}
Print-Info "Binary test successful: $versionInfo"
}
catch {
Print-Error "Binary test failed. The downloaded file may be corrupted or incompatible."
Print-Error "Output: $_"
exit 1
}
# Create target directory if it doesn't exist
if (-not (Test-Path $installDir)) {
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
}
# Move the tested binary to final location
try {
Move-Item -Path $tempBinary -Destination $finalPath -Force
}
catch {
Print-Error "Failed to move binary to final location: $_"
exit 1
}
Print-Info "Binary installed successfully to $finalPath"
# Add to the machine-wide PATH if not already there (system-wide install,
# so this must go in Machine scope, not User).
$machinePath = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
if ($machinePath -notlike "*$installDir*") {
Print-Warn "$installDir is not in the system PATH"
Print-Warn "Adding to machine PATH..."
try {
$newPath = if ($machinePath) { "$machinePath;$installDir" } else { $installDir }
[System.Environment]::SetEnvironmentVariable("Path", $newPath, "Machine")
Print-Info "Added to PATH. You may need to restart your terminal for changes to take effect."
}
catch {
Print-Warn "Failed to add to PATH automatically. Please add manually:"
Print-Warn "$installDir"
}
}
else {
Print-Info "$installDir is already in the system PATH"
}
}
finally {
# Clean up temp directory
if (Test-Path $tempDir) {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
# Display usage information
function Show-Usage {
Write-Host @"
Usage: .\install.ps1 [OPTIONS] [RELEASE_TAG]
Download and install flextunnel binary system-wide (C:\Program Files\flextunnel)
Options:
-DownloadOnly Download binary to current directory without installing
-PreRelease Use latest prerelease if it includes a Windows asset
-h, --help Show this help message
Arguments:
RELEASE_TAG GitHub release tag to download (default: latest)
Environment variables:
`$env:RELEASE_TAG Alternative way to specify release tag
Examples:
.\install.ps1 # Install latest release
.\install.ps1 vX.Y.Z # Install specific release
.\install.ps1 -PreRelease # Install latest prerelease
.\install.ps1 -DownloadOnly # Download latest to current directory
.\install.ps1 -DownloadOnly vX.Y.Z # Download specific release
`$env:RELEASE_TAG='latest'; .\install.ps1 # Use environment variable
Supported platforms: Windows (amd64)
Note: Installing (not -DownloadOnly) requires an elevated (Administrator) PowerShell session.
"@
}
# Check for administrator privileges, required for a system-wide install.
function Test-AdminPrivileges {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Print-Error "Installing to C:\Program Files\flextunnel requires an elevated (Administrator) PowerShell session."
Print-Error "Re-run this script from an Administrator PowerShell, or use -DownloadOnly to skip installation."
exit 1
}
}
# Main installation function
function Start-Installation {
param(
[string]$Tag,
[bool]$DownloadOnly
)
if ($DownloadOnly) {
Print-Info "flextunnel downloader"
}
else {
Print-Info "flextunnel installer"
}
Print-Info "Release: $Tag"
Print-Info "Repository: $REPO_OWNER/$REPO_NAME"
$arch = Get-Architecture
$binaryName = Get-BinaryName -Arch $arch
Print-Info "Platform detected: windows-$arch"
Print-Info "Binary name: $binaryName"
$baseUrl = "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$Tag"
# Fetch release info for checksum verification
Print-Info "Fetching release information..."
$releaseInfo = Get-ReleaseInfo -Tag $Tag
if (-not $releaseInfo) {
Print-Error "Could not fetch release info from GitHub. Cannot verify binary integrity."
exit 1
}
$expectedChecksum = Get-ExpectedChecksum -ReleaseInfo $releaseInfo -BinaryName $binaryName
if (-not $expectedChecksum) {
Print-Error "No checksum found for $binaryName in release. Cannot verify binary integrity."
exit 1
}
$shortHash = $expectedChecksum.Substring(0, 16)
Print-Info "Expected checksum: $shortHash..."
if ($DownloadOnly) {
Download-Only -BaseUrl $baseUrl -BinaryName $binaryName -ExpectedChecksum $expectedChecksum
Print-Info "Download completed successfully!"
}
else {
Install-Binary -BaseUrl $baseUrl -BinaryName $binaryName -ExpectedChecksum $expectedChecksum
Print-Info "Installation completed successfully!"
Print-Info "You can now run 'flextunnel' from your terminal."
}
}
# Main execution
function Main {
# Handle help flags - check both parameter and ReleaseTag value
if ($args -contains "--help" -or $args -contains "-h" -or $args -contains "-?" -or $args -contains "/?" -or $args -contains "/h" -or
$ReleaseTag -eq "--help" -or $ReleaseTag -eq "-h" -or $ReleaseTag -eq "-?" -or $ReleaseTag -eq "/?" -or $ReleaseTag -eq "/h") {
Show-Usage
exit 0
}
if ($DownloadOnly) {
Print-Info "Starting flextunnel download..."
}
else {
Print-Info "Starting flextunnel installation..."
}
# Determine release tag
$tag = $ReleaseTag
if (-not $tag) {
$tag = $env:RELEASE_TAG
}
if (-not $tag) {
if ($PreRelease) {
Print-Info "Fetching latest prerelease tag from GitHub..."
$tag = Get-LatestPrereleaseTag
}
else {
Print-Info "Fetching latest release tag from GitHub..."
$tag = Get-LatestReleaseTag
}
}
if (-not $DownloadOnly) {
Test-AdminPrivileges
}
Start-Installation -Tag $tag -DownloadOnly:$DownloadOnly
}
# Run main function
Main