From 0a35106df9b430436493bcab531e500a3d3c04f9 Mon Sep 17 00:00:00 2001 From: Rob Nagler Date: Thu, 19 May 2016 10:23:58 -0600 Subject: [PATCH] verify_socket: show what interface:port is in error; DRY refactoring (#33320) * verify_socket: show what interface:port is in error; DRY refactoring * indexes format strings --- salt/utils/verify.py | 45 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/salt/utils/verify.py b/salt/utils/verify.py index 96563aecc7..46d8920074 100644 --- a/salt/utils/verify.py +++ b/salt/utils/verify.py @@ -117,32 +117,27 @@ def verify_socket(interface, pub_port, ret_port): ''' addr_family = lookup_family(interface) - pubsock = socket.socket(addr_family, socket.SOCK_STREAM) - retsock = socket.socket(addr_family, socket.SOCK_STREAM) - try: - pubsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - pubsock.bind((interface, int(pub_port))) - pubsock.close() - retsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - retsock.bind((interface, int(ret_port))) - retsock.close() - result = True - except Exception as exc: - if exc.args: - msg = ('Unable to bind socket, error: {0}'.format(str(exc))) - else: - msg = ('Unable to bind socket, this might not be a problem.' - ' Is there another salt-master running?') - if is_console_configured(): - log.warning(msg) - else: - sys.stderr.write('WARNING: {0}\n'.format(msg)) - result = False - finally: - pubsock.close() - retsock.close() + for port in pub_port, ret_port: + sock = socket.socket(addr_family, socket.SOCK_STREAM) + try: + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind((interface, int(port))) + except Exception as exc: + msg = 'Unable to bind socket {0}:{1}'.format(interface, port) + if exc.args: + msg = '{0}, error: {1}'.format(msg, str(exc)) + else: + msg = '{0}, this might not be a problem.'.format(msg) + msg += '; Is there another salt-master running?' + if is_console_configured(): + log.warning(msg) + else: + sys.stderr.write('WARNING: {0}\n'.format(msg)) + return False + finally: + sock.close() - return result + return True def verify_files(files, user):