mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-02 14:40:19 -07:00
Add integration tests for fetch/slurp, make powershell fetch/slurp work as close as possible to existing fetch/slurp modules.
This commit is contained in:
parent
ef968efa8b
commit
243cd877ae
13 changed files with 403 additions and 44 deletions
|
@ -62,5 +62,5 @@ Function Fail-Json($obj, $message)
|
|||
Set-Attr $obj "msg" $message
|
||||
Set-Attr $obj "failed" $true
|
||||
echo $obj | ConvertTo-Json
|
||||
Exit
|
||||
Exit 1
|
||||
}
|
||||
|
|
|
@ -57,19 +57,24 @@ class ActionModule(object):
|
|||
return ReturnData(conn=conn, result=results)
|
||||
|
||||
source = os.path.expanduser(source)
|
||||
source = conn.shell.join_path(source)
|
||||
if os.path.sep not in conn.shell.join_path('a', ''):
|
||||
source_local = source.replace('\\', '/')
|
||||
else:
|
||||
source_local = source
|
||||
|
||||
if flat:
|
||||
if dest.endswith("/"): # CCTODO: Fix path for Windows hosts.
|
||||
if dest.endswith("/"):
|
||||
# if the path ends with "/", we'll use the source filename as the
|
||||
# destination filename
|
||||
base = os.path.basename(source)
|
||||
base = os.path.basename(source_local)
|
||||
dest = os.path.join(dest, base)
|
||||
if not dest.startswith("/"):
|
||||
# if dest does not start with "/", we'll assume a relative path
|
||||
dest = utils.path_dwim(self.runner.basedir, dest)
|
||||
else:
|
||||
# files are saved in dest dir, with a subdir for each host, then the filename
|
||||
dest = "%s/%s/%s" % (utils.path_dwim(self.runner.basedir, dest), conn.host, source)
|
||||
dest = "%s/%s/%s" % (utils.path_dwim(self.runner.basedir, dest), conn.host, source_local)
|
||||
|
||||
dest = os.path.expanduser(dest.replace("//","/"))
|
||||
|
||||
|
|
|
@ -178,8 +178,8 @@ class Connection(object):
|
|||
cmd_parts = powershell._encode_script(script, as_list=True)
|
||||
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
|
||||
if result.status_code != 0:
|
||||
raise RuntimeError(result.std_err.encode('utf-8'))
|
||||
except Exception: # IOError?
|
||||
raise IOError(result.std_err.encode('utf-8'))
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
|
||||
|
||||
|
@ -189,31 +189,61 @@ class Connection(object):
|
|||
buffer_size = 2**20 # 1MB chunks
|
||||
if not os.path.exists(os.path.dirname(out_path)):
|
||||
os.makedirs(os.path.dirname(out_path))
|
||||
with open(out_path, 'wb') as out_file:
|
||||
out_file = None
|
||||
try:
|
||||
offset = 0
|
||||
while True:
|
||||
try:
|
||||
script = '''
|
||||
$bufferSize = %d;
|
||||
$stream = [System.IO.File]::OpenRead("%s");
|
||||
$stream.Seek(%d, [System.IO.SeekOrigin]::Begin) | Out-Null;
|
||||
$buffer = New-Object Byte[] $bufferSize;
|
||||
$bytesRead = $stream.Read($buffer, 0, $bufferSize);
|
||||
$bytes = $buffer[0..($bytesRead-1)];
|
||||
[System.Convert]::ToBase64String($bytes);
|
||||
$stream.Close() | Out-Null;
|
||||
''' % (buffer_size, powershell._escape(in_path), offset)
|
||||
If (Test-Path -PathType Leaf "%(path)s")
|
||||
{
|
||||
$stream = [System.IO.File]::OpenRead("%(path)s");
|
||||
$stream.Seek(%(offset)d, [System.IO.SeekOrigin]::Begin) | Out-Null;
|
||||
$buffer = New-Object Byte[] %(buffer_size)d;
|
||||
$bytesRead = $stream.Read($buffer, 0, %(buffer_size)d);
|
||||
$bytes = $buffer[0..($bytesRead-1)];
|
||||
[System.Convert]::ToBase64String($bytes);
|
||||
$stream.Close() | Out-Null;
|
||||
}
|
||||
ElseIf (Test-Path -PathType Container "%(path)s")
|
||||
{
|
||||
Write-Host "[DIR]";
|
||||
}
|
||||
Else
|
||||
{
|
||||
Write-Error "%(path)s does not exist";
|
||||
Exit 1;
|
||||
}
|
||||
''' % dict(buffer_size=buffer_size, path=powershell._escape(in_path), offset=offset)
|
||||
vvvv("WINRM FETCH %s to %s (offset=%d)" % (in_path, out_path, offset), host=self.host)
|
||||
cmd_parts = powershell._encode_script(script, as_list=True)
|
||||
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
|
||||
data = base64.b64decode(result.std_out.strip())
|
||||
out_file.write(data)
|
||||
if len(data) < buffer_size:
|
||||
if result.status_code != 0:
|
||||
raise IOError(result.std_err.encode('utf-8'))
|
||||
if result.std_out.strip() == '[DIR]':
|
||||
data = None
|
||||
else:
|
||||
data = base64.b64decode(result.std_out.strip())
|
||||
if data is None:
|
||||
if not os.path.exists(out_path):
|
||||
os.makedirs(out_path)
|
||||
break
|
||||
offset += len(data)
|
||||
except Exception: # IOError?
|
||||
else:
|
||||
if not out_file:
|
||||
# If out_path is a directory and we're expecting a file, bail out now.
|
||||
if os.path.isdir(out_path):
|
||||
break
|
||||
out_file = open(out_path, 'wb')
|
||||
out_file.write(data)
|
||||
if len(data) < buffer_size:
|
||||
break
|
||||
offset += len(data)
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
|
||||
finally:
|
||||
if out_file:
|
||||
out_file.close()
|
||||
|
||||
def close(self):
|
||||
if self.protocol and self.shell_id:
|
||||
|
|
|
@ -85,7 +85,21 @@ class ShellModule(object):
|
|||
|
||||
def md5(self, path):
|
||||
path = _escape(path)
|
||||
return _encode_script('''(Get-FileHash -Path "%s" -Algorithm MD5).Hash.ToLower();''' % path)
|
||||
script = '''
|
||||
If (Test-Path -PathType Leaf "%(path)s")
|
||||
{
|
||||
(Get-FileHash -Path "%(path)s" -Algorithm MD5).Hash.ToLower();
|
||||
}
|
||||
ElseIf (Test-Path -PathType Container "%(path)s")
|
||||
{
|
||||
Write-Host "3";
|
||||
}
|
||||
Else
|
||||
{
|
||||
Write-Host "1";
|
||||
}
|
||||
''' % dict(path=path)
|
||||
return _encode_script(script)
|
||||
|
||||
def build_module_command(self, env_string, shebang, cmd, rm_tmp=None):
|
||||
cmd_parts = shlex.split(cmd, posix=False)
|
||||
|
|
|
@ -608,9 +608,9 @@ def md5s(data):
|
|||
return digest.hexdigest()
|
||||
|
||||
def md5(filename):
|
||||
''' Return MD5 hex digest of local file, or None if file is not present. '''
|
||||
''' Return MD5 hex digest of local file, None if file is not present or a directory. '''
|
||||
|
||||
if not os.path.exists(filename):
|
||||
if not os.path.exists(filename) or os.path.isdir(filename):
|
||||
return None
|
||||
digest = _md5()
|
||||
blocksize = 64 * 1024
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue