[Windows] Rename the Invoke-SBWithRetry function (#8880)

This commit is contained in:
Vasilii Polikarpov
2023-11-29 12:58:37 +01:00
committed by GitHub
parent d2cbbb901b
commit c73276d3f6
11 changed files with 73 additions and 48 deletions

View File

@@ -73,7 +73,7 @@ Expand-7ZipArchive -Path $cmdlineToolsArchPath -DestinationPath "${SDKInstallRoo
# cmdline tools should be installed in ${SDKInstallRoot}\cmdline-tools\latest\bin, but archive contains ${SDKInstallRoot}\cmdline-tools\bin # cmdline tools should be installed in ${SDKInstallRoot}\cmdline-tools\latest\bin, but archive contains ${SDKInstallRoot}\cmdline-tools\bin
# we need to create the proper folder structure # we need to create the proper folder structure
Invoke-SBWithRetry -Command { Invoke-ScriptBlockWithRetry -Command {
Rename-Item "${SDKInstallRoot}\cmdline-tools\cmdline-tools" "latest" -ErrorAction Stop Rename-Item "${SDKInstallRoot}\cmdline-tools\cmdline-tools" "latest" -ErrorAction Stop
} }

View File

@@ -90,7 +90,7 @@ function Install-JavaJDK {
# Complete the installation by extracting Java binaries to toolcache and creating the complete file # Complete the installation by extracting Java binaries to toolcache and creating the complete file
Expand-7ZipArchive -Path $archivePath -DestinationPath $javaVersionPath Expand-7ZipArchive -Path $archivePath -DestinationPath $javaVersionPath
Invoke-SBWithRetry -Command { Invoke-ScriptBlockWithRetry -Command {
Get-ChildItem -Path $javaVersionPath | Rename-Item -NewName $javaArchPath -ErrorAction Stop Get-ChildItem -Path $javaVersionPath | Rename-Item -NewName $javaArchPath -ErrorAction Stop
} }
New-Item -ItemType File -Path $javaVersionPath -Name "$Architecture.complete" | Out-Null New-Item -ItemType File -Path $javaVersionPath -Name "$Architecture.complete" | Out-Null

View File

@@ -41,7 +41,7 @@ function Install-PyPy
New-Item -ItemType Directory -Path $pypyVersionPath -Force | Out-Null New-Item -ItemType Directory -Path $pypyVersionPath -Force | Out-Null
Write-Host "Move PyPy '${pythonVersion}' files to '${pypyArchPath}'" Write-Host "Move PyPy '${pythonVersion}' files to '${pypyArchPath}'"
Invoke-SBWithRetry -Command { Invoke-ScriptBlockWithRetry -Command {
Move-Item -Path $tempFolder -Destination $pypyArchPath -ErrorAction Stop | Out-Null Move-Item -Path $tempFolder -Destination $pypyArchPath -ErrorAction Stop | Out-Null
} }

View File

@@ -62,7 +62,7 @@ function Install-Ruby {
New-Item -ItemType Directory -Path $rubyVersionPath -Force | Out-Null New-Item -ItemType Directory -Path $rubyVersionPath -Force | Out-Null
Write-Host "Moving Ruby '${rubyVersion}' files to '${rubyArchPath}'" Write-Host "Moving Ruby '${rubyVersion}' files to '${rubyArchPath}'"
Invoke-SBWithRetry -Command { Invoke-ScriptBlockWithRetry -Command {
Move-Item -Path $tempFolder -Destination $rubyArchPath -ErrorAction Stop | Out-Null Move-Item -Path $tempFolder -Destination $rubyArchPath -ErrorAction Stop | Out-Null
} }

View File

@@ -38,14 +38,18 @@ $tools = Get-ToolsetContent | Select-Object -ExpandProperty toolcache | Where-Ob
foreach ($tool in $tools) { foreach ($tool in $tools) {
# Get versions manifest for current tool # Get versions manifest for current tool
$assets = Invoke-SBWithRetry -Command { Invoke-RestMethod $tool.url } # Invoke-RestMethod doesn't support retry in PowerShell 5.1
$assets = Invoke-ScriptBlockWithRetry -Command {
Invoke-RestMethod $tool.url
}
# Get github release asset for each version # Get github release asset for each version
foreach ($toolVersion in $tool.versions) { foreach ($toolVersion in $tool.versions) {
$asset = $assets | Where-Object version -like $toolVersion ` $asset = $assets `
| Select-Object -ExpandProperty files ` | Where-Object version -like $toolVersion `
| Where-Object { ($_.platform -eq $tool.platform) -and ($_.arch -eq $tool.arch) -and ($_.toolset -eq $tool.toolset) } ` | Select-Object -ExpandProperty files `
| Select-Object -First 1 | Where-Object { ($_.platform -eq $tool.platform) -and ($_.arch -eq $tool.arch) -and ($_.toolset -eq $tool.toolset) } `
| Select-Object -First 1
Write-Host "Installing $($tool.name) $toolVersion $($tool.arch)..." Write-Host "Installing $($tool.name) $toolVersion $($tool.arch)..."
if ($null -ne $asset) { if ($null -ne $asset) {

View File

@@ -13,6 +13,7 @@ if (-not $vsixPackagesList) {
$vsixPackagesList | ForEach-Object { $vsixPackagesList | ForEach-Object {
# Retrieve cdn endpoint to avoid HTTP error 429 https://github.com/actions/runner-images/issues/3074 # Retrieve cdn endpoint to avoid HTTP error 429 https://github.com/actions/runner-images/issues/3074
$vsixPackage = Get-VsixExtenstionFromMarketplace -ExtensionMarketPlaceName $_ $vsixPackage = Get-VsixExtenstionFromMarketplace -ExtensionMarketPlaceName $_
Write-Host "Installing $vsixPackage"
if ($vsixPackage.FileName.EndsWith(".vsix")) { if ($vsixPackage.FileName.EndsWith(".vsix")) {
Install-VSIXFromUrl $vsixPackage.DownloadUri Install-VSIXFromUrl $vsixPackage.DownloadUri
} else { } else {

View File

@@ -3,7 +3,7 @@
## Desc: Waits for Windows Updates to finish installing after reboot ## Desc: Waits for Windows Updates to finish installing after reboot
################################################################################ ################################################################################
Invoke-SBWithRetry -RetryCount 10 -RetryIntervalSeconds 120 -Command { Invoke-ScriptBlockWithRetry -RetryCount 10 -RetryIntervalSeconds 120 -Command {
$inProgress = Get-WindowsUpdateStates | Where-Object State -eq "Running" | Where-Object Title -notmatch "Microsoft Defender Antivirus" $inProgress = Get-WindowsUpdateStates | Where-Object State -eq "Running" | Where-Object Title -notmatch "Microsoft Defender Antivirus"
if ( $inProgress ) { if ( $inProgress ) {
$title = $inProgress.Title -join "`n" $title = $inProgress.Title -join "`n"

View File

@@ -15,7 +15,7 @@ $filesInArchive = 7z l $zstdArchivePath | Out-String
if ($filesInArchive.Contains($zstdParentName)) { if ($filesInArchive.Contains($zstdParentName)) {
Expand-7ZipArchive -Path $zstdArchivePath -DestinationPath $toolPath Expand-7ZipArchive -Path $zstdArchivePath -DestinationPath $toolPath
Invoke-SBWithRetry -Command { Invoke-ScriptBlockWithRetry -Command {
Move-Item -Path "${zstdPath}*" -Destination $zstdPath -ErrorAction Stop Move-Item -Path "${zstdPath}*" -Destination $zstdPath -ErrorAction Stop
} }
} else { } else {

View File

@@ -38,7 +38,7 @@ Export-ModuleMember -Function @(
'Resolve-ChocoPackageVersion' 'Resolve-ChocoPackageVersion'
'Resolve-GithubReleaseAssetUrl' 'Resolve-GithubReleaseAssetUrl'
'Expand-7ZipArchive' 'Expand-7ZipArchive'
'Invoke-SBWithRetry' 'Invoke-ScriptBlockWithRetry'
'Get-VsCatalogJsonPath' 'Get-VsCatalogJsonPath'
'Get-AndroidPackages' 'Get-AndroidPackages'
'Get-AndroidPlatformPackages' 'Get-AndroidPlatformPackages'

View File

@@ -182,8 +182,10 @@ function Get-VsixExtenstionFromMarketplace {
[string] $MarketplaceUri = "https://marketplace.visualstudio.com/items?itemName=" [string] $MarketplaceUri = "https://marketplace.visualstudio.com/items?itemName="
) )
$extensionUri = $MarketplaceUri + $ExtensionMarketPlaceName # Invoke-WebRequest doesn't support retry in PowerShell 5.1
$request = Invoke-SBWithRetry -Command { Invoke-WebRequest -Uri $extensionUri -UseBasicParsing } -RetryCount 20 -RetryIntervalSeconds 30 $request = Invoke-ScriptBlockWithRetry -RetryCount 20 -RetryIntervalSeconds 30 -Command {
Invoke-WebRequest -Uri "${MarketplaceUri}${ExtensionMarketPlaceName}" -UseBasicParsing
}
$request -match 'UniqueIdentifierValue":"(?<extensionname>[^"]*)' | Out-Null $request -match 'UniqueIdentifierValue":"(?<extensionname>[^"]*)' | Out-Null
$extensionName = $Matches.extensionname $extensionName = $Matches.extensionname
$request -match 'VsixId":"(?<vsixid>[^"]*)' | Out-Null $request -match 'VsixId":"(?<vsixid>[^"]*)' | Out-Null
@@ -212,9 +214,9 @@ function Get-VsixExtenstionFromMarketplace {
return [PSCustomObject] @{ return [PSCustomObject] @{
"ExtensionName" = $extensionName "ExtensionName" = $extensionName
"VsixId" = $vsixId "VsixId" = $vsixId
"FileName" = $fileName "FileName" = $fileName
"DownloadUri" = $downloadUri "DownloadUri" = $downloadUri
} }
} }
@@ -277,17 +279,15 @@ function Install-VSIXFromUrl {
Remove-Item -Force -Confirm:$false $filePath Remove-Item -Force -Confirm:$false $filePath
} }
function Get-VSExtensionVersion function Get-VSExtensionVersion {
{
Param Param
( (
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string] $packageName [string] $packageName
) )
$instanceFolders = Get-ChildItem -Path "C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances" $instanceFolders = Get-ChildItem -Path "C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances"
if ($instanceFolders -is [array]) if ($instanceFolders -is [array]) {
{
Write-Host ($instanceFolders | Out-String) Write-Host ($instanceFolders | Out-String)
Write-Host ($instanceFolders | Get-ChildItem | Out-String) Write-Host ($instanceFolders | Get-ChildItem | Out-String)
Write-Host "More than one instance installed" Write-Host "More than one instance installed"
@@ -298,8 +298,7 @@ function Get-VSExtensionVersion
$state = $stateContent | ConvertFrom-Json $state = $stateContent | ConvertFrom-Json
$packageVersion = ($state.packages | Where-Object { $_.id -eq $packageName }).version $packageVersion = ($state.packages | Where-Object { $_.id -eq $packageName }).version
if (-not $packageVersion) if (-not $packageVersion) {
{
Write-Host "Installed package $packageName for Visual Studio was not found" Write-Host "Installed package $packageName for Visual Studio was not found"
exit 1 exit 1
} }
@@ -307,8 +306,7 @@ function Get-VSExtensionVersion
return $packageVersion return $packageVersion
} }
function Get-ToolsetContent function Get-ToolsetContent {
{
$toolsetPath = Join-Path "C:\\image" "toolset.json" $toolsetPath = Join-Path "C:\\image" "toolset.json"
$toolsetJson = Get-Content -Path $toolsetPath -Raw $toolsetJson = Get-Content -Path $toolsetPath -Raw
ConvertFrom-Json -InputObject $toolsetJson ConvertFrom-Json -InputObject $toolsetJson
@@ -395,27 +393,24 @@ function Get-TCToolVersionPath {
return Join-Path $foundVersion $Arch return Join-Path $foundVersion $Arch
} }
function Get-WinVersion function Get-WinVersion {
{
(Get-CimInstance -ClassName Win32_OperatingSystem).Caption (Get-CimInstance -ClassName Win32_OperatingSystem).Caption
} }
function Test-IsWin22 function Test-IsWin22 {
{
(Get-WinVersion) -match "2022" (Get-WinVersion) -match "2022"
} }
function Test-IsWin19 function Test-IsWin19 {
{
(Get-WinVersion) -match "2019" (Get-WinVersion) -match "2019"
} }
function Expand-7ZipArchive { function Expand-7ZipArchive {
Param Param
( (
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string]$Path, [string]$Path,
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string]$DestinationPath, [string]$DestinationPath,
[ValidateSet("x", "e")] [ValidateSet("x", "e")]
[char]$ExtractMethod = "x" [char]$ExtractMethod = "x"
@@ -424,8 +419,7 @@ function Expand-7ZipArchive {
Write-Host "Expand archive '$PATH' to '$DestinationPath' directory" Write-Host "Expand archive '$PATH' to '$DestinationPath' directory"
7z.exe $ExtractMethod "$Path" -o"$DestinationPath" -y | Out-Null 7z.exe $ExtractMethod "$Path" -o"$DestinationPath" -y | Out-Null
if ($LASTEXITCODE -ne 0) if ($LASTEXITCODE -ne 0) {
{
Write-Host "There is an error during expanding '$Path' to '$DestinationPath' directory" Write-Host "There is an error during expanding '$Path' to '$DestinationPath' directory"
exit 1 exit 1
} }
@@ -659,7 +653,29 @@ function Get-WindowsUpdateStates {
} }
} }
function Invoke-SBWithRetry { function Invoke-ScriptBlockWithRetry {
<#
.SYNOPSIS
Executes a script block with retry logic.
.DESCRIPTION
The Invoke-ScriptBlockWithRetry function executes a specified script block with retry logic. It allows you to specify the number of retries and the interval between retries.
.PARAMETER Command
The script block to be executed.
.PARAMETER RetryCount
The number of times to retry executing the script block. The default value is 10.
.PARAMETER RetryIntervalSeconds
The interval in seconds between each retry. The default value is 5.
.EXAMPLE
Invoke-ScriptBlockWithRetry -Command { Get-Process } -RetryCount 3 -RetryIntervalSeconds 10
This example executes the script block { Get-Process } with 3 retries and a 10-second interval between each retry.
#>
param ( param (
[scriptblock] $Command, [scriptblock] $Command,
[int] $RetryCount = 10, [int] $RetryCount = 10,
@@ -670,8 +686,7 @@ function Invoke-SBWithRetry {
try { try {
& $Command & $Command
return return
} } catch {
catch {
Write-Host "There is an error encountered:`n $_" Write-Host "There is an error encountered:`n $_"
$RetryCount-- $RetryCount--
@@ -771,9 +786,9 @@ function Resolve-GithubReleaseAssetUrl {
function Use-ChecksumComparison { function Use-ChecksumComparison {
param ( param (
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string]$LocalFileHash, [string]$LocalFileHash,
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string]$DistributorFileHash [string]$DistributorFileHash
) )
@@ -790,7 +805,7 @@ function Get-HashFromGitHubReleaseBody {
param ( param (
[string]$RepoOwner, [string]$RepoOwner,
[string]$RepoName, [string]$RepoName,
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string]$FileName, [string]$FileName,
[string]$Url, [string]$Url,
[string]$Version = "latest", [string]$Version = "latest",
@@ -815,7 +830,7 @@ function Get-HashFromGitHubReleaseBody {
$releaseUrl = "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases/tag/$tag" $releaseUrl = "https://api.github.com/repos/${RepoOwner}/${RepoName}/releases/tag/$tag"
} }
} }
$body = (Invoke-RestMethod -Uri $releaseUrl).body -replace('`', "") -join "`n" $body = (Invoke-RestMethod -Uri $releaseUrl).body -replace ('`', "") -join "`n"
$matchingLine = $body.Split("`n") | Where-Object { $_ -like "*$FileName*" } $matchingLine = $body.Split("`n") | Where-Object { $_ -like "*$FileName*" }
if ([string]::IsNullOrEmpty($matchingLine)) { if ([string]::IsNullOrEmpty($matchingLine)) {
throw "File name '$FileName' not found in release body." throw "File name '$FileName' not found in release body."
@@ -828,9 +843,9 @@ function Get-HashFromGitHubReleaseBody {
} }
function Test-FileSignature { function Test-FileSignature {
param( param(
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string]$FilePath, [string]$FilePath,
[Parameter(Mandatory=$true)] [Parameter(Mandatory = $true)]
[string[]]$ExpectedThumbprint [string[]]$ExpectedThumbprint
) )
@@ -850,8 +865,7 @@ function Test-FileSignature {
if ($signatureMatched) { if ($signatureMatched) {
Write-Output "Signature for $FilePath is valid" Write-Output "Signature for $FilePath is valid"
} } else {
else {
throw "Signature thumbprint do not match expected." throw "Signature thumbprint do not match expected."
} }
} }

View File

@@ -141,7 +141,13 @@ Describe "Sbt" {
Describe "ServiceFabricSDK" { Describe "ServiceFabricSDK" {
It "PowerShell Module" { It "PowerShell Module" {
Get-Module -Name ServiceFabric -ListAvailable | Should -Not -BeNullOrEmpty # Ignore PowerShell version check if running in PowerShell Core
# https://github.com/microsoft/service-fabric/issues/1343
if ($PSVersionTable.PSEdition -eq 'Core') {
Get-Module -Name ServiceFabric -SkipEditionCheck -ListAvailable | Should -Not -BeNullOrEmpty
} else {
Get-Module -Name ServiceFabric -ListAvailable | Should -Not -BeNullOrEmpty
}
} }
It "ServiceFabricSDK version" { It "ServiceFabricSDK version" {