Oleksandr Shyshko | fda15b0 | 2018-11-21 15:53:21 +0200 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
| 13 | import time |
| 14 | from salt.exceptions import CommandExecutionError |
| 15 | |
| 16 | from heatv1.common import send |
| 17 | |
| 18 | @send('get') |
| 19 | def service_list(cloud_name): |
| 20 | """Return list of Heat services.""" |
| 21 | url = '/services' |
| 22 | return url, {} |
| 23 | |
| 24 | def wait_for_service(cloud_name, host=None, binary=None, retries=18, timeout=10): |
| 25 | """Ensure the service is up and running on specified host. |
| 26 | |
| 27 | :param host: name of a host where service is running |
| 28 | :param binary: name of the service |
| 29 | :param timeout: number of seconds to wait before retries |
| 30 | :param retries: number of retries |
| 31 | """ |
| 32 | for _i in range(retries): |
| 33 | services = service_list(cloud_name=cloud_name)['body']['services'] |
| 34 | |
| 35 | down_services = [s for s in services if s['status'] == 'down'] |
| 36 | |
| 37 | # When Heat worker is reloaded, it creates new instance with different Engine ID. |
| 38 | # This is needed for checking that at least one instance on the host is running. |
| 39 | up_services_hosts = {s['host'] for s in services if s['status'] == 'up'} |
| 40 | |
| 41 | if host: |
| 42 | down_services = [s for s in down_services if s['host'] == host \ |
| 43 | and s['host'] not in up_services_hosts] |
| 44 | |
| 45 | if binary: |
| 46 | down_services = [s for s in down_services if s['binary'] == binary \ |
| 47 | and s['host'] not in up_services_hosts] |
| 48 | |
| 49 | if not host and not binary: |
| 50 | down_services = [s for s in down_services if s['host'] not in up_services_hosts] |
| 51 | |
| 52 | if not down_services: |
| 53 | return 'Heat services are running.' |
| 54 | time.sleep(timeout) |
| 55 | |
| 56 | raise CommandExecutionError("Heat services {} are still down".format(down_services)) |