ps/Modules/Alkami.PowerShell.Common/Public/Move-LogsAndDeleteDotNetTemps.ps1

94 lines
3.4 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Move-LogsAndDeleteDotNetTemps {
<#
.SYNOPSIS
Deletes or archives logs based off a cutoff date and then cleans the .NET temporary files
.DESCRIPTION
By default, deletes current and rolled logs in a given folder, then, if not on a FAB host,
cleans .NET temp files and archives chocolatey logs and prunes old chocolatey log archives.
.PARAMETER logDirectory
String represenation of the path to the folder that should be cleaned up.
Optional. If omitted, Get-OrbLogsPath is used instead
.PARAMETER cutoffDays
Number of days of past log archives to retain. Anything older will be deleted
.PARAMETER forceRecycle
Switch to clean up non-FAB host logs and temps even if there are Alkami worker processes
running
.PARAMETER skipActiveLogs
Switch to ignore any currently active logs (ones that have not rolled) in the case of
either a FAB server, where services never stop, or an element deploy or "hot" tier restart
.PARAMETER ArchiveLogFiles
Switch to enable zipping up of log files, instead of simple deleting
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[Alias("LogPath")]
[string]$logDirectory,
[Parameter(Mandatory=$false)]
[Alias("CutoffThreshold")]
[int]$cutoffDays = 1,
[Parameter(Mandatory=$false)]
[Alias("Force")]
[switch]$forceRecycle,
[Parameter(Mandatory=$false)]
[Alias("SkipActive")]
[switch]$skipActiveLogs,
[Parameter(Mandatory=$false)]
[switch]$ArchiveLogFiles
)
$logLead = (Get-LogLeadName)
if ([string]::IsNullOrEmpty($logDirectory)) {
$logDirectory = (Get-OrbLogsPath)
}
if (Test-IsServiceFabricServer) {
Write-Warning "$logLead : FAB tier servers do not have .net Temps"
if ($ArchiveLogFiles) {
Write-Warning "$logLead : Calling Backup-ORBLogFiles -SkipActiveLogs"
Backup-AlkamiLogs -skipActiveLogs -CutoffThreshold $cutoffDays -LogPath $logDirectory
} else {
# Delete current log files but -skipActiveLogs (regardless of passed param)
Write-Warning "$logLead : Skipped Backup, deleting current logs ONLY(FAB)"
Remove-ORBLogFiles -SkipActiveLogs -LogPath $logDirectory
}
return
}
if ((Search-ForRunningWorkerProcesses) -or $forceRecycle) {
Remove-DotNetTemporaryFiles
if ($ArchiveLogFiles) {
Backup-AlkamiLogs -skipActiveLogs:$skipActiveLogs -CutoffThreshold $cutoffDays -LogPath $logDirectory
# Backup-ORBLogFiles -skipActiveLogs:$skipActiveLogs
# Remove-OldArchivedLogFiles -ArchivePath (Join-Path $logDirectory "Archive") -CutoffThreshold $cutoffDays
} else {
# Delete current log files but -skipActiveLogs (regardless of passed param)
Write-Warning "$logLead : Skipped Backup, deleting current logs ONLY"
Remove-ORBLogFiles -SkipActiveLogs:$skipActiveLogs -LogPath $logDirectory
}
$chocoInstallPath = Get-ChocolateyInstallPath
$chocoDirectory = Join-Path $chocoInstallPath "logs"
Backup-LogFiles -logDirectory $chocoDirectory -CutoffDays 7 # Preserve choco logs for a week.
Remove-OldArchivedLogFiles -ArchivePath (Join-Path $chocoDirectory "Archive") -CutoffThreshold 14 # Preserve 2 weeks of logs.
} else {
Write-Warning ("$logLead : Alkami worker processes are still running. Cannot continue.")
}
}