blob: 7060e1424c0d4e7cf59ce8a632ddf56da80b1867 [file] [log] [blame]
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +00001# 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
13import common
14import six.moves.urllib.parse as urllib_parse
15import time
16from 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')
26def 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 Shyshkoc74c4772018-11-29 15:17:34 +000034def update(host, binary, action, **kwargs):
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000035 """Enable/Disable nova service"""
36 if kwargs.get('disabled_reason') and action == 'disable':
37 url = '/os-services/disable-log-reason'
Oleksandr Shyshkoc74c4772018-11-29 15:17:34 +000038 req = {"host": host, "binary": binary, "disabled_reason": kwargs['disabled_reason']}
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000039 else:
40 url = '/os-services/%s' % action
Oleksandr Shyshkoc74c4772018-11-29 15:17:34 +000041 req = {"host": host, "binary": binary}
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000042 return url, {"json": req}
43
44
Oleksandr Shyshkoc74c4772018-11-29 15:17:34 +000045def wait_for_services(cloud_name, host=None, binary=None, admin_up_only=True, retries=18, timeout=10, **kwargs):
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000046 """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 Shyshkoc74c4772018-11-29 15:17:34 +000050 :param binary: name of the service (by default nova-compute)
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000051 :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 Shyshkoc74c4772018-11-29 15:17:34 +000057 if binary is not None:
58 kwargs['binary'] = binary
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000059
60 for i in range(retries):
61 services = list_(cloud_name=cloud_name, **kwargs)['body'].get('services')
62
63 if admin_up_only:
Oleksandr Shyshkoc74c4772018-11-29 15:17:34 +000064 down_services = [s for s in services if (not binary or s['binary'] == binary) and s['status'] == 'enabled' and s['state'] == 'down']
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000065 else:
Oleksandr Shyshkoc74c4772018-11-29 15:17:34 +000066 down_services = [s for s in services if (not binary or s['binary'] == binary) and s['state'] == 'down']
Oleh Hryhorov5cfb9d32018-09-11 16:55:24 +000067
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))