ps/Modules/Alkami.PowerShell.Services/Public/Invoke-ServiceInstall.ps1
2023-05-30 22:51:22 -07:00

123 lines
4.9 KiB
PowerShell

function Invoke-ServiceInstall {
<#
.SYNOPSIS
This function can be called to install a choco service
.DESCRIPTION
This function is used in the pipeline to run migraions and then install the service
.PARAMETER CallingFolder
Full path to the folder containing chocolateyInstall.ps1 for the service to be installed
.PARAMETER SetNewRelicConfiguration
Should the New Relic configuration be applied to this service?
.EXAMPLE
Invoke-ServiceInstall -CallingFolder C:\ProgramData\chocolatey\lib\alkami.ms.package.name\tools -SetNewRelicConfiguration $true
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$CallingFolder,
[Parameter(Mandatory = $false)]
[switch]$SetNewRelicConfiguration = $false
)
$logLead = Get-LogLeadName
# Handle inputs where callers followed old helptext and
# passed the full filename path to chocoInstall.ps1
# We need the path to the folder that *contains* chocoInstall.ps1
if (Test-Path -Path $CallingFolder -PathType Container) {
$folderWithChocoInstall = $CallingFolder
} else {
$folderWithChocoInstall = Split-Path -Path $CallingFolder -Parent
}
$packageFolder = Split-Path -Path $folderWithChocoInstall
$manifestData = Get-PackageManifest -Path $packageFolder
$serviceManifest = $manifestData.serviceManifest
$databaseAccessRequired = Test-ServiceManifestRequiresDbAccess -ServiceManifest $serviceManifest
$hasMigrations = Test-ServiceManifestHasMigrations -ServiceManifest $serviceManifest
$isDeveloperMachine = (Test-IsDeveloperMachine) -and (!(Test-IsAws))
# Massage the runtime type
$runtime = $serviceManifest.runtime
if ($runtime -eq "legacy") {
$runtime = "framework"
}
if ($runtime -eq "core") {
$runtime = "dotnetcore"
}
$parameters = @{
ServicePath = $packageFolder
IsDatabaseAccessRequired = $databaseAccessRequired
StartOnInstall = $isDeveloperMachine
AssemblyInfo = $serviceManifest.entryPoint
SetNewRelicConfiguration = $SetNewRelicConfiguration
}
# Only run if pkg has migrations and on dev machine
if ($hasMigrations -eq $true) {
if ($isDeveloperMachine -eq $true) {
$missingAssemblies = @()
$assemblyNames = $serviceManifest.migrations.assembly.assembly
foreach ($assemblyName in $assemblyNames) {
# Our AlkamiManifest standard for Asssembly names is "flexible"
# We handle assembly names that have extensions or not
# simplitic regex :
# `^` means "beginning of line/string" **in this context**
# ".+" means "one or more of any character"
# "\." means "one dot"
# "dll" means "dll"
# `$` means "end of line/string" **in this context**
# so...
# something that ends in ".dll"
if ($assemblyName -match "^.+\.dll$") {
Write-Verbose "$loglead : Assembly name already has dll extension"
$assemblyFilename = $assemblyName
} else {
Write-Verbose "$loglead : Adding dll extension to assembly name"
$assemblyFilename = "$($assemblyName).dll"
}
$assemblyFile = Get-ChildItem -Path $packageFolder -Recurse -Filter $assemblyFilename -File
if (-not $assemblyFile) {
$missingAssemblies += $assemblyName
}
}
if (-NOT (Test-IsCollectionNullOrEmpty -Collection $missingAssemblies)) {
$missingAssembliesString = $missingAssemblies -join ","
throw "MISSING MIGRATION ASSEMBLIES - $missingAssembliesString"
}
$nuspecFSO = Get-ChildItem -Path $packageFolder -Filter "*.nuspec" | Select-Object -First 1
$nuspecXml = [xml](Get-Content -Path $nuspecFSO.FullName)
$packageId = $nuspecXml.package.metadata.id
$packageVersion = $nuspecXml.package.metadata.version
Invoke-AlkamiMigrationRunner -Runtime $runtime -PackageId $packageId -PackageVersion $packageVersion
}
}
switch ($runtime) {
"dotnetcore" {
Write-Host "$logLead : Installing a dotnetcore service"
Install-AlkamiService @parameters
<# installing migrations goes here #>
}
"framework" {
Write-Host "$logLead : Installing a framework microservice"
Install-LegacyMicroservice @parameters
<# installing migrations goes here #>
}
<# "nodejs" {
throw "this is where node would go"
} #>
default {
Write-Error "$logLead : Nothing to install for [$packageFolder] or is unsupported runtime for [$runtime]"
}
}
}