Add check service status for Designate
In Designatev2 module we check service status by compare `updated_at`
field and current system date plus delta time, that we add for service
response.
We do it becauce designate 'v2/service_statuses'show the list of services
running, and when they last reported as running.
Upstream releasenotes link:
https://github.com/openstack/designate/blob
/6a368fb5d3cc70d73820d8f613ceaad28e5f3f0c/releasenotes/notes
/service-status-ab0e696c8f5fdef8.yaml
Change-Id: Ic888cf249b00cc6bc8af58219d206412c07b6512
Related-Prod: PROD-25159 (PROD:25159)
diff --git a/_modules/designatev2/__init__.py b/_modules/designatev2/__init__.py
index 0becffe..e2379fa 100644
--- a/_modules/designatev2/__init__.py
+++ b/_modules/designatev2/__init__.py
@@ -7,8 +7,11 @@
from designatev2 import lists
from designatev2 import zones
+from designatev2 import services
zone_list = lists.zone_list
+status_list = lists.status_list
+service_wait = services.wait_for_services
zone_create = zones.zone_create
zone_get_details = zones.zone_get_details
zone_get_name_servers = zones.zone_get_name_servers
@@ -16,7 +19,7 @@
zone_delete = zones.zone_delete
-__all__ = ('zone_list', 'zone_create', 'zone_delete', 'zone_update',
+__all__ = ('zone_list', 'status_list', 'service_wait', 'zone_create', 'zone_delete', 'zone_update',
'zone_get_details', 'zone_get_name_servers',)
diff --git a/_modules/designatev2/lists.py b/_modules/designatev2/lists.py
index aebd573..4aa1c29 100644
--- a/_modules/designatev2/lists.py
+++ b/_modules/designatev2/lists.py
@@ -10,3 +10,9 @@
def zone_list(**kwargs):
url = '/zones?{}'.format(urlencode(kwargs))
return url, None
+
+
+@send('get')
+def status_list(**kwargs):
+ url = '/service_statuses'
+ return url, None
\ No newline at end of file
diff --git a/_modules/designatev2/services.py b/_modules/designatev2/services.py
new file mode 100644
index 0000000..d18b156
--- /dev/null
+++ b/_modules/designatev2/services.py
@@ -0,0 +1,44 @@
+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)))