ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Edit-AlkamiServiceFabricPackageLogConfig.ps1

138 lines
6.2 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Edit-AlkamiServiceFabricPackageLogConfig {
<#
.SYNOPSIS
Edit the log config for a given package on a service fabric node, or fetches the log config
.DESCRIPTION
Can be used to fetch a log (and optionally open it in the registered local text editor)
Can be used to bulk-overwrite the existing config in an environment with the specified file
Can be used to roll back the last change made (only one restore point in time saved)
Changes are saved before pushing a new file (for rollbacks)
Each push overwrites the previous saved changes (for rollbacks)
.PARAMETER packageName
[string] The package being targeted
.PARAMETER serviceFabricWorkerNode
[string] The node to be targeted. Aliases: Server, ComputerName
.PARAMETER savePath
[string] The file target to read-to or read-from for writing to a node. Alias: ConfigPath
.PARAMETER pull
[switch] Pull the file for this package
.PARAMETER openInEditor
[switch] Open the pull'd file in the registered text-editor
.PARAMETER rollback
[switch] Roll back the very last saved-changes for this package
.PARAMETER push
[switch] Push the named file for this package
.EXAMPLE
Edit-AlkamiServiceFabricPackageLogConfig -packageName $packageName -Server $environmentTarget -pull -openInEditor
Opens the first package log config from the found environment into the registered text editor application
.EXAMPLE
Edit-AlkamiServiceFabricPackageLogConfig -packageName $packageName -Server $environmentTarget -push -ConfigPath $newFileToCopyPath
Sets the log config on each node in the environmentTarget to the file specified by the path at $newFileToCopyPath
#>
[cmdletbinding()]
param(
[Parameter(Mandatory=$true, ParameterSetName = "pull")]
[Parameter(Mandatory=$true, ParameterSetName = "push")]
[Parameter(Mandatory=$true, ParameterSetName = "rollback")]
$packageName,
[Parameter(Mandatory=$false, ParameterSetName = "pull")]
[Parameter(Mandatory=$false, ParameterSetName = "push")]
[Parameter(Mandatory=$false, ParameterSetName = "rollback")]
[Alias("Server","ComputerName")]
$serviceFabricWorkerNode = "localhost",
[Parameter(Mandatory=$false, ParameterSetName = "pull")]
[Parameter(Mandatory=$false, ParameterSetName = "push")]
[Alias("ConfigPath")]
$savePath = $PSScriptRoot,
[parameter(Mandatory=$false, ParameterSetName = "pull")]
[parameter(Mandatory=$false, ParameterSetName = "push")]
[parameter(Mandatory=$false, ParameterSetName = "rollback")]
[switch]$openInEditor,
[parameter(Mandatory=$false, ParameterSetName = "pull")]
[switch]$pull,
[parameter(Mandatory=$false, ParameterSetName = "push")]
[switch]$push,
[parameter(Mandatory=$false, ParameterSetName = "rollback")]
[switch]$rollback
)
$loglead = Get-LogLeadName
(Connect-AlkamiServiceFabricCluster -hostname $serviceFabricWorkerNode) | Out-Null
$environment = Format-AlkamiEnvironmentName (Get-AppSetting -AppSettingKey "Environment.Name" -ComputerName $serviceFabricWorkerNode)
# Gets list of worker nodes for given environment
Write-Verbose "$logLead : Getting list of worker nodes"
$workerNodeType = (Format-AlkamiEnvironmentWorkerNodeType $environment)
$workerNodes = Get-ServiceFabricNode | Where-Object { $_.NodeType -eq $workerNodeType }
$workerNodeFqdns = $workerNodes | Select-Object -ExpandProperty IpAddressOrFQDN
Write-Verbose "$workerNodeFqdns"
# Find config file on any server
Write-Verbose "$logLead : Searching for log4net.config"
# Find the application name.
$application = Get-AlkamiServiceFabricApplications -Name $packageName -EnvironmentName $environment -ComputerName $serviceFabricWorkerNode;
if($null -eq $application) {
Write-Error "$logLead : Could not find application name $packageName running in environment $environment"
return
}
# Replace the package name with the application type name.
$packageName = $application.ServiceFabricApplicationTypeName
# If the $SavePath is a directory, and not a specific file, auto-generate a file name.
if(Test-Path -Path $savePath -PathType Container) {
$savePath = (Join-Path $savePath "$packageName-Log4Net.config")
}
# If we're pushing a file and the path doesn't exist, fail out.
if($push -and (!(Test-Path $savePath))) {
Write-Error "Log4net config file to push [$savePath] does not exist."
return
}
# Search for the log4net configs on each of the worker nodes for the environment.
:workerNodeLoop foreach ($workerNode in $workerNodes) {
$nodeName = $workerNode.NodeName
$fqdn = $workerNode.IpAddressOrFQDN
$searchPath = "\\$fqdn\C$\ProgramData\SF\$nodeName\Fabric\work\Applications\$packageName*\*\log4net.config"
$FoundConfigs = Get-ChildItem -Path $searchPath | Sort-Object -property $_.Directory.name -Descending
if ((Test-IsCollectionNullOrEmpty $FoundConfigs)) {
Write-Verbose "$logLead : Unable to find log4net config for service $packageName on $nodeName";
} else {
foreach ($foundConfig in $FoundConfigs) {
if ($pull) {
Write-Host "$logLead : Downloading log4net.config from $foundConfig"
Copy-Item -Path $foundConfig -Destination $savePath
if ($openInEditor) {
& (Get-TextEditorPath) $savePath
}
break workerNodeLoop
}
if ($rollback) {
Write-Host "$logLead : Reverting Config on $nodeName"
Copy-Item -Path "$foundConfig.backup" -Destination $foundConfig.FullName
}
if ($push) {
Write-Verbose "$logLead : Creating Backup on $nodeName"
$backupDestinationFile = "$($foundConfig.FullName).backup"
Copy-Item -Path $foundConfig.FullName -Destination $backupDestinationFile
Write-Host "$logLead : Pushing Config to $nodeName"
Copy-Item -Path $savePath -Destination $foundConfig.FullName
}
}
}
}
}