From ca9dd7338adfd371705f27793d8bd20e99762d8d Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Wed, 2 Nov 2016 13:35:48 +0100 Subject: [PATCH] Support IPv6 in python code. --- cdist/exec/remote.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/cdist/exec/remote.py b/cdist/exec/remote.py index 9c70bdf4..f374262f 100644 --- a/cdist/exec/remote.py +++ b/cdist/exec/remote.py @@ -32,6 +32,43 @@ import cdist import cdist.exec.util as exec_util +# check whether addr is IPv6 +try: + # python 3.3+ + import ipaddress + + def _is_ipv6(addr): + try: + return ipaddress.ip_address(addr).version == 6 + except ValueError: + return False +except ImportError: + # fallback for older python versions + import socket + + def _is_ipv6(addr): + try: + socket.inet_aton(addr) + return False + except socket.error: + pass + try: + socket.inet_pton(socket.AF_INET6, addr) + return True + except socket.error: + pass + return False + + +def _wrap_addr(addr): + """If addr is IPv6 then return addr wrapped between '[' and ']', + otherwise return it intact.""" + if _is_ipv6(addr): + return "".join(("[", addr, "]", )) + else: + return addr + + class DecodeError(cdist.Error): def __init__(self, command): self.command = command @@ -118,12 +155,12 @@ class Remote(object): command = self._copy.split() path = os.path.join(source, f) command.extend([path, '{0}:{1}'.format( - self.target_host[0], destination)]) + _wrap_addr(self.target_host[0]), destination)]) self._run_command(command) else: command = self._copy.split() command.extend([source, '{0}:{1}'.format( - self.target_host[0], destination)]) + _wrap_addr(self.target_host[0]), destination)]) self._run_command(command) def transfer_dir_parallel(self, source, destination, jobs): @@ -145,7 +182,7 @@ class Remote(object): command = self._copy.split() path = os.path.join(source, f) command.extend([path, '{0}:{1}'.format( - self.target_host[0], destination)]) + _wrap_addr(self.target_host[0]), destination)]) commands.append(command) results = [ pool.apply_async(self._run_command, (cmd,))