]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephadm: correct ipv6 support in port open detection
authorPaul Cuzner <pcuzner@redhat.com>
Tue, 17 Dec 2019 02:23:49 +0000 (15:23 +1300)
committerPaul Cuzner <pcuzner@redhat.com>
Thu, 19 Dec 2019 21:29:44 +0000 (10:29 +1300)
Updated the port_in_use function to work better
in ipv4 and ipv6 environments.

Signed-off-by: Paul Cuzner <pcuzner@redhat.com>
src/cephadm/cephadm

index 9fea0b3add05e476a34f1b00823d903059a7d65d..169f88d0092b3279d496f6c2ada4f50804bd0e0a 100755 (executable)
@@ -46,6 +46,7 @@ import subprocess
 import sys
 import tempfile
 import time
+import errno
 try:
     from typing import Dict, List, Tuple, Optional, Union
 except ImportError:
@@ -108,15 +109,26 @@ def port_in_use(port_num):
     # type (int) -> bool
     """Detect whether a port is in use on the local machine - IPv4 and IPv6"""
 
+    def attempt_bind(address):
+        # type (str) -> None
+        try:
+            s.bind((address, port_num))
+        except (socket.error, OSError) as e:  # py2 and py3
+            if e.errno == errno.EADDRINUSE:
+                raise OSError
+            elif e.errno == errno.EADDRNOTAVAIL:
+                return
+        finally:
+            s.close()
+
     try:
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        s.bind(("127.0.0.1", port_num))
-        s.close()
+        attempt_bind('0.0.0.0')
+
         s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
-        s.bind(("::1", port_num))
-        s.close()
+        attempt_bind('::')
+
     except OSError:
-        s.close()
         return True
     else:
         return False