diff options
author | Rob Crittenden <rcritten@redhat.com> | 2012-05-24 11:23:36 -0400 |
---|---|---|
committer | Rob Crittenden <rcritten@redhat.com> | 2012-07-02 17:08:58 -0400 |
commit | e5b6260008a3a7132fdaef99d800406eb8872316 (patch) | |
tree | 9981186bd06f5574570f5743cba05cd0aa9ee963 /ipapython/ipautil.py | |
parent | 6fb802152add24aa1842f4adccf59b23850ab336 (diff) | |
download | freeipa-e5b6260008a3a7132fdaef99d800406eb8872316.tar.gz freeipa-e5b6260008a3a7132fdaef99d800406eb8872316.tar.xz freeipa-e5b6260008a3a7132fdaef99d800406eb8872316.zip |
Centralize timeout for waiting for servers to start.
All service start/restart currently go through ipapython/platform so
move the "wait for service to start" code there as well.
A dictionary of known services and ports to wait on is defined in base.py
This is referenced by the platforms by instance name to determine what
to wait for. For the case of dirsrv if we get that as a plain name
(no specific instance) it is assumed to be the main IPA service.
https://fedorahosted.org/freeipa/ticket/2375
https://fedorahosted.org/freeipa/ticket/2610
Diffstat (limited to 'ipapython/ipautil.py')
-rw-r--r-- | ipapython/ipautil.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py index 8884e7be9..e80434cfd 100644 --- a/ipapython/ipautil.py +++ b/ipapython/ipautil.py @@ -41,6 +41,7 @@ import re import xmlrpclib import datetime import netaddr +import time from dns import resolver, rdatatype from dns.exception import DNSException @@ -1010,3 +1011,56 @@ def utf8_encode_values(values): return map(utf8_encode_value, values) else: return utf8_encode_value(values) + +def wait_for_open_ports(host, ports, timeout=0): + """ + Wait until the specified port(s) on the remote host are open. Timeout + in seconds may be specified to limit the wait. + """ + if not isinstance(ports, (tuple, list)): + ports = [ports] + + root_logger.debug('wait_for_open_ports: %s %s timeout %d' % (host, ports, timeout)) + op_timeout = time.time() + timeout + ipv6_failover = False + + for port in ports: + while True: + try: + if ipv6_failover: + s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + else: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((host, port)) + s.close() + break + except socket.error, e: + if e.errno == 111: # 111: Connection refused + if timeout and time.time() > op_timeout: # timeout exceeded + raise e + time.sleep(1) + elif not ipv6_failover: # fallback to IPv6 connection + ipv6_failover = True + else: + raise e + +def wait_for_open_socket(socket_name, timeout=0): + """ + Wait until the specified socket on the local host is open. Timeout + in seconds may be specified to limit the wait. + """ + op_timeout = time.time() + timeout + + while True: + try: + s = socket.socket(socket.AF_UNIX) + s.connect(socket_name) + s.close() + break + except socket.error, e: + if e.errno in (2,111): # 111: Connection refused, 2: File not found + if timeout and time.time() > op_timeout: # timeout exceeded + raise e + time.sleep(1) + else: + raise e |