pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0229-control-logging-of-host_port_open-from-caller.patch

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