Last active
April 30, 2025 18:09
-
-
Save rleap-m/8de09605581641402f4c7d4e9f0933d6 to your computer and use it in GitHub Desktop.
Script to populate each MSR org with one or more repositories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Script to populate each MSR org with one or more repositories | |
.DESCRIPTION | |
This script will take a list of local images and push them to the MSR registry for each MKE organization. | |
It will create a new repository for each image in the MSR registry and push the image to that repository. | |
.PARAMETER MkeUrl | |
The URL of the MKE server. | |
.PARAMETER MkrCred | |
The credentials for the MKE server. | |
.PARAMETER MsrRegistryHost | |
The MSR Registry hostname. | |
.PARAMETER ImageNamespaceFilter | |
The namespace filter for local images to be pushed to MSR. | |
.PARAMETER ImageTagFilter | |
The tag filter for local images to be pushed to MSR. | |
.PARAMETER MkeMgmtModuleName | |
The name of the MKE management module to import. | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string] $MkeUrl, | |
[Parameter(Mandatory=$true,ParameterSetName='Cred')] | |
[ValidateNotNull()] | |
[System.Management.Automation.PSCredential] | |
[System.Management.Automation.Credential()] | |
$MkeCred, | |
[Parameter(Mandatory = $true)] | |
[string] $MsrRegistryHost, | |
[Parameter(Mandatory=$false)] | |
[string] $ImageNamespaceFilter = "mirantis", | |
[Parameter(Mandatory=$false)] | |
[string] $ImageTagFilter = "3.8.5", | |
[Parameter(Mandatory=$false)] | |
[ValidateRange(1,50)] | |
[int] $ImagesPerOrg = 3, | |
[Parameter(Mandatory=$false)] | |
[string] $MkeMgmtModuleName = 'mke.mgmt' | |
) | |
Try { | |
Import-Module -Name $MkeMgmtModuleName -ErrorAction Stop | |
} | |
Catch { | |
Write-Warning "Unable to import the MKE management module. Please ensure it is available on the system." | |
return | |
} | |
Write-Verbose "Logging in to container registry [$MsrRegistryHost]..." | |
ConvertFrom-SecureString -AsPlainText $MkeCred.Password | docker login -u $MkeCred.UserName $MsrRegistryHost --password-stdin | |
if ($LASTEXITCODE -ne 0) { | |
Write-Warning "Docker login to registry [$MsrRegistryHost] failed. Please check your credentials and registry host." | |
return | |
} | |
Write-Verbose "Logging in to container registry [$MsrRegistryHost] complete." | |
Write-Verbose "Establishing MKE session..." | |
New-MkeSession -url $MkeUrl -Credential $MkeCred -AllowInsecureTransfer | |
if ($null -eq (Get-MkeSession -Active)) { | |
Write-Warning "Unable to create an MKE session. Please check your credentials and URL." | |
return | |
} | |
Write-Verbose "Establishing MKE session complete." | |
Write-Verbose "Obtaining MKE organization list..." | |
$mkeOrgs = Get-MkeOrg | |
if ($null -eq $mkeOrgs) { | |
Write-Warning "Unable to obtain MKE organization list." | |
return | |
} | |
# Looking for the Orgs with a numeric name as that is the convention we used for scale testing | |
$mkeOrgs = $mkeOrgs | Where-Object { $_.name -match '^\d+$' } | |
$localImages = @(& docker image ls --format '{{json .}}' | ConvertFrom-Json | Where-Object { ($_.Tag -eq $ImageTagFilter) -and ($_.Repository -match $ImageNamespaceFilter) }) | |
if ($localImages.Count -eq 0) { | |
Write-Warning "No images found matching the specified tag [$ImageTagFilter] and namespace [$ImageNamespaceFilter] filters." | |
return | |
} | |
if ($localImages.Count -lt $imagesPerOrg) { | |
Write-Warning "Not enough images found matching the specified tag [$ImageTagFilter] and namespace [$ImageNamespaceFilter] filters. Found [$($localImages.Count)] images, but need [$ImagesPerOrg] images per org." | |
return | |
} | |
Write-Verbose "Obtaining MKE organization list complete. Populating [$($mkeOrgs.Count)] organizations with images..." | |
foreach ($mkeOrg in $mkeOrgs) { | |
$randomLocalImages = Get-Random -InputObject $localImages -Count $ImagesPerOrg | |
$randomLocalImages | ForEach-Object -Parallel { | |
$VerbosePreference = $using:VerbosePreference | |
Write-Verbose "Adding image [$($_.Repository):$($_.Tag)] to organization [$($using:mkeOrg.name)]..." | |
& docker image tag "$($_.Repository):$($_.Tag)" "$using:MsrRegistryHost/$($using:mkeOrg.name)/$(($_.Repository -split '/')[-1]):$($_.Tag)" | |
& docker image push "$using:MsrRegistryHost/$($using:mkeOrg.name)/$(($_.Repository -split '/')[-1]):$($_.Tag)" | |
& docker image rm "$using:MsrRegistryHost/$($using:mkeOrg.name)/$(($_.Repository -split '/')[-1]):$($_.Tag)" | |
Write-Verbose "Adding image [$($_.Repository):$($_.Tag)] to organization [$($using:mkeOrg.name)] complete." | |
} -ThrottleLimit $ImagesPerOrg -Verbose:($VerbosePreference -eq 'Continue') | |
} | |
Write-Verbose "Obtaining MKE organization list complete. Populating [$($mkeOrgs.Count)] organizations with images complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment