[stable-2.7] Support transfering empty files to target host. Fixes #36725 (#45751)

(cherry picked from commit b6fcbfe)

Co-authored-by: Matt Martz <matt@sivel.net>
This commit is contained in:
Matt Martz 2018-09-18 09:05:20 -05:00 committed by Toshio Kuratomi
parent 190cdd3a29
commit cf28d22211
7 changed files with 39 additions and 8 deletions

View file

@ -0,0 +1,7 @@
bugfixes:
- docker connection - Support empty files with copying to target (https://github.com/ansible/ansible/issues/36725)
- chroot connection - Support empty files with copying to target (https://github.com/ansible/ansible/issues/36725)
- jail connection - Support empty files with copying to target (https://github.com/ansible/ansible/issues/36725)
- kubectl connection - Support empty files with copying to target (https://github.com/ansible/ansible/issues/36725)
- libvirt_lxc connection - Support empty files with copying to target (https://github.com/ansible/ansible/issues/36725)
- zone connection - Support empty files with copying to target (https://github.com/ansible/ansible/issues/36725)

View file

@ -148,8 +148,12 @@ class Connection(ConnectionBase):
out_path = shlex_quote(self._prefix_login_path(out_path)) out_path = shlex_quote(self._prefix_login_path(out_path))
try: try:
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
if not os.fstat(in_file.fileno()).st_size:
count = ' count=0'
else:
count = ''
try: try:
p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) p = self._buffered_exec_command('dd of=%s bs=%s%s' % (out_path, BUFSIZE, count), stdin=in_file)
except OSError: except OSError:
raise AnsibleError("chroot connection requires dd command in the chroot") raise AnsibleError("chroot connection requires dd command in the chroot")
try: try:

View file

@ -246,9 +246,13 @@ class Connection(ConnectionBase):
# running containers, so we use docker exec to implement this # running containers, so we use docker exec to implement this
# Although docker version 1.8 and later provide support, the # Although docker version 1.8 and later provide support, the
# owner and group of the files are always set to root # owner and group of the files are always set to root
args = self._build_exec_cmd([self._play_context.executable, "-c", "dd of=%s bs=%s" % (out_path, BUFSIZE)])
args = [to_bytes(i, errors='surrogate_or_strict') for i in args]
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
if not os.fstat(in_file.fileno()).st_size:
count = ' count=0'
else:
count = ''
args = self._build_exec_cmd([self._play_context.executable, "-c", "dd of=%s bs=%s%s" % (out_path, BUFSIZE, count)])
args = [to_bytes(i, errors='surrogate_or_strict') for i in args]
try: try:
p = subprocess.Popen(args, stdin=in_file, p = subprocess.Popen(args, stdin=in_file,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)

View file

@ -158,8 +158,12 @@ class Connection(ConnectionBase):
out_path = shlex_quote(self._prefix_login_path(out_path)) out_path = shlex_quote(self._prefix_login_path(out_path))
try: try:
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
if not os.fstat(in_file.fileno()).st_size:
count = ' count=0'
else:
count = ''
try: try:
p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) p = self._buffered_exec_command('dd of=%s bs=%s%s' % (out_path, BUFSIZE, count), stdin=in_file)
except OSError: except OSError:
raise AnsibleError("jail connection requires dd command in the jail") raise AnsibleError("jail connection requires dd command in the jail")
try: try:

View file

@ -300,9 +300,13 @@ class Connection(ConnectionBase):
out_path = shlex_quote(out_path) out_path = shlex_quote(out_path)
# kubectl doesn't have native support for copying files into # kubectl doesn't have native support for copying files into
# running containers, so we use kubectl exec to implement this # running containers, so we use kubectl exec to implement this
args = self._build_exec_cmd([self._play_context.executable, "-c", "dd of=%s bs=%s" % (out_path, BUFSIZE)])
args = [to_bytes(i, errors='surrogate_or_strict') for i in args]
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
if not os.fstat(in_file.fileno()).st_size:
count = ' count=0'
else:
count = ''
args = self._build_exec_cmd([self._play_context.executable, "-c", "dd of=%s bs=%s%s" % (out_path, BUFSIZE, count)])
args = [to_bytes(i, errors='surrogate_or_strict') for i in args]
try: try:
p = subprocess.Popen(args, stdin=in_file, p = subprocess.Popen(args, stdin=in_file,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)

View file

@ -138,8 +138,12 @@ class Connection(ConnectionBase):
out_path = shlex_quote(self._prefix_login_path(out_path)) out_path = shlex_quote(self._prefix_login_path(out_path))
try: try:
with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file:
if not os.fstat(in_file.fileno()).st_size:
count = ' count=0'
else:
count = ''
try: try:
p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) p = self._buffered_exec_command('dd of=%s bs=%s%s' % (out_path, BUFSIZE, count), stdin=in_file)
except OSError: except OSError:
raise AnsibleError("chroot connection requires dd command in the chroot") raise AnsibleError("chroot connection requires dd command in the chroot")
try: try:

View file

@ -157,8 +157,12 @@ class Connection(ConnectionBase):
out_path = shlex_quote(self._prefix_login_path(out_path)) out_path = shlex_quote(self._prefix_login_path(out_path))
try: try:
with open(in_path, 'rb') as in_file: with open(in_path, 'rb') as in_file:
if not os.fstat(in_file.fileno()).st_size:
count = ' count=0'
else:
count = ''
try: try:
p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) p = self._buffered_exec_command('dd of=%s bs=%s%s' % (out_path, BUFSIZE, count), stdin=in_file)
except OSError: except OSError:
raise AnsibleError("jail connection requires dd command in the jail") raise AnsibleError("jail connection requires dd command in the jail")
try: try: