460745
From fbaa55fbe8447745a20c99a68d62790f5dd5a0f7 Mon Sep 17 00:00:00 2001
460745
From: Petr Vobornik <pvoborni@redhat.com>
460745
Date: Thu, 3 Aug 2017 15:48:33 +0200
460745
Subject: [PATCH] control logging of host_port_open from caller
460745
460745
host_port_open copied logging behavior of ipa-replica-conncheck utility
460745
which doesn't make it much reusable.
460745
460745
Now log level can be controlled from caller so other callers might use
460745
other logging level without host_port_open guessing what was the
460745
intention.
460745
460745
https://pagure.io/freeipa/issue/7083
460745
460745
Reviewed-By: Tomas Krizek <tkrizek@redhat.com>
460745
---
460745
 install/tools/ipa-replica-conncheck |  9 ++++++++-
460745
 ipapython/ipautil.py                | 17 ++++++-----------
460745
 2 files changed, 14 insertions(+), 12 deletions(-)
460745
460745
diff --git a/install/tools/ipa-replica-conncheck b/install/tools/ipa-replica-conncheck
460745
index 528242268f9992e903781b76a379039d533853c0..f10b7e3d2f94540dba3956bf460c4b9f38da90da 100755
460745
--- a/install/tools/ipa-replica-conncheck
460745
+++ b/install/tools/ipa-replica-conncheck
460745
@@ -46,6 +46,8 @@ import distutils.spawn
460745
 from ipaplatform.paths import paths
460745
 import gssapi
460745
 from cryptography.hazmat.primitives import serialization
460745
+import logging
460745
+
460745
 
460745
 CONNECT_TIMEOUT = 5
460745
 RESPONDER = None
460745
@@ -379,11 +381,16 @@ class PortResponder(threading.Thread):
460745
 def port_check(host, port_list):
460745
     ports_failed = []
460745
     ports_udp_warning = []  # conncheck could not verify that port is open
460745
+    log_level = {
460745
+        SOCK_DGRAM: logging.WARNING,
460745
+        SOCK_STREAM: logging.ERROR
460745
+    }
460745
     for port in port_list:
460745
         try:
460745
             port_open = ipautil.host_port_open(
460745
                 host, port.port, port.port_type,
460745
-                socket_timeout=CONNECT_TIMEOUT, log_errors=True)
460745
+                socket_timeout=CONNECT_TIMEOUT, log_errors=True,
460745
+                log_level=log_level[port.port_type])
460745
         except socket.gaierror:
460745
             raise RuntimeError("Port check failed! Unable to resolve host name '%s'" % host)
460745
         if port_open:
460745
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
460745
index 5a6bf5a27d5a6e25c51fbaa6e2b1167652e2735d..e1e6e32b15559486caecb070627db82e14a57bdf 100644
460745
--- a/ipapython/ipautil.py
460745
+++ b/ipapython/ipautil.py
460745
@@ -42,6 +42,7 @@ from contextlib import contextmanager
460745
 import locale
460745
 import collections
460745
 from subprocess import CalledProcessError
460745
+import logging
460745
 
460745
 from dns import resolver, reversename
460745
 from dns.exception import DNSException
460745
@@ -948,7 +949,8 @@ def user_input(prompt, default = None, allow_empty = True):
460745
 
460745
 
460745
 def host_port_open(host, port, socket_type=socket.SOCK_STREAM,
460745
-                   socket_timeout=None, log_errors=False):
460745
+                   socket_timeout=None, log_errors=False,
460745
+                   log_level=logging.DEBUG):
460745
     """
460745
     host: either hostname or IP address;
460745
           if hostname is provided, port MUST be open on ALL resolved IPs
460745
@@ -970,23 +972,16 @@ def host_port_open(host, port, socket_type=socket.SOCK_STREAM,
460745
             s.connect(sa)
460745
 
460745
             if socket_type == socket.SOCK_DGRAM:
460745
-                s.send('')
460745
+                s.send(b'')
460745
                 s.recv(512)
460745
         except socket.error:
460745
             port_open = False
460745
-
460745
             if log_errors:
460745
-                msg = ('Failed to connect to port %(port)d %(proto)s on '
460745
+                msg = ('Failed to connect to port %(port)s %(proto)s on '
460745
                        '%(addr)s' % dict(port=port,
460745
                                          proto=PROTOCOL_NAMES[socket_type],
460745
                                          addr=sa[0]))
460745
-
460745
-                # Do not log udp failures as errors (to be consistent with
460745
-                # the rest of the code that checks for open ports)
460745
-                if socket_type == socket.SOCK_DGRAM:
460745
-                    root_logger.warning(msg)
460745
-                else:
460745
-                    root_logger.error(msg)
460745
+                root_logger.log(log_level, msg)
460745
         finally:
460745
             if s is not None:
460745
                 s.close()
460745
-- 
460745
2.13.5
460745