From c73276d3f6f97177d6c9000fd37a1107cc47c0b6 Mon Sep 17 00:00:00 2001 From: Vasilii Polikarpov <126792224+vpolikarpov-akvelon@users.noreply.github.com> Date: Wed, 29 Nov 2023 12:58:37 +0100 Subject: [PATCH] [Windows] Rename the Invoke-SBWithRetry function (#8880) --- .../scripts/build/Install-AndroidSDK.ps1 | 2 +- .../scripts/build/Install-JavaTools.ps1 | 2 +- images/windows/scripts/build/Install-PyPy.ps1 | 2 +- images/windows/scripts/build/Install-Ruby.ps1 | 2 +- .../windows/scripts/build/Install-Toolset.ps1 | 14 ++-- .../scripts/build/Install-VSExtensions.ps1 | 1 + .../Install-WindowsUpdatesAfterReboot.ps1 | 2 +- images/windows/scripts/build/Install-Zstd.ps1 | 2 +- .../windows/scripts/helpers/ImageHelpers.psm1 | 2 +- .../scripts/helpers/InstallHelpers.ps1 | 84 +++++++++++-------- images/windows/scripts/tests/Tools.Tests.ps1 | 8 +- 11 files changed, 73 insertions(+), 48 deletions(-) diff --git a/images/windows/scripts/build/Install-AndroidSDK.ps1 b/images/windows/scripts/build/Install-AndroidSDK.ps1 index 5233e92b1..834e9f052 100644 --- a/images/windows/scripts/build/Install-AndroidSDK.ps1 +++ b/images/windows/scripts/build/Install-AndroidSDK.ps1 @@ -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 } diff --git a/images/windows/scripts/build/Install-JavaTools.ps1 b/images/windows/scripts/build/Install-JavaTools.ps1 index a20e3ee40..efbb8a897 100644 --- a/images/windows/scripts/build/Install-JavaTools.ps1 +++ b/images/windows/scripts/build/Install-JavaTools.ps1 @@ -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 diff --git a/images/windows/scripts/build/Install-PyPy.ps1 b/images/windows/scripts/build/Install-PyPy.ps1 index 1fb8d0360..33a6c7b39 100644 --- a/images/windows/scripts/build/Install-PyPy.ps1 +++ b/images/windows/scripts/build/Install-PyPy.ps1 @@ -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 } diff --git a/images/windows/scripts/build/Install-Ruby.ps1 b/images/windows/scripts/build/Install-Ruby.ps1 index a0869d959..b3036cc07 100644 --- a/images/windows/scripts/build/Install-Ruby.ps1 +++ b/images/windows/scripts/build/Install-Ruby.ps1 @@ -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 } diff --git a/images/windows/scripts/build/Install-Toolset.ps1 b/images/windows/scripts/build/Install-Toolset.ps1 index 09c2bac85..e032464b6 100644 --- a/images/windows/scripts/build/Install-Toolset.ps1 +++ b/images/windows/scripts/build/Install-Toolset.ps1 @@ -38,14 +38,18 @@ $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 ` - | Select-Object -ExpandProperty files ` - | Where-Object { ($_.platform -eq $tool.platform) -and ($_.arch -eq $tool.arch) -and ($_.toolset -eq $tool.toolset) } ` - | Select-Object -First 1 + $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 Write-Host "Installing $($tool.name) $toolVersion $($tool.arch)..." if ($null -ne $asset) { diff --git a/images/windows/scripts/build/Install-VSExtensions.ps1 b/images/windows/scripts/build/Install-VSExtensions.ps1 index f33e9678c..a866876ff 100644 --- a/images/windows/scripts/build/Install-VSExtensions.ps1 +++ b/images/windows/scripts/build/Install-VSExtensions.ps1 @@ -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 { diff --git a/images/windows/scripts/build/Install-WindowsUpdatesAfterReboot.ps1 b/images/windows/scripts/build/Install-WindowsUpdatesAfterReboot.ps1 index 401a45ed0..ef9b562bf 100644 --- a/images/windows/scripts/build/Install-WindowsUpdatesAfterReboot.ps1 +++ b/images/windows/scripts/build/Install-WindowsUpdatesAfterReboot.ps1 @@ -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" diff --git a/images/windows/scripts/build/Install-Zstd.ps1 b/images/windows/scripts/build/Install-Zstd.ps1 index 97d44b56d..224f2cf5f 100644 --- a/images/windows/scripts/build/Install-Zstd.ps1 +++ b/images/windows/scripts/build/Install-Zstd.ps1 @@ -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 { diff --git a/images/windows/scripts/helpers/ImageHelpers.psm1 b/images/windows/scripts/helpers/ImageHelpers.psm1 index 38c2ac596..d8d04b9ac 100644 --- a/images/windows/scripts/helpers/ImageHelpers.psm1 +++ b/images/windows/scripts/helpers/ImageHelpers.psm1 @@ -38,7 +38,7 @@ Export-ModuleMember -Function @( 'Resolve-ChocoPackageVersion' 'Resolve-GithubReleaseAssetUrl' 'Expand-7ZipArchive' - 'Invoke-SBWithRetry' + 'Invoke-ScriptBlockWithRetry' 'Get-VsCatalogJsonPath' 'Get-AndroidPackages' 'Get-AndroidPlatformPackages' diff --git a/images/windows/scripts/helpers/InstallHelpers.ps1 b/images/windows/scripts/helpers/InstallHelpers.ps1 index df88bdd07..036866c27 100644 --- a/images/windows/scripts/helpers/InstallHelpers.ps1 +++ b/images/windows/scripts/helpers/InstallHelpers.ps1 @@ -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":"(?[^"]*)' | Out-Null $extensionName = $Matches.extensionname $request -match 'VsixId":"(?[^"]*)' | Out-Null @@ -212,9 +214,9 @@ function Get-VsixExtenstionFromMarketplace { return [PSCustomObject] @{ "ExtensionName" = $extensionName - "VsixId" = $vsixId - "FileName" = $fileName - "DownloadUri" = $downloadUri + "VsixId" = $vsixId + "FileName" = $fileName + "DownloadUri" = $downloadUri } } @@ -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." } } diff --git a/images/windows/scripts/tests/Tools.Tests.ps1 b/images/windows/scripts/tests/Tools.Tests.ps1 index be8864d8b..4c8690fce 100644 --- a/images/windows/scripts/tests/Tools.Tests.ps1 +++ b/images/windows/scripts/tests/Tools.Tests.ps1 @@ -141,7 +141,13 @@ Describe "Sbt" { Describe "ServiceFabricSDK" { 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" {