ps/Modules/Alkami.PowerShell.Choco/Public/Get-CategorizedChocoPackages.ps1
2023-05-30 22:51:22 -07:00

167 lines
6.8 KiB
PowerShell

function Get-CategorizedChocoPackages {
<#
.SYNOPSIS
Returns a list of categorized chocolatey packages by 'migrations', 'new', or 'other'.
#>
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
Param(
[Parameter(Mandatory=$false)]
[string]$packagesToInstallInput = $null,
[Parameter(Mandatory=$false)]
[switch]$includeFeeds
)
# Temporary fix. Add overrides to force certain packages to be treated like dbms accounts.
# This should be used when a microservice changes from a micro to a dbms account, which breaks service-account migration determination logic.
$migrationPackageOverrides = @(
"Alkami.MicroServices.Aggregation.Service.Host",
"Alkami.MicroServices.CardManagement.Service.Host",
"Alkami.MicroServices.AggregationProviders.Yodlee.Host",
"Alkami.MicroServices.Risk.Alkami.Service.Host",
"Alkami.MicroServices.SymConnectMultiplexer.Service.Host"
)
$loglead = (Get-LogLeadName);
Write-Host "$loglead Getting list of locally installed chocolatey packages."
$localChocos = Get-ChocoState -l
Write-Host "`n$loglead Reading feeds for local choco packages, and discarding default/internal tools choco packages."
Set-ChocoPackageSourceFeeds $localChocos
$localChocos = $localChocos | Where-Object { (!$_.Feed.IsDefault) -and ($_.Feed.Source -notlike "*nuget/nuget.internal*") -and ($_.Feed.Source -notlike "*SRETools*") }
$splitOption = [System.StringSplitOptions]::RemoveEmptyEntries
$newPackages = @();
if($packagesToInstallInput) {
Write-Host "`n$loglead Parsing list of packages to be installed."
$splitPackagesToInstall = $packagesToInstallInput.Split([Environment]::NewLine, $splitOption)
$packagesToInstall = Format-ParseChocoPackages $splitPackagesToInstall " "
if($includeFeeds.IsPresent) {
Set-ChocoPackageSourceFeeds $packagesToInstall
}
Write-Host "`n$loglead Determining which packages haven't been installed before."
foreach($package in $packagesToInstall) {
$installedAlreadySearch = $localChocos | Where-Object { $_.Name -eq $package.Name }
if(!$installedAlreadySearch) {
$newPackages += $package;
}
}
}
Write-Host "`n$loglead Finding chocolatey services running as windows services."
$chocoServices = Get-ChocolateyServices -IncludeDisabled
Write-Host "`n$loglead Determining which chocolatey services have migrations, or not"
$servicesWithoutMigrations = @();
$servicesWithMigrations = @();
$chocoInstallPath = Get-ChocolateyInstallPath
$chocoPath = "$chocoInstallPath\lib\"
foreach($service in $chocoServices) {
Write-Verbose "Checking if service $($service.Name) has migrations"
$serviceUser = Get-WindowsServiceUser $service.Name
$execName = (Get-WindowsServiceApplicationName $service.Name).Replace("`"", "")
$chocoName = $null
if($execName.length -ge $chocoPath.length) {
$endIndex = $execName.IndexOf('\', $chocoPath.length)
if($endIndex -ge 0) {
$chocoName = $execName.Substring($chocoPath.length, $endIndex - $chocoPath.length)
}
}
# Determine services with/without migrations.
# Search from the $packagesToInstall first so newer choco versions are preferred.
# Otherwise fall back to the one that's already installed.
$package = $null
if($packagesToInstall) {
$package = $packagesToInstall | Where-Object { $_.Name -eq $chocoName } | Select-Object -First 1
}
if(!$package) {
$package = $localChocos | Where-Object { $_.Name -eq $chocoName } | Select-Object -First 1
}
if($package) {
if($serviceUser -match "dbms\$*$") {
$servicesWithMigrations += $package;
}
# Handle dbms migration overrides. Not using a hash map because hash lookups are case sensitive.
elseif($null -ne ($migrationPackageOverrides | Where-Object {$package.Name -eq $_})) {
Write-Warning "Overriding $($package.Name) to be considered a microservice with migrations."
$servicesWithMigrations += $package;
} elseif($serviceUser -match "micro\$*$") {
$servicesWithoutMigrations += $package;
} else {
Write-Warning "$loglead Package $($package.Name) is not recognized as a micro/dbms Alkami windows service."
}
} else {
Write-Warning "$loglead No recognized windows service for choco package: $($service.Name)"
}
}
# Now come up with a list of every package that is not in the list of services with/without migrations.
# This will catch the packages that only copy files.
Write-Host "$loglead Now determining which chocolatey packages are not windows services."
$otherPackages = @()
foreach($package in $localChocos) {
$search = $servicesWithMigrations | Where-Object { $_.Name -eq $package.Name } | Select-Object -First 1
if(!$search) {
$search = $servicesWithoutMigrations | Where-Object { $_.Name -eq $package.Name } | Select-Object -First 1
}
if(!$search) {
$search = $packagesToInstall | Where-Object { $_.Name -eq $package.Name } | Select-Object -First 1
if($search) {
$otherPackages += $search
} else {
$otherPackages += $package
}
}
}
# Separate out the choco installer packages from the 'other' packages.
$installerPackages = $otherPackages | Where-Object { (Test-IsPackageInstaller -packageName $_.Name) }
$otherPackages = $otherPackages | Where-Object { !(Test-IsPackageInstaller -packageName $_.Name) }
Write-Host "`n$loglead New Packages:"
foreach($package in $newPackages) {
Write-Host " $($package.Name) $($package.Version)"
}
Write-Host "`n$loglead Other Packages:"
foreach($package in $otherPackages) {
Write-Host " $($package.Name) $($package.Version)"
}
Write-Host "`n$loglead Services With Migrations:"
foreach($package in $servicesWithMigrations) {
Write-Host " $($package.Name) $($package.Version)"
}
Write-Host "`n$loglead Services Without Migrations:"
foreach($package in $servicesWithoutMigrations) {
Write-Host " $($package.Name) $($package.Version)"
}
Write-Host "`n$loglead Microservice Installer Packages:"
foreach($package in $installerPackages) {
Write-Host " $($package.Name) $($package.Version)"
}
$result = @{
New = $newPackages
Other = $otherPackages
WithMigrations = $servicesWithMigrations
WithoutMigrations = $servicesWithoutMigrations
Installer = $installerPackages
}
return $result;
}