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