[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
# we need to create the proper folder structure
Invoke-SBWithRetry -Command {
Invoke-ScriptBlockWithRetry -Command {
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
Expand-7ZipArchive -Path $archivePath -DestinationPath $javaVersionPath
Invoke-SBWithRetry -Command {
Invoke-ScriptBlockWithRetry -Command {
Get-ChildItem -Path $javaVersionPath | Rename-Item -NewName $javaArchPath -ErrorAction Stop
}
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
Write-Host "Move PyPy '${pythonVersion}' files to '${pypyArchPath}'"
Invoke-SBWithRetry -Command {
Invoke-ScriptBlockWithRetry -Command {
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
Write-Host "Moving Ruby '${rubyVersion}' files to '${rubyArchPath}'"
Invoke-SBWithRetry -Command {
Invoke-ScriptBlockWithRetry -Command {
Move-Item -Path $tempFolder -Destination $rubyArchPath -ErrorAction Stop | Out-Null
}

View File

@@ -38,11 +38,15 @@ $tools = Get-ToolsetContent | Select-Object -ExpandProperty toolcache | Where-Ob
foreach ($tool in $tools) {
# 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
foreach ($toolVersion in $tool.versions) {
$asset = $assets | Where-Object version -like $toolVersion `
$asset = $assets `
| Where-Object version -like $toolVersion `
| Select-Object -ExpandProperty files `
| Where-Object { ($_.platform -eq $tool.platform) -and ($_.arch -eq $tool.arch) -and ($_.toolset -eq $tool.toolset) } `
| Select-Object -First 1

View File

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

View File

@@ -3,7 +3,7 @@
## 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"
if ( $inProgress ) {
$title = $inProgress.Title -join "`n"

View File

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

View File

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

View File

@@ -182,8 +182,10 @@ function Get-VsixExtenstionFromMarketplace {
[string] $MarketplaceUri = "https://marketplace.visualstudio.com/items?itemName="
)
$extensionUri = $MarketplaceUri + $ExtensionMarketPlaceName
$request = Invoke-SBWithRetry -Command { Invoke-WebRequest -Uri $extensionUri -UseBasicParsing } -RetryCount 20 -RetryIntervalSeconds 30
# Invoke-WebRequest doesn't support retry in PowerShell 5.1
$request = Invoke-ScriptBlockWithRetry -RetryCount 20 -RetryIntervalSeconds 30 -Command {
Invoke-WebRequest -Uri "${MarketplaceUri}${ExtensionMarketPlaceName}" -UseBasicParsing
}
$request -match 'UniqueIdentifierValue":"(?<extensionname>[^"]*)' | Out-Null
$extensionName = $Matches.extensionname
$request -match 'VsixId":"(?<vsixid>[^"]*)' | Out-Null
@@ -277,17 +279,15 @@ function Install-VSIXFromUrl {
Remove-Item -Force -Confirm:$false $filePath
}
function Get-VSExtensionVersion
{
function Get-VSExtensionVersion {
Param
(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string] $packageName
)
$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 | Get-ChildItem | Out-String)
Write-Host "More than one instance installed"
@@ -298,8 +298,7 @@ function Get-VSExtensionVersion
$state = $stateContent | ConvertFrom-Json
$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"
exit 1
}
@@ -307,8 +306,7 @@ function Get-VSExtensionVersion
return $packageVersion
}
function Get-ToolsetContent
{
function Get-ToolsetContent {
$toolsetPath = Join-Path "C:\\image" "toolset.json"
$toolsetJson = Get-Content -Path $toolsetPath -Raw
ConvertFrom-Json -InputObject $toolsetJson
@@ -395,27 +393,24 @@ function Get-TCToolVersionPath {
return Join-Path $foundVersion $Arch
}
function Get-WinVersion
{
function Get-WinVersion {
(Get-CimInstance -ClassName Win32_OperatingSystem).Caption
}
function Test-IsWin22
{
function Test-IsWin22 {
(Get-WinVersion) -match "2022"
}
function Test-IsWin19
{
function Test-IsWin19 {
(Get-WinVersion) -match "2019"
}
function Expand-7ZipArchive {
Param
(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string]$DestinationPath,
[ValidateSet("x", "e")]
[char]$ExtractMethod = "x"
@@ -424,8 +419,7 @@ function Expand-7ZipArchive {
Write-Host "Expand archive '$PATH' to '$DestinationPath' directory"
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"
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 (
[scriptblock] $Command,
[int] $RetryCount = 10,
@@ -670,8 +686,7 @@ function Invoke-SBWithRetry {
try {
& $Command
return
}
catch {
} catch {
Write-Host "There is an error encountered:`n $_"
$RetryCount--
@@ -771,9 +786,9 @@ function Resolve-GithubReleaseAssetUrl {
function Use-ChecksumComparison {
param (
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string]$LocalFileHash,
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string]$DistributorFileHash
)
@@ -790,7 +805,7 @@ function Get-HashFromGitHubReleaseBody {
param (
[string]$RepoOwner,
[string]$RepoName,
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string]$FileName,
[string]$Url,
[string]$Version = "latest",
@@ -815,7 +830,7 @@ function Get-HashFromGitHubReleaseBody {
$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*" }
if ([string]::IsNullOrEmpty($matchingLine)) {
throw "File name '$FileName' not found in release body."
@@ -828,9 +843,9 @@ function Get-HashFromGitHubReleaseBody {
}
function Test-FileSignature {
param(
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string]$FilePath,
[Parameter(Mandatory=$true)]
[Parameter(Mandatory = $true)]
[string[]]$ExpectedThumbprint
)
@@ -850,8 +865,7 @@ function Test-FileSignature {
if ($signatureMatched) {
Write-Output "Signature for $FilePath is valid"
}
else {
} else {
throw "Signature thumbprint do not match expected."
}
}

View File

@@ -141,8 +141,14 @@ Describe "Sbt" {
Describe "ServiceFabricSDK" {
It "PowerShell Module" {
# 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" {
Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Service Fabric\' -Name FabricVersion | Should -Not -BeNullOrEmpty