ps/Modules/Alkami.DevOps.Installation/Public/Get-EnvironmentData.ps1
2023-05-30 22:51:22 -07:00

136 lines
4.8 KiB
PowerShell

function Get-EnvironmentData {
<#
.SYNOPSIS
Get basic data about the environment. Called by Get-ServerPackageInformation as part of the Classify_Packages part of the Deploy pipeline
.PARAMETER PackageMetadata
Packages object to populate
.LINK
Get-ServerPackageInformation
.LINK
Test-IsEclairInstalled
#>
[CmdletBinding()]
param(
[PSObject]$PackageMetadata
)
$loglead = Get-LogLeadName
$PackageMetadata.ServersToQuery = (
@($PackageMetadata.WebServers) +
@($PackageMetadata.AppServers) +
@($PackageMetadata.MicServers) +
@($PackageMetadata.SelectedFabServer)
) | Where-Object { $_ }
$serverToQuery = $PackageMetadata.ServersToQuery | Select-Object -First 1
# Read the machine.config into a variable to avoid going across the network to read
# it every time we call Get-AppSetting... which we were doing before... :/
$dncPath = Get-DotNetConfigPath
$dncUncPath = Get-UncPath -ComputerName $serverToQuery -filePath $dncPath -IgnoreLocalPaths
$machineConfigContent = Read-XMLFile -xmlPath $dncUncPath
# Map from $packageMetadata KEY to machine.config KEY
$envDataKeyMap = @{
EnvironmentName = "Environment.Name"
EnvironmentType = "Environment.Type"
EnvironmentHosting = "Environment.Hosting"
EnvironmentNameSafeDesignation = "Environment.NameSafeDesignation"
}
foreach ($envDataKey in $envDataKeyMap.keys) {
Write-Host "$loglead : deployData Key: $envDataKey"
$mcKey = $envDataKeyMap[$envDataKey]
Write-Host "$loglead : Machine config key: $mcKey"
$mcValue = Get-AppSetting -Key $mcKey -XmlDocument $machineConfigContent -SuppressWarnings
Write-Host "$loglead : Machine config value: $mcValue"
$packageMetadata.$envDataKey = $mcValue
}
if ($PackageMetadata.EnvironmentNameSafeDesignation) {
Write-Host "$loglead : EnvironmentNameSafeDesignation: $($PackageMetadata.EnvironmentNameSafeDesignation)"
} else {
Write-Warning "$loglead : NameSafeDesignation MISSING from the machine.config. Getting from tags..."
$tags = Get-InstanceTags -ServerToTest $serverToQuery
foreach ($tag in $tags) {
if ($tag.Key -eq "alk:env") {
$designation = Get-DesignationTagNameByEnvironment $tag.Value
}
}
if ($designation) {
foreach ($tag in $tags) {
if ($tag.Key -eq "alk:$designation") {
$PackageMetadata.EnvironmentNameSafeDesignation = $tag.Value.Replace('.', '-').Trim().ToLower()
}
}
}
if (!$designation -or !$PackageMetadata.EnvironmentNameSafeDesignation) {
Write-Error "$loglead : Did not find a Designation tag. This server ($serverToQuery) is missing both machine.config values and tags. Investigation is required."
}
}
if ($PackageMetadata.EnvironmentHosting) {
Write-Host "$loglead : EnvironmentHosting: $($PackageMetadata.EnvironmentHosting)"
} else {
Write-Warning "$loglead : Could not obtain a EnvironmentHosting. This will result in no Infrastructure Migrations being run."
}
#region IsEclairInstalled
$sbIsEclairInstalled = {
param($server)
try {
$results = @{
Hostname = $server
IsEclairInstalled = $false
Error = $null
Success = $false
}
$ei = Test-IsEclairInstalled -ComputerName $server
$results.IsEclairInstalled = $ei
$results.Success = $true
} catch {
$ex = $_
$msg = $_.Exception.Message
Write-Warning "$loglead : There was an error testing for Eclair"
Write-Warning "$msg"
$results.Error = $ex
$results.Success = $false
}
return $results
}
$ieiResults = Invoke-Parallel -objects $PackageMetadata.ServersToQuery -script $sbIsEclairInstalled
$isEclairInstalledOnAllHosts = $ieiResults.IsEclairInstalled -notcontains $false
Write-Host "$loglead : Is Eclair installed on all hosts: $isEclairInstalledOnAllHosts"
$PackageMetadata.IsEclairInstalledOnAllHosts = $isEclairInstalledOnAllHosts
# Trim out crap from (RunspaceId, PSSourceJobInstanceId, etc.) ReceiveJob so the Classify Packages tests will pass
$trimmedResults = @()
foreach($result in $ieiResults)
{
$hash = @{}
foreach($enum in $result.GetEnumerator()) {
$hash.add($enum.key, $enum.value)
}
$trimmedResults += $hash
}
$PackageMetadata.EclairInstallData = $trimmedResults
#endregion IsEclairInstalled
return $PackageMetadata
}