sgarbuz | 0ebf574 | 2018-11-21 17:39:45 +0200 | [diff] [blame] | 1 | from designatev2.lists import status_list |
| 2 | from salt.exceptions import CommandExecutionError |
| 3 | import datetime |
| 4 | import time |
| 5 | |
| 6 | |
| 7 | def wait_for_services(cloud_name, host, service=None, retries=18, timeout=10, time_delta=5): |
| 8 | """Ensure the service is up and running on specified host. |
| 9 | :example: salt-call designatev2.wait_for_services admin_identity host=cfg01 service=mdns |
| 10 | :param host: name of a host where service is running |
| 11 | :param service: name of the service (by default designate) |
| 12 | :param timeout: number of seconds to wait before retries |
| 13 | :param retries: number of retries |
| 14 | :time_delta: time interval that can be between the request and service response |
| 15 | """ |
| 16 | |
| 17 | for _i in range(retries): |
| 18 | time_actual = datetime.datetime.now() |
| 19 | services = status_list(cloud_name=cloud_name)['service_statuses'] |
| 20 | service_list = [] |
| 21 | service_active = [] |
| 22 | |
| 23 | for s in services: |
| 24 | if s['hostname'] == host: |
| 25 | service_list.append(s['service_name']) |
| 26 | |
| 27 | for s in services: |
| 28 | updated_at = datetime.datetime.strptime(s['updated_at'], '%Y-%m-%dT%H:%M:%S.%f') |
| 29 | time_diff = abs((time_actual - updated_at).total_seconds()) <= time_delta |
| 30 | service_name = s['service_name'] |
| 31 | |
| 32 | if s['hostname'] == host and time_diff: |
| 33 | if service is None: |
| 34 | service_active.append(s['service_name']) |
| 35 | else: |
| 36 | if service_name == service: |
| 37 | return 'Designate-%s service are UP' % (service) |
| 38 | elif service_name == service and not time_diff: |
| 39 | continue |
| 40 | |
| 41 | if len(service_list) == len(service_active): |
| 42 | return 'All Designate services are UP' |
| 43 | time.sleep(timeout) |
| 44 | raise CommandExecutionError("Designate-{} services are still down".format(set(service_list) - set(service_active))) |