forked from dagger/dagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
450 lines (387 loc) · 15 KB
/
install.ps1
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
#Requires -Version 7.0
<#
.Description
Download and install dagger.
.PARAMETER DaggerVersion
Semver version of dagger to install.
.PARAMETER DaggerCommit
Commit SHA for a dev build of dagger to install.
.EXAMPLE
.\install.ps1
Install with default settings.
.EXAMPLE
.\install.ps1 -InstallPath path\to\dir
Install to path/to/dir.
.EXAMPLE
.\install.ps1 -DaggerVersion vX.Y.Z
Install specified version vX.Y.Z.
.EXAMPLE
.\install.ps1 -DaggerCommit head
Install latest dev build.
.EXAMPLE
.\install.ps1 -DaggerCommit [commit]
Install specified dev build [commit].
#>
Param (
[Parameter(Mandatory = $false)][System.Management.Automation.SemanticVersion]$DaggerVersion,
[Parameter(Mandatory = $false)][string][ValidatePattern("^(?:head|(?:[0-9a-fA-F]{40}))?$")]$DaggerCommit,
[Parameter(Mandatory = $false)][string]$DownloadPath = [System.IO.Path]::GetTempFileName(),
[Parameter(Mandatory = $false)][string]$InstallPath = "$env:USERPROFILE\dagger",
[Parameter(Mandatory = $false)][switch]$AddToPath = $false,
[Parameter(Mandatory = $false)][switch]$Interactive = $false
)
# ---------------------------------------------------------------------------------
# Author: Alessandro Festa
# Co Author: Brittan DeYoung
# Dagger Installation Utility for the windows dagger.exe binary
# ---------------------------------------------------------------------------------
# This function prompts the user for a download location and validates it.
function Get-DownloadPath {
$defaultPath = $DownloadPath
while ($true) {
$inputPath = Read-Host -Prompt "Enter the download location or leave empty and hit Enter for default ($defaultPath)"
if ([string]::IsNullOrWhiteSpace($inputPath)) {
return $defaultPath
}
elseif (Test-Path $inputPath -IsValid) {
return $inputPath
}
else {
Write-Output "Invalid path: $inputPath. Please enter a valid path."
}
}
}
function Get-ProcessorArchitecture {
$arch = $env:PROCESSOR_ARCHITECTURE
switch ($arch) {
"AMD64" {
return "amd64"
}
"ARM64" {
return "arm64"
}
"ARM" {
return "armv7"
}
default {
throw "Unsupported architecture: $arch"
}
}
}
function Get-ValidPath {
Param (
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $true)]
[string]$Default
)
$path = $null
while ($null -eq $path) {
$path = Read-Host -Prompt $Message
if ([string]::IsNullOrWhiteSpace($path)) {
return $Default
}
else {
if ((Test-Path $path -IsValid) -and (Test-Path $path -IsValid -PathType Leaf)) {
return $path
}
else {
Write-Warning "Invalid path: $path. Please enter a valid path."
$path = $null
}
}
}
}
function Confirm-GitCommit {
# Check if the hash matches the basic pattern of a Git commit hash
if (-not ([string]::IsNullOrWhiteSpace($DaggerCommit)) -and $DaggerCommit -match '^[0-9a-fA-F]{40}$') {
Write-Output @"
---------------------------------------------------------------------------
The commit hash provided does not seem to be a valid Git commit hash.
Please provide a valid Git commit hash.
---------------------------------------------------------------------------
"@
exit 1
}
}
function Get-DownloadUrl {
$fileName = Get-FileName
if (-not [string]::IsNullOrWhiteSpace($DaggerCommit)) {
return "https://dl.dagger.io/dagger/main/${DaggerCommit}/${fileName}"
}
return "https://dl.dagger.io/dagger/releases/${DaggerVersion}/${fileName}"
}
function Get-ChecksumUrl {
if (-not [string]::IsNullOrWhiteSpace($DaggerCommit)) {
return "https://dl.dagger.io/dagger/main/${DaggerCommit}/checksums.txt"
}
return "https://dl.dagger.io/dagger/releases/${DaggerVersion}/checksums.txt"
}
# Used for interactive mode to get a true or false response from the user
function Get-TrueFalse {
Param (
[Parameter(Mandatory = $true)][string]$Message,
[Parameter(Mandatory = $true)][bool]$Default
)
$response = ""
while ($response -notmatch "^(y|n)$") {
$response = Read-Host -Prompt $Message
if ([string]::IsNullOrWhiteSpace($response)) {
$response = $Default ? "y" : "n"
break
}
}
return $response.StartsWith("y")
}
function Find-LatestVersion {
$body = $null
$response = Invoke-RestMethod "https://dl.dagger.io/dagger/latest_version" -Body $body -ErrorVariable LatestVersionError
if ($LatestVersionError) {
Write-Error @"
---------------------------------------------------------------------------
Houston we have a problem!
Apparently we had an issue finding the latest version of Dagger.
Please check https://docs.dagger.io/install
----------------------------------------------------------------------------
"@
exit 1
}
$latestVersion = $response -replace "v", ""
return [System.Management.Automation.SemanticVersion]::Parse($latestVersion)
}
# This function returns the file name of the Dagger zip file to download.
function Get-FileName {
$arch = Get-ProcessorArchitecture
if (-not [string]::IsNullOrWhiteSpace($DaggerCommit)) {
return "dagger_${DaggerCommit}_windows_${arch}.zip"
}
return "dagger_v${DaggerVersion}_windows_${arch}.zip"
}
# This function prompts the user for a version string and validates it.
$semVerPattern = "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$"
function Get-SemVer {
Param (
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $true)]
[System.Management.Automation.SemanticVersion]$Default
)
$version = $null
while ($null -eq $version) {
$inputString = Read-Host -Prompt $Message
if ([string]::IsNullOrWhiteSpace($inputString)) {
return $Default
}
$isValid = [System.Management.Automation.SemanticVersion]::TryParse($inputString, [ref]$version) -and $inputString -match $semVerPattern
if (-not $isValid) {
$version = $null
Write-Warning "Invalid version string: $inputString. Please enter a valid semantic version (e.g., 0.11.6)."
}
elseif ($isvalid -and $version -gt $Default) {
$version = $null
Write-Warning "Please enter a valid version."
}
}
return $version
}
# This gets a full path name from the install path i.e C:\users\username\dagger
function Get-InstallPath {
if (-not (Test-Path $InstallPath)) {
New-Item -ItemType Directory -Path $InstallPath -ErrorAction Stop -ErrorVariable InstallPathError | Out-Null
if ($InstallPathError) {
Write-Error @"
---------------------------------------------------------------------------
Whoops, apparently we had an issue in creating the install path.
Please check you have the right permission to do so or try to create the path manually.
---------------------------------------------------------------------------
"@
exit 1
}
}
return (Get-Item -Path $InstallPath).FullName
}
# This function extracts the checksum of the downloaded file which contains all checksums for a version
function Get-Checksum {
$checksumUrl = Get-ChecksumUrl
$arch = Get-ProcessorArchitecture
$response = Invoke-RestMethod -Uri $checksumUrl -UserAgent "PowerShell"
$checksums = $response -split "`n"
$checksum = $null
$target = $null
if (-not [string]::IsNullOrWhiteSpace($DaggerCommit)) {
$target = "dagger_${DaggerCommit}_windows_${arch}.zip"
}
else {
$target = "dagger_v${DaggerVersion}_windows_${arch}.zip"
}
# Find the checksum for the target file
foreach ($line in $checksums) {
if ($line -match $target) {
$checksum = $line -split " " | Select-Object -First 1
break
}
}
return $checksum
}
function Compare-Checksum {
Param (
[Parameter(Mandatory = $true)]
[string]$DownloadPath,
[Parameter(Mandatory = $true)]
[string]$Checksum
)
$hash = Get-FileHash -Path $DownloadPath -Algorithm SHA256
if ($hash.Hash -ne $Checksum) {
Remove-Item -Path $DownloadPath
Write-Error @"
---------------------------------------------------------------------------
The file checksum does not match the expected checksum!
Expected: $Checksum
File : $($hash.Hash)
The downloaded file has been removed.
---------------------------------------------------------------------------
"@
exit 1
}
}
function Main {
# Powershell is cross-platform, notice about windows binary when used on non-windows
if (-not $IsWindows) {
Write-Warning @"
---------------------------------------------------------------------------
Note: This script will install the Windows binary of Dagger.
---------------------------------------------------------------------------
"@
}
# Dagger compiles for AMD64, ARM64, and ARM architectures only
Get-ProcessorArchitecture -ErrorAction Stop -ErrorVariable ArchitectureError | Out-Null
if ($ArchitectureError) {
Write-Error @"
---------------------------------------------------------------------------
Whoops, apparently we had an issue in determining the architecture of your system.
Dagger compiles for AMD64, ARM64, and ARM architectures only.
---------------------------------------------------------------------------
"@
exit 1
}
# If the user does not provide a version, we will find the latest version
if ($null -eq $DaggerVersion) {
$DaggerVersion = Find-LatestVersion
}
# Interactive allows customisation of the installation
if ($Interactive) {
$DaggerVersion = Get-SemVer `
-Message "Enter the Dagger version to install or leave empty and hit Enter for default ($DaggerVersion)" `
-Default $DaggerVersion `
-ErrorVariable InteractiveDaggerVersionError
if ($InteractiveDaggerVersionError) {
Write-Error @"
---------------------------------------------------------------------------
Whoops, we had an issue in finding the selected version of Dagger.
Please check your internet connection and try again.
---------------------------------------------------------------------------
"@
exit 1
}
$DownloadPath = Get-ValidPath `
-Message "Enter the download location or leave empty and hit Enter for default ($DownloadPath)" `
-Default $DownloadPath `
-ErrorVariable InteractiveDownloadPathError
if ($InteractiveDownloadPathError) {
Write-Error @"
---------------------------------------------------------------------------
Whoops, we had an issue in getting the download path.
Please check the path and try again.
---------------------------------------------------------------------------
"@
exit 1
}
$InstallPath = Get-ValidPath `
-Message "Enter the destination unzip path or leave empty and hit Enter for default ($(Get-InstallPath))" `
-Default $InstallPath `
-ErrorVariable InteractiveInstallPathError
if ($InteractiveInstallPathError) {
Write-Error @"
---------------------------------------------------------------------------
Whoops, we had an issue in getting the install path.
Please check the path and try again.
---------------------------------------------------------------------------
"@
exit 1
}
$defaultString = $AddToPath ? "y" : "n"
$AddToPath = Get-TrueFalse `
-Message "Enter (y/n) to add dagger.exe to your PATH or hit Enter for default ($defaultString)" `
-Default $AddToPath `
-ErrorVariable InteractiveAddToPathError
if ($InteractiveAddToPathError) {
Write-Error @"
---------------------------------------------------------------------------
Whoops, we had an issue checking if you want to add dagger.exe to your PATH.
Please check the option and try again.
---------------------------------------------------------------------------
"@
exit 1
}
}
$zipUrl = Get-DownloadUrl
Write-Output "Downloading Dagger from $zipUrl"
Invoke-RestMethod -Uri $zipUrl -OutFile $DownloadPath -UserAgent "PowerShell"
$checksum = Get-Checksum -ErrorAction Stop -ErrorVariable ChecksumError
Write-Output "Checksum is $checksum"
Compare-Checksum -DownloadPath $DownloadPath -Checksum $checksum
Expand-Archive -Path $downloadPath -DestinationPath $InstallPath -Force -ErrorVariable ProcessError
if ($ProcessError) {
Write-Error @"
---------------------------------------------------------------------------
Whoops, apparently we had an issue in unzipping the file, please check
you have the right permission to do so or try to unzip the file manually.
Dagger is currently downloaded at $DownloadPath.
---------------------------------------------------------------------------
"@
}
else {
$installPath = Get-InstallPath
Write-Output @"
---------------------------------------------------------------------------
Thank you for downloading Dagger!
Dagger has been successfully installed at $(Get-InstallPath)
"@
$path = [Environment]::GetEnvironmentVariable("Path", "User")
$existsInPath = $path -like "*$installPath*"
if ($AddToPath) {
if (-not $existsInPath) {
[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path", "User") + ";$installPath", "User")
Write-Output "Dagger has been added to your PATH"
}
else {
Write-Warning "Dagger is already in your PATH"
}
}
else {
if (-not $existsInPath) {
Write-Output "Please add dagger.exe to your PATH in order to use it"
}
}
Write-Output "---------------------------------------------------------------------------"
}
}
$isInvoked = [string]::IsNullOrWhiteSpace($MyInvocation.MyCommand.Path)
if ($isInvoked) {
# Allow Invoke-Expression to customise the installation by producing the function Install-Dagger
# This is because the Param of the Script file is not available unless the script is invoked
function Install-Dagger {
Param (
[Parameter(Mandatory = $false)][System.Management.Automation.SemanticVersion]$DaggerVersion,
[Parameter(Mandatory = $false)][string][ValidatePattern("^(?:head|(?:[0-9a-fA-F]{40}))?$")]$DaggerCommit,
[Parameter(Mandatory = $false)][string]$DownloadPath = [System.IO.Path]::GetTempFileName(),
[Parameter(Mandatory = $false)][string]$InstallPath = "$env:USERPROFILE\dagger",
[Parameter(Mandatory = $false)][switch]$AddToPath = $false,
[Parameter(Mandatory = $false)][switch]$Interactive = $false
)
Main
}
}
else {
Main
}