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

85 lines
3.4 KiB
PowerShell

function Get-Environment {
<#
.SYNOPSIS
Tries to determine the Environment type (AWS, QA, Prod, Staging, etc.) using environment details
#>
[CmdletBinding()]
[OutputType([System.String])]
Param()
$logLead = (Get-LogLeadName);
$unknown = "UNKNOWN"
# Get Environment From Tags if Possible
if (Test-IsAws) {
Write-Verbose "$logLead : Hosting provider is AWS -- checking tags to determine environment"
$serverEnvironment = Get-CurrentInstanceTags ($Global:AlkamiTagKeyEnvironment) -ValueOnly
if ([String]::IsNullOrEmpty($serverEnvironment)) {
Write-Warning "$logLead : Could not determine environment from the $Global:AlkamiTagKeyEnvironment tag. Value returned: <$serverEnvironment>"
return $unknown
}
$validTagValues = @(
$Global:AlkamiTagValueEnvironmentProd,
$Global:AlkamiTagValueEnvironmentStaging,
$Global:AlkamiTagValueEnvironmentQa,
$Global:AlkamiTagValueEnvironmentDr,
$Global:AlkamiTagValueEnvironmentSandbox,
$Global:AlkamiTagValueEnvironmentDev,
$Global:AlkamiTagValueEnvironmentLTM,
$Global:AlkamiTagValueEnvironmentLoadtest
)
if ($serverEnvironment -notin $validTagValues) {
Write-Warning "$logLead : Unknown tag value '$serverEnvironment' identified. What is going on with your tags?"
return $unknown
}
Write-Host "$logLead : Environment determined to be $serverEnvironment based on the $Global:AlkamiTagKeyEnvironment tag value"
return $serverEnvironment
}
$environmentType = Get-AppSetting "Environment.Type" -ErrorAction SilentlyContinue
# Get Environment from Features / Beacon Values if Tags Not Available. So long as it's an ORB server it should never really get past this stage
if (!([String]::IsNullOrEmpty($environmentType))) {
Write-Host "$logLead : Checking Environment.Type Value $environmentType from Machine Config Against Known List"
$environment = switch ($environmentType)
{
"Production" { $Global:AlkamiTagValueEnvironmentProd }
"Staging" { $Global:AlkamiTagValueEnvironmentStaging }
"QA" { $Global:AlkamiTagValueEnvironmentQa }
"TeamQA" { $Global:AlkamiTagValueEnvironmentQa }
"Integration" { $Global:AlkamiTagValueEnvironmentQa }
"Development" { $Global:AlkamiTagValueEnvironmentDev }
}
Write-Host "$logLead : Environment determined to be $environment based on machine.config"
return $environment
}
# Get Environment from Computer Name if We Absolutely Have To. Some misconfigured CORP dev/qa environments might hit this
$compName = $env:ComputerName
if ($compName -like "ALK-*") {
if ($compName -like "*QA*") {
Write-Host ("$logLead : Environment determined to be {0} based on hostname {1}" -f $Global:AlkamiTagValueEnvironmentQa, $compName)
return $Global:AlkamiTagValueEnvironmentQa
}
else {
Write-Host ("$logLead : Environment determined to be {0} based on hostname {1}" -f $Global:AlkamiTagValueEnvironmentDev, $compName)
return $Global:AlkamiTagValueEnvironmentDev
}
}
# Give up
Write-Warning ("$logLead : Unable to determine environment automatically")
return $unknown
}