From 2b64c9b8aa5232c77c000479f7a4d645e15a38dc Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 8 Mar 2016 22:25:57 -0800 Subject: [PATCH] Fix unicode handling in connection plugins. --- lib/ansible/plugins/action/fetch.py | 3 ++- lib/ansible/plugins/connection/chroot.py | 2 +- lib/ansible/plugins/connection/docker.py | 14 +++++++------- lib/ansible/plugins/connection/jail.py | 6 +++--- lib/ansible/plugins/connection/libvirt_lxc.py | 6 +++--- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/ansible/plugins/action/fetch.py b/lib/ansible/plugins/action/fetch.py index 0dacd021457..7b04b526906 100644 --- a/lib/ansible/plugins/action/fetch.py +++ b/lib/ansible/plugins/action/fetch.py @@ -25,6 +25,7 @@ from ansible.plugins.action import ActionBase from ansible.utils.boolean import boolean from ansible.utils.hashing import checksum, checksum_s, md5, secure_hash from ansible.utils.path import makedirs_safe +from ansible.utils.unicode import to_bytes class ActionModule(ActionBase): @@ -158,7 +159,7 @@ class ActionModule(ActionBase): self._connection.fetch_file(source, dest) else: try: - f = open(dest, 'w') + f = open(to_bytes(dest, errors='strict'), 'w') f.write(remote_data) f.close() except (IOError, OSError) as e: diff --git a/lib/ansible/plugins/connection/chroot.py b/lib/ansible/plugins/connection/chroot.py index 0778a5e22ca..7918ac5602c 100644 --- a/lib/ansible/plugins/connection/chroot.py +++ b/lib/ansible/plugins/connection/chroot.py @@ -91,7 +91,7 @@ class Connection(ConnectionBase): local_cmd = [self.chroot_cmd, self.chroot, executable, '-c', cmd] display.vvv("EXEC %s" % (local_cmd), host=self.chroot) - local_cmd = map(to_bytes, local_cmd) + local_cmd = [to_bytes(i, errors='strict') for i in local_cmd] p = subprocess.Popen(local_cmd, shell=False, stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE) diff --git a/lib/ansible/plugins/connection/docker.py b/lib/ansible/plugins/connection/docker.py index 130317f24aa..b1c499ed725 100644 --- a/lib/ansible/plugins/connection/docker.py +++ b/lib/ansible/plugins/connection/docker.py @@ -127,7 +127,7 @@ class Connection(ConnectionBase): local_cmd = [self.docker_cmd, "exec", '-i', self._play_context.remote_addr, executable, '-c', cmd] display.vvv("EXEC %s" % (local_cmd,), host=self._play_context.remote_addr) - local_cmd = map(to_bytes, local_cmd) + local_cmd = [to_bytes(i, errors='strict') for i in local_cmd] p = subprocess.Popen(local_cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -154,14 +154,14 @@ class Connection(ConnectionBase): display.vvv("PUT %s TO %s" % (in_path, out_path), host=self._play_context.remote_addr) out_path = self._prefix_login_path(out_path) - if not os.path.exists(in_path): + if not os.path.exists(to_bytes(in_path, errors='strict')): raise AnsibleFileNotFound( "file or module does not exist: %s" % in_path) if self.can_copy_bothways: # only docker >= 1.8.1 can do this natively args = [ self.docker_cmd, "cp", in_path, "%s:%s" % (self._play_context.remote_addr, out_path) ] - args = map(to_bytes, args) + args = [to_bytes(i, errors='strict') for i in args] p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: @@ -173,8 +173,8 @@ class Connection(ConnectionBase): executable = C.DEFAULT_EXECUTABLE.split()[0] if C.DEFAULT_EXECUTABLE else '/bin/sh' args = [self.docker_cmd, "exec", "-i", self._play_context.remote_addr, executable, "-c", "dd of=%s bs=%s" % (out_path, BUFSIZE)] - args = map(to_bytes, args) - with open(in_path, 'rb') as in_file: + args = [to_bytes(i, errors='strict') for i in args] + with open(to_bytes(in_path, errors='strict'), 'rb') as in_file: try: p = subprocess.Popen(args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -196,7 +196,7 @@ class Connection(ConnectionBase): out_dir = os.path.dirname(out_path) args = [self.docker_cmd, "cp", "%s:%s" % (self._play_context.remote_addr, in_path), out_dir] - args = map(to_bytes, args) + args = [to_bytes(i, errors='strict') for i in args] p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -205,7 +205,7 @@ class Connection(ConnectionBase): # Rename if needed actual_out_path = os.path.join(out_dir, os.path.basename(in_path)) if actual_out_path != out_path: - os.rename(actual_out_path, out_path) + os.rename(to_bytes(actual_out_path, errors='strict'), to_bytes(out_path, errors='strict')) def close(self): """ Terminate the connection. Nothing to do for Docker""" diff --git a/lib/ansible/plugins/connection/jail.py b/lib/ansible/plugins/connection/jail.py index 2196b1bf8f5..d44213f439b 100644 --- a/lib/ansible/plugins/connection/jail.py +++ b/lib/ansible/plugins/connection/jail.py @@ -111,7 +111,7 @@ class Connection(ConnectionBase): local_cmd = [self.jexec_cmd, self.jail, executable, '-c', cmd] display.vvv("EXEC %s" % (local_cmd,), host=self.jail) - local_cmd = map(to_bytes, local_cmd) + local_cmd = [to_bytes(i, errors='strict') for i in local_cmd] p = subprocess.Popen(local_cmd, shell=False, stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -154,7 +154,7 @@ class Connection(ConnectionBase): out_path = pipes.quote(self._prefix_login_path(out_path)) try: - with open(in_path, 'rb') as in_file: + with open(to_bytes(in_path, errors='strict'), 'rb') as in_file: try: p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) except OSError: @@ -180,7 +180,7 @@ class Connection(ConnectionBase): except OSError: raise AnsibleError("jail connection requires dd command in the jail") - with open(out_path, 'wb+') as out_file: + with open(to_bytes(out_path, errors='strict'), 'wb+') as out_file: try: chunk = p.stdout.read(BUFSIZE) while chunk: diff --git a/lib/ansible/plugins/connection/libvirt_lxc.py b/lib/ansible/plugins/connection/libvirt_lxc.py index 3bfff8b1c35..03e9771a2e5 100644 --- a/lib/ansible/plugins/connection/libvirt_lxc.py +++ b/lib/ansible/plugins/connection/libvirt_lxc.py @@ -91,7 +91,7 @@ class Connection(ConnectionBase): local_cmd = [self.virsh, '-q', '-c', 'lxc:///', 'lxc-enter-namespace', self.lxc, '--', executable , '-c', cmd] display.vvv("EXEC %s" % (local_cmd,), host=self.lxc) - local_cmd = map(to_bytes, local_cmd) + local_cmd = [to_bytes(i, errors='strict') for i in local_cmd] p = subprocess.Popen(local_cmd, shell=False, stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -127,7 +127,7 @@ class Connection(ConnectionBase): out_path = pipes.quote(self._prefix_login_path(out_path)) try: - with open(in_path, 'rb') as in_file: + with open(to_bytes(in_path, errors='strict'), 'rb') as in_file: try: p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) except OSError: @@ -153,7 +153,7 @@ class Connection(ConnectionBase): except OSError: raise AnsibleError("chroot connection requires dd command in the chroot") - with open(out_path, 'wb+') as out_file: + with open(to_bytes(out_path, errors='strict'), 'wb+') as out_file: try: chunk = p.stdout.read(BUFSIZE) while chunk: