blob: f5093a1c4246864fd469ce2d5a5ade2b428094b0 [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')
34def update(host, service, action, **kwargs):
35 """Enable/Disable nova service"""
36 if kwargs.get('disabled_reason') and action == 'disable':
37 url = '/os-services/disable-log-reason'
38 req = {"host": host, "binary": service, "disabled_reason": kwargs['disabled_reason']}
39 else:
40 url = '/os-services/%s' % action
41 req = {"host": host, "binary": service}
42 return url, {"json": req}
43
44
45def wait_for_services(cloud_name, host=None, service=None, admin_up_only=True, retries=18, timeout=10, **kwargs):
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
50 :param service: name of the service (by default nova-compute)
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
57 if service is not None:
58 kwargs['service'] = service
59
60 for i in range(retries):
61 services = list_(cloud_name=cloud_name, **kwargs)['body'].get('services')
62
63 if admin_up_only:
64 down_services = [s for s in services if (not service or s['binary'] == service) and s['status'] == 'enabled' and s['state'] == 'down']
65 else:
66 down_services = [s for s in services if (not service or s['binary'] == service) and s['state'] == 'down']
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))