| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2015, Phil Schwartz <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 7 |
|
| 8 |
# TODO: This module is not idempotent (it will always unzip and report change) |
| 9 |
|
| 10 |
$ErrorActionPreference = "Stop" |
| 11 |
|
| 12 |
$pcx_extensions = @('.bz2', '.gz', '.msu', '.tar', '.zip') |
| 13 |
|
| 14 |
$params = Parse-Args $args -supports_check_mode $true |
| 15 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 16 |
|
| 17 |
$src = Get-AnsibleParam -obj $params -name "src" -type "path" -failifempty $true |
| 18 |
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true |
| 19 |
$creates = Get-AnsibleParam -obj $params -name "creates" -type "path" |
| 20 |
$recurse = Get-AnsibleParam -obj $params -name "recurse" -type "bool" -default $false |
| 21 |
$delete_archive = Get-AnsibleParam -obj $params -name "delete_archive" -type "bool" -default $false -aliases 'rm' |
| 22 |
$password = Get-AnsibleParam -obj $params -name "password" -type "str" |
| 23 |
|
| 24 |
# Fixes a fail error message (when the task actually succeeds) for a |
| 25 |
# "Convert-ToJson: The converted JSON string is in bad format" |
| 26 |
# This happens when JSON is parsing a string that ends with a "\", |
| 27 |
# which is possible when specifying a directory to download to. |
| 28 |
# This catches that possible error, before assigning the JSON $result |
| 29 |
$result = @{ |
| 30 |
changed = $false |
| 31 |
dest = $dest -replace '\$', '' |
| 32 |
removed = $false |
| 33 |
src = $src -replace '\$', '' |
| 34 |
} |
| 35 |
|
| 36 |
Function Expand-Zip($src, $dest) { |
| 37 |
$archive = [System.IO.Compression.ZipFile]::Open($src, [System.IO.Compression.ZipArchiveMode]::Read, [System.Text.Encoding]::UTF8) |
| 38 |
foreach ($entry in $archive.Entries) { |
| 39 |
$archive_name = $entry.FullName |
| 40 |
|
| 41 |
$entry_target_path = [System.IO.Path]::Combine($dest, $archive_name) |
| 42 |
$entry_dir = [System.IO.Path]::GetDirectoryName($entry_target_path) |
| 43 |
|
| 44 |
# Normalize paths for further evaluation |
| 45 |
$full_target_path = [System.IO.Path]::GetFullPath($entry_target_path) |
| 46 |
$full_dest_path = [System.IO.Path]::GetFullPath($dest + [System.IO.Path]::DirectorySeparatorChar) |
| 47 |
|
| 48 |
# Ensure file in the archive does not escape the extraction path |
| 49 |
if (-not $full_target_path.StartsWith($full_dest_path)) { |
| 50 |
$msg = "Error unzipping '$src' to '$dest'! Filename contains relative paths which would extract outside the destination: $entry_target_path" |
| 51 |
Fail-Json -obj $result -message $msg |
| 52 |
} |
| 53 |
|
| 54 |
if (-not (Test-Path -LiteralPath $entry_dir)) { |
| 55 |
New-Item -Path $entry_dir -ItemType Directory -WhatIf:$check_mode | Out-Null |
| 56 |
$result.changed = $true |
| 57 |
} |
| 58 |
|
| 59 |
if ((-not ($entry_target_path.EndsWith("\") -or $entry_target_path.EndsWith("/"))) -and (-not $check_mode)) { |
| 60 |
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $entry_target_path, $true) |
| 61 |
} |
| 62 |
$result.changed = $true |
| 63 |
} |
| 64 |
$archive.Dispose() |
| 65 |
} |
| 66 |
|
| 67 |
Function Expand-ZipLegacy($src, $dest) { |
| 68 |
# [System.IO.Compression.ZipFile] was only added in .net 4.5, this is used |
| 69 |
# when .net is older than that. |
| 70 |
$shell = New-Object -ComObject Shell.Application |
| 71 |
$zip = $shell.NameSpace([IO.Path]::GetFullPath($src)) |
| 72 |
$dest_path = $shell.NameSpace([IO.Path]::GetFullPath($dest)) |
| 73 |
|
| 74 |
$shell = New-Object -ComObject Shell.Application |
| 75 |
|
| 76 |
if (-not $check_mode) { |
| 77 |
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb787866.aspx |
| 78 |
# From Folder.CopyHere documentation, 1044 means: |
| 79 |
# - 1024: do not display a user interface if an error occurs |
| 80 |
# - 16: respond with "yes to all" for any dialog box that is displayed |
| 81 |
# - 4: do not display a progress dialog box |
| 82 |
$dest_path.CopyHere($zip.Items(), 1044) |
| 83 |
} |
| 84 |
$result.changed = $true |
| 85 |
} |
| 86 |
|
| 87 |
If ($creates -and (Test-Path -LiteralPath $creates)) { |
| 88 |
$result.skipped = $true |
| 89 |
$result.msg = "The file or directory '$creates' already exists." |
| 90 |
Exit-Json -obj $result |
| 91 |
} |
| 92 |
|
| 93 |
If (-Not (Test-Path -LiteralPath $src)) { |
| 94 |
Fail-Json -obj $result -message "File '$src' does not exist." |
| 95 |
} |
| 96 |
|
| 97 |
$ext = [System.IO.Path]::GetExtension($src) |
| 98 |
|
| 99 |
If (-Not (Test-Path -LiteralPath $dest -PathType Container)) { |
| 100 |
Try { |
| 101 |
New-Item -ItemType "directory" -path $dest -WhatIf:$check_mode | out-null |
| 102 |
} |
| 103 |
Catch { |
| 104 |
Fail-Json -obj $result -message "Error creating '$dest' directory! Msg: $($_.Exception.Message)" |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
If ($ext -eq ".zip" -And $recurse -eq $false -And -Not $password) { |
| 109 |
# TODO: PS v5 supports zip extraction, use that if available |
| 110 |
$use_legacy = $false |
| 111 |
try { |
| 112 |
# determines if .net 4.5 is available, if this fails we need to fall |
| 113 |
# back to the legacy COM Shell.Application to extract the zip |
| 114 |
Add-Type -AssemblyName System.IO.Compression.FileSystem | Out-Null |
| 115 |
Add-Type -AssemblyName System.IO.Compression | Out-Null |
| 116 |
} |
| 117 |
catch { |
| 118 |
$use_legacy = $true |
| 119 |
} |
| 120 |
|
| 121 |
if ($use_legacy) { |
| 122 |
try { |
| 123 |
Expand-ZipLegacy -src $src -dest $dest |
| 124 |
} |
| 125 |
catch { |
| 126 |
Fail-Json -obj $result -message "Error unzipping '$src' to '$dest'!. Method: COM Shell.Application, Exception: $($_.Exception.Message)" |
| 127 |
} |
| 128 |
} |
| 129 |
else { |
| 130 |
try { |
| 131 |
Expand-Zip -src $src -dest $dest |
| 132 |
} |
| 133 |
catch { |
| 134 |
Fail-Json -obj $result -message "Error unzipping '$src' to '$dest'!. Method: System.IO.Compression.ZipFile, Exception: $($_.Exception.Message)" |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
Else { |
| 139 |
# Check if PSCX is installed |
| 140 |
$list = Get-Module -ListAvailable |
| 141 |
|
| 142 |
If (-Not ($list -match "PSCX")) { |
| 143 |
Fail-Json -obj $result -message "PowerShellCommunityExtensions PowerShell Module (PSCX) is required for non-'.zip' compressed archive types." |
| 144 |
} |
| 145 |
Else { |
| 146 |
$result.pscx_status = "present" |
| 147 |
} |
| 148 |
|
| 149 |
Try { |
| 150 |
Import-Module PSCX |
| 151 |
} |
| 152 |
Catch { |
| 153 |
Fail-Json $result "Error importing module PSCX" |
| 154 |
} |
| 155 |
|
| 156 |
$expand_params = @{ |
| 157 |
OutputPath = $dest |
| 158 |
WhatIf = $check_mode |
| 159 |
} |
| 160 |
if ($null -ne $password) { |
| 161 |
$expand_params.Password = ConvertTo-SecureString -String $password -AsPlainText -Force |
| 162 |
} |
| 163 |
Try { |
| 164 |
Expand-Archive -Path $src @expand_params |
| 165 |
} |
| 166 |
Catch { |
| 167 |
Fail-Json -obj $result -message "Error expanding '$src' to '$dest'! Msg: $($_.Exception.Message)" |
| 168 |
} |
| 169 |
|
| 170 |
If ($recurse) { |
| 171 |
Get-ChildItem -LiteralPath $dest -recurse | Where-Object { $pcx_extensions -contains $_.extension } | ForEach-Object { |
| 172 |
Try { |
| 173 |
Expand-Archive -Path $_.FullName -Force @expand_params |
| 174 |
} |
| 175 |
Catch { |
| 176 |
Fail-Json -obj $result -message "Error recursively expanding '$src' to '$dest'! Msg: $($_.Exception.Message)" |
| 177 |
} |
| 178 |
If ($delete_archive) { |
| 179 |
Remove-Item -LiteralPath $_.FullName -Force -WhatIf:$check_mode |
| 180 |
$result.removed = $true |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$result.changed = $true |
| 186 |
} |
| 187 |
|
| 188 |
If ($delete_archive) { |
| 189 |
try { |
| 190 |
Remove-Item -LiteralPath $src -Recurse -Force -WhatIf:$check_mode |
| 191 |
} |
| 192 |
catch { |
| 193 |
Fail-Json -obj $result -message "failed to delete archive at '$src': $($_.Exception.Message)" |
| 194 |
} |
| 195 |
$result.removed = $true |
| 196 |
} |
| 197 |
Exit-Json $result |