Skip to content

Commit

Permalink
win_get_url - Make checksum work more like ansible.builtin.get_url
Browse files Browse the repository at this point in the history
  • Loading branch information
Knut Arne Bjørndal committed Dec 30, 2024
1 parent 94f8a84 commit dc9c0da
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/717-win_get_url-checksum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- win_get_url - if checksum is passed and destination file exists with identical checksum no download is done unless force=yes (https://github.com/ansible-collections/ansible.windows/issues/717)
- win_get_url - if checksum is passed and destination file exists with different checksum file is always downloaded (https://github.com/ansible-collections/ansible.windows/issues/717)
31 changes: 23 additions & 8 deletions plugins/modules/win_get_url.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Function Invoke-DownloadFile {
[Parameter(Mandatory = $true)][Uri]$Uri,
[Parameter(Mandatory = $true)][String]$Dest,
[String]$Checksum,
[String]$DestChecksum,
[String]$ChecksumAlgorithm
)

Expand Down Expand Up @@ -187,14 +188,12 @@ Function Invoke-DownloadFile {

$download = $true
if (Test-Path -LiteralPath $Dest) {
# Validate the remote checksum against the existing downloaded file
$dest_checksum = Get-Checksum -Path $Dest -Algorithm $ChecksumAlgorithm

# Validate the remote checksum against the existing downloaded file.
# If we don't need to download anything, save the dest checksum so we don't waste time calculating it
# again at the end of the script
if ($dest_checksum -eq $tmp_checksum) {
if ($DestChecksum -eq $tmp_checksum) {
$download = $false
$Module.Result.checksum_dest = $dest_checksum
$Module.Result.checksum_dest = $DestChecksum
$Module.Result.size = (Get-AnsibleItem -Path $Dest).Length
}
}
Expand Down Expand Up @@ -260,17 +259,33 @@ if ($checksum_url) {
$checksum = Get-ChecksumFromUri -Module $Module -Uri $checksum_uri -SourceUri $url
}

if ($force -or -not (Test-Path -LiteralPath $dest)) {
$dest_checksum = $null
if (Test-Path -LiteralPath $dest) {
$dest_checksum = Get-Checksum -Path $dest -Algorithm $checksum_algorithm
if ($dest_checksum -ne $checksum) {
# Destination does not match checksum, force fetching
$force = $true
}
}

if ($dest_checksum -and $checksum -and $dest_checksum -eq $checksum -and -not $force) {
# No need to do any requests, the file is already as expected
$module.Result.checksum_dest = $dest_checksum
$module.Result.size = (Get-AnsibleItem -Path $dest).Length
}
elseif ($force -or -not (Test-Path -LiteralPath $dest)) {
# force=yes or dest does not exist, download the file
# Note: Invoke-DownloadFile will compare the checksums internally if dest exists
Invoke-DownloadFile -Module $module -Uri $url -Dest $dest -Checksum $checksum `
Invoke-DownloadFile -Module $module -Uri $url -Checksum $checksum `
-Dest $dest -DestChecksum $dest_checksum `
-ChecksumAlgorithm $checksum_algorithm
}
else {
# force=no, we want to check the last modified dates and only download if they don't match
$is_modified = Compare-ModifiedFile -Module $module -Uri $url -Dest $dest
if ($is_modified) {
Invoke-DownloadFile -Module $module -Uri $url -Dest $dest -Checksum $checksum `
Invoke-DownloadFile -Module $module -Uri $url -Checksum $checksum `
-Dest $dest -DestChecksum $dest_checksum `
-ChecksumAlgorithm $checksum_algorithm
}
}
Expand Down
5 changes: 5 additions & 0 deletions plugins/modules/win_get_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
- If a I(checksum) is passed to this parameter, the digest of the
destination file will be calculated after it is downloaded to ensure
its integrity and verify that the transfer completed successfully.
- If a checksum is passed in this parameter or via C(checksum_url) and the
file in C(dest) exists then C(checksum_dest) is calculated.
If C(checksum_dest) equals the checksum no download is done unless
C(force) is C(true). If the checksum does not match the file is always
downloaded, as if C(force) was set.
- This option cannot be set with I(checksum_url).
type: str
checksum_algorithm:
Expand Down

0 comments on commit dc9c0da

Please sign in to comment.