control logging of host_port_open from caller

host_port_open copied logging behavior of ipa-replica-conncheck utility
which doesn't make it much reusable.

Now log level can be controlled from caller so other callers might use
other logging level without host_port_open guessing what was the
intention.

https://pagure.io/freeipa/issue/7083

Reviewed-By: Tomas Krizek <tkrizek@redhat.com>
This commit is contained in:
Petr Vobornik 2017-08-03 15:48:33 +02:00 committed by Tomas Krizek
parent 8d3924dc98
commit cc72db67e2
No known key found for this signature in database
GPG Key ID: 22A2A94B5E49415A
2 changed files with 10 additions and 11 deletions

View File

@ -376,11 +376,16 @@ class PortResponder(threading.Thread):
def port_check(host, port_list):
ports_failed = []
ports_udp_warning = [] # conncheck could not verify that port is open
log_level = {
SOCK_DGRAM: logging.WARNING,
SOCK_STREAM: logging.ERROR
}
for port in port_list:
try:
port_open = ipautil.host_port_open(
host, port.port, port.port_type,
socket_timeout=CONNECT_TIMEOUT, log_errors=True)
socket_timeout=CONNECT_TIMEOUT, log_errors=True,
log_level=log_level[port.port_type])
except socket.gaierror:
raise RuntimeError("Port check failed! Unable to resolve host name '%s'" % host)
if port_open:

View File

@ -960,7 +960,8 @@ def user_input(prompt, default = None, allow_empty = True):
def host_port_open(host, port, socket_type=socket.SOCK_STREAM,
socket_timeout=None, log_errors=False):
socket_timeout=None, log_errors=False,
log_level=logging.DEBUG):
"""
host: either hostname or IP address;
if hostname is provided, port MUST be open on ALL resolved IPs
@ -986,19 +987,12 @@ def host_port_open(host, port, socket_type=socket.SOCK_STREAM,
s.recv(512)
except socket.error:
port_open = False
if log_errors:
msg = ('Failed to connect to port %(port)d %(proto)s on '
msg = ('Failed to connect to port %(port)s %(proto)s on '
'%(addr)s' % dict(port=port,
proto=PROTOCOL_NAMES[socket_type],
addr=sa[0]))
# Do not log udp failures as errors (to be consistent with
# the rest of the code that checks for open ports)
if socket_type == socket.SOCK_DGRAM:
logger.warning('%s', msg)
else:
logger.error('%s', msg)
logger.log(log_level, msg)
finally:
if s is not None:
s.close()