From 12c398e1d79cdeb0e7bf718ea6c9b7010691ca9a Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Mon, 31 Mar 2014 16:23:00 -0500 Subject: [PATCH] Fix traceback in salt.utils.network.get_fqhostname This traceback occurs when the lookup fails in some way, raising an exception. --- salt/utils/network.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/salt/utils/network.py b/salt/utils/network.py index 421f93b5b5..d100aacd33 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -89,10 +89,25 @@ def get_fqhostname(): if h_name.find('.') >= 0: return h_name else: - family, socktype, proto, canonname, sockaddr = socket.getaddrinfo( + try: + addrinfo = socket.getaddrinfo( h_name, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, - socket.SOL_TCP, socket.AI_CANONNAME)[0] - return canonname + socket.SOL_TCP, socket.AI_CANONNAME + )[0] + except IndexError: + # Handle possible empty struct returns + return 'localhost' + except socket.gaierror: + return 'localhost' + else: + # Struct contanis the following elements: + # family, socktype, proto, canonname, sockaddr + try: + # Prevent returning an empty string by falling back to + # 'localhost' + return addrinfo[3] or 'localhost' + except IndexError: + return 'localhost' def ip_to_host(ip):