| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2015, Corwin Brown <[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 |
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSCustomUseLiteralPath', '', |
| 9 |
Justification = 'This module has supported wildcard comparison since it was created')] |
| 10 |
param() |
| 11 |
|
| 12 |
$params = Parse-Args $args -supports_check_mode $true |
| 13 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 14 |
|
| 15 |
$src = Get-AnsibleParam -obj $params -name "src" -type "path" -failifempty $true |
| 16 |
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -failifempty $true |
| 17 |
$purge = Get-AnsibleParam -obj $params -name "purge" -type "bool" -default $false |
| 18 |
$recurse = Get-AnsibleParam -obj $params -name "recurse" -type "bool" -default $false |
| 19 |
$flags = Get-AnsibleParam -obj $params -name "flags" -type "str" |
| 20 |
|
| 21 |
$result = @{ |
| 22 |
changed = $false |
| 23 |
dest = $dest |
| 24 |
purge = $purge |
| 25 |
recurse = $recurse |
| 26 |
src = $src |
| 27 |
} |
| 28 |
|
| 29 |
# Search for an Error Message |
| 30 |
# Robocopy seems to display an error after 3 '-----' separator lines |
| 31 |
Function SearchForError($cmd_output, $default_msg) { |
| 32 |
$separator_count = 0 |
| 33 |
$error_msg = $default_msg |
| 34 |
ForEach ($line in $cmd_output) { |
| 35 |
if (-not $line) { |
| 36 |
continue |
| 37 |
} |
| 38 |
|
| 39 |
if ($separator_count -ne 3) { |
| 40 |
if (Select-String -InputObject $line -Pattern "^(\s+)?(\-+)(\s+)?$") { |
| 41 |
$separator_count += 1 |
| 42 |
} |
| 43 |
} |
| 44 |
else { |
| 45 |
if (Select-String -InputObject $line -Pattern "error") { |
| 46 |
$error_msg = $line |
| 47 |
break |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $error_msg |
| 53 |
} |
| 54 |
|
| 55 |
if (-not (Test-Path -Path $src)) { |
| 56 |
Fail-Json $result "$src does not exist!" |
| 57 |
} |
| 58 |
|
| 59 |
# Build Arguments |
| 60 |
$robocopy_opts = @($src, $dest) |
| 61 |
|
| 62 |
if ($check_mode) { |
| 63 |
$robocopy_opts += "/l" |
| 64 |
} |
| 65 |
|
| 66 |
if ($null -eq $flags) { |
| 67 |
if ($purge) { |
| 68 |
$robocopy_opts += "/purge" |
| 69 |
} |
| 70 |
|
| 71 |
if ($recurse) { |
| 72 |
$robocopy_opts += "/e" |
| 73 |
} |
| 74 |
} |
| 75 |
else { |
| 76 |
ForEach ($f in $flags.split(" ")) { |
| 77 |
$robocopy_opts += $f |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$result.flags = $flags |
| 82 |
$result.cmd = "robocopy $robocopy_opts" |
| 83 |
|
| 84 |
Try { |
| 85 |
$robocopy_output = &robocopy $robocopy_opts |
| 86 |
$rc = $LASTEXITCODE |
| 87 |
} |
| 88 |
Catch { |
| 89 |
Fail-Json $result "Error synchronizing $src to $dest! Msg: $($_.Exception.Message)" |
| 90 |
} |
| 91 |
|
| 92 |
$result.msg = "Success" |
| 93 |
$result.output = $robocopy_output |
| 94 |
$result.return_code = $rc # Backward compatibility |
| 95 |
$result.rc = $rc |
| 96 |
|
| 97 |
switch ($rc) { |
| 98 |
|
| 99 |
0 { |
| 100 |
$result.msg = "No files copied." |
| 101 |
} |
| 102 |
1 { |
| 103 |
$result.msg = "Files copied successfully!" |
| 104 |
$result.changed = $true |
| 105 |
$result.failed = $false |
| 106 |
} |
| 107 |
2 { |
| 108 |
$result.msg = "Some Extra files or directories were detected. No files were copied." |
| 109 |
Add-Warning $result $result.msg |
| 110 |
$result.failed = $false |
| 111 |
} |
| 112 |
3 { |
| 113 |
$result.msg = "(2+1) Some files were copied. Additional files were present." |
| 114 |
Add-Warning $result $result.msg |
| 115 |
$result.changed = $true |
| 116 |
$result.failed = $false |
| 117 |
} |
| 118 |
4 { |
| 119 |
$result.msg = "Some mismatched files or directories were detected. Housekeeping might be required!" |
| 120 |
Add-Warning $result $result.msg |
| 121 |
$result.changed = $true |
| 122 |
$result.failed = $false |
| 123 |
} |
| 124 |
5 { |
| 125 |
$result.msg = "(4+1) Some files were copied. Some files were mismatched." |
| 126 |
Add-Warning $result $result.msg |
| 127 |
$result.changed = $true |
| 128 |
$result.failed = $false |
| 129 |
} |
| 130 |
6 { |
| 131 |
$result.msg = "(4+2) Additional files and mismatched files exist. No files were copied." |
| 132 |
$result.failed = $false |
| 133 |
} |
| 134 |
7 { |
| 135 |
$result.msg = "(4+1+2) Files were copied, a file mismatch was present, and additional files were present." |
| 136 |
Add-Warning $result $result.msg |
| 137 |
$result.changed = $true |
| 138 |
$result.failed = $false |
| 139 |
} |
| 140 |
8 { |
| 141 |
Fail-Json $result (SearchForError $robocopy_output "Some files or directories could not be copied!") |
| 142 |
} |
| 143 |
{ @(9, 10, 11, 12, 13, 14, 15) -contains $_ } { |
| 144 |
Fail-Json $result (SearchForError $robocopy_output "Fatal error. Check log message!") |
| 145 |
} |
| 146 |
16 { |
| 147 |
Fail-Json $result (SearchForError $robocopy_output "Serious Error! No files were copied! Do you have permissions to access $src and $dest?") |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
Exit-Json $result |