Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 1 | # Copyright 2016 Mirantis, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | import copy |
| 15 | import os |
| 16 | import pkg_resources |
| 17 | |
| 18 | from oslo_config import cfg |
| 19 | from oslo_config import generator |
| 20 | |
| 21 | from tcp_tests.helpers import ext |
| 22 | from tcp_tests.helpers import oslo_cfg_types as ct |
| 23 | from tcp_tests import settings |
| 24 | |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 25 | print ("\n" + "-" * 10 + " Initialize oslo.config variables with defaults" |
| 26 | " from environment" + "-" * 10) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 27 | |
| 28 | _default_conf = pkg_resources.resource_filename( |
Dennis Dmitriev | ab83ea4 | 2017-05-12 16:52:45 +0300 | [diff] [blame] | 29 | __name__, 'templates/{0}/underlay.yaml'.format(settings.LAB_CONFIG_NAME)) |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 30 | |
| 31 | _default_salt_steps = pkg_resources.resource_filename( |
Dennis Dmitriev | ab83ea4 | 2017-05-12 16:52:45 +0300 | [diff] [blame] | 32 | __name__, 'templates/{0}/salt.yaml'.format(settings.LAB_CONFIG_NAME)) |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 33 | _default_common_services_steps = pkg_resources.resource_filename( |
| 34 | __name__, |
Dennis Dmitriev | ab83ea4 | 2017-05-12 16:52:45 +0300 | [diff] [blame] | 35 | 'templates/{0}/common-services.yaml'.format( |
Dennis Dmitriev | 2a13a13 | 2016-11-04 00:56:23 +0200 | [diff] [blame] | 36 | settings.LAB_CONFIG_NAME)) |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 37 | _default_openstack_steps = pkg_resources.resource_filename( |
Dennis Dmitriev | ab83ea4 | 2017-05-12 16:52:45 +0300 | [diff] [blame] | 38 | __name__, 'templates/{0}/openstack.yaml'.format( |
Dennis Dmitriev | 2a13a13 | 2016-11-04 00:56:23 +0200 | [diff] [blame] | 39 | settings.LAB_CONFIG_NAME)) |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 40 | _default_opencontrail_prepare_tests_steps_path = pkg_resources.resource_filename( |
Dennis Dmitriev | ab83ea4 | 2017-05-12 16:52:45 +0300 | [diff] [blame] | 41 | __name__, 'templates/{0}/opencontrail.yaml'.format( |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 42 | settings.LAB_CONFIG_NAME)) |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 43 | _default_sl_prepare_tests_steps_path = pkg_resources.resource_filename( |
| 44 | __name__, 'templates/{0}/sl.yaml'.format( |
| 45 | settings.LAB_CONFIG_NAME)) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 46 | |
| 47 | |
| 48 | hardware_opts = [ |
| 49 | ct.Cfg('manager', ct.String(), |
| 50 | help="Hardware manager name", default="devops"), |
| 51 | ct.Cfg('conf_path', ct.String(), |
| 52 | help="Hardware config file", default=_default_conf), |
| 53 | ct.Cfg('current_snapshot', ct.String(), |
| 54 | help="Latest environment status name", |
| 55 | default=ext.SNAPSHOT.hardware), |
| 56 | ] |
| 57 | |
| 58 | |
| 59 | underlay_opts = [ |
| 60 | ct.Cfg('ssh', ct.JSONList(), |
| 61 | help="""SSH Settings for Underlay: [{ |
| 62 | 'node_name': node1, |
Dennis Dmitriev | 23e5d19 | 2016-10-31 11:27:10 +0200 | [diff] [blame] | 63 | 'roles': ['salt-master', 'salt-minion', ], |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 64 | 'host': hostname, |
| 65 | 'login': login, |
| 66 | 'password': password, |
| 67 | 'address_pool': (optional), |
| 68 | 'port': (optional), |
| 69 | 'keys': [(optional)], |
| 70 | }, ...]""", default=[]), |
| 71 | ct.Cfg('roles', ct.JSONList(), |
| 72 | help="Node roles managed by underlay in the environment", |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 73 | default=[ext.UNDERLAY_NODE_ROLES.salt_master, |
| 74 | ext.UNDERLAY_NODE_ROLES.salt_minion, ]), |
Dennis Dmitriev | 7b9538f | 2017-05-15 17:01:34 +0300 | [diff] [blame] | 75 | ct.Cfg('bootstrap_timeout', ct.Integer(), |
| 76 | help="Timeout of waiting SSH for nodes with specified roles", |
| 77 | default=480), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 78 | ct.Cfg('nameservers', ct.JSONList(), |
| 79 | help="IP addresses of DNS servers", |
| 80 | default=[]), |
| 81 | ct.Cfg('upstream_dns_servers', ct.JSONList(), |
| 82 | help="IP addresses of upstream DNS servers (dnsmasq)", |
| 83 | default=[]), |
| 84 | ct.Cfg('lvm', ct.JSONDict(), |
| 85 | help="LVM settings for Underlay", default={}), |
Dennis Dmitriev | 99b26fe | 2017-04-26 12:34:44 +0300 | [diff] [blame] | 86 | ct.Cfg('address_pools', ct.JSONDict(), |
| 87 | help="""Address pools (dynamically) allocated for the environment. |
| 88 | May be used to determine CIDR for a specific network from |
| 89 | tests or during the deployment process. |
| 90 | {'pool_name1': '<cidr>', 'pool_name2': '<cidr>', ...}""", |
| 91 | default={}), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 92 | ] |
| 93 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 94 | |
| 95 | salt_deploy_opts = [ |
| 96 | ct.Cfg('salt_steps_path', ct.String(), |
| 97 | help="Path to YAML with steps to deploy salt", |
| 98 | default=_default_salt_steps), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 99 | ] |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 100 | salt_opts = [ |
| 101 | ct.Cfg('salt_master_host', ct.IPAddress(), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 102 | help="", default='0.0.0.0'), |
Dennis Dmitriev | 2d60c8e | 2017-05-12 18:34:01 +0300 | [diff] [blame] | 103 | ct.Cfg('salt_master_port', ct.String(), |
| 104 | help="", default='6969'), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 105 | ] |
| 106 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 107 | common_services_deploy_opts = [ |
| 108 | ct.Cfg('common_services_steps_path', ct.String(), |
| 109 | help="Path to YAML with steps to deploy common services", |
| 110 | default=_default_common_services_steps), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 111 | ] |
| 112 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 113 | common_services_opts = [ |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 114 | ct.Cfg('common_services_installed', ct.Boolean(), |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 115 | help="", default=False), |
| 116 | ] |
| 117 | |
| 118 | openstack_deploy_opts = [ |
| 119 | ct.Cfg('openstack_steps_path', ct.String(), |
| 120 | help="Path to YAML with steps to deploy openstack", |
| 121 | default=_default_openstack_steps), |
| 122 | ] |
| 123 | openstack_opts = [ |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 124 | ct.Cfg('openstack_installed', ct.Boolean(), |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 125 | help="", default=False), |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 126 | ct.Cfg('openstack_keystone_endpoint', ct.String(), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 127 | help="", default=''), |
| 128 | ] |
| 129 | |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 130 | opencontrail_opts = [ |
| 131 | ct.Cfg('opencontrail_tags', ct.String(), |
| 132 | help="", default=''), |
| 133 | ct.Cfg('opencontrail_features', ct.String(), |
| 134 | help="", default=''), |
| 135 | ct.Cfg('opencontrail_prepare_tests_steps_path', ct.String(), |
| 136 | help="Path to YAML with steps to prepare contrail-tests", |
| 137 | default=_default_opencontrail_prepare_tests_steps_path), |
| 138 | ] |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 139 | |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 140 | sl_deploy_opts = [ |
| 141 | ct.Cfg('sl_steps_path', ct.String(), |
| 142 | help="Path to YAML with steps to deploy sl", |
| 143 | default=_default_sl_prepare_tests_steps_path), |
| 144 | ] |
| 145 | sl_opts = [ |
| 146 | ct.Cfg('sl_installed', ct.Boolean(), |
| 147 | help="", default=False), |
| 148 | ] |
| 149 | |
| 150 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 151 | _group_opts = [ |
| 152 | ('hardware', hardware_opts), |
| 153 | ('underlay', underlay_opts), |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 154 | ('salt_deploy', salt_deploy_opts), |
| 155 | ('salt', salt_opts), |
| 156 | ('common_services_deploy', common_services_deploy_opts), |
| 157 | ('common_services', common_services_opts), |
| 158 | ('openstack_deploy', openstack_deploy_opts), |
| 159 | ('openstack', openstack_opts), |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 160 | ('opencontrail', opencontrail_opts), |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 161 | ('stack_light', sl_opts), |
| 162 | ('sl_deploy', sl_deploy_opts), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 163 | ] |
| 164 | |
| 165 | |
| 166 | def register_opts(config): |
| 167 | config.register_group(cfg.OptGroup(name='hardware', |
| 168 | title="Hardware settings", help="")) |
| 169 | config.register_opts(group='hardware', opts=hardware_opts) |
| 170 | |
| 171 | config.register_group(cfg.OptGroup(name='underlay', |
| 172 | title="Underlay configuration", help="")) |
| 173 | config.register_opts(group='underlay', opts=underlay_opts) |
| 174 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 175 | config.register_group(cfg.OptGroup(name='salt_deploy', |
| 176 | title="salt deploy configuration", help="")) |
| 177 | config.register_opts(group='salt_deploy', opts=salt_deploy_opts) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 178 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 179 | config.register_group(cfg.OptGroup(name='salt', |
| 180 | title="salt config and credentials", help="")) |
| 181 | config.register_opts(group='salt', opts=salt_opts) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 182 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 183 | config.register_group(cfg.OptGroup(name='common_services', |
| 184 | title="Common services for Openstack", help="")) |
| 185 | config.register_opts(group='common_services', opts=common_services_opts) |
| 186 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 187 | config.register_group( |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 188 | cfg.OptGroup(name='common_services_deploy', |
| 189 | title="Common services for Openstack deploy config", |
| 190 | help="")) |
| 191 | config.register_opts(group='common_services_deploy', |
| 192 | opts=common_services_deploy_opts) |
| 193 | |
| 194 | config.register_group(cfg.OptGroup(name='openstack', |
| 195 | title="Openstack config and credentials", help="")) |
| 196 | config.register_opts(group='openstack', opts=openstack_opts) |
| 197 | |
| 198 | config.register_group( |
| 199 | cfg.OptGroup(name='openstack_deploy', |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 200 | title="Openstack deploy config and credentials", |
| 201 | help="")) |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 202 | config.register_opts(group='openstack_deploy', opts=openstack_deploy_opts) |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 203 | |
| 204 | config.register_group(cfg.OptGroup(name='opencontrail', |
| 205 | title="Options for Juniper contrail-tests", help="")) |
| 206 | config.register_opts(group='opencontrail', opts=opencontrail_opts) |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 207 | config.register_group(cfg.OptGroup(name='stack_light', |
| 208 | title="StackLight config and credentials", help="")) |
Tatyana Leontovich | 5acc82a | 2017-05-23 15:41:35 +0300 | [diff] [blame^] | 209 | config.register_opts(group='stack_light', opts=sl_opts) |
Tatyana Leontovich | c8b8ca2 | 2017-05-19 13:37:05 +0300 | [diff] [blame] | 210 | config.register_group( |
| 211 | cfg.OptGroup(name='sl_deploy', |
| 212 | title="SL deploy config and credentials", |
| 213 | help="")) |
| 214 | config.register_opts(group='sl_deploy', opts=sl_deploy_opts) |
| 215 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 216 | return config |
| 217 | |
| 218 | |
| 219 | def load_config(config_files): |
| 220 | config = cfg.CONF |
| 221 | register_opts(config) |
| 222 | config(args=[], default_config_files=config_files) |
| 223 | return config |
| 224 | |
| 225 | |
| 226 | def reload_snapshot_config(config, test_config_path): |
| 227 | """Reset config to the state from test_config file""" |
| 228 | config(args=[], default_config_files=[test_config_path]) |
| 229 | return config |
| 230 | |
| 231 | |
| 232 | def list_opts(): |
| 233 | """Return a list of oslo.config options available in the tcp_tests. |
| 234 | """ |
| 235 | return [(group, copy.deepcopy(opts)) for group, opts in _group_opts] |
| 236 | |
| 237 | |
| 238 | def list_current_opts(config): |
| 239 | """Return a list of oslo.config options available in the tcp_tests. |
| 240 | """ |
| 241 | result_opts = [] |
| 242 | for group, opts in _group_opts: |
| 243 | current_opts = copy.deepcopy(opts) |
| 244 | for opt in current_opts: |
| 245 | if hasattr(config, group): |
| 246 | if hasattr(config[group], opt.name): |
| 247 | opt.default = getattr(config[group], opt.name) |
| 248 | result_opts.append((group, current_opts)) |
| 249 | return result_opts |
| 250 | |
| 251 | |
| 252 | def save_config(config, snapshot_name, env_name=None): |
| 253 | if env_name is None: |
| 254 | env_name = 'config' |
| 255 | test_config_path = os.path.join( |
| 256 | settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name)) |
| 257 | |
| 258 | with open(test_config_path, 'w') as output_file: |
| 259 | formatter = generator._OptFormatter(output_file=output_file) |
| 260 | for group, opts in list_current_opts(config): |
| 261 | formatter.format_group(group) |
| 262 | for opt in opts: |
| 263 | formatter.format(opt, group, minimal=True) |
| 264 | formatter.write('\n') |
| 265 | formatter.write('\n') |