ps/Modules/Alkami.DevOps.Installation/Public/Set-InfrastructureConfiguration.ps1

102 lines
4.2 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Set-InfrastructureConfiguration {
<#
.SYNOPSIS
Writes the expected NewRelic Infrastructure configuration file using environment specific parameters
.DESCRIPTION
Writes the expected NewRelic Infrastructure configuration file using environment specific parameters. Sets
custom attributes for Pod and ServerRole based on user input. Calculated license key is looked up from
function Get-NewRelicAccountDetails using an environment key
.PARAMETER ServerRole
[string] The literal value to use for the ServerRole custom attribute
.PARAMETER Pod
[string] The literal value to use for the Pod custom attribute
.PARAMETER EnvironmentKey
[string] The environment key, such as "AWS Production Pod 5", which is used to determine the appropriate license key for NewRelic
.PARAMETER ConfigurationFilePath
[string] The filepath to the NewRelic infrastructure configuration file. Defaults to ""C:\Program Files\New Relic\newrelic-infra\newrelic-infra.yml"
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
Set-InfrastructureConfiguration -ServerRole "BitcoinMiner" -Pod "99" -EnvironmentKey "AWS Dev Team 99" -Verbose
VERBOSE: [Set-InfrastructureConfiguration] : Using ServerRole: [BitcoinMiner]
VERBOSE: [Set-InfrastructureConfiguration] : Using Pod: [99]
VERBOSE: [Set-InfrastructureConfiguration] : ConfigurationFilePath: [C:\Program Files\New Relic\newrelic-infra\newrelic-infra.yml]
VERBOSE: [Get-NewRelicAccountDetails] : No specific account found for 'AWS Dev Team 99'; looking for default.
VERBOSE: [Get-NewRelicAccountDetails] : Returning entry with EnvironmentRegex '((?<!AWS )\bQ(A|W)\d*\b)|^AWS (DEV|CICD)'.
VERBOSE: [Set-InfrastructureConfiguration] : Using license key: [exampleApiKey]
[Set-InfrastructureConfiguration] : NewRelic Infrastructure configuration saved to C:\Program Files\New Relic\newrelic-infra\newrelic-infra.yml
#>
#TODO replace with AWS logic and pull tags instead once we move to AWS
#TODO this function name is terrible
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$ServerRole,
[Parameter(Mandatory = $true)]
[string]$Pod,
[Parameter(Mandatory = $true)]
[string]$EnvironmentKey
)
$logLead = Get-LogLeadName
Import-AWSModule
$ConfigurationFilePath = Get-NewRelicYamlPath
# Returning/stopping exactly where the error is
if (Test-StringIsNullOrWhitespace $ConfigurationFilePath) { return }
Write-Verbose "$logLead : Using ServerRole: [$ServerRole]"
Write-Verbose "$logLead : Using Pod: [$Pod]"
Write-Verbose "$logLead : ConfigurationFilePath: [$ConfigurationFilePath]"
$licenseKey = (Get-NewRelicAccountDetails $environmentKey).LicenseKey
Write-Verbose "$logLead : Using license key: $licenseKey"
Set-Content $ConfigurationFilePath @"
license_key: $licenseKey
disable_cloud_instance_id: true
custom_attributes:
ServerRole: $serverRole
Pod: '$Pod'
"@
if (Test-IsAWS) {
# We already know the instance exists, so we want to see if it has a tag for alk:overflow
# It can only return if all three are true, so there will never be two record-sets returned in the response
# If the instance has this tag, we will get some set of results back in the response, so we should always set the custom_attribute.Overflow to true
# If the instance does not have this tag, we will get no results back in the response, so we should always set the custom_attribute.Overflow to false
# We should always set the flag (even if it is to false) to ensure that we are properly reporting server intent to NR
$hostname = $env:COMPUTERNAME.ToLower()
$Ec2TagFilter = @{Name = "tag:alk:hostname"; Values = "$hostname" },
@{Name = "resource-type"; Values = "instance" },
@{Name = "tag:alk:overflow"; Values = "true" }
$scriptBlock = { Get-EC2Tag -Filter $Ec2TagFilter }
$instanceOverflowTag = Invoke-CommandWithRetry -Arguments @($hostname) -ScriptBlock $scriptBlock -MaxRetries 3 -SecondsDelay 1
if (!(Test-IsCollectionNullOrEmpty $instanceOverflowTag)) {
Add-OverflowCustomAttribute -IsOverflow $true
} else {
Add-OverflowCustomAttribute -IsOverflow $false
}
}
Write-Host "$logLead : NewRelic Infrastructure configuration saved to $ConfigurationFilePath"
}