From 659c6d3a7eefcf301f33270c2c1d4f886009b6a6 Mon Sep 17 00:00:00 2001 From: Andrey Trubachev Date: Sat, 3 Jan 2015 10:32:27 +0300 Subject: [PATCH 1/2] Fix 'wait_for' doesn't work with ipv6only host --- utilities/logic/wait_for.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/utilities/logic/wait_for.py b/utilities/logic/wait_for.py index ae316fe1a17..41852148971 100644 --- a/utilities/logic/wait_for.py +++ b/utilities/logic/wait_for.py @@ -351,10 +351,8 @@ def main(): except IOError: break elif port: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.settimeout(connect_timeout) try: - s.connect( (host, port) ) + s = socket.create_connection( (host, port), connect_timeout) s.shutdown(socket.SHUT_RDWR) s.close() time.sleep(1) @@ -397,10 +395,8 @@ def main(): elapsed = datetime.datetime.now() - start module.fail_json(msg="Failed to stat %s, %s" % (path, e.strerror), elapsed=elapsed.seconds) elif port: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.settimeout(connect_timeout) try: - s.connect( (host, port) ) + s = socket.create_connection( (host, port), connect_timeout) if search_regex: data = '' matched = False From 10e48d18f7c81106bfd5fa663492e06fd9ef7c9b Mon Sep 17 00:00:00 2001 From: Andrey Trubachev Date: Mon, 5 Jan 2015 21:05:01 +0300 Subject: [PATCH 2/2] Fix python-2.4 compatibility --- utilities/logic/wait_for.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/utilities/logic/wait_for.py b/utilities/logic/wait_for.py index 41852148971..3e40d4b0027 100644 --- a/utilities/logic/wait_for.py +++ b/utilities/logic/wait_for.py @@ -293,6 +293,25 @@ def _little_endian_convert_32bit(block): # which lets us start at the end of the string block and work to the begining return "".join([ block[x:x+2] for x in xrange(6, -2, -2) ]) +def _create_connection( (host, port), connect_timeout): + """ + Connect to a 2-tuple (host, port) and return + the socket object. + + Args: + 2-tuple (host, port) and connection timeout + Returns: + Socket object + """ + if sys.version_info < (2, 6): + (family, _) = _convert_host_to_ip(host) + connect_socket = socket.socket(family, socket.SOCK_STREAM) + connect_socket.settimeout(connect_timeout) + connect_socket.connect( (host, port) ) + else: + connect_socket = socket.create_connection( (host, port), connect_timeout) + return connect_socket + def main(): module = AnsibleModule( @@ -352,7 +371,7 @@ def main(): break elif port: try: - s = socket.create_connection( (host, port), connect_timeout) + s = _create_connection( (host, port), connect_timeout) s.shutdown(socket.SHUT_RDWR) s.close() time.sleep(1) @@ -396,7 +415,7 @@ def main(): module.fail_json(msg="Failed to stat %s, %s" % (path, e.strerror), elapsed=elapsed.seconds) elif port: try: - s = socket.create_connection( (host, port), connect_timeout) + s = _create_connection( (host, port), connect_timeout) if search_regex: data = '' matched = False