From b7db164c7bc13b0a74003ae0715175d883640024 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Thu, 20 Sep 2018 12:34:54 -0400 Subject: [PATCH 1/4] catch socket.error too; and reflow for readability (why list?) --- salt/utils/network.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/salt/utils/network.py b/salt/utils/network.py index 79fbbad059af..3d774634d015 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -198,9 +198,7 @@ def get_fqhostname(): ''' Returns the fully qualified hostname ''' - l = [socket.getfqdn()] - - # try socket.getaddrinfo + # try getaddrinfo() try: addrinfo = socket.getaddrinfo( socket.gethostname(), 0, socket.AF_UNSPEC, socket.SOCK_STREAM, @@ -211,11 +209,15 @@ def get_fqhostname(): # On Windows `canonname` can be an empty string # This can cause the function to return `None` if len(info) >= 4 and info[3]: - l = [info[3]] + return info[3] except socket.gaierror: pass - - return l and l[0] or None + except socket.error: + pass + # if that fails, try getfqdn() + fqdn = socket.getfqdn() + if fqdn: + return fqdn def ip_to_host(ip): From 05a9304b048fa05a39c633edbfd68453f8bea2bb Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Mon, 22 Oct 2018 15:40:42 -0400 Subject: [PATCH 2/4] changes requested in code reviews * one return * log errors --- salt/utils/network.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/salt/utils/network.py b/salt/utils/network.py index 3d774634d015..7d98cd5890c4 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -199,6 +199,7 @@ def get_fqhostname(): Returns the fully qualified hostname ''' # try getaddrinfo() + fqdn = None try: addrinfo = socket.getaddrinfo( socket.gethostname(), 0, socket.AF_UNSPEC, socket.SOCK_STREAM, @@ -208,16 +209,16 @@ def get_fqhostname(): # info struct [family, socktype, proto, canonname, sockaddr] # On Windows `canonname` can be an empty string # This can cause the function to return `None` - if len(info) >= 4 and info[3]: - return info[3] + if len(info) > 3 and info[3]: + fqdn = info[3] + break except socket.gaierror: - pass - except socket.error: - pass - # if that fails, try getfqdn() - fqdn = socket.getfqdn() - if fqdn: - return fqdn + pass # NOTE: this used to log.error() but it was later disabled + except socket.error as err: + log.debug('socket.getaddrinfo() failure while finding fqdn: %s', err) + if fqdn is not None: + fqdn = socket.getfqdn() + return fqdn def ip_to_host(ip): From 0014e8123f30b2cb5a42797341b8e1524ebfbf52 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 23 Oct 2018 10:16:56 -0400 Subject: [PATCH 3/4] pylint 2-space inline comment rule --- salt/utils/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/utils/network.py b/salt/utils/network.py index 7d98cd5890c4..34f26d19f20d 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -213,7 +213,7 @@ def get_fqhostname(): fqdn = info[3] break except socket.gaierror: - pass # NOTE: this used to log.error() but it was later disabled + pass # NOTE: this used to log.error() but it was later disabled except socket.error as err: log.debug('socket.getaddrinfo() failure while finding fqdn: %s', err) if fqdn is not None: From 25db3a9b938b75c1fbdbd92ee7c7c6795cf630b4 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 23 Oct 2018 10:17:16 -0400 Subject: [PATCH 4/4] fix reversed logical meaning --- salt/utils/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/utils/network.py b/salt/utils/network.py index 34f26d19f20d..2aa50efffe7e 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -216,7 +216,7 @@ def get_fqhostname(): pass # NOTE: this used to log.error() but it was later disabled except socket.error as err: log.debug('socket.getaddrinfo() failure while finding fqdn: %s', err) - if fqdn is not None: + if fqdn is None: fqdn = socket.getfqdn() return fqdn