Oleksandr Shyshko | c08432f | 2018-11-23 17:15:51 +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 six.moves.urllib.parse as urllib_parse |
| 14 | import time |
| 15 | |
| 16 | from salt.exceptions import CommandExecutionError |
| 17 | from manilang.common import send, MANILA_HEADER |
| 18 | |
| 19 | @send('get', MANILA_HEADER) |
| 20 | def service_list(*args, **kwargs): |
| 21 | """Return list of Manila services.""" |
| 22 | |
| 23 | # TODO For API versions 2.6 and prior, replace services in the URLs with os-services. |
| 24 | # https://developer.openstack.org/api-ref/shared-file-system/#list-services |
| 25 | url = '/os-services?{}'.format(urllib_parse.urlencode(kwargs)) |
| 26 | return url, {} |
| 27 | |
| 28 | |
| 29 | @send('put', MANILA_HEADER) |
| 30 | def service_update(host, binary, action, **kwargs): |
| 31 | """Enable/Disable Manila service""" |
| 32 | |
| 33 | url = '/services/{}'.format(action) |
| 34 | req = {"host": host, "binary": binary} |
| 35 | |
| 36 | return url, req |
| 37 | |
| 38 | def wait_for_service(cloud_name, host=None, admin_up_only=True, retries=18, timeout=10, **kwargs): |
| 39 | """Ensure the service is up and running on specified host. |
| 40 | |
| 41 | :param host: name of a host where service is running |
| 42 | :param admin_up_only: do not check status for admin disabled service |
| 43 | :param timeout: number of seconds to wait before retries |
| 44 | :param retries: number of retries |
| 45 | """ |
| 46 | if admin_up_only: |
| 47 | kwargs['status'] = 'enabled' |
| 48 | |
| 49 | kwargs['state'] = 'down' |
| 50 | |
| 51 | for _i in range(retries): |
| 52 | |
| 53 | services = service_list(cloud_name=cloud_name, **kwargs)['services'] |
| 54 | |
| 55 | # You are able to wait status either certain manila-share instance by host@driver |
| 56 | # or all existed instances on a node by host. |
| 57 | # Also you are able to wait status of all instances from every node if you don't define host. |
| 58 | if host: |
| 59 | if '@' in str(host): |
| 60 | down_services = [s for s in services if host == s['host']] |
| 61 | else: |
| 62 | down_services = [s for s in services if host == s['host'].split('@')[0]] |
| 63 | else: |
| 64 | down_services = [s for s in services] |
| 65 | |
| 66 | if len(down_services) == 0: |
| 67 | return 'Manila services with admin_up_only={} are up or disabled administratively'.format(admin_up_only) |
| 68 | time.sleep(timeout) |
| 69 | |
| 70 | raise CommandExecutionError("Manila services {} are still down or disabled".format(down_services)) |