Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [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 common |
| 14 | import six.moves.urllib.parse as urllib_parse |
| 15 | import time |
| 16 | from salt.exceptions import CommandExecutionError |
| 17 | |
| 18 | # Function alias to not shadow built-ins |
| 19 | __func_alias__ = { |
| 20 | 'list_': 'list' |
| 21 | } |
| 22 | |
| 23 | |
| 24 | @common.function_descriptor('find', 'Compute services', 'services') |
| 25 | @common.send('get') |
| 26 | def list_(*args, **kwargs): |
| 27 | """Return list of nova services.""" |
| 28 | url = '/os-services?{}'.format(urllib_parse.urlencode(kwargs)) |
| 29 | return url, {} |
| 30 | |
| 31 | |
| 32 | @common.function_descriptor('update', 'Compute service', 'service') |
| 33 | @common.send('put') |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 34 | def update(host, binary, action, **kwargs): |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 35 | """Enable/Disable nova service""" |
| 36 | if kwargs.get('disabled_reason') and action == 'disable': |
| 37 | url = '/os-services/disable-log-reason' |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 38 | req = {"host": host, "binary": binary, "disabled_reason": kwargs['disabled_reason']} |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 39 | else: |
| 40 | url = '/os-services/%s' % action |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 41 | req = {"host": host, "binary": binary} |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 42 | return url, {"json": req} |
| 43 | |
| 44 | |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 45 | def wait_for_services(cloud_name, host=None, binary=None, admin_up_only=True, retries=18, timeout=10, **kwargs): |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 46 | """Ensure the service is up and running on specified host. |
| 47 | |
| 48 | :param host: name of a host where service is running |
| 49 | :param admin_up_only: do not check status for admin disabled service |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 50 | :param binary: name of the service (by default nova-compute) |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 51 | :param timeout: number of seconds to wait before retries |
| 52 | :param retries: number of retries |
| 53 | """ |
| 54 | kwargs = {} |
| 55 | if host is not None: |
| 56 | kwargs['host'] = host |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 57 | if binary is not None: |
| 58 | kwargs['binary'] = binary |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 59 | |
| 60 | for i in range(retries): |
| 61 | services = list_(cloud_name=cloud_name, **kwargs)['body'].get('services') |
| 62 | |
| 63 | if admin_up_only: |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 64 | down_services = [s for s in services if (not binary or s['binary'] == binary) and s['status'] == 'enabled' and s['state'] == 'down'] |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 65 | else: |
Oleksandr Shyshko | c74c477 | 2018-11-29 15:17:34 +0000 | [diff] [blame] | 66 | down_services = [s for s in services if (not binary or s['binary'] == binary) and s['state'] == 'down'] |
Oleh Hryhorov | 5cfb9d3 | 2018-09-11 16:55:24 +0000 | [diff] [blame] | 67 | |
| 68 | if len(down_services) == 0: |
| 69 | return 'Compute services with admin_up_only=%s are up or disabled administratively' % (admin_up_only) |
| 70 | time.sleep(timeout) |
| 71 | |
| 72 | raise CommandExecutionError("Compute services {} are still down or disabled".format(down_services)) |