ps/Modules/Alkami.PowerShell.Services/Public/Uninstall-LegacyMicroservice.ps1

142 lines
5.9 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Uninstall-LegacyMicroservice {
<#
.SYNOPSIS
Uninstalls a legacy Alkami Windows Microservice
.DESCRIPTION
Uninstalls a legacy Alkami Windows Microservice.
Legacy Microservices are considered to be Microservices that implement Alkami.Services.Subscriptions.ParticipatingService.DistributedServiceBase or derivatives.
Legacy Microservices uninstalled via this function are not uninstalled from a Service Fabric or other mesh implementation.
This installer does not remove k8s or Lambdas or anything that is not a Windows Service.
.PARAMETER ServicePath
[string] The path to the service folder or service file. If to a folder, assumes that the file matches the path name.
Ex: C:\ProgramData\chocolatey\lib\Alkami.Services.Subscriptions.Host as a param would then find the file that matches Alkami.Services.Subscriptions.Host.exe under this path
.PARAMETER AssemblyInfo
[string] The expected name of the service. This overrides the default option of matching the path ID.
.PARAMETER UseLegacyConfigForServiceName
[switch] Look up the legacy service name from the config.ps1 file. This is non-preferred. This is only used if the AssemblyInfo parameter is empty.
.EXAMPLE
Uninstall-LegacyMicroservice C:\ProgramData\chocolatey\lib\Alkami.Services.Subscriptions.Host
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[Alias("Path")]
[string]$ServicePath,
[Parameter(Mandatory = $false, Position = 1)]
[Alias("Name")]
[string]$AssemblyInfo,
[switch]$UseLegacyConfigForServiceName
)
$logLead = (Get-LogLeadName)
<#
# This is gated behind the pipeline, so we have no need to test right now.
# If we do want to go this route, we definitely have to have a flag passed in.
if (!(Test-IsDeveloperMachine) -and (Test-IsWebServer)) {
Write-Warning "$loglead : Can not install microservices on the web tier"
return
}
#>
$ServicePath = (Resolve-Path $ServicePath)
$folderPath = $ServicePath
if (!(Test-Path $ServicePath)) {
Write-Warning "$logLead : Path passed in does not represent a valid path: [$ServicePath]. Can not install something which does not exist."
return
}
$stopWatch = [System.Diagnostics.Stopwatch]::StartNew()
if ([string]::IsNullOrWhiteSpace($AssemblyInfo) -and $UseLegacyConfigForServiceName) {
Write-Verbose "$logLead : Trying to find the `$AssemblyInfo from the legacy config.ps1"
$configPs1s = (Get-ChildItem -Path $ServicePath -Filter "config.ps1" -Recurse)
$serviceId = ""
foreach($config in $configPs1s) {
$configFullName = $config.FullName
# Dotsource the file to get the contents into our runspace
. $configFullName
if (![string]::IsNullOrWhiteSpace($serviceId)) {
# Write out where we got it from in case we need to debug why we got this from a wrong location, etc
Write-Verbose "$logLead : Found [$serviceId] in [$configFullName]]"
break
}
}
# Store the value we got back so we can use it in other places
$AssemblyInfo = $serviceId
}
$item = (Get-Item $ServicePath)
if ($item.PSIsContainer) {
# path was a folder
$folderName = (Split-Path -Path $ServicePath -Leaf)
if ($folderName -match 'tools') {
$ServicePath = (Split-Path -Path $ServicePath -Parent)
$folderName = (Split-Path -Path $ServicePath -Leaf)
}
$exeName = "$folderName.exe"
if (![string]::IsNullOrWhiteSpace($AssemblyInfo)){
$exeName = "$AssemblyInfo.exe"
}
# We still don't have this value, let's pretend it's the folder name then
if ([string]::IsNullOrWhiteSpace($AssemblyInfo)) {
$AssemblyInfo = $folderName
}
Write-Host "$logLead : looking for $exeName in $ServicePath"
$exeItem = (Get-ChildItem -Path $ServicePath -Filter $exeName -Recurse) | Select-Object -First 1 # there should only be one exe with the [package name].exe inside the folders
if ($null -eq $exeItem) {
$stopWatch.Stop()
Write-Warning "$logLead : Path passed in does not contain an exe with the filename: [$exeName]. Can not uninstall a non-existent microservice in [$($stopWatch.Elapsed)]."
return
}
$ServicePath = $exeItem.FullName
} else {
# The $item was not a .PSIsContainer so it was a file
# That means we want the folderPath to be the folder of the file
$folderPath = (Split-Path $folderPath -Parent)
# We still don't have this value, let's pretend it's the folder name then
if ([string]::IsNullOrWhiteSpace($AssemblyInfo)) {
$AssemblyInfo = (Split-Path -Path $ServicePath -Leaf).Replace(".exe","")
}
}
# Check to see if the service is already registered before we try and do extra stuff
# This is where we could unregister it before we continue if we want to force a re-registration
$serviceCandidates = (Get-ServiceInfoByCIMFragment -Fragment $AssemblyInfo)
# If we didn't find anything by the assemblyinfo, let's double check the path in case something was found over there
if (Test-IsCollectionNullOrEmpty $serviceCandidates) {
$serviceCandidates = (Get-ServiceInfoByCIMFragment -Fragment $folderPath)
}
# If we found something, log it and quit
if (Test-IsCollectionNullOrEmpty $serviceCandidates) {
$stopWatch.Stop()
Write-Warning "$logLead : No services were found, nothing to do, returning in [$($stopWatch.Elapsed)]"
return
}
foreach($serviceCandidate in $serviceCandidates) {
Invoke-DeleteLegacyMicroserviceFromServiceCandidate $serviceCandidate
}
$stopWatch.Stop()
Write-Host "$logLead : [$assemblyinfo] uninstalled at [$ServicePath] in [$($stopWatch.Elapsed)]"
}