11<#
22. SYNOPSIS
3- Gets the latest ziti from github and adds it to your path
3+ Gets ziti from github and adds it to your path
44
55. DESCRIPTION
66This script will:
7- - detect the latest version of ziti
8- - download the latest version of ziti into the folder of your choice, defaulting to $env:userprofile.ziti\bin)
7+ - detect the requested version of ziti from github, defaulting to latest
8+ - download the selected version of ziti into the folder of your choice, defaulting to $env:userprofile.ziti\bin)
99 - unzip the downloaded file
1010 - optionally add the extracted path to your path if executed with a "dot" as in: . getLatestZiti.ps1
1111
12+ . PARAMETER Version
13+ Optional ziti release tag, e.g. v2.0.0-pre8. If omitted, latest is used.
14+
15+ . PARAMETER NonInteractive
16+ Skip all prompts, using defaults (install to $env:USERPROFILE\.ziti\bin and add to session PATH).
17+
1218. INPUTS
1319None.
1420
@@ -17,7 +23,15 @@ None. If "dot sourced" this script will add the resultant directory to your path
1723
1824. EXAMPLE
1925PS> . .\getZiti.ps1
26+
27+ . EXAMPLE
28+ PS> . .\getZiti.ps1 -Version v2.0.0-pre8
2029#>
30+ param (
31+ [string ]$Version ,
32+ [switch ]$NonInteractive
33+ )
34+
2135Add-Type - AssemblyName System.Runtime.InteropServices
2236
2337$osDescription = [System.Runtime.InteropServices.RuntimeInformation ]::OSDescription
@@ -31,8 +45,7 @@ if($arch -match "x64") {
3145if ($osDescription.ToLower () -match " windows" ) {
3246 $matchFilter = " ziti-windows-$arch "
3347} elseif ($osDescription.ToLower () -match " darwin" ) {
34- $matchFilter = " ziti-darwin-amd64"
35- # todo: replace $arch some day
48+ $matchFilter = " ziti-darwin-$arch "
3649} elseif ($osDescription.ToLower () -match " linux" ) {
3750 $matchFilter = " ziti-linux-$arch "
3851} else {
@@ -41,16 +54,34 @@ if($osDescription.ToLower() -match "windows") {
4154}
4255$dirSeparator = [System.IO.Path ]::DirectorySeparatorChar
4356$pathSeparator = [System.IO.Path ]::PathSeparator
44- $latestFromGitHub = (irm https:// api.github.com / repos/ openziti/ ziti/ releases/ latest)
45- $version = ($latestFromGitHub.tag_name )
46- $zitidl = ($latestFromGitHub ).assets | where {$_.browser_download_url -Match " $matchFilter .*" }
57+
58+ if ([string ]::IsNullOrWhiteSpace($Version )) {
59+ $releaseFromGitHub = irm https:// api.github.com / repos/ openziti/ ziti/ releases/ latest
60+ } else {
61+ $releaseFromGitHub = irm " https://api.github.com/repos/openziti/ziti/releases/tags/$Version "
62+ }
63+
64+ $version = ($releaseFromGitHub.tag_name )
65+ $zitidl = ($releaseFromGitHub ).assets | where {$_.browser_download_url -Match " $matchFilter .*" }
4766$downloadUrl = ($zitidl.browser_download_url )
4867$name = $zitidl.name
68+ $checksumAsset = ($releaseFromGitHub ).assets | where {$_.name -eq " checksums.sha256.txt" }
69+ $checksumUrl = ($checksumAsset.browser_download_url )
70+
71+ if ([string ]::IsNullOrWhiteSpace($downloadUrl )) {
72+ Write-Error " No matching asset found for version '$version ' using filter '$matchFilter '"
73+ return
74+ }
75+
4976$homeDirectory = [System.Environment ]::GetFolderPath([System.Environment + SpecialFolder ]::UserProfile)
5077$defaultFolder = " $homeDirectory${dirSeparator} .ziti${dirSeparator} bin"
51- $toDir = $ (Read-Host " Where should ziti be installed? [default: ${defaultfolder} ]" )
52- if ($toDir.Trim () -eq " " ) {
53- $toDir = (" ${defaultfolder} " )
78+ if ($NonInteractive ) {
79+ $toDir = $defaultFolder
80+ } else {
81+ $toDir = $ (Read-Host " Where should ziti be installed? [default: ${defaultfolder} ]" )
82+ if ($toDir.Trim () -eq " " ) {
83+ $toDir = (" ${defaultfolder} " )
84+ }
5485}
5586
5687$zipFile = " ${toDir}${dirSeparator}${name} "
@@ -67,6 +98,30 @@ if($(Test-Path -Path $zipFile -PathType Leaf)) {
6798 $ProgressPreference = $SavedProgressPreference
6899}
69100
101+ if (-not [string ]::IsNullOrWhiteSpace($checksumUrl )) {
102+ Write-Output " Verifying checksum..."
103+ $checksumContent = (irm $checksumUrl )
104+ $expectedLine = $checksumContent -split " `n " | where { $_ -match [regex ]::Escape($name ) }
105+ if ($expectedLine ) {
106+ $expectedHash = ($expectedLine -split " \s+" )[0 ].ToUpper()
107+ if ($osDescription.ToLower () -match " windows" ) {
108+ $actualHash = (Get-FileHash - Algorithm SHA256 - Path $zipFile ).Hash.ToUpper()
109+ } else {
110+ $actualHash = (sha256sum $zipFile -split " \s+" )[0 ].ToUpper()
111+ }
112+ if ($actualHash -ne $expectedHash ) {
113+ Write-Error " Checksum mismatch for $name ! Expected $expectedHash but got $actualHash . Aborting."
114+ Remove-Item - Force $zipFile
115+ return
116+ }
117+ Write-Output " Checksum verified."
118+ } else {
119+ Write-Warning " Could not find checksum entry for '$name ' in checksums.sha256.txt. Proceeding without verification."
120+ }
121+ } else {
122+ Write-Warning " No checksums.sha256.txt asset found in release. Proceeding without verification."
123+ }
124+
70125if ($osDescription.ToLower () -match " windows" ) {
71126 Expand-Archive - Path $zipFile - DestinationPath " ${toDir}${dirSeparator}${version} " - ErrorAction SilentlyContinue
72127} else {
@@ -78,12 +133,16 @@ if($osDescription.ToLower() -match "windows") {
78133Write-Output " "
79134Write-Output " Extracted binaries to ${toDir}${dirSeparator}${version}${dirSeparator} ziti"
80135Write-Output " "
81- $addToPath = $ (Read-Host " Would you like to add ziti to this session's path? [default: Y]" )
82- if ($addToPath.Trim () -eq " " ) {
83- $addToPath = (" Y" )
136+ if ($NonInteractive ) {
137+ $addToPath = " Y"
138+ } else {
139+ $addToPath = $ (Read-Host " Would you like to add ziti to this session's path? [default: Y]" )
140+ if ($addToPath.Trim () -eq " " ) {
141+ $addToPath = (" Y" )
142+ }
84143}
85144
86145if ($addToPath -ilike " y*" ) {
87146 $env: PATH += " $pathSeparator${toDir}${dirSeparator}${version} "
88147 Write-Output " ziti added to your path!"
89- }
148+ }
0 commit comments