-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.ps1
More file actions
178 lines (147 loc) · 6.13 KB
/
Copy pathinstall.ps1
File metadata and controls
178 lines (147 loc) · 6.13 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
<#
.SYNOPSIS
Downloads and installs UCloud Sandbox CLI on Windows.
.PARAMETER Version
Release tag to install. Defaults to the latest release.
.PARAMETER InstallDir
Directory where ucloud-sandbox-cli.exe is installed.
.PARAMETER BaseUrl
Base URL of the GitHub releases page.
.EXAMPLE
Invoke-RestMethod https://raw.githubusercontent.com/ucloud/ucloud-sandbox-cli/main/install.ps1 | Invoke-Expression
.EXAMPLE
.\install.ps1 -Version v1.2.3
#>
[CmdletBinding()]
param(
[ValidateNotNullOrEmpty()]
[string]$Version = "latest",
[string]$InstallDir = "",
[ValidateNotNullOrEmpty()]
[string]$BaseUrl = "https://github.com/ucloud/ucloud-sandbox-cli/releases"
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$BinaryName = "ucloud-sandbox-cli"
$DocsUrl = "https://astraflow.ucloud.cn/docs/agent-sandbox/product/cli"
function Write-Info {
param([string]$Message)
Write-Host "> $Message"
}
function Write-Success {
param([string]$Message)
Write-Host "+ $Message" -ForegroundColor Green
}
function Get-WindowsArchitecture {
$machineArchitecture = if ($env:PROCESSOR_ARCHITEW6432) {
$env:PROCESSOR_ARCHITEW6432
} else {
$env:PROCESSOR_ARCHITECTURE
}
if ([string]::IsNullOrWhiteSpace($machineArchitecture)) {
throw "Unable to detect the Windows architecture."
}
switch ($machineArchitecture.ToUpperInvariant()) {
"AMD64" { return "amd64" }
"ARM64" { return "arm64" }
default { throw "Unsupported Windows architecture: $machineArchitecture. Supported architectures are: amd64, arm64." }
}
}
function Add-InstallDirToPath {
param([string]$Directory)
$normalizedDirectory = [Environment]::ExpandEnvironmentVariables($Directory).TrimEnd("\")
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$userEntries = @($userPath -split ";" | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
$isInUserPath = $userEntries | Where-Object {
[Environment]::ExpandEnvironmentVariables($_).Trim().TrimEnd("\") -ieq $normalizedDirectory
}
if (-not $isInUserPath) {
$newUserPath = if ([string]::IsNullOrWhiteSpace($userPath)) {
$Directory
} else {
"$Directory;$userPath"
}
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
Write-Info "Added $Directory to the user PATH."
}
$processEntries = @($env:Path -split ";" | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
$isInProcessPath = $processEntries | Where-Object {
[Environment]::ExpandEnvironmentVariables($_).Trim().TrimEnd("\") -ieq $normalizedDirectory
}
if (-not $isInProcessPath) {
$env:Path = "$Directory;$env:Path"
}
}
if ($env:OS -ne "Windows_NT") {
throw "install.ps1 only supports Windows. Use install.sh on Linux or macOS."
}
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
if ([string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) {
throw "LOCALAPPDATA is not set. Pass -InstallDir with a writable installation directory."
}
$InstallDir = Join-Path $env:LOCALAPPDATA "Programs\ucloud-sandbox-cli"
}
$InstallDir = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($InstallDir)
$architecture = Get-WindowsArchitecture
$assetName = "${BinaryName}_windows_${architecture}.zip"
$releaseBaseUrl = $BaseUrl.TrimEnd("/")
$downloadUrl = if ($Version -eq "latest") {
"$releaseBaseUrl/latest/download/$assetName"
} else {
"$releaseBaseUrl/download/$Version/$assetName"
}
$checksumUrl = "$downloadUrl.sha256"
$targetBinary = Join-Path $InstallDir "$BinaryName.exe"
$tempDir = Join-Path ([IO.Path]::GetTempPath()) "$BinaryName-install-$([Guid]::NewGuid().ToString('N'))"
$archivePath = Join-Path $tempDir $assetName
$checksumPath = "$archivePath.sha256"
$extractDir = Join-Path $tempDir "extract"
Write-Host ""
Write-Info "Welcome to the $BinaryName installer."
Write-Info "Installer configuration:"
Write-Info " Version: $Version"
Write-Info " OS: windows"
Write-Info " Arch: $architecture"
Write-Info " Install dir: $InstallDir"
Write-Info " Download URL: $downloadUrl"
Write-Host ""
try {
New-Item -ItemType Directory -Force -Path $tempDir, $extractDir, $InstallDir | Out-Null
# Windows PowerShell 5.1 can otherwise negotiate an obsolete TLS version.
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Write-Info "Downloading $BinaryName..."
Invoke-WebRequest -Uri $downloadUrl -OutFile $archivePath -UseBasicParsing
Invoke-WebRequest -Uri $checksumUrl -OutFile $checksumPath -UseBasicParsing
Write-Info "Verifying release SHA256..."
$expectedHash = (Get-Content -LiteralPath $checksumPath -Raw).Trim().ToLowerInvariant()
if ($expectedHash -notmatch "^[0-9a-f]{64}$") {
throw "Release checksum must contain exactly 64 hexadecimal characters."
}
$actualHash = (Get-FileHash -LiteralPath $archivePath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($actualHash -ne $expectedHash) {
throw "SHA256 verification failed for the downloaded release asset."
}
Write-Info "Installing $BinaryName to $InstallDir..."
Expand-Archive -LiteralPath $archivePath -DestinationPath $extractDir -Force
$extractedBinary = Join-Path $extractDir "$BinaryName.exe"
if (-not (Test-Path -LiteralPath $extractedBinary -PathType Leaf)) {
throw "Archive did not contain $BinaryName.exe."
}
Copy-Item -LiteralPath $extractedBinary -Destination $targetBinary -Force
Unblock-File -LiteralPath $targetBinary -ErrorAction SilentlyContinue
Add-InstallDirToPath -Directory $InstallDir
Write-Success "$BinaryName installed successfully."
Write-Host ""
Write-Info "Installed binary version:"
& $targetBinary version
if ($LASTEXITCODE -ne 0) {
throw "$BinaryName version exited with code $LASTEXITCODE."
}
} finally {
if (Test-Path -LiteralPath $tempDir) {
Remove-Item -LiteralPath $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
Write-Host ""
Write-Info "Documentation: $DocsUrl"
Write-Info "Run '$BinaryName login' first, then start using the CLI."