Skip to content

Commit 2331a3e

Browse files
benbpazure-sdk
authored andcommitted
Add fallback repository support
1 parent e7acf9c commit 2331a3e

File tree

1 file changed

+81
-53
lines changed

1 file changed

+81
-53
lines changed

eng/common/scripts/Helpers/PSModule-Helpers.ps1

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
$DefaultPSRepositoryUrl = "https://www.powershellgallery.com/api/v2"
21
$global:CurrentUserModulePath = ""
32

43
function Update-PSModulePathForCI()
@@ -47,83 +46,112 @@ function Update-PSModulePathForCI()
4746
}
4847
}
4948

50-
# Manual test at eng/common-tests/psmodule-helpers/Install-Module-Parallel.ps1
51-
# If we want to use another default repository other then PSGallery we can update the default parameters
52-
function Install-ModuleIfNotInstalled()
53-
{
54-
[CmdletBinding(SupportsShouldProcess = $true)]
55-
param(
56-
[string]$moduleName,
57-
[string]$version,
58-
[string]$repositoryUrl
59-
)
60-
49+
function Get-ModuleRepositories([string]$moduleName) {
50+
$DefaultPSRepositoryUrl = "https://www.powershellgallery.com/api/v2"
6151
# List of modules+versions we want to replace with internal feed sources for reliability, security, etc.
6252
$packageFeedOverrides = @{
6353
'powershell-yaml' = 'https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-tools/nuget/v2'
6454
}
6555

66-
try {
67-
$mutex = New-Object System.Threading.Mutex($false, "Install-ModuleIfNotInstalled")
68-
$null = $mutex.WaitOne()
56+
$repoUrls = if ($packageFeedOverrides.Contains("${moduleName}")) {
57+
@($packageFeedOverrides["${moduleName}"], $DefaultPSRepositoryUrl)
58+
} else {
59+
@($DefaultPSRepositoryUrl)
60+
}
6961

70-
# Check installed modules again after acquiring lock
71-
$modules = (Get-Module -ListAvailable $moduleName)
72-
if ($version -as [Version]) {
73-
$modules = $modules.Where({ [Version]$_.Version -ge [Version]$version })
74-
}
62+
return $repoUrls
63+
}
7564

65+
function moduleIsInstalled([string]$moduleName, [string]$version) {
66+
$modules = (Get-Module -ListAvailable $moduleName)
67+
if ($version -as [Version]) {
68+
$modules = $modules.Where({ [Version]$_.Version -ge [Version]$version })
7669
if ($modules.Count -gt 0)
7770
{
7871
Write-Host "Using module $($modules[0].Name) with version $($modules[0].Version)."
7972
return $modules[0]
8073
}
74+
}
75+
return $null
76+
}
8177

82-
$repositoryUrl = if ($repositoryUrl) {
83-
$repositoryUrl
84-
} elseif ($mirrorFeedOverrides.Contains("${moduleName}")) {
85-
$mirrorFeedOverrides["${moduleName}"]
86-
} else {
87-
$DefaultPSRepositoryUrl
78+
function installModule([string]$moduleName, [string]$version, $repoUrl) {
79+
$repo = (Get-PSRepository).Where({ $_.SourceLocation -eq $repoUrl })
80+
if ($repo.Count -eq 0)
81+
{
82+
Register-PSRepository -Name $repoUrl -SourceLocation $repoUrl -InstallationPolicy Trusted
83+
$repo = (Get-PSRepository).Where({ $_.SourceLocation -eq $repoUrl })
84+
if ($repo.Count -eq 0) {
85+
throw "Failed to register package repository $repoUrl."
8886
}
87+
}
8988

90-
$repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl })
91-
if ($repositories.Count -eq 0)
92-
{
93-
Register-PSRepository -Name $repositoryUrl -SourceLocation $repositoryUrl -InstallationPolicy Trusted
94-
$repositories = (Get-PSRepository).Where({ $_.SourceLocation -eq $repositoryUrl })
95-
if ($repositories.Count -eq 0) {
96-
Write-Error "Failed to register package repository $repositoryUrl."
97-
return
98-
}
99-
}
100-
$repository = $repositories[0]
89+
if ($repo.InstallationPolicy -ne "Trusted") {
90+
Set-PSRepository -Name $repo.Name -InstallationPolicy "Trusted"
91+
}
10192

102-
if ($repository.InstallationPolicy -ne "Trusted") {
103-
Set-PSRepository -Name $repository.Name -InstallationPolicy "Trusted"
104-
}
93+
Write-Host "Installing module $moduleName with min version $version from $repoUrl"
94+
# Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching
95+
Install-Module $moduleName -MinimumVersion $version -Repository $repo.Name -Scope CurrentUser -Force
96+
# Ensure module installed
97+
$modules = (Get-Module -ListAvailable $moduleName)
98+
if ($version -as [Version]) {
99+
$modules = $modules.Where({ [Version]$_.Version -ge [Version]$version })
100+
}
101+
if ($modules.Count -eq 0) {
102+
throw "Failed to install module $moduleName with version $version"
103+
}
105104

106-
Write-Host "Installing module $moduleName with min version $version from $repositoryUrl"
107-
# Install under CurrentUser scope so that the end up under $CurrentUserModulePath for caching
108-
Install-Module $moduleName -MinimumVersion $version -Repository $repository.Name -Scope CurrentUser -Force
105+
return $modules[0]
106+
}
109107

110-
# Ensure module installed
111-
$modules = (Get-Module -ListAvailable $moduleName)
112-
if ($version -as [Version]) {
113-
$modules = $modules.Where({ [Version]$_.Version -ge [Version]$version })
114-
}
108+
# Manual test at eng/common-tests/psmodule-helpers/Install-Module-Parallel.ps1
109+
# If we want to use another default repository other then PSGallery we can update the default parameters
110+
function Install-ModuleIfNotInstalled()
111+
{
112+
[CmdletBinding(SupportsShouldProcess = $true)]
113+
param(
114+
[string]$moduleName,
115+
[string]$version,
116+
[string]$repositoryUrl
117+
)
118+
119+
# Check installed modules before after acquiring lock to avoid a big queue
120+
$module = moduleIsInstalled -moduleName $moduleName -version $version
121+
if ($module) { return $module }
115122

116-
if ($modules.Count -eq 0) {
117-
Write-Error "Failed to install module $moduleName with version $version"
118-
return
123+
try {
124+
$mutex = New-Object System.Threading.Mutex($false, "Install-ModuleIfNotInstalled")
125+
$null = $mutex.WaitOne()
126+
127+
# Check installed modules again after acquiring lock, in case it has been installed
128+
$module = moduleIsInstalled -moduleName $moduleName -version $version
129+
if ($module) { return $module }
130+
131+
$repoUrls = Get-ModuleRepositories $moduleName
132+
133+
foreach ($url in $repoUrls) {
134+
try {
135+
$module = installModule -moduleName $moduleName -version $version -repoUrl $url
136+
} catch {
137+
if ($url -ne $repoUrls[-1]) {
138+
Write-Warning "Failed to install powershell module from '$url'. Retrying with fallback repository"
139+
Write-Warning $_
140+
continue
141+
} else {
142+
Write-Warning "Failed to install powershell module from $url"
143+
throw
144+
}
145+
}
146+
break
119147
}
120148

121-
Write-Host "Using module $($modules[0].Name) with version $($modules[0].Version)."
149+
Write-Host "Using module '$($module.Name)' with version '$($module.Version)'."
122150
} finally {
123151
$mutex.ReleaseMutex()
124152
}
125153

126-
return $modules[0]
154+
return $module
127155
}
128156

129157
if ($null -ne $env:SYSTEM_TEAMPROJECTID) {

0 commit comments

Comments
 (0)