Skip to content

Commit 43252ce

Browse files
authored
Merge pull request #479 from Icinga:fix/download_packages_removal_exception
Fix: Exceptions while removing temp files from repository download Fixes possible exceptions while trying to remove downloaded repository temp files which might still contain a file lock from virusscanners or other tasks
2 parents 9c545f2 + 99f8013 commit 43252ce

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

doc/100-General/10-Changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1414
### Bugfixes
1515

1616
* [#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
17-
* [#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
17+
* [#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
18+
* [#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
1819

1920
### Enhancements
2021

lib/core/framework/Get-IcingaPowerShellModuleArchive.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function Get-IcingaPowerShellModuleArchive()
119119
if ((Invoke-IcingaWebRequest -UseBasicParsing -Uri $DownloadUrl -OutFile $DownloadDestination).HasErrors) {
120120
Write-IcingaConsoleError ([string]::Format('Failed to download "{0}" into "{1}". Starting cleanup process', $ModuleName, $DownloadDirectory));
121121
Start-Sleep -Seconds 2;
122-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
122+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
123123

124124
Write-IcingaConsoleNotice 'Starting to re-run the download wizard';
125125

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<#
22
.SYNOPSIS
3-
Wrapper for Remove-Item to secuerly remove items allowing better handling for errors
3+
Wrapper for Remove-Item to securely remove items allowing better handling for errors
44
.DESCRIPTION
55
Removes files and folders from disk and catches possible exceptions with proper return
66
values to handle errors better
77
.FUNCTIONALITY
8-
Wrapper for Remove-Item to secuerly remove items allowing better handling for errors
8+
Wrapper for Remove-Item to securely remove items allowing better handling for errors
99
.EXAMPLE
1010
PS>Remove-ItemSecure -Path C:\icinga;
1111
.EXAMPLE
@@ -17,8 +17,12 @@
1717
.PARAMETER Recurse
1818
Removes sub-folders and sub-files for a given location
1919
.PARAMETER Force
20-
Tries to forefully removes a files and folders if they are either being used or a folder is
20+
Tries to forcefully removes a files and folders if they are either being used or a folder is
2121
still containing items
22+
.PARAMETER Retries
23+
In case the object/folder is not removed without errors, the function will re-try the attempt
24+
as often as specified with a sleep of 2 seconds between attempts to avoid possible file locks
25+
for other operations
2226
.INPUTS
2327
System.String
2428
.OUTPUTS
@@ -29,23 +33,38 @@
2933

3034
function Remove-ItemSecure()
3135
{
32-
param(
36+
param (
3337
[string]$Path,
3438
[switch]$Recurse = $FALSE,
35-
[switch]$Force = $FALSE
36-
)
39+
[switch]$Force = $FALSE,
40+
[int]$Retries = 1
41+
);
3742

3843
if ([string]::IsNullOrEmpty($Path) -Or (Test-Path $Path) -eq $FALSE) {
3944
Write-IcingaConsoleError 'The provided path "{0}" does not exist' -Objects $Path;
4045
return $FALSE;
4146
}
4247

43-
try {
44-
Remove-Item -Path $Path -Recurse:$Recurse -Force:$Force -ErrorAction Stop;
45-
return $TRUE;
46-
} catch {
47-
$ExMsg = $_.Exception;
48-
Write-IcingaConsoleError 'Failed to remove items from path "{0}". Recurse is "{1}", Force is "{2}": "{3}"' -Objects $Path, $Recurse, $Force, $ExMsg;
48+
if ($Retries -le 0) {
49+
$Retries = 1;
4950
}
51+
52+
$ExMsg = $null;
53+
54+
while ($Retries -gt 0) {
55+
try {
56+
Remove-Item -Path $Path -Recurse:$Recurse -Force:$Force -ErrorAction Stop;
57+
return $TRUE;
58+
} catch {
59+
$ExMsg = $_.Exception;
60+
}
61+
62+
Start-Sleep -Seconds 2;
63+
Write-IcingaConsoleNotice -Message 'Waiting for lock on path "{0}" to be released before trying to remove it' -Objects $Path;
64+
$Retries -= 1;
65+
}
66+
67+
Write-IcingaConsoleError 'Failed to remove items from path "{0}". Recurse is "{1}", Force is "{2}": "{3}"' -Objects $Path, $Recurse, $Force, $ExMsg;
68+
5069
return $FALSE;
5170
}

lib/core/repository/Install-IcingaComponent.psm1

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function Install-IcingaComponent()
8383
if ((Invoke-IcingaWebRequest -UseBasicParsing -Uri $FileSource -OutFile $DownloadDestination).HasErrors) {
8484
Write-IcingaConsoleError ([string]::Format('Failed to download "{0}" from "{1}" into "{2}". Starting cleanup process', $Name.ToLower(), $FileSource, $DownloadDestination));
8585
Start-Sleep -Seconds 2;
86-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
86+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
8787

8888
return;
8989
}
@@ -128,7 +128,7 @@ function Install-IcingaComponent()
128128
if ($null -eq $ManifestFile) {
129129
Write-IcingaConsoleError ([string]::Format('Unable to read manifest for package "{0}". Aborting installation', $Name.ToLower()));
130130
Start-Sleep -Seconds 2;
131-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
131+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
132132
return;
133133
}
134134

@@ -145,7 +145,7 @@ function Install-IcingaComponent()
145145
if ($ManifestFile.ModuleVersion -eq $InstallVersion -And $Force -eq $FALSE) {
146146
Write-IcingaConsoleWarning ([string]::Format('The package "{0}" with version "{1}" is already installed. Use "-Force" to re-install the component', $Name.ToLower(), $ManifestFile.ModuleVersion));
147147
Start-Sleep -Seconds 2;
148-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
148+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
149149
return;
150150
}
151151

@@ -285,21 +285,21 @@ function Install-IcingaComponent()
285285

286286
if ($Success -eq 0) {
287287
Write-IcingaConsoleWarning ([string]::Format('The package "service" with version "{0}" is already installed. Use "-Force" to re-install the component', $InstalledService.ProductVersion));
288-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
288+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
289289

290290
return;
291291
}
292292

293293
if ($Success -eq 1) {
294-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
294+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
295295
Write-IcingaConsoleNotice 'Installation of component "service" was successful';
296296

297297
return;
298298
}
299299

300300
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';
301301
Start-Sleep -Seconds 2;
302-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
302+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
303303
return;
304304
} else {
305305
Write-IcingaConsoleError 'There was no manifest file found inside the package';
@@ -352,7 +352,7 @@ function Install-IcingaComponent()
352352

353353
if ($InstalledVersion.Full -eq $MSIData.ProductVersion -And $Force -eq $FALSE) {
354354
Write-IcingaConsoleWarning 'The package "agent" with version "{0}" is already installed. Use "-Force" to re-install the component' -Objects $InstalledVersion.Full;
355-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
355+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
356356

357357
return;
358358
}
@@ -391,6 +391,5 @@ function Install-IcingaComponent()
391391
Write-IcingaConsoleError ([string]::Format('Unsupported file extension "{0}" found for package "{1}". Aborting installation', ([IO.Path]::GetExtension($FileName)), $Name.ToLower()));
392392
}
393393

394-
Start-Sleep -Seconds 1;
395-
Remove-Item -Path $DownloadDirectory -Recurse -Force;
394+
Remove-ItemSecure -Path $DownloadDirectory -Recurse -Force -Retries 5 | Out-Null;
396395
}

0 commit comments

Comments
 (0)