Browse Source

Fix getzig sort-order again

master
Julian Noble 2 weeks ago
parent
commit
16dc8e94e4
  1. 452
      bin/getzig.cmd
  2. 2
      src/scriptapps/getzig.ps1

452
bin/getzig.cmd

@ -832,232 +832,232 @@ if ($matches.count) {
# -- --- --- --- --- --- --- --- --- --- --- --- --- ---begin powershell Payload # -- --- --- --- --- --- --- --- --- --- --- --- --- ---begin powershell Payload
#<powershell-payload> #<powershell-payload>
#launch example #launch example
#powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'cmd.exe' -ArgumentList @('/c', 'getzig.cmd') -NoNewWindow -Wait" #powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'cmd.exe' -ArgumentList @('/c', 'getzig.cmd') -NoNewWindow -Wait"
#powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'getzig.cmd' -NoNewWindow -Wait" #powershell -Command "Invoke-WebRequest -Uri 'https://www.gitea1.intx.com.au/jn/punkshell/raw/branch/master/bin/getzig.cmd' -OutFile 'getzig.cmd'; Start-Process 'getzig.cmd' -NoNewWindow -Wait"
#Join-Path using verbose method to support powershell 5? #Join-Path using verbose method to support powershell 5?
#$outbase = Join-Path -Path $PSScriptRoot -ChildPath "../.." #$outbase = Join-Path -Path $PSScriptRoot -ChildPath "../.."
$outbase = $PSScriptRoot $outbase = $PSScriptRoot
$outbase = Resolve-Path -Path $outbase $outbase = Resolve-Path -Path $outbase
$toolsfolder = Join-Path -Path $outbase -ChildPath "tools" $toolsfolder = Join-Path -Path $outbase -ChildPath "tools"
if (-not(Test-Path -Path $toolsfolder -PathType Container)) { if (-not(Test-Path -Path $toolsfolder -PathType Container)) {
#create folder - (can include missing intermediaries) #create folder - (can include missing intermediaries)
New-Item -Path $toolsfolder -ItemType Directory New-Item -Path $toolsfolder -ItemType Directory
} }
$zigfolder = Join-Path $toolsfolder -ChildPath "zig" $zigfolder = Join-Path $toolsfolder -ChildPath "zig"
$zigexe = Join-Path $zigfolder "zig.exe" $zigexe = Join-Path $zigfolder "zig.exe"
$releasearchive = "zig-x86_64-windows-0.15.1.zip" ;#zip on windows, tarball on every other platform $releasearchive = "zig-x86_64-windows-0.15.1.zip" ;#zip on windows, tarball on every other platform
Write-Output "powershell version: $($PSVersionTable.PSVersion)" Write-Output "powershell version: $($PSVersionTable.PSVersion)"
if (Get-Command $zigexe -ErrorAction SilentlyContinue) { if (Get-Command $zigexe -ErrorAction SilentlyContinue) {
Write-Host "zig.exe is installed in tools/zig" Write-Host "zig.exe is installed in tools/zig"
$zigv = tools/zig/zig.exe version 2>&1 $zigv = tools/zig/zig.exe version 2>&1
$stdout = $zigv | Where-Object {$_ -is [string]} $stdout = $zigv | Where-Object {$_ -is [string]}
$stderr = $zigv | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]} $stderr = $zigv | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]}
if ($stderr) { if ($stderr) {
Write-Host "Unexpected output from tools/zig/zig.exe: $stderr" Write-Host "Unexpected output from tools/zig/zig.exe: $stderr"
Write-Host "Consider deleting tools/zig and re-downloading" Write-Host "Consider deleting tools/zig and re-downloading"
} else { } else {
Write-Host "tools/zig/zig.exe version is: $stdout" Write-Host "tools/zig/zig.exe version is: $stdout"
} }
exit exit
} }
if (Get-Command "minisign" -ErrorAction SilentlyContinue) { if (Get-Command "minisign" -ErrorAction SilentlyContinue) {
Write-Host "minisign is available" Write-Host "minisign is available"
} else { } else {
Write-Host "minisign is missing. Will attempt to install using winget." Write-Host "minisign is missing. Will attempt to install using winget."
#Find-Module/Install-Module: older mechanism, available in powershell #Find-Module/Install-Module: older mechanism, available in powershell
#Find-PSResource/Install-PSResource: only available in newer pwsh etc? #Find-PSResource/Install-PSResource: only available in newer pwsh etc?
$wgclient = Get-Module -ListAvailable -Name Microsoft.WinGet.Client $wgclient = Get-Module -ListAvailable -Name Microsoft.WinGet.Client
if (${wgclient}.Length -eq 0) { if (${wgclient}.Length -eq 0) {
Write-Host "Microsoft.WinGet.Client module not installed.. will try to install." Write-Host "Microsoft.WinGet.Client module not installed.. will try to install."
Install-PackageProvider -Name NuGet -Force Install-PackageProvider -Name NuGet -Force
$psgallery_existing_policy = (Get-PSRepository -Name PSGallery).InstallationPolicy $psgallery_existing_policy = (Get-PSRepository -Name PSGallery).InstallationPolicy
if ($psgallery_existing_policy -eq "Untrusted") { if ($psgallery_existing_policy -eq "Untrusted") {
#Applies to all versions of PowerShell for the user, and is persistent for current user. #Applies to all versions of PowerShell for the user, and is persistent for current user.
#This has risks in that a powershell session started after this call, and before we reset it, will treat PSGallery as trusted #This has risks in that a powershell session started after this call, and before we reset it, will treat PSGallery as trusted
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
} }
Install-Module -Scope CurrentUser -Name Microsoft.Winget.Client -Force -Repository PSGallery Install-Module -Scope CurrentUser -Name Microsoft.Winget.Client -Force -Repository PSGallery
Repair-WinGetPackageManager Repair-WinGetPackageManager
import-module -name Microsoft.Winget.client import-module -name Microsoft.Winget.client
if ($psgallery_existing_policy -eq "Untrusted") { if ($psgallery_existing_policy -eq "Untrusted") {
Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted
} }
} else { } else {
Write-Host "Microsoft.WinGet.Client is available" Write-Host "Microsoft.WinGet.Client is available"
} }
$wingetversion = (Find-WinGetPackage jedisct1.minisign).Version $wingetversion = (Find-WinGetPackage jedisct1.minisign).Version
if ($wingetversion) { if ($wingetversion) {
Write-Host "Installing minisign version: ${wingetversion}" Write-Host "Installing minisign version: ${wingetversion}"
Install-WinGetPackage -Id "jedisct1.minisign" Install-WinGetPackage -Id "jedisct1.minisign"
} }
if (Get-Command "minisign" -ErrorAction SilentlyContinue) { if (Get-Command "minisign" -ErrorAction SilentlyContinue) {
Write-Host "minisign is now available" Write-Host "minisign is now available"
} else { } else {
Write-Host "minisign is still not available" Write-Host "minisign is still not available"
#if we automatically relaunch - we could get stuck in a loop - ask user. #if we automatically relaunch - we could get stuck in a loop - ask user.
$response = read-host -Prompt "Relaunching process may make minizip available. Relaunch? Y|N" $response = read-host -Prompt "Relaunching process may make minizip available. Relaunch? Y|N"
#if ($PSVersionTable.PSEdition -eq "Desktop") { #if ($PSVersionTable.PSEdition -eq "Desktop") {
#powershell.exe #powershell.exe
#} else { #} else {
#pwsh.exe #pwsh.exe
#} #}
if ($response -ieq "y") { if ($response -ieq "y") {
Start-Process 'getzig.cmd' -NoNewWindow -Wait Start-Process 'getzig.cmd' -NoNewWindow -Wait
} }
exit exit
} }
} }
$zigpubkey = "RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U" $zigpubkey = "RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U"
$mirrors_url = "https://ziglang.org/download/community-mirrors.txt" $mirrors_url = "https://ziglang.org/download/community-mirrors.txt"
$mirrors_response = $(Invoke-WebRequest -Uri $mirrors_url) $mirrors_response = $(Invoke-WebRequest -Uri $mirrors_url)
if ($mirrors_response.StatusCode -eq 200) { if ($mirrors_response.StatusCode -eq 200) {
#https://www.gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools/zig-x86_64-windows-0.15.1.zip #https://www.gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools/zig-x86_64-windows-0.15.1.zip
$mirror_array = @("https://gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools") $mirror_array = @("https://gitea1.intx.com.au/jn/punkbin/raw/branch/master/win64/tools")
$mirror_array += $mirrors_response.Content.TrimEnd("`r`n") -split "`r`n|`n" $mirror_array += $mirrors_response.Content.TrimEnd("`r`n") -split "`r`n|`n"
#$mirror_array += "https://bogusxxx.org" #test behaviour of a bogus/down entry #$mirror_array += "https://bogusxxx.org" #test behaviour of a bogus/down entry
$mirror_array += "https://ziglang.org" #main site $mirror_array += "https://ziglang.org" #main site
$dict_mirrors = [ordered]@{} $dict_mirrors = [ordered]@{}
$host_list = @() #same ordering as dict_mirrors $host_list = @() #same ordering as dict_mirrors
foreach ($mirror in $mirror_array) { foreach ($mirror in $mirror_array) {
$uri = New-Object System.Uri($mirror) $uri = New-Object System.Uri($mirror)
$hostname = $uri.Host $hostname = $uri.Host
$dict_mirrors[$hostname] = @{} $dict_mirrors[$hostname] = @{}
$dict_mirrors[$hostname]["uri"] = $mirror $dict_mirrors[$hostname]["uri"] = $mirror
$dict_mirrors[$hostname]["latency"] = 888888 ;#default $dict_mirrors[$hostname]["latency"] = 888888 ;#default
$host_list += $hostname $host_list += $hostname
#write-host "Host name: $hostname" #write-host "Host name: $hostname"
} }
#write-host "dict: $($dict_mirrors | out-String)" #write-host "dict: $($dict_mirrors | out-String)"
write-host "host_list: $host_list" write-host "host_list: $host_list"
if ($PSVersionTable.PSEdition -eq "Desktop") { if ($PSVersionTable.PSEdition -eq "Desktop") {
#powershell.exe #powershell.exe
#we seem to need -ErrorAction Ignore for bad/unresponsive host entries - but unlike with pwsh, we don't get any corresponding entry in the test_results, and -Quiet doesn't seem to give us results either REVIEW #we seem to need -ErrorAction Ignore for bad/unresponsive host entries - but unlike with pwsh, we don't get any corresponding entry in the test_results, and -Quiet doesn't seem to give us results either REVIEW
$test_results = Test-Connection $host_list -Count 1 -ErrorAction Ignore $test_results = Test-Connection $host_list -Count 1 -ErrorAction Ignore
for ($i = 0; $i -lt $test_results.Count; $i++) { for ($i = 0; $i -lt $test_results.Count; $i++) {
$result = $test_results[$i] $result = $test_results[$i]
if ($result) { if ($result) {
$targethost = $result.Address $targethost = $result.Address
if ($result.StatusCode -eq 0) { if ($result.StatusCode -eq 0) {
$dict_mirrors[$targethost]["latency"] = $result.ResponseTime $dict_mirrors[$targethost]["latency"] = $result.ResponseTime
} else { } else {
$dict_mirrors[$targethost]["latency"] = 999999 $dict_mirrors[$targethost]["latency"] = 999999
} }
} else { } else {
$targethost = $host_list[$i] $targethost = $host_list[$i]
$dict_mirrors[$targethost]["latency"] = 999999 $dict_mirrors[$targethost]["latency"] = 999999
} }
} }
} else { } else {
#pwsh.exe #pwsh.exe
$test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet $test_results = Test-Connection -TargetName $host_list -Count 1 -Ipv4 -Detailed -TcpPort 443 -Quiet
for ($i = 0; $i -lt $test_results.Count; $i++) { for ($i = 0; $i -lt $test_results.Count; $i++) {
$result = $test_results[$i] $result = $test_results[$i]
if ($result) { if ($result) {
$targethost = $result.Target $targethost = $result.Target
if ($result.Status -eq "Success") { if ($result.Status -eq "Success") {
$dict_mirrors[$targethost]["latency"] = $result.Latency $dict_mirrors[$targethost]["latency"] = $result.Latency
} else { } else {
$dict_mirrors[$targethost]["latency"] = 999999 $dict_mirrors[$targethost]["latency"] = 999999
} }
} else { } else {
$targethost = $host_list[$i] $targethost = $host_list[$i]
$dict_mirrors[$targethost]["latency"] = 999999 $dict_mirrors[$targethost]["latency"] = 999999
} }
} }
} }
$list_mirror_dicts = @() $list_mirror_dicts = @()
#write-host "dict tested: $($dict_mirrors | Out-String)" #write-host "dict tested: $($dict_mirrors | Out-String)"
foreach ($key in $dict_mirrors.Keys) { foreach ($key in $dict_mirrors.Keys) {
$list_mirror_dicts += $($dict_mirrors[$key]) $list_mirror_dicts += $($dict_mirrors[$key])
} }
#need to ensure latency cast to integer (on powershell 5 at least) #need to ensure latency cast to integer (on powershell 5 at least)
$sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency } $sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency }
#Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)" #Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)"
Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-List -AutoSize | Out-String)" Write-Host "Sorted by latency: $($sorted_mirror_dicts | Out-String)"
$automation_name = "punkshell+julian@precisium.com.au_target_by_latency" $automation_name = "punkshell+julian@precisium.com.au_target_by_latency"
foreach ($hostinfo in $sorted_mirror_dicts) { foreach ($hostinfo in $sorted_mirror_dicts) {
$uristring = $hostinfo.uri $uristring = $hostinfo.uri
if ($uristring -eq "https://ziglang.org") { if ($uristring -eq "https://ziglang.org") {
$full_uristring = "${uristring}/download/0.15.1/${releasearchive}?source=${automation_name}" $full_uristring = "${uristring}/download/0.15.1/${releasearchive}?source=${automation_name}"
$sig_uristring = "${uristring}/download/0.15.1/${releasearchive}.minisig?source=${automation_name}" $sig_uristring = "${uristring}/download/0.15.1/${releasearchive}.minisig?source=${automation_name}"
} else { } else {
$full_uristring = "${uristring}/${releasearchive}?source=${automation_name}" $full_uristring = "${uristring}/${releasearchive}?source=${automation_name}"
$sig_uristring = "${uristring}/${releasearchive}.minisig?source=${automation_name}" $sig_uristring = "${uristring}/${releasearchive}.minisig?source=${automation_name}"
} }
Write-Host "Downloading zig from $full_uristring" Write-Host "Downloading zig from $full_uristring"
#$uriobj = [uri]$full_uristring #$uriobj = [uri]$full_uristring
#$lastSegment = $uriobj.Segments[-1] #$lastSegment = $uriobj.Segments[-1]
#$outfile = Join-Path $toolsfolder -ChildPath $lastSegment #$outfile = Join-Path $toolsfolder -ChildPath $lastSegment
$outfile = Join-Path $toolsfolder -ChildPath $releasearchive $outfile = Join-Path $toolsfolder -ChildPath $releasearchive
Write-Host "Download to: $outfile" Write-Host "Download to: $outfile"
Invoke-WebRequest -Uri $full_uristring -OutFile $outfile Invoke-WebRequest -Uri $full_uristring -OutFile $outfile
if (-not(Test-Path -Path $outfile -PathType Leaf)) { if (-not(Test-Path -Path $outfile -PathType Leaf)) {
write-Host "Failed to download zig package from $full_uristring to ${outfile} .. aborting download from $uristring" write-Host "Failed to download zig package from $full_uristring to ${outfile} .. aborting download from $uristring"
continue continue
} }
Write-Host "Downloading minisig signature from $sig_uristring" Write-Host "Downloading minisig signature from $sig_uristring"
Invoke-WebRequest -Uri $sig_uristring -Outfile "${outfile}.minisig" Invoke-WebRequest -Uri $sig_uristring -Outfile "${outfile}.minisig"
if (-not(Test-Path -Path "${outfile}.minisig" -PathType Leaf)) { if (-not(Test-Path -Path "${outfile}.minisig" -PathType Leaf)) {
write-Host "Failed to download minisig from $sig_uristring to ${outfile}.minisig .. aborting download from $uristring" write-Host "Failed to download minisig from $sig_uristring to ${outfile}.minisig .. aborting download from $uristring"
continue continue
} }
write-host "downloaded $outfile and ${outfile}.minisig - validating signature..." write-host "downloaded $outfile and ${outfile}.minisig - validating signature..."
#validate releasearchive (tarball/zip) #validate releasearchive (tarball/zip)
$sigresult = minisign -V -P $zigpubkey -m $outfile 2>&1 $sigresult = minisign -V -P $zigpubkey -m $outfile 2>&1
$stdout = $sigresult | Where-Object {$_ -is [string]} $stdout = $sigresult | Where-Object {$_ -is [string]}
$stderr = $sigresult | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]} $stderr = $sigresult | Where-Object {$_ -is [System.Management.Automation.ErrorRecord]}
$is_valid = $false $is_valid = $false
if ($stderr) { if ($stderr) {
write-host "Signature validation failed with message: $stderr" write-host "Signature validation failed with message: $stderr"
} else { } else {
if (($stdout | Out-String).Contains("signature verified")) { if (($stdout | Out-String).Contains("signature verified")) {
write-host $stdout write-host $stdout
$is_valid = $true $is_valid = $true
} else { } else {
write-host "Unexpected output from minisign: $stdout" write-host "Unexpected output from minisign: $stdout"
write-host "Did not contain 'signature verified'" write-host "Did not contain 'signature verified'"
} }
} }
if (-not($is_valid)) { if (-not($is_valid)) {
write-Host "Couldn't verify signature from download site $uristring" write-Host "Couldn't verify signature from download site $uristring"
Remove-Item -Path $outfile -ErrorAction SilentlyContinue Remove-Item -Path $outfile -ErrorAction SilentlyContinue
Remove-Item -Path "${outfile}.minisig" -ErrorAction SilentlyContinue Remove-Item -Path "${outfile}.minisig" -ErrorAction SilentlyContinue
continue continue
} }
Write-Host "Signature OK - extracting archive" Write-Host "Signature OK - extracting archive"
Expand-Archive -path $outfile -DestinationPath $toolsfolder -Force Expand-Archive -path $outfile -DestinationPath $toolsfolder -Force
#Remove-Item -Path "$outfile" #Remove-Item -Path "$outfile"
$zip_rootname = [System.IO.Path]::GetFileNameWithoutExtension($releasearchive) $zip_rootname = [System.IO.Path]::GetFileNameWithoutExtension($releasearchive)
#Rename-Item -Path #Rename-Item -Path
write-host "zip_rootname: $zip_rootname" write-host "zip_rootname: $zip_rootname"
Rename-Item -Path $(Join-Path -Path $toolsfolder -ChildPath $zip_rootname) -NewName $zigfolder Rename-Item -Path $(Join-Path -Path $toolsfolder -ChildPath $zip_rootname) -NewName $zigfolder
Write-Host "Zig installed in ${zigfolder}" Write-Host "Zig installed in ${zigfolder}"
$zigexe = Join-Path $zigfolder -ChildPath "zig.exe" $zigexe = Join-Path $zigfolder -ChildPath "zig.exe"
$v = Invoke-Expression "$zigexe version" $v = Invoke-Expression "$zigexe version"
Write-Host "ZIG VERSION: ${v}" Write-Host "ZIG VERSION: ${v}"
$test_ok = 1 ;#todo $test_ok = 1 ;#todo
if ($test_ok) { if ($test_ok) {
break break
} }
} }
} else { } else {
Write-Host "Unable to retrieve list of zig community mirrors from $mirrors_url" Write-Host "Unable to retrieve list of zig community mirrors from $mirrors_url"
} }
#</powershell-payload> #</powershell-payload>

2
src/scriptapps/getzig.ps1

@ -149,7 +149,7 @@ if ($mirrors_response.StatusCode -eq 200) {
#need to ensure latency cast to integer (on powershell 5 at least) #need to ensure latency cast to integer (on powershell 5 at least)
$sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency } $sorted_mirror_dicts = $list_mirror_dicts | Sort-Object -Property { [int]$_.latency }
#Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)" #Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-Table -AutoSize | Out-String)"
Write-Host "Sorted by latency: $($sorted_mirror_dicts | Format-List -AutoSize | Out-String)" Write-Host "Sorted by latency: $($sorted_mirror_dicts | Out-String)"
$automation_name = "punkshell+julian@precisium.com.au_target_by_latency" $automation_name = "punkshell+julian@precisium.com.au_target_by_latency"
foreach ($hostinfo in $sorted_mirror_dicts) { foreach ($hostinfo in $sorted_mirror_dicts) {

Loading…
Cancel
Save