| from designatev2.lists import status_list |
| from salt.exceptions import CommandExecutionError |
| import datetime |
| import time |
| |
| |
| def wait_for_services(cloud_name, host, service=None, retries=18, timeout=10, time_delta=5): |
| """Ensure the service is up and running on specified host. |
| :example: salt-call designatev2.wait_for_services admin_identity host=cfg01 service=mdns |
| :param host: name of a host where service is running |
| :param service: name of the service (by default designate) |
| :param timeout: number of seconds to wait before retries |
| :param retries: number of retries |
| :time_delta: time interval that can be between the request and service response |
| """ |
| |
| for _i in range(retries): |
| time_actual = datetime.datetime.now() |
| services = status_list(cloud_name=cloud_name)['service_statuses'] |
| service_list = [] |
| service_active = [] |
| |
| for s in services: |
| if s['hostname'] == host: |
| service_list.append(s['service_name']) |
| |
| for s in services: |
| updated_at = datetime.datetime.strptime(s['updated_at'], '%Y-%m-%dT%H:%M:%S.%f') |
| time_diff = abs((time_actual - updated_at).total_seconds()) <= time_delta |
| service_name = s['service_name'] |
| |
| if s['hostname'] == host and time_diff: |
| if service is None: |
| service_active.append(s['service_name']) |
| else: |
| if service_name == service: |
| return 'Designate-%s service are UP' % (service) |
| elif service_name == service and not time_diff: |
| continue |
| |
| if len(service_list) == len(service_active): |
| return 'All Designate services are UP' |
| time.sleep(timeout) |
| raise CommandExecutionError("Designate-{} services are still down".format(set(service_list) - set(service_active))) |