144 lines
7.6 KiB
PowerShell
144 lines
7.6 KiB
PowerShell
|
Function Get-DefaultLog4NetPathForPackage {
|
|||
|
<#
|
|||
|
|
|||
|
.SYNOPSIS
|
|||
|
Return a [string] path to the most environment-specific log4net.config file available in a base path or $null
|
|||
|
|
|||
|
.DESCRIPTION
|
|||
|
File path is determined by PackageName and SourcePath, which will usually be a TeamCity Agent parameter, but may
|
|||
|
be a local folder for testing or manual execution.
|
|||
|
Where EnvironmentName > EnvironmentType > base (Production) default:
|
|||
|
Given an EnvironmentName, or Designation, if a default log4net config file exists with that Designation as its suffix,
|
|||
|
returns path to that file.
|
|||
|
Given an EnvironmentType (other than Production), if a default log4net config file exists with that EnvironmentType as its
|
|||
|
suffix, returns path to that file.
|
|||
|
Given neither of the above, or if a matching file does not exist in the 'config-defaults' repo referenced by SourcePath
|
|||
|
for prior cases, return path to non-suffixed log4net.config for given PackageName, if it exists.
|
|||
|
Otherwise, returns $null.
|
|||
|
|
|||
|
.PARAMETER SourcePath
|
|||
|
[string] Path to the '/config-defaults/log4net' folder, probably partially derived from TeamCity Agent or Build parameters
|
|||
|
|
|||
|
.PARAMETER PackageName
|
|||
|
[string] The package name, as it exists in folder format on-disk, for the Chocolatey package needing a default log4net.config file
|
|||
|
|
|||
|
.PARAMETER EnvironmentType
|
|||
|
[string] Taken from machine.config AppSetting 'Environment.Type'. Example: "Staging" or "Production"
|
|||
|
|
|||
|
.PARAMETER EnvironmentName
|
|||
|
[string] The smallest form of the environment(pod,lane,box,designation) name;can be found in 'alk:pod', 'alk:lane', etc. Example: NI1, or 12.
|
|||
|
|
|||
|
.PARAMETER IsReliableService
|
|||
|
[switch] Boolean to determine if the package is a Reliable Service package. This changes the path where Log4Net defaults will look.
|
|||
|
|
|||
|
.OUTPUTS
|
|||
|
[string] Path to most environment-specific log4net default available for parameters given, or $null
|
|||
|
|
|||
|
.EXAMPLE
|
|||
|
Get-DefaultLog4NetPathForPackage -SourcePath "C:\AWSBuildAgentB1\work\blah\config-defaults\log4net" -PackageName "Alkami.MicroServices.Alerts.Service.Host" -EnvironmentName "SDKStg" -EnvironmentType "Staging"
|
|||
|
|
|||
|
Will return (if it exists): C:\AWSBuildAgentB1\work\blah\config-defaults\log4net\ProgramData\chocolatey\lib\Alkami.MicroServices.Alerts.Service.Host\tools\log4net.config.SDKStg
|
|||
|
Else, will return (if it exists):
|
|||
|
C:\AWSBuildAgentB1\work\blah\config-defaults\log4net\ProgramData\chocolatey\lib\Alkami.MicroServices.Alerts.Service.Host\tools\log4net.config.staging
|
|||
|
Else, will return (if it exists):
|
|||
|
C:\AWSBuildAgentB1\work\blah\config-defaults\log4net\ProgramData\chocolatey\lib\Alkami.MicroServices.Alerts.Service.Host\tools\log4net.config
|
|||
|
Else, will return
|
|||
|
$null
|
|||
|
|
|||
|
.EXAMPLE
|
|||
|
Get-DefaultLog4NetPathForPackage -SourcePath "C:\AWSBuildAgentB1\work\blah\config-defaults\log4net" -PackageName "Alkami.MicroServices.Alerts.Service.Host"
|
|||
|
|
|||
|
Will return (if it exists): C:\AWSBuildAgentB1\work\blah\config-defaults\log4net\ProgramData\chocolatey\lib\Alkami.MicroServices.Alerts.Service.Host\tools\log4net.config
|
|||
|
|
|||
|
.EXAMPLE
|
|||
|
Get-DefaultLog4NetPathForPackage -SourcePath "C:\AWSBuildAgentB1\work\blah\config-defaults\log4net" -PackageName "Alkami.MicroServices.Alerts.Service.Host" -EnvironmentName "SDKStg"
|
|||
|
|
|||
|
Will return (if it exists): C:\AWSBuildAgentB1\work\blah\config-defaults\log4net\ProgramData\chocolatey\lib\Alkami.MicroServices.Alerts.Service.Host\tools\log4net.config.SDKStg
|
|||
|
|
|||
|
.EXAMPLE
|
|||
|
Get-DefaultLog4NetPathForPackage -SourcePath "C:\AWSBuildAgentB1\work\blah\config-defaults\log4net" -PackageName "Alkami.MicroServices.Alerts.Service.Host" -EnvironmentType "Staging"
|
|||
|
|
|||
|
Will return (if it exists): C:\AWSBuildAgentB1\work\blah\config-defaults\log4net\ProgramData\chocolatey\lib\Alkami.MicroServices.Alerts.Service.Host\tools\log4net.config.staging
|
|||
|
#>
|
|||
|
[CmdletBinding()]
|
|||
|
param (
|
|||
|
[Parameter(Mandatory = $true)]
|
|||
|
[string]$SourcePath,
|
|||
|
[Parameter(Mandatory = $true)]
|
|||
|
[Alias('ElementName','ChocoPackageName')]
|
|||
|
[string]$PackageName,
|
|||
|
[Parameter()]
|
|||
|
[string]$EnvironmentType = "Production",
|
|||
|
[Parameter()]
|
|||
|
[Alias('Pod','Lane','Box','Designation')]
|
|||
|
[string]$EnvironmentName,
|
|||
|
[Parameter(Mandatory = $false)]
|
|||
|
[switch]$IsReliableService
|
|||
|
)
|
|||
|
$logLead = Get-LogLeadName
|
|||
|
Write-Host ("Find log4net default for: {0} : {1}" -f $EnvironmentType, $EnvironmentName)
|
|||
|
$baseFilter = "log4net.config"
|
|||
|
|
|||
|
# {0} => Drive and path prefix, like %checkoutDir% or %systemDrive% sort of thing.
|
|||
|
# {1} => package name
|
|||
|
$agentPathTemplate = "{0}\ProgramData\chocolatey\lib\{1}\tools"
|
|||
|
if($IsReliableService) {
|
|||
|
# Reliable Services have a different folder structure.
|
|||
|
$agentPathTemplate = "{0}\ProgramData\chocolatey\lib\{1}\svc\Code"
|
|||
|
}
|
|||
|
# {0} => baseline filename - log4net.config
|
|||
|
# {1} => EnvironmentName or EnvironmentType filename suffix - "staging" or "production" or "NI1" or "12"
|
|||
|
$filenameTemplate = "{0}.{1}"
|
|||
|
|
|||
|
# Determine if it's a load test environment, and override the environment type from what is in the environment.
|
|||
|
# In load test environments the environment type is generally Staging and we can't use it. If the ltm environment log4net cannot be found it will fall through to the production config.
|
|||
|
$isLoadTestEnvironment = (Test-IsLoadTestEnvironment -EnvironmentName $EnvironmentName)
|
|||
|
if($isLoadTestEnvironment) {
|
|||
|
$EnvironmentType = "ltm"
|
|||
|
}
|
|||
|
|
|||
|
$filters = @{}
|
|||
|
$filters["base"] = $baseFilter
|
|||
|
$filters["podOrLane"] = $EnvironmentName
|
|||
|
$filters["environmentType"] = ($EnvironmentType.ToLower())
|
|||
|
|
|||
|
$packageLog4NetFolderPath = ($agentPathTemplate -f $SourcePath, $PackageName)
|
|||
|
|
|||
|
# Select the environment name level config if it exists.
|
|||
|
if ([string]::IsNullOrEmpty($filters["podOrLane"])) {
|
|||
|
Write-Host "$logLead : No environment name/designation specified"
|
|||
|
} else {
|
|||
|
$designationSpecificFilename = $filenameTemplate -f $filters["base"], $filters["podOrLane"]
|
|||
|
$designationSpecificPath = Join-Path -Path $packageLog4NetFolderPath -ChildPath $designationSpecificFilename
|
|||
|
if (($null -ne $designationSpecificPath) -and (Test-Path $designationSpecificPath)) {
|
|||
|
Write-Host ("$logLead : Found file for {0}. Selecting {1}" -f $filters["podOrLane"], $designationSpecificFilename)
|
|||
|
return $designationSpecificPath
|
|||
|
} else {
|
|||
|
Write-Host "$logLead : $designationSpecificPath not found"
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
# Select the environment type level config if it exists.
|
|||
|
if ([string]::IsNullOrEmpty($filters["environmentType"]) -or $filters["environmentType"] -eq "Production") {
|
|||
|
Write-Host "$logLead : No environment type or Production specified"
|
|||
|
} else {
|
|||
|
$environmentSpecificFilename = $filenameTemplate -f $filters["base"], $filters["environmentType"]
|
|||
|
$environmentSpecificPath = Join-Path -Path $packageLog4NetFolderPath -ChildPath $environmentSpecificFilename
|
|||
|
if (($null -ne $environmentSpecificPath) -and (Test-Path $environmentSpecificPath)) {
|
|||
|
Write-Host ("$logLead : Found file for {0}. Selecting {1}" -f $filters["podOrLane"], $environmentSpecificFilename)
|
|||
|
return $environmentSpecificPath
|
|||
|
} else {
|
|||
|
Write-Host "$logLead : $environmentSpecificPath not found"
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
# Otherwise default to the production log4net configs.
|
|||
|
$baseProdDefaultPath = Join-Path -Path $packageLog4NetFolderPath -ChildPath $filters["base"]
|
|||
|
if (Test-Path $baseProdDefaultPath) {
|
|||
|
Write-Host ("$logLead : Found Production log4net file. Selecting {0}" -f $filters["base"])
|
|||
|
return $baseProdDefaultPath
|
|||
|
} else {
|
|||
|
Write-Host "$logLead : $baseProdDefaultPath not found"
|
|||
|
return $null
|
|||
|
}
|
|||
|
}
|