3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # pylint: disable=missing-docstring
12 from __future__ import print_function
19 from shade import _utils
22 LOGGER = logging.getLogger(__name__)
25 def execute_command_raise(cmd, info=False, error_msg="",
26 verbose=True, output_file=None):
27 ret = execute_command(cmd, info, error_msg, verbose, output_file)
29 raise Exception(error_msg)
32 def execute_command(cmd, info=False, error_msg="",
33 verbose=True, output_file=None):
35 error_msg = ("The command '%s' failed." % cmd)
36 msg_exec = ("Executing command: '%s'" % cmd)
41 LOGGER.debug(msg_exec)
42 popen = subprocess.Popen(
43 cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
45 ofd = open(output_file, "w")
46 for line in iter(popen.stdout.readline, b''):
48 ofd.write(line.decode("utf-8"))
50 line = line.decode("utf-8").replace('\n', '')
56 returncode = popen.wait()
59 LOGGER.error(error_msg)
64 def get_parameter_from_yaml(parameter, yfile):
66 Returns the value of a given parameter in file.yaml
67 parameter must be given in string format with dots
68 Example: general.openstack.image_name
70 with open(yfile) as yfd:
71 file_yaml = yaml.safe_load(yfd)
73 for element in parameter.split("."):
74 value = value.get(element)
76 raise ValueError("The parameter %s is not defined in"
77 " %s" % (parameter, yfile))
81 def get_nova_version(cloud):
82 """ Get Nova API microversion
86 - Nova API microversion
87 - None on operation error
89 # pylint: disable=protected-access
91 request = cloud._compute_client.request("/", "GET")
92 LOGGER.debug('cloud._compute_client.request: %s', request)
93 version = request["version"]["version"]
94 major, minor = version.split('.')
95 LOGGER.debug('nova version: %s', (int(major), int(minor)))
96 return (int(major), int(minor))
97 except Exception: # pylint: disable=broad-except
98 LOGGER.exception("Cannot detect Nova version")
102 def get_openstack_version(cloud):
103 """ Detect OpenStack version via Nova API microversion
105 It follows `MicroversionHistory
106 <https://docs.openstack.org/nova/latest/reference/api-microversion-history.html>`_.
111 - Unknown on operation error
113 version = get_nova_version(cloud)
116 if version > (2, 72):
118 elif version > (2, 65):
120 elif version > (2, 60):
122 elif version > (2, 53):
124 elif version > (2, 42):
126 elif version > (2, 38):
128 elif version > (2, 25):
130 elif version > (2, 12):
132 elif version > (2, 3):
133 osversion = "Liberty"
134 elif version >= (2, 1):
137 osversion = "Unknown"
138 LOGGER.info('Detect OpenStack version: %s', osversion)
140 except AssertionError:
141 LOGGER.exception("Cannot detect OpenStack version")
145 def list_services(cloud):
146 # pylint: disable=protected-access
147 """Search Keystone services via $OS_INTERFACE.
149 It mainly conforms with `Shade
150 <latest/reference/api-microversion-history.html>`_ but allows testing vs
151 public endpoints. It's worth mentioning that it doesn't support keystone
154 :returns: a list of ``munch.Munch`` containing the services description
156 :raises: ``OpenStackCloudException`` if something goes wrong during the
159 url, key = '/services', 'services'
160 data = cloud._identity_client.get(
161 url, endpoint_filter={
162 'interface': os.environ.get('OS_INTERFACE', 'public')},
163 error_message="Failed to list services")
164 services = cloud._get_and_munchify(key, data)
165 return _utils.normalize_keystone_services(services)
168 def search_services(cloud, name_or_id=None, filters=None):
169 # pylint: disable=protected-access
170 """Search Keystone services ia $OS_INTERFACE.
172 It mainly conforms with `Shade
173 <latest/reference/api-microversion-history.html>`_ but allows testing vs
174 public endpoints. It's worth mentioning that it doesn't support keystone
177 :param name_or_id: Name or id of the desired service.
178 :param filters: a dict containing additional filters to use. e.g.
181 :returns: a list of ``munch.Munch`` containing the services description
183 :raises: ``OpenStackCloudException`` if something goes wrong during the
186 services = list_services(cloud)
187 return _utils._filter_list(services, name_or_id, filters)
190 def convert_dict_to_ini(value):
191 "Convert dict to oslo.conf input"
192 assert isinstance(value, dict)
193 return ",".join("{}:{}".format(
194 key, val) for (key, val) in six.iteritems(value))
197 def convert_list_to_ini(value):
198 "Convert list to oslo.conf input"
199 assert isinstance(value, list)
200 return ",".join("{}".format(val) for val in value)
203 def convert_ini_to_dict(value):
204 "Convert oslo.conf input to dict"
205 assert isinstance(value, str)
207 return {k: v for k, v in (x.rsplit(':', 1) for x in value.split(','))}
212 def convert_ini_to_list(value):
213 "Convert list to oslo.conf input"
214 assert isinstance(value, str)
217 return [x for x in value.split(',')]