-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathinstall.ps1
More file actions
207 lines (182 loc) · 7.32 KB
/
Copy pathinstall.ps1
File metadata and controls
207 lines (182 loc) · 7.32 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
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
[CmdletBinding()]
param(
[string]$InstallDir,
[switch]$Help
)
$ErrorActionPreference = 'Stop'
$Repository = 'NVIDIA/NeMo-Relay'
$GitHubUrl = "https://github.com/$Repository"
$GitHubApiUrl = "https://api.github.com/repos/$Repository"
function Show-Usage {
@'
Install the NeMo Relay CLI from GitHub Releases.
Usage:
irm https://raw.githubusercontent.com/NVIDIA/NeMo-Relay/main/install.ps1 | iex
.\install.ps1 [-InstallDir DIR] [-Help]
Environment:
NEMO_RELAY_VERSION Release to install, for example 0.5.0 or v0.5.0.
Defaults to the latest stable release.
Options:
-InstallDir DIR Destination directory (default: %LOCALAPPDATA%\nemo-relay\bin).
-Help Show this help text.
Examples:
irm https://raw.githubusercontent.com/NVIDIA/NeMo-Relay/main/install.ps1 | iex
$env:NEMO_RELAY_VERSION = '0.5.0'; irm https://raw.githubusercontent.com/NVIDIA/NeMo-Relay/main/install.ps1 | iex
.\install.ps1 -InstallDir "$HOME\bin"
'@ | Write-Output
}
function Fail([string]$Message) {
throw "nemo-relay installer: $Message"
}
function Get-ReleaseVersion {
$version = $env:NEMO_RELAY_VERSION
if ([string]::IsNullOrWhiteSpace($version)) {
Write-Host 'Finding the latest stable NeMo Relay release...'
try {
$headers = @{
Accept = 'application/vnd.github+json'
'User-Agent' = 'nemo-relay-install-script'
}
if (-not [string]::IsNullOrWhiteSpace($env:GH_TOKEN)) {
$headers.Authorization = "Bearer $env:GH_TOKEN"
}
$release = Invoke-RestMethod -Uri "$GitHubApiUrl/releases/latest" -Headers $headers -TimeoutSec 300
}
catch {
Fail 'could not resolve the latest stable release'
}
$version = $release.tag_name
if ([string]::IsNullOrWhiteSpace($version)) {
Fail 'latest release response did not contain a tag name'
}
}
$version = $version -replace '^v', ''
if ($version -notmatch '^[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9]+)?$') {
Fail "unsupported version '$version'; expected 0.5.0 or a prerelease such as 0.5.0-rc.1"
}
return $version
}
function Get-Target {
$architecture = $env:PROCESSOR_ARCHITEW6432
if ([string]::IsNullOrWhiteSpace($architecture)) {
$architecture = $env:PROCESSOR_ARCHITECTURE
}
if ([string]::IsNullOrWhiteSpace($architecture)) {
Fail "unsupported Windows architecture '$architecture'. Supported platforms: Windows x86_64 and Windows ARM64. For other platforms, use 'cargo install nemo-relay-cli'."
}
switch ($architecture.ToUpperInvariant()) {
'AMD64' { return 'x86_64-pc-windows-msvc' }
'X86_64' { return 'x86_64-pc-windows-msvc' }
'ARM64' { return 'aarch64-pc-windows-msvc' }
default { Fail "unsupported Windows architecture '$architecture'. Supported platforms: Windows x86_64 and Windows ARM64. For other platforms, use 'cargo install nemo-relay-cli'." }
}
}
function Download-File([string]$Uri, [string]$Path) {
try {
Invoke-WebRequest -Uri $Uri -OutFile $Path -UseBasicParsing -TimeoutSec 300
}
catch {
Fail "could not download $Uri"
}
}
function Add-ToPath([string]$Value, [string]$Directory) {
foreach ($entry in ($Value -split ';')) {
if ($entry -and [string]::Equals($entry.Trim().TrimEnd('\'), $Directory, [System.StringComparison]::OrdinalIgnoreCase)) {
return $Value
}
}
if ([string]::IsNullOrEmpty($Value)) {
return $Directory
}
return "$Value;$Directory"
}
function Add-InstallDirectoryToPath([string]$Directory) {
$directory = [System.IO.Path]::GetFullPath($Directory).TrimEnd('\')
$userEnvironmentKey = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true)
if ($null -eq $userEnvironmentKey) {
Fail 'could not open the Windows user environment registry key'
}
try {
$userPath = $userEnvironmentKey.GetValue(
'Path',
$null,
[Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
)
if ($null -eq $userPath) {
$userPath = ''
$userPathKind = [Microsoft.Win32.RegistryValueKind]::String
}
else {
$userPath = [string]$userPath
$userPathKind = $userEnvironmentKey.GetValueKind('Path')
}
$updatedUserPath = Add-ToPath $userPath $directory
if ($updatedUserPath -ne $userPath) {
$userEnvironmentKey.SetValue('Path', $updatedUserPath, $userPathKind)
}
}
finally {
$userEnvironmentKey.Dispose()
}
$env:Path = Add-ToPath $env:Path $directory
}
if ($Help) {
Show-Usage
exit 0
}
try {
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
if ([string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) {
Fail 'LOCALAPPDATA must be set to choose the default install directory'
}
$InstallDir = Join-Path $env:LOCALAPPDATA 'nemo-relay\bin'
}
if ([string]::IsNullOrWhiteSpace($InstallDir)) {
Fail 'install directory must not be empty'
}
$version = Get-ReleaseVersion
$target = Get-Target
$asset = "nemo-relay-cli-$target-$version.exe"
$assetUrl = "$GitHubUrl/releases/download/$version/$asset"
$checksumUrl = "$assetUrl.sha256"
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
if (-not (Test-Path -LiteralPath $InstallDir -PathType Container)) {
Fail "install path is not a directory: $InstallDir"
}
$InstallDir = [System.IO.Path]::GetFullPath($InstallDir)
$downloadFile = Join-Path $InstallDir ".nemo-relay.download.$([guid]::NewGuid().ToString('N'))"
$checksumFile = Join-Path $InstallDir ".nemo-relay.checksum.$([guid]::NewGuid().ToString('N'))"
$backupFile = Join-Path $InstallDir ".nemo-relay.backup.$([guid]::NewGuid().ToString('N'))"
$destination = Join-Path $InstallDir 'nemo-relay.exe'
try {
Write-Output "Downloading NeMo Relay CLI $version for $target..."
Download-File $assetUrl $downloadFile
Download-File $checksumUrl $checksumFile
$expectedChecksum = ((Get-Content -LiteralPath $checksumFile -TotalCount 1).Trim() -split '\s+')[0].ToLowerInvariant()
if ($expectedChecksum -notmatch '^[0-9a-f]{64}$') {
Fail "invalid checksum file for $asset"
}
$actualChecksum = (Get-FileHash -LiteralPath $downloadFile -Algorithm SHA256).Hash.ToLowerInvariant()
if ($actualChecksum -ne $expectedChecksum) {
Fail "checksum verification failed for $asset"
}
if (Test-Path -LiteralPath $destination) {
[System.IO.File]::Replace($downloadFile, $destination, $backupFile)
}
else {
[System.IO.File]::Move($downloadFile, $destination)
}
}
finally {
Remove-Item -LiteralPath $downloadFile, $checksumFile, $backupFile -Force -ErrorAction SilentlyContinue
}
Add-InstallDirectoryToPath $InstallDir
Write-Output "Installed NeMo Relay CLI $version to $destination"
Write-Output "Added $InstallDir to the Windows user PATH. Newly opened shells inherit this change."
}
catch {
Write-Error $_
exit 1
}