Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools for PR 6521 (#21180)
Browse files Browse the repository at this point in the history
* Use System.Threading.Mutex to make threadsafe

* Rename test, add comments

---------

Co-authored-by: Mike Harder <[email protected]>
  • Loading branch information
azure-sdk and mikeharder authored Jul 15, 2023
1 parent 9f67e71 commit 38bc7d3
Showing 1 changed file with 45 additions and 26 deletions.
71 changes: 45 additions & 26 deletions eng/common/scripts/Helpers/PSModule-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function Update-PSModulePathForCI()
}
}

# Manual test at eng/common-tests/psmodule-helpers/Install-Module-Parallel.ps1
# If we want to use another default repository other then PSGallery we can update the default parameters
function Install-ModuleIfNotInstalled()
{
Expand All @@ -65,35 +66,53 @@ function Install-ModuleIfNotInstalled()

if ($modules.Count -eq 0)
{
$repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl })
if ($repositories.Count -eq 0)
{
Register-PSRepository -Name $repositoryUrl -SourceLocation $repositoryUrl -InstallationPolicy Trusted
$repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl })
if ($repositories.Count -eq 0) {
Write-Error "Failed to registory package repository $repositoryUrl."
return
# Use double-checked locking to avoid locking when module is already installed
$mutex = New-Object System.Threading.Mutex($false, "Install-ModuleIfNotInstalled")
$null = $mutex.WaitOne()

try {
# Check installed modules again after acquiring lock
$modules = (Get-Module -ListAvailable $moduleName)
if ($version -as [Version]) {
$modules = $modules.Where({ [Version]$_.Version -ge [Version]$version })
}
}
$repository = $repositories[0]

if ($repository.InstallationPolicy -ne "Trusted") {
Set-PSRepository -Name $repository.Name -InstallationPolicy "Trusted"
}

Write-Host "Installing module $moduleName with min version $version from $repositoryUrl"
# Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching
Install-Module $moduleName -MinimumVersion $version -Repository $repository.Name -Scope CurrentUser -Force

# Ensure module installed
$modules = (Get-Module -ListAvailable $moduleName)
if ($version -as [Version]) {
$modules = $modules.Where({ [Version]$_.Version -ge [Version]$version })
if ($modules.Count -eq 0)
{
$repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl })
if ($repositories.Count -eq 0)
{
Register-PSRepository -Name $repositoryUrl -SourceLocation $repositoryUrl -InstallationPolicy Trusted
$repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl })
if ($repositories.Count -eq 0) {
Write-Error "Failed to register package repository $repositoryUrl."
return
}
}
$repository = $repositories[0]

if ($repository.InstallationPolicy -ne "Trusted") {
Set-PSRepository -Name $repository.Name -InstallationPolicy "Trusted"
}

Write-Host "Installing module $moduleName with min version $version from $repositoryUrl"
# Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching
Install-Module $moduleName -MinimumVersion $version -Repository $repository.Name -Scope CurrentUser -Force

# Ensure module installed
$modules = (Get-Module -ListAvailable $moduleName)
if ($version -as [Version]) {
$modules = $modules.Where({ [Version]$_.Version -ge [Version]$version })
}

if ($modules.Count -eq 0) {
Write-Error "Failed to install module $moduleName with version $version"
return
}
}
}

if ($modules.Count -eq 0) {
Write-Error "Failed to install module $moduleName with version $version"
return
finally {
$mutex.ReleaseMutex()
}
}

Expand Down

0 comments on commit 38bc7d3

Please sign in to comment.