Fixes exceptions while removing tmp repo download

This commit is contained in:
Lord Hepipud 2022-02-15 14:38:29 +01:00
parent 9c545f26b7
commit 99f8013252
4 changed files with 42 additions and 23 deletions

View file

@ -14,7 +14,8 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Bugfixes ### Bugfixes
* [#472](https://github.com/Icinga/icinga-powershell-framework/pull/472) Fixes random errors while dynamically compiling Add-Type code by now writing a DLL inside `cache/dll` for later usage * [#472](https://github.com/Icinga/icinga-powershell-framework/pull/472) Fixes random errors while dynamically compiling Add-Type code by now writing a DLL inside `cache/dll` for later usage
* [#478](https://github.com/Icinga/icinga-powershell-framework/pull/478) Fixes connection option "Connecting from parent system" which is not asking for ca.crt location * [#472](https://github.com/Icinga/icinga-powershell-framework/pull/472) Fixes random errors while dynamically compiling Add-Type code by now writing a DLL inside `cache/dll` for later usage
* [#479](https://github.com/Icinga/icinga-powershell-framework/pull/479) Fixes possible exceptions while trying to remove downloaded repository temp files which might still contain a file lock from virusscanners or other tasks
### Enhancements ### Enhancements

View file

@ -119,7 +119,7 @@ function Get-IcingaPowerShellModuleArchive()
if ((Invoke-IcingaWebRequest -UseBasicParsing -Uri $DownloadUrl -OutFile $DownloadDestination).HasErrors) { if ((Invoke-IcingaWebRequest -UseBasicParsing -Uri $DownloadUrl -OutFile $DownloadDestination).HasErrors) {
Write-IcingaConsoleError ([string]::Format('Failed to download "{0}" into "{1}". Starting cleanup process', $ModuleName, $DownloadDirectory)); Write-IcingaConsoleError ([string]::Format('Failed to download "{0}" into "{1}". Starting cleanup process', $ModuleName, $DownloadDirectory));
Start-Sleep -Seconds 2; Start-Sleep -Seconds 2;
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
Write-IcingaConsoleNotice 'Starting to re-run the download wizard'; Write-IcingaConsoleNotice 'Starting to re-run the download wizard';

View file

@ -1,11 +1,11 @@
<# <#
.SYNOPSIS .SYNOPSIS
Wrapper for Remove-Item to secuerly remove items allowing better handling for errors Wrapper for Remove-Item to securely remove items allowing better handling for errors
.DESCRIPTION .DESCRIPTION
Removes files and folders from disk and catches possible exceptions with proper return Removes files and folders from disk and catches possible exceptions with proper return
values to handle errors better values to handle errors better
.FUNCTIONALITY .FUNCTIONALITY
Wrapper for Remove-Item to secuerly remove items allowing better handling for errors Wrapper for Remove-Item to securely remove items allowing better handling for errors
.EXAMPLE .EXAMPLE
PS>Remove-ItemSecure -Path C:\icinga; PS>Remove-ItemSecure -Path C:\icinga;
.EXAMPLE .EXAMPLE
@ -17,8 +17,12 @@
.PARAMETER Recurse .PARAMETER Recurse
Removes sub-folders and sub-files for a given location Removes sub-folders and sub-files for a given location
.PARAMETER Force .PARAMETER Force
Tries to forefully removes a files and folders if they are either being used or a folder is Tries to forcefully removes a files and folders if they are either being used or a folder is
still containing items still containing items
.PARAMETER Retries
In case the object/folder is not removed without errors, the function will re-try the attempt
as often as specified with a sleep of 2 seconds between attempts to avoid possible file locks
for other operations
.INPUTS .INPUTS
System.String System.String
.OUTPUTS .OUTPUTS
@ -29,23 +33,38 @@
function Remove-ItemSecure() function Remove-ItemSecure()
{ {
param( param (
[string]$Path, [string]$Path,
[switch]$Recurse = $FALSE, [switch]$Recurse = $FALSE,
[switch]$Force = $FALSE [switch]$Force = $FALSE,
) [int]$Retries = 1
);
if ([string]::IsNullOrEmpty($Path) -Or (Test-Path $Path) -eq $FALSE) { if ([string]::IsNullOrEmpty($Path) -Or (Test-Path $Path) -eq $FALSE) {
Write-IcingaConsoleError 'The provided path "{0}" does not exist' -Objects $Path; Write-IcingaConsoleError 'The provided path "{0}" does not exist' -Objects $Path;
return $FALSE; return $FALSE;
} }
try { if ($Retries -le 0) {
Remove-Item -Path $Path -Recurse:$Recurse -Force:$Force -ErrorAction Stop; $Retries = 1;
return $TRUE;
} catch {
$ExMsg = $_.Exception;
Write-IcingaConsoleError 'Failed to remove items from path "{0}". Recurse is "{1}", Force is "{2}": "{3}"' -Objects $Path, $Recurse, $Force, $ExMsg;
} }
$ExMsg = $null;
while ($Retries -gt 0) {
try {
Remove-Item -Path $Path -Recurse:$Recurse -Force:$Force -ErrorAction Stop;
return $TRUE;
} catch {
$ExMsg = $_.Exception;
}
Start-Sleep -Seconds 2;
Write-IcingaConsoleNotice -Message 'Waiting for lock on path "{0}" to be released before trying to remove it' -Objects $Path;
$Retries -= 1;
}
Write-IcingaConsoleError 'Failed to remove items from path "{0}". Recurse is "{1}", Force is "{2}": "{3}"' -Objects $Path, $Recurse, $Force, $ExMsg;
return $FALSE; return $FALSE;
} }

View file

@ -83,7 +83,7 @@ function Install-IcingaComponent()
if ((Invoke-IcingaWebRequest -UseBasicParsing -Uri $FileSource -OutFile $DownloadDestination).HasErrors) { if ((Invoke-IcingaWebRequest -UseBasicParsing -Uri $FileSource -OutFile $DownloadDestination).HasErrors) {
Write-IcingaConsoleError ([string]::Format('Failed to download "{0}" from "{1}" into "{2}". Starting cleanup process', $Name.ToLower(), $FileSource, $DownloadDestination)); Write-IcingaConsoleError ([string]::Format('Failed to download "{0}" from "{1}" into "{2}". Starting cleanup process', $Name.ToLower(), $FileSource, $DownloadDestination));
Start-Sleep -Seconds 2; Start-Sleep -Seconds 2;
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
return; return;
} }
@ -128,7 +128,7 @@ function Install-IcingaComponent()
if ($null -eq $ManifestFile) { if ($null -eq $ManifestFile) {
Write-IcingaConsoleError ([string]::Format('Unable to read manifest for package "{0}". Aborting installation', $Name.ToLower())); Write-IcingaConsoleError ([string]::Format('Unable to read manifest for package "{0}". Aborting installation', $Name.ToLower()));
Start-Sleep -Seconds 2; Start-Sleep -Seconds 2;
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
return; return;
} }
@ -145,7 +145,7 @@ function Install-IcingaComponent()
if ($ManifestFile.ModuleVersion -eq $InstallVersion -And $Force -eq $FALSE) { if ($ManifestFile.ModuleVersion -eq $InstallVersion -And $Force -eq $FALSE) {
Write-IcingaConsoleWarning ([string]::Format('The package "{0}" with version "{1}" is already installed. Use "-Force" to re-install the component', $Name.ToLower(), $ManifestFile.ModuleVersion)); Write-IcingaConsoleWarning ([string]::Format('The package "{0}" with version "{1}" is already installed. Use "-Force" to re-install the component', $Name.ToLower(), $ManifestFile.ModuleVersion));
Start-Sleep -Seconds 2; Start-Sleep -Seconds 2;
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
return; return;
} }
@ -285,13 +285,13 @@ function Install-IcingaComponent()
if ($Success -eq 0) { if ($Success -eq 0) {
Write-IcingaConsoleWarning ([string]::Format('The package "service" with version "{0}" is already installed. Use "-Force" to re-install the component', $InstalledService.ProductVersion)); Write-IcingaConsoleWarning ([string]::Format('The package "service" with version "{0}" is already installed. Use "-Force" to re-install the component', $InstalledService.ProductVersion));
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
return; return;
} }
if ($Success -eq 1) { if ($Success -eq 1) {
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
Write-IcingaConsoleNotice 'Installation of component "service" was successful'; Write-IcingaConsoleNotice 'Installation of component "service" was successful';
return; return;
@ -299,7 +299,7 @@ function Install-IcingaComponent()
Write-IcingaConsoleError 'Failed to install component "service". Either the package did not include a service binary or the checksum of the binary did not match'; Write-IcingaConsoleError 'Failed to install component "service". Either the package did not include a service binary or the checksum of the binary did not match';
Start-Sleep -Seconds 2; Start-Sleep -Seconds 2;
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
return; return;
} else { } else {
Write-IcingaConsoleError 'There was no manifest file found inside the package'; Write-IcingaConsoleError 'There was no manifest file found inside the package';
@ -352,7 +352,7 @@ function Install-IcingaComponent()
if ($InstalledVersion.Full -eq $MSIData.ProductVersion -And $Force -eq $FALSE) { if ($InstalledVersion.Full -eq $MSIData.ProductVersion -And $Force -eq $FALSE) {
Write-IcingaConsoleWarning 'The package "agent" with version "{0}" is already installed. Use "-Force" to re-install the component' -Objects $InstalledVersion.Full; Write-IcingaConsoleWarning 'The package "agent" with version "{0}" is already installed. Use "-Force" to re-install the component' -Objects $InstalledVersion.Full;
Remove-Item -Path $DownloadDirectory -Recurse -Force; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
return; return;
} }
@ -391,6 +391,5 @@ function Install-IcingaComponent()
Write-IcingaConsoleError ([string]::Format('Unsupported file extension "{0}" found for package "{1}". Aborting installation', ([IO.Path]::GetExtension($FileName)), $Name.ToLower())); Write-IcingaConsoleError ([string]::Format('Unsupported file extension "{0}" found for package "{1}". Aborting installation', ([IO.Path]::GetExtension($FileName)), $Name.ToLower()));
} }
Start-Sleep -Seconds 1; Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
Remove-Item -Path $DownloadDirectory -Recurse -Force;
} }