ps/Modules/Alkami.PowerShell.Services/Public/Set-ChocolateyPackageNewRelicState.ps1

74 lines
2.9 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Set-ChocolateyPackageNewRelicState {
<#
.SYNOPSIS
Enables or disables new relic for a specific microservice chocolatey package. The microservice still requires a restart before it will take effect.
.PARAMETER Name
The name of the package to set the new-relic state for.
.PARAMETER Directory
The directory to search for app.configs and change the new relic state.
.PARAMETER Enabled
True to enable New Relic, false to disable it.
#>
[CmdletBinding(DefaultParameterSetName = 'ByPackageName')]
Param(
[Parameter(ParameterSetName = 'ByPackageName', Mandatory = $true)]
[string]$Name,
[Parameter(ParameterSetName = 'ByDirectory', Mandatory = $true)]
[string]$Directory,
[Parameter(ParameterSetName = 'ByDirectory', Mandatory = $true)]
[Parameter(ParameterSetName = 'ByPackageName', Mandatory = $true)]
[bool]$Enabled
)
$loglead = (Get-LogLeadName)
# $Name$Directory will write both values, but it can only be one or the other. Cheap hack putting them both in the same brackets
Write-Host "$logLead : Setting NR state for [$Name$Directory]"
$directoryToSearch = $null
if($PSCmdlet.ParameterSetName -eq "ByPackageName") {
# Construct the path to the microservice in the chocolatey install path.
$chocoPath = (Get-ChocolateyInstallPath)
$chocoPath = Join-Path $chocoPath "lib"
$chocoPath = Join-Path $chocoPath $name
$directoryToSearch = $chocoPath
} elseif($PSCmdlet.ParameterSetName -eq "ByDirectory") {
$directoryToSearch = $Directory
}
# $Name$Directory will write both values, but it can only be one or the other. Cheap hack putting them both in the same brackets
Write-Host "$logLead : Found [$directoryToSearch] for [$Name$Directory]"
if(!(Test-Path $directoryToSearch)) {
Write-Warning "$loglead : Could not locate app.config path '$directoryToSearch'"
return
}
# Find the app configs. Start with "legacy" .exe.config for framework apps
$appConfigs = (Get-ChildItem -Path $directoryToSearch -Include "*.exe.config" -Recurse)
# If no .exe.config, look for an appsettings.json
if(Test-IsCollectionNullOrEmpty $appConfigs) {
$appConfigs = (Get-ChildItem -Path $directoryToSearch -Include "appsettings.json" -Recurse)
}
if(Test-IsCollectionNullOrEmpty $appConfigs) {
Write-Warning "$loglead : Could not locate app.config for $Name"
return
}
# Determine the value of the app setting.
$desiredValue = if($Enabled) { "true" } else { "false" }
# Update all the app.configs.
foreach($configPath in $appConfigs) {
Set-AppSetting -FilePath $configPath -Key "NewRelic.AgentEnabled" -Value $desiredValue -UpdateOnly
}
Set-DotNetCoreProfiling -Path $directoryToSearch -Enabled $enabled
Write-Host "$logLead : Finished setting NR state for [$directoryToSearch]"
}