Oleksandr Shyshko | 04099ea | 2018-11-22 16:02:52 +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 | from cinderv3.common import send |
| 16 | from cinderv3.lists import service_list |
| 17 | |
| 18 | @send("put") |
| 19 | def service_update(host, binary, action, **kwargs): |
| 20 | """ |
| 21 | Enable/Disable Cinder service |
| 22 | """ |
| 23 | req = {"host": host, "binary": binary} |
| 24 | if kwargs.get('disabled_reason') and action == 'disable': |
| 25 | url = '/os-services/disable-log-reason' |
| 26 | req['disabled_reason'] = kwargs['disabled_reason'] |
| 27 | else: |
| 28 | url = '/os-services/{}'.format(action) |
| 29 | |
| 30 | return url, req |
| 31 | |
| 32 | |
| 33 | def wait_for_service(cloud_name, host=None, binary=None, admin_up_only=True, retries=18, timeout=10, **kwargs): |
| 34 | """ |
| 35 | Ensure the service is up and running on specified host. |
| 36 | |
| 37 | :param host: name of a host where service is running |
| 38 | :param admin_up_only: do not check status for admin disabled service |
| 39 | :param binary: name of the service |
| 40 | :param timeout: number of seconds to wait before retries |
| 41 | :param retries: number of retries |
| 42 | """ |
| 43 | |
| 44 | if host: |
| 45 | kwargs['host'] = host |
| 46 | if binary: |
| 47 | kwargs['binary'] = binary |
| 48 | |
| 49 | for _i in range(retries): |
| 50 | services = service_list(cloud_name=cloud_name, **kwargs)['services'] |
| 51 | |
| 52 | down_services = [s for s in services if s['state'] == 'down'] |
| 53 | |
| 54 | if admin_up_only: |
| 55 | down_services = [s for s in down_services if s['status'] == 'enabled'] |
| 56 | |
| 57 | if not down_services: |
| 58 | return 'Cinder services with admin_up_only=%s are up or disabled administratively' % (admin_up_only) |
| 59 | |
| 60 | time.sleep(timeout) |
| 61 | |
| 62 | raise CommandExecutionError("Cinder services {} are still down or disabled".format(down_services)) |