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 | 2a13a13 | 2016-11-04 00:56:23 +0200 | [diff] [blame] | 29 | __name__, 'templates/underlay/{0}.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 | 2a13a13 | 2016-11-04 00:56:23 +0200 | [diff] [blame] | 32 | __name__, 'templates/salt/{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 | 2a13a13 | 2016-11-04 00:56:23 +0200 | [diff] [blame] | 35 | 'templates/common-services/{0}-common-services.yaml'.format( |
| 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 | 2a13a13 | 2016-11-04 00:56:23 +0200 | [diff] [blame] | 38 | __name__, 'templates/openstack/{0}-openstack.yaml'.format( |
| 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( |
| 41 | __name__, 'templates/opencontrail/{0}-opencontrail.yaml'.format( |
| 42 | settings.LAB_CONFIG_NAME)) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 43 | |
| 44 | |
| 45 | hardware_opts = [ |
| 46 | ct.Cfg('manager', ct.String(), |
| 47 | help="Hardware manager name", default="devops"), |
| 48 | ct.Cfg('conf_path', ct.String(), |
| 49 | help="Hardware config file", default=_default_conf), |
| 50 | ct.Cfg('current_snapshot', ct.String(), |
| 51 | help="Latest environment status name", |
| 52 | default=ext.SNAPSHOT.hardware), |
| 53 | ] |
| 54 | |
| 55 | |
| 56 | underlay_opts = [ |
| 57 | ct.Cfg('ssh', ct.JSONList(), |
| 58 | help="""SSH Settings for Underlay: [{ |
| 59 | 'node_name': node1, |
Dennis Dmitriev | 23e5d19 | 2016-10-31 11:27:10 +0200 | [diff] [blame] | 60 | 'roles': ['salt-master', 'salt-minion', ], |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 61 | 'host': hostname, |
| 62 | 'login': login, |
| 63 | 'password': password, |
| 64 | 'address_pool': (optional), |
| 65 | 'port': (optional), |
| 66 | 'keys': [(optional)], |
| 67 | }, ...]""", default=[]), |
| 68 | ct.Cfg('roles', ct.JSONList(), |
| 69 | help="Node roles managed by underlay in the environment", |
Dennis Dmitriev | 53d3b77 | 2016-10-18 14:31:58 +0300 | [diff] [blame] | 70 | default=[ext.UNDERLAY_NODE_ROLES.salt_master, |
| 71 | ext.UNDERLAY_NODE_ROLES.salt_minion, ]), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 72 | ct.Cfg('nameservers', ct.JSONList(), |
| 73 | help="IP addresses of DNS servers", |
| 74 | default=[]), |
| 75 | ct.Cfg('upstream_dns_servers', ct.JSONList(), |
| 76 | help="IP addresses of upstream DNS servers (dnsmasq)", |
| 77 | default=[]), |
| 78 | ct.Cfg('lvm', ct.JSONDict(), |
| 79 | help="LVM settings for Underlay", default={}), |
| 80 | ] |
| 81 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 82 | |
| 83 | salt_deploy_opts = [ |
| 84 | ct.Cfg('salt_steps_path', ct.String(), |
| 85 | help="Path to YAML with steps to deploy salt", |
| 86 | default=_default_salt_steps), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 87 | ] |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 88 | salt_opts = [ |
| 89 | ct.Cfg('salt_master_host', ct.IPAddress(), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 90 | help="", default='0.0.0.0'), |
| 91 | ] |
| 92 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 93 | common_services_deploy_opts = [ |
| 94 | ct.Cfg('common_services_steps_path', ct.String(), |
| 95 | help="Path to YAML with steps to deploy common services", |
| 96 | default=_default_common_services_steps), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 97 | ] |
| 98 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 99 | common_services_opts = [ |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 100 | ct.Cfg('common_services_installed', ct.Boolean(), |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 101 | help="", default=False), |
| 102 | ] |
| 103 | |
| 104 | openstack_deploy_opts = [ |
| 105 | ct.Cfg('openstack_steps_path', ct.String(), |
| 106 | help="Path to YAML with steps to deploy openstack", |
| 107 | default=_default_openstack_steps), |
| 108 | ] |
| 109 | openstack_opts = [ |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 110 | ct.Cfg('openstack_installed', ct.Boolean(), |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 111 | help="", default=False), |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 112 | ct.Cfg('openstack_keystone_endpoint', ct.String(), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 113 | help="", default=''), |
| 114 | ] |
| 115 | |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 116 | opencontrail_opts = [ |
| 117 | ct.Cfg('opencontrail_tags', ct.String(), |
| 118 | help="", default=''), |
| 119 | ct.Cfg('opencontrail_features', ct.String(), |
| 120 | help="", default=''), |
| 121 | ct.Cfg('opencontrail_prepare_tests_steps_path', ct.String(), |
| 122 | help="Path to YAML with steps to prepare contrail-tests", |
| 123 | default=_default_opencontrail_prepare_tests_steps_path), |
| 124 | ] |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 125 | |
| 126 | _group_opts = [ |
| 127 | ('hardware', hardware_opts), |
| 128 | ('underlay', underlay_opts), |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 129 | ('salt_deploy', salt_deploy_opts), |
| 130 | ('salt', salt_opts), |
| 131 | ('common_services_deploy', common_services_deploy_opts), |
| 132 | ('common_services', common_services_opts), |
| 133 | ('openstack_deploy', openstack_deploy_opts), |
| 134 | ('openstack', openstack_opts), |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 135 | ('opencontrail', opencontrail_opts), |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 136 | ] |
| 137 | |
| 138 | |
| 139 | def register_opts(config): |
| 140 | config.register_group(cfg.OptGroup(name='hardware', |
| 141 | title="Hardware settings", help="")) |
| 142 | config.register_opts(group='hardware', opts=hardware_opts) |
| 143 | |
| 144 | config.register_group(cfg.OptGroup(name='underlay', |
| 145 | title="Underlay configuration", help="")) |
| 146 | config.register_opts(group='underlay', opts=underlay_opts) |
| 147 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 148 | config.register_group(cfg.OptGroup(name='salt_deploy', |
| 149 | title="salt deploy configuration", help="")) |
| 150 | config.register_opts(group='salt_deploy', opts=salt_deploy_opts) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 151 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 152 | config.register_group(cfg.OptGroup(name='salt', |
| 153 | title="salt config and credentials", help="")) |
| 154 | config.register_opts(group='salt', opts=salt_opts) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 155 | |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 156 | config.register_group(cfg.OptGroup(name='common_services', |
| 157 | title="Common services for Openstack", help="")) |
| 158 | config.register_opts(group='common_services', opts=common_services_opts) |
| 159 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 160 | config.register_group( |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 161 | cfg.OptGroup(name='common_services_deploy', |
| 162 | title="Common services for Openstack deploy config", |
| 163 | help="")) |
| 164 | config.register_opts(group='common_services_deploy', |
| 165 | opts=common_services_deploy_opts) |
| 166 | |
| 167 | config.register_group(cfg.OptGroup(name='openstack', |
| 168 | title="Openstack config and credentials", help="")) |
| 169 | config.register_opts(group='openstack', opts=openstack_opts) |
| 170 | |
| 171 | config.register_group( |
| 172 | cfg.OptGroup(name='openstack_deploy', |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 173 | title="Openstack deploy config and credentials", |
| 174 | help="")) |
Dennis Dmitriev | 6860ba6 | 2016-11-01 21:43:46 +0200 | [diff] [blame] | 175 | config.register_opts(group='openstack_deploy', opts=openstack_deploy_opts) |
Dennis Dmitriev | 2cbf235 | 2016-11-11 15:34:21 +0200 | [diff] [blame] | 176 | |
| 177 | config.register_group(cfg.OptGroup(name='opencontrail', |
| 178 | title="Options for Juniper contrail-tests", help="")) |
| 179 | config.register_opts(group='opencontrail', opts=opencontrail_opts) |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 180 | return config |
| 181 | |
| 182 | |
| 183 | def load_config(config_files): |
| 184 | config = cfg.CONF |
| 185 | register_opts(config) |
| 186 | config(args=[], default_config_files=config_files) |
| 187 | return config |
| 188 | |
| 189 | |
| 190 | def reload_snapshot_config(config, test_config_path): |
| 191 | """Reset config to the state from test_config file""" |
| 192 | config(args=[], default_config_files=[test_config_path]) |
| 193 | return config |
| 194 | |
| 195 | |
| 196 | def list_opts(): |
| 197 | """Return a list of oslo.config options available in the tcp_tests. |
| 198 | """ |
| 199 | return [(group, copy.deepcopy(opts)) for group, opts in _group_opts] |
| 200 | |
| 201 | |
| 202 | def list_current_opts(config): |
| 203 | """Return a list of oslo.config options available in the tcp_tests. |
| 204 | """ |
| 205 | result_opts = [] |
| 206 | for group, opts in _group_opts: |
| 207 | current_opts = copy.deepcopy(opts) |
| 208 | for opt in current_opts: |
| 209 | if hasattr(config, group): |
| 210 | if hasattr(config[group], opt.name): |
| 211 | opt.default = getattr(config[group], opt.name) |
| 212 | result_opts.append((group, current_opts)) |
| 213 | return result_opts |
| 214 | |
| 215 | |
| 216 | def save_config(config, snapshot_name, env_name=None): |
| 217 | if env_name is None: |
| 218 | env_name = 'config' |
| 219 | test_config_path = os.path.join( |
| 220 | settings.LOGS_DIR, '{0}_{1}.ini'.format(env_name, snapshot_name)) |
| 221 | |
| 222 | with open(test_config_path, 'w') as output_file: |
| 223 | formatter = generator._OptFormatter(output_file=output_file) |
| 224 | for group, opts in list_current_opts(config): |
| 225 | formatter.format_group(group) |
| 226 | for opt in opts: |
| 227 | formatter.format(opt, group, minimal=True) |
| 228 | formatter.write('\n') |
| 229 | formatter.write('\n') |