Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 2 | |
| 3 | # Copyright 2013 IBM Corp. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
Martin Kopec | bd9dd8e | 2017-06-28 20:52:18 +0000 | [diff] [blame] | 17 | """ |
| 18 | Verifies user's current tempest configuration. |
| 19 | |
| 20 | This command is used for updating or user's tempest configuration file based on |
| 21 | api queries or replacing all option in a tempest configuration file for a full |
| 22 | list of extensions. |
| 23 | |
| 24 | General Options |
| 25 | =============== |
| 26 | |
| 27 | -u, --update |
| 28 | ------------ |
| 29 | Update the config file with results from api queries. This assumes whatever is |
| 30 | set in the config file is incorrect. |
| 31 | |
| 32 | -o FILE, --output=FILE |
| 33 | ---------------------- |
| 34 | Output file to write an updated config file to. This has to be a separate file |
| 35 | from the original one. If one isn't specified with -u the values which should |
| 36 | be changed will be printed to STDOUT. |
| 37 | |
| 38 | -r, --replace-ext |
| 39 | ----------------- |
| 40 | If specified the all option will be replaced with a full list of extensions. |
| 41 | |
| 42 | Environment Variables |
| 43 | ===================== |
| 44 | |
| 45 | The command is workspace aware - it uses tempest config file tempest.conf |
| 46 | located in ./etc/ directory. |
| 47 | The path to the config file and it's name can be changed through environment |
| 48 | variables. |
| 49 | |
| 50 | TEMPEST_CONFIG_DIR |
| 51 | ------------------ |
| 52 | Path to a directory where tempest configuration file is stored. If the variable |
| 53 | is set, the default path (./etc/) is overridden. |
| 54 | |
| 55 | TEMPEST_CONFIG |
| 56 | -------------- |
| 57 | Name of a tempest configuration file. If the variable is specified, the default |
| 58 | name (tempest.conf) is overridden. |
| 59 | |
| 60 | """ |
| 61 | |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 62 | import argparse |
Andreas Jaeger | 2f27324 | 2020-05-05 13:27:32 +0200 | [diff] [blame] | 63 | import configparser |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 64 | import os |
Brant Knudson | 28de8d5 | 2016-03-18 13:50:02 -0500 | [diff] [blame] | 65 | import re |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 66 | import sys |
David Paterson | e45aa84 | 2015-11-18 16:12:27 -0800 | [diff] [blame] | 67 | import traceback |
Andreas Jaeger | 2f27324 | 2020-05-05 13:27:32 +0200 | [diff] [blame] | 68 | from urllib import parse as urlparse |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 69 | |
David Paterson | e45aa84 | 2015-11-18 16:12:27 -0800 | [diff] [blame] | 70 | from cliff import command |
| 71 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 72 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 4f30eb8 | 2014-01-07 21:04:49 +0000 | [diff] [blame] | 73 | |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 74 | from tempest import clients |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 75 | from tempest.common import credentials_factory as credentials |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 76 | from tempest import config |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 77 | import tempest.lib.common.http |
Andrea Frittoli | b33dd46 | 2017-07-21 10:14:25 +0100 | [diff] [blame] | 78 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 79 | |
| 80 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 81 | CONF = config.CONF |
David Kranz | f20ac32 | 2014-05-02 16:46:15 -0400 | [diff] [blame] | 82 | CONF_PARSER = None |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 83 | |
David Paterson | e45aa84 | 2015-11-18 16:12:27 -0800 | [diff] [blame] | 84 | LOG = logging.getLogger(__name__) |
| 85 | |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 86 | |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 87 | def _get_config_file(): |
Martin Kopec | bd9dd8e | 2017-06-28 20:52:18 +0000 | [diff] [blame] | 88 | config_dir = os.getcwd() |
| 89 | default_config_dir = os.path.join(config_dir, "etc") |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 90 | default_config_file = "tempest.conf" |
| 91 | |
| 92 | conf_dir = os.environ.get('TEMPEST_CONFIG_DIR', default_config_dir) |
| 93 | conf_file = os.environ.get('TEMPEST_CONFIG', default_config_file) |
| 94 | path = os.path.join(conf_dir, conf_file) |
Sirushti Murugesan | 12dc973 | 2016-07-13 22:49:17 +0530 | [diff] [blame] | 95 | fd = open(path, 'r+') |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 96 | return fd |
| 97 | |
| 98 | |
| 99 | def change_option(option, group, value): |
David Kranz | f20ac32 | 2014-05-02 16:46:15 -0400 | [diff] [blame] | 100 | if not CONF_PARSER.has_section(group): |
| 101 | CONF_PARSER.add_section(group) |
| 102 | CONF_PARSER.set(group, option, str(value)) |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def print_and_or_update(option, group, value, update): |
| 106 | print('Config option %s in group %s should be changed to: %s' |
| 107 | % (option, group, value)) |
| 108 | if update: |
| 109 | change_option(option, group, value) |
| 110 | |
| 111 | |
David Kranz | 0df154d | 2015-06-02 17:02:27 -0400 | [diff] [blame] | 112 | def contains_version(prefix, versions): |
| 113 | return any([x for x in versions if x.startswith(prefix)]) |
| 114 | |
| 115 | |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 116 | def verify_glance_api_versions(os, update): |
Matthew Treinish | 99afd07 | 2013-10-22 18:03:06 +0000 | [diff] [blame] | 117 | # Check glance api versions |
Andrea Frittoli | b33dd46 | 2017-07-21 10:14:25 +0100 | [diff] [blame] | 118 | # Since we want to verify that the configuration is correct, we cannot |
| 119 | # rely on a specific version of the API being available. |
| 120 | try: |
| 121 | _, versions = os.image_v1.ImagesClient().get_versions() |
| 122 | except lib_exc.NotFound: |
| 123 | # If not found, we use v2. The assumption is that either v1 or v2 |
| 124 | # are available, since glance is marked as available in the catalog. |
| 125 | # If not, glance should be disabled in Tempest conf. |
| 126 | try: |
| 127 | versions = os.image_v2.VersionsClient().list_versions()['versions'] |
| 128 | versions = [x['id'] for x in versions] |
| 129 | except lib_exc.NotFound: |
| 130 | msg = ('Glance is available in the catalog, but no known version, ' |
| 131 | '(v1.x or v2.x) of Glance could be found, so Glance should ' |
| 132 | 'be configured as not available') |
| 133 | LOG.warn(msg) |
| 134 | print_and_or_update('glance', 'service-available', False, update) |
| 135 | return |
| 136 | |
David Kranz | 0df154d | 2015-06-02 17:02:27 -0400 | [diff] [blame] | 137 | if CONF.image_feature_enabled.api_v1 != contains_version('v1.', versions): |
Nikita Gerasimov | 35fbdc1 | 2015-08-07 19:58:24 +0300 | [diff] [blame] | 138 | print_and_or_update('api_v1', 'image-feature-enabled', |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 139 | not CONF.image_feature_enabled.api_v1, update) |
David Kranz | 0df154d | 2015-06-02 17:02:27 -0400 | [diff] [blame] | 140 | if CONF.image_feature_enabled.api_v2 != contains_version('v2.', versions): |
Nikita Gerasimov | 35fbdc1 | 2015-08-07 19:58:24 +0300 | [diff] [blame] | 141 | print_and_or_update('api_v2', 'image-feature-enabled', |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 142 | not CONF.image_feature_enabled.api_v2, update) |
Matthew Treinish | 99afd07 | 2013-10-22 18:03:06 +0000 | [diff] [blame] | 143 | |
| 144 | |
Brant Knudson | 28de8d5 | 2016-03-18 13:50:02 -0500 | [diff] [blame] | 145 | def _remove_version_project(url_path): |
| 146 | # The regex matches strings like /v2.0, /v3/, /v2.1/project-id/ |
| 147 | return re.sub(r'/v\d+(\.\d+)?(/[^/]+)?', '', url_path) |
| 148 | |
| 149 | |
Matthew Treinish | 9b89624 | 2014-04-23 21:25:27 +0000 | [diff] [blame] | 150 | def _get_unversioned_endpoint(base_url): |
| 151 | endpoint_parts = urlparse.urlparse(base_url) |
Brant Knudson | 28de8d5 | 2016-03-18 13:50:02 -0500 | [diff] [blame] | 152 | new_path = _remove_version_project(endpoint_parts.path) |
| 153 | endpoint_parts = endpoint_parts._replace(path=new_path) |
| 154 | endpoint = urlparse.urlunparse(endpoint_parts) |
Matthew Treinish | 9b89624 | 2014-04-23 21:25:27 +0000 | [diff] [blame] | 155 | return endpoint |
| 156 | |
| 157 | |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 158 | def _get_api_versions(os, service): |
Andrea Frittoli | b33dd46 | 2017-07-21 10:14:25 +0100 | [diff] [blame] | 159 | # Clients are used to obtain the base_url. Each client applies the |
| 160 | # appropriate filters to the catalog to extract a base_url which |
| 161 | # matches the configured region and endpoint_type. |
| 162 | # The base URL is used to obtain the list of versions available. |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 163 | client_dict = { |
Andrea Frittoli | b33dd46 | 2017-07-21 10:14:25 +0100 | [diff] [blame] | 164 | 'nova': os.compute.ServersClient(), |
| 165 | 'keystone': os.identity_v3.IdentityClient( |
| 166 | endpoint_type=CONF.identity.v3_endpoint_type), |
| 167 | 'cinder': os.volume_v3.VolumesClient(), |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 168 | } |
Ivan Kolodyazhny | b24f904 | 2017-06-12 18:54:18 +0300 | [diff] [blame] | 169 | if service != 'keystone' and service != 'cinder': |
| 170 | # Since keystone and cinder may be listening on a path, |
| 171 | # do not remove the path. |
Brant Knudson | 28de8d5 | 2016-03-18 13:50:02 -0500 | [diff] [blame] | 172 | client_dict[service].skip_path() |
Matthew Treinish | 9b89624 | 2014-04-23 21:25:27 +0000 | [diff] [blame] | 173 | endpoint = _get_unversioned_endpoint(client_dict[service].base_url) |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 174 | |
| 175 | http = tempest.lib.common.http.ClosingHttp( |
Daniel Mellado | cad3f3d | 2016-08-19 14:17:16 +0000 | [diff] [blame] | 176 | CONF.identity.disable_ssl_certificate_validation, |
| 177 | CONF.identity.ca_certificates_file) |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 178 | |
| 179 | __, body = http.request(endpoint, 'GET') |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 180 | client_dict[service].reset_path() |
Brant Knudson | 5a59f87 | 2016-03-18 13:07:00 -0500 | [diff] [blame] | 181 | try: |
| 182 | body = json.loads(body) |
| 183 | except ValueError: |
| 184 | LOG.error( |
| 185 | 'Failed to get a JSON response from unversioned endpoint %s ' |
| 186 | '(versioned endpoint was %s). Response is:\n%s', |
| 187 | endpoint, client_dict[service].base_url, body[:100]) |
| 188 | raise |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 189 | if service == 'keystone': |
| 190 | versions = map(lambda x: x['id'], body['versions']['values']) |
| 191 | else: |
| 192 | versions = map(lambda x: x['id'], body['versions']) |
Matthew Treinish | 0948724 | 2015-05-10 12:43:58 -0400 | [diff] [blame] | 193 | return list(versions) |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 194 | |
| 195 | |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 196 | def verify_keystone_api_versions(os, update): |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 197 | # Check keystone api versions |
| 198 | versions = _get_api_versions(os, 'keystone') |
David Kranz | 0df154d | 2015-06-02 17:02:27 -0400 | [diff] [blame] | 199 | if (CONF.identity_feature_enabled.api_v3 != |
| 200 | contains_version('v3.', versions)): |
Nikita Gerasimov | 35fbdc1 | 2015-08-07 19:58:24 +0300 | [diff] [blame] | 201 | print_and_or_update('api_v3', 'identity-feature-enabled', |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 202 | not CONF.identity_feature_enabled.api_v3, update) |
Matthew Treinish | 864fe07 | 2014-03-02 03:47:26 +0000 | [diff] [blame] | 203 | |
| 204 | |
Adam Gandelman | 03af556 | 2014-10-07 12:22:48 -0700 | [diff] [blame] | 205 | def verify_api_versions(os, service, update): |
| 206 | verify = { |
Adam Gandelman | 03af556 | 2014-10-07 12:22:48 -0700 | [diff] [blame] | 207 | 'glance': verify_glance_api_versions, |
| 208 | 'keystone': verify_keystone_api_versions, |
Adam Gandelman | 03af556 | 2014-10-07 12:22:48 -0700 | [diff] [blame] | 209 | } |
| 210 | if service not in verify: |
| 211 | return |
| 212 | verify[service](os, update) |
| 213 | |
| 214 | |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 215 | def get_extension_client(os, service): |
| 216 | extensions_client = { |
Andrea Frittoli | b33dd46 | 2017-07-21 10:14:25 +0100 | [diff] [blame] | 217 | 'nova': os.compute.ExtensionsClient(), |
| 218 | 'neutron': os.network.ExtensionsClient(), |
Andrea Frittoli | 986407d | 2017-10-11 10:23:17 +0000 | [diff] [blame] | 219 | 'swift': os.object_storage.CapabilitiesClient(), |
Ken'ichi Ohmichi | 8b876dd | 2017-05-04 14:30:31 -0700 | [diff] [blame] | 220 | # NOTE: Cinder v3 API is current and v2 and v1 are deprecated. |
| 221 | # V3 extension API is the same as v2, so we reuse the v2 client |
| 222 | # for v3 API also. |
Andrea Frittoli | b33dd46 | 2017-07-21 10:14:25 +0100 | [diff] [blame] | 223 | 'cinder': os.volume_v2.ExtensionsClient(), |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 224 | } |
Ivan Kolodyazhny | bcfc32e | 2015-08-06 13:31:36 +0300 | [diff] [blame] | 225 | |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 226 | if service not in extensions_client: |
| 227 | print('No tempest extensions client for %s' % service) |
caoyue | ab33302 | 2016-01-25 16:45:21 +0800 | [diff] [blame] | 228 | sys.exit(1) |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 229 | return extensions_client[service] |
| 230 | |
| 231 | |
| 232 | def get_enabled_extensions(service): |
| 233 | extensions_options = { |
| 234 | 'nova': CONF.compute_feature_enabled.api_extensions, |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 235 | 'cinder': CONF.volume_feature_enabled.api_extensions, |
Matthew Treinish | 8c6706d | 2014-01-07 19:28:18 +0000 | [diff] [blame] | 236 | 'neutron': CONF.network_feature_enabled.api_extensions, |
Matthew Treinish | c0120ba | 2014-01-31 20:10:19 +0000 | [diff] [blame] | 237 | 'swift': CONF.object_storage_feature_enabled.discoverable_apis, |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 238 | } |
| 239 | if service not in extensions_options: |
| 240 | print('No supported extensions list option for %s' % service) |
caoyue | ab33302 | 2016-01-25 16:45:21 +0800 | [diff] [blame] | 241 | sys.exit(1) |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 242 | return extensions_options[service] |
| 243 | |
| 244 | |
| 245 | def verify_extensions(os, service, results): |
| 246 | extensions_client = get_extension_client(os, service) |
David Kranz | 5cf4ba4 | 2015-02-10 14:00:50 -0500 | [diff] [blame] | 247 | if service != 'swift': |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 248 | resp = extensions_client.list_extensions() |
| 249 | else: |
ghanshyam | 17d8a48 | 2017-07-21 03:10:48 +0000 | [diff] [blame] | 250 | resp = extensions_client.list_capabilities() |
Matthew Treinish | 54176ce | 2014-12-08 21:28:05 +0000 | [diff] [blame] | 251 | # For Nova, Cinder and Neutron we use the alias name rather than the |
| 252 | # 'name' field because the alias is considered to be the canonical |
| 253 | # name. |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 254 | if isinstance(resp, dict): |
Matthew Treinish | 54176ce | 2014-12-08 21:28:05 +0000 | [diff] [blame] | 255 | if service == 'swift': |
Matthew Treinish | c0120ba | 2014-01-31 20:10:19 +0000 | [diff] [blame] | 256 | # Remove Swift general information from extensions list |
| 257 | resp.pop('swift') |
| 258 | extensions = resp.keys() |
Matthew Treinish | 8c6706d | 2014-01-07 19:28:18 +0000 | [diff] [blame] | 259 | else: |
Matthew Treinish | 54176ce | 2014-12-08 21:28:05 +0000 | [diff] [blame] | 260 | extensions = map(lambda x: x['alias'], resp['extensions']) |
Matthew Treinish | 8c6706d | 2014-01-07 19:28:18 +0000 | [diff] [blame] | 261 | |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 262 | else: |
Matthew Treinish | 54176ce | 2014-12-08 21:28:05 +0000 | [diff] [blame] | 263 | extensions = map(lambda x: x['alias'], resp) |
Matthew Treinish | 0948724 | 2015-05-10 12:43:58 -0400 | [diff] [blame] | 264 | extensions = list(extensions) |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 265 | if not results.get(service): |
| 266 | results[service] = {} |
| 267 | extensions_opt = get_enabled_extensions(service) |
Martin Kopec | d7e05dd | 2018-08-14 09:20:48 +0000 | [diff] [blame] | 268 | if not extensions_opt: |
| 269 | LOG.info("'%s' has no api_extensions set.", service) |
| 270 | return results |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 271 | if extensions_opt[0] == 'all': |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 272 | results[service]['extensions'] = extensions |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 273 | return results |
| 274 | # Verify that all configured extensions are actually enabled |
| 275 | for extension in extensions_opt: |
| 276 | results[service][extension] = extension in extensions |
| 277 | # Verify that there aren't additional extensions enabled that aren't |
| 278 | # specified in the config list |
| 279 | for extension in extensions: |
| 280 | if extension not in extensions_opt: |
| 281 | results[service][extension] = False |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 282 | return results |
| 283 | |
| 284 | |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 285 | def display_results(results, update, replace): |
| 286 | update_dict = { |
| 287 | 'swift': 'object-storage-feature-enabled', |
| 288 | 'nova': 'compute-feature-enabled', |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 289 | 'cinder': 'volume-feature-enabled', |
| 290 | 'neutron': 'network-feature-enabled', |
| 291 | } |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 292 | for service in results: |
| 293 | # If all extensions are specified as being enabled there is no way to |
| 294 | # verify this so we just assume this to be true |
| 295 | if results[service].get('extensions'): |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 296 | if replace: |
| 297 | output_list = results[service].get('extensions') |
| 298 | else: |
| 299 | output_list = ['all'] |
| 300 | else: |
| 301 | extension_list = get_enabled_extensions(service) |
| 302 | output_list = [] |
| 303 | for extension in results[service]: |
| 304 | if not results[service][extension]: |
| 305 | if extension in extension_list: |
| 306 | print("%s extension: %s should not be included in the " |
| 307 | "list of enabled extensions" % (service, |
| 308 | extension)) |
| 309 | else: |
| 310 | print("%s extension: %s should be included in the list" |
| 311 | " of enabled extensions" % (service, extension)) |
| 312 | output_list.append(extension) |
Matthew Treinish | 8b006d2 | 2014-01-07 15:37:20 +0000 | [diff] [blame] | 313 | else: |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 314 | output_list.append(extension) |
| 315 | if update: |
| 316 | # Sort List |
| 317 | output_list.sort() |
| 318 | # Convert list to a string |
| 319 | output_string = ', '.join(output_list) |
| 320 | if service == 'swift': |
| 321 | change_option('discoverable_apis', update_dict[service], |
| 322 | output_string) |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 323 | else: |
| 324 | change_option('api_extensions', update_dict[service], |
| 325 | output_string) |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 326 | |
| 327 | |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 328 | def check_service_availability(os, update): |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 329 | services = [] |
| 330 | avail_services = [] |
| 331 | codename_match = { |
| 332 | 'volume': 'cinder', |
| 333 | 'network': 'neutron', |
| 334 | 'image': 'glance', |
| 335 | 'object_storage': 'swift', |
| 336 | 'compute': 'nova', |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 337 | 'baremetal': 'ironic', |
Matthew Treinish | 42d50f6 | 2014-04-11 19:47:13 +0000 | [diff] [blame] | 338 | 'identity': 'keystone', |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 339 | } |
| 340 | # Get catalog list for endpoints to use for validation |
David Kranz | 4571408 | 2015-04-01 14:47:33 -0400 | [diff] [blame] | 341 | _token, auth_data = os.auth_provider.get_auth() |
David Kranz | 799eee1 | 2015-04-08 11:18:19 -0400 | [diff] [blame] | 342 | if os.auth_version == 'v2': |
| 343 | catalog_key = 'serviceCatalog' |
| 344 | else: |
| 345 | catalog_key = 'catalog' |
| 346 | for entry in auth_data[catalog_key]: |
David Kranz | 4571408 | 2015-04-01 14:47:33 -0400 | [diff] [blame] | 347 | services.append(entry['type']) |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 348 | # Pull all catalog types from config file and compare against endpoint list |
| 349 | for cfgname in dir(CONF._config): |
| 350 | cfg = getattr(CONF, cfgname) |
| 351 | catalog_type = getattr(cfg, 'catalog_type', None) |
| 352 | if not catalog_type: |
| 353 | continue |
Martin Kopec | 07a572c | 2019-01-18 14:23:06 +0000 | [diff] [blame] | 354 | if cfgname == 'identity': |
| 355 | # Keystone is a required service for tempest |
| 356 | continue |
| 357 | if catalog_type not in services: |
| 358 | try: |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 359 | if getattr(CONF.service_available, codename_match[cfgname]): |
| 360 | print('Endpoint type %s not found either disable service ' |
| 361 | '%s or fix the catalog_type in the config file' % ( |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 362 | catalog_type, codename_match[cfgname])) |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 363 | if update: |
| 364 | change_option(codename_match[cfgname], |
| 365 | 'service_available', False) |
Martin Kopec | 07a572c | 2019-01-18 14:23:06 +0000 | [diff] [blame] | 366 | except KeyError: |
| 367 | print('%s is a third party plugin, cannot be verified ' |
| 368 | 'automatically, but it is suggested that it is set to ' |
| 369 | 'False because %s service is not available ' % ( |
| 370 | cfgname, catalog_type)) |
| 371 | else: |
| 372 | try: |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 373 | if not getattr(CONF.service_available, |
| 374 | codename_match[cfgname]): |
| 375 | print('Endpoint type %s is available, service %s should be' |
| 376 | ' set as available in the config file.' % ( |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 377 | catalog_type, codename_match[cfgname])) |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 378 | if update: |
| 379 | change_option(codename_match[cfgname], |
| 380 | 'service_available', True) |
David Kranz | f20ac32 | 2014-05-02 16:46:15 -0400 | [diff] [blame] | 381 | # If we are going to enable this we should allow |
| 382 | # extension checks. |
| 383 | avail_services.append(codename_match[cfgname]) |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 384 | else: |
| 385 | avail_services.append(codename_match[cfgname]) |
Martin Kopec | 07a572c | 2019-01-18 14:23:06 +0000 | [diff] [blame] | 386 | except KeyError: |
| 387 | print('%s is a third party plugin, cannot be verified ' |
| 388 | 'automatically, but it is suggested that it is set to ' |
| 389 | 'True because %s service is available ' % ( |
| 390 | cfgname, catalog_type)) |
Matthew Treinish | 221bd7f | 2014-02-07 21:16:09 +0000 | [diff] [blame] | 391 | return avail_services |
Matthew Treinish | d44fe03 | 2014-01-31 20:07:24 +0000 | [diff] [blame] | 392 | |
| 393 | |
David Paterson | e45aa84 | 2015-11-18 16:12:27 -0800 | [diff] [blame] | 394 | def _parser_add_args(parser): |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 395 | parser.add_argument('-u', '--update', action='store_true', |
| 396 | help='Update the config file with results from api ' |
| 397 | 'queries. This assumes whatever is set in the ' |
| 398 | 'config file is incorrect. In the case of ' |
| 399 | 'endpoint checks where it could either be the ' |
| 400 | 'incorrect catalog type or the service available ' |
| 401 | 'option the service available option is assumed ' |
| 402 | 'to be incorrect and is thus changed') |
| 403 | parser.add_argument('-o', '--output', |
| 404 | help="Output file to write an updated config file to. " |
| 405 | "This has to be a separate file from the " |
| 406 | "original config file. If one isn't specified " |
Martin Kopec | bd9dd8e | 2017-06-28 20:52:18 +0000 | [diff] [blame] | 407 | "with -u the values which should be changed " |
| 408 | "will be printed to STDOUT") |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 409 | parser.add_argument('-r', '--replace-ext', action='store_true', |
| 410 | help="If specified the all option will be replaced " |
| 411 | "with a full list of extensions") |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 412 | |
| 413 | |
David Paterson | e45aa84 | 2015-11-18 16:12:27 -0800 | [diff] [blame] | 414 | def parse_args(): |
| 415 | parser = argparse.ArgumentParser() |
| 416 | _parser_add_args(parser) |
| 417 | opts = parser.parse_args() |
| 418 | return opts |
| 419 | |
| 420 | |
| 421 | def main(opts=None): |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 422 | update = opts.update |
| 423 | replace = opts.replace_ext |
David Kranz | f20ac32 | 2014-05-02 16:46:15 -0400 | [diff] [blame] | 424 | global CONF_PARSER |
| 425 | |
Matthew Treinish | f097171 | 2014-04-11 20:08:53 +0000 | [diff] [blame] | 426 | if update: |
David Kranz | f20ac32 | 2014-05-02 16:46:15 -0400 | [diff] [blame] | 427 | conf_file = _get_config_file() |
Andreas Jaeger | 2f27324 | 2020-05-05 13:27:32 +0200 | [diff] [blame] | 428 | CONF_PARSER = configparser.ConfigParser() |
David Kranz | f20ac32 | 2014-05-02 16:46:15 -0400 | [diff] [blame] | 429 | CONF_PARSER.optionxform = str |
Andreas Jaeger | 2f27324 | 2020-05-05 13:27:32 +0200 | [diff] [blame] | 430 | CONF_PARSER.read_file(conf_file) |
Brad Behle | 43e4fd8 | 2016-04-13 17:15:21 -0500 | [diff] [blame] | 431 | |
| 432 | # Indicate not to create network resources as part of getting credentials |
| 433 | net_resources = { |
| 434 | 'network': False, |
| 435 | 'router': False, |
| 436 | 'subnet': False, |
| 437 | 'dhcp': False |
| 438 | } |
| 439 | icreds = credentials.get_credentials_provider( |
| 440 | 'verify_tempest_config', network_resources=net_resources) |
David Kranz | 5fcac94 | 2015-05-08 17:43:45 -0400 | [diff] [blame] | 441 | try: |
Andrea Frittoli (andreaf) | 848c4a1 | 2016-06-09 11:09:02 +0100 | [diff] [blame] | 442 | os = clients.Manager(icreds.get_primary_creds().credentials) |
David Kranz | 5fcac94 | 2015-05-08 17:43:45 -0400 | [diff] [blame] | 443 | services = check_service_availability(os, update) |
| 444 | results = {} |
| 445 | for service in ['nova', 'cinder', 'neutron', 'swift']: |
| 446 | if service not in services: |
| 447 | continue |
| 448 | results = verify_extensions(os, service, results) |
Adam Gandelman | 03af556 | 2014-10-07 12:22:48 -0700 | [diff] [blame] | 449 | |
David Kranz | 5fcac94 | 2015-05-08 17:43:45 -0400 | [diff] [blame] | 450 | # Verify API versions of all services in the keystone catalog and |
| 451 | # keystone itself. |
| 452 | services.append('keystone') |
| 453 | for service in services: |
| 454 | verify_api_versions(os, service, update) |
Adam Gandelman | 03af556 | 2014-10-07 12:22:48 -0700 | [diff] [blame] | 455 | |
David Kranz | 5fcac94 | 2015-05-08 17:43:45 -0400 | [diff] [blame] | 456 | display_results(results, update, replace) |
| 457 | if update: |
| 458 | conf_file.close() |
zhang.lei | a4b1cef | 2016-03-01 10:50:01 +0800 | [diff] [blame] | 459 | if opts.output: |
| 460 | with open(opts.output, 'w+') as outfile: |
| 461 | CONF_PARSER.write(outfile) |
David Kranz | 5fcac94 | 2015-05-08 17:43:45 -0400 | [diff] [blame] | 462 | finally: |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 463 | icreds.clear_creds() |
Matthew Treinish | 1f7b33d | 2013-10-21 18:07:02 +0000 | [diff] [blame] | 464 | |
| 465 | |
David Paterson | e45aa84 | 2015-11-18 16:12:27 -0800 | [diff] [blame] | 466 | class TempestVerifyConfig(command.Command): |
| 467 | """Verify your current tempest configuration""" |
| 468 | |
| 469 | def get_parser(self, prog_name): |
| 470 | parser = super(TempestVerifyConfig, self).get_parser(prog_name) |
| 471 | _parser_add_args(parser) |
| 472 | return parser |
| 473 | |
| 474 | def take_action(self, parsed_args): |
| 475 | try: |
Masayuki Igawa | 9262943 | 2016-06-09 12:28:09 +0900 | [diff] [blame] | 476 | main(parsed_args) |
David Paterson | e45aa84 | 2015-11-18 16:12:27 -0800 | [diff] [blame] | 477 | except Exception: |
| 478 | LOG.exception("Failure verifying configuration.") |
| 479 | traceback.print_exc() |
| 480 | raise |