2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
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
9 ##############################################################################
10 '''This file realize the function of how to parser a config file.
11 This contain Two part:
12 Frist is Init some variables the will be used.
13 Second is reading config file.'''
19 from pyroute2 import IPDB
20 import utils.infra_setup.runner.docker_env as docker_env
25 bottlenecks_config = {}
30 cls.code_dir = os.path.dirname(os.path.abspath(__file__))
31 cls.root_dir = os.path.dirname(cls.code_dir)
32 cls.test_dir = os.path.join(cls.root_dir, 'testsuites')
33 config_dir = os.path.join(
38 with open(config_dir) as file:
39 config_info = yaml.load(file)
40 common_config = config_info['common_config']
41 cls.bottlenecks_config["releng_dir"] = common_config["releng_dir"]
42 cls.bottlenecks_config["fetch_os"] = common_config["fetch_os_file"]
43 cls.bottlenecks_config["log_dir"] = common_config['log_dir']
44 cls.bottlenecks_config["rc_dir"] = common_config['rc_dir']
45 cls.bottlenecks_config["yardstick_rc_dir"] = \
46 common_config['yardstick_rc_dir']
47 cls.config_dir_check(cls.bottlenecks_config["log_dir"])
50 def story_read(cls, testcase, story_name):
51 story_dir = os.path.join(
56 with open(story_dir) as file:
57 story_parser = yaml.load(file)
58 for case_name in story_parser['testcase']:
59 Parser.testcase_read(testcase, case_name)
61 return cls.bottlenecks_test
64 def testcase_read(cls, testcase, testcase_name):
66 testcase_dir = os.path.join(
71 testcase_local = testcase_dir + ".yaml"
72 with open(testcase_local) as f:
73 cls.bottlenecks_test[testcase_name] = yaml.load(f)
75 return cls.bottlenecks_test
78 def config_dir_check(cls, dirname):
81 if not os.path.exists(dirname):
85 def testcase_out_dir(cls, testcase):
86 file_time = time.strftime('%H_%M', time.localtime(time.time()))
87 out_name = cls.bottlenecks_config["log_dir"] + testcase + file_time
88 outfile_name = out_name + ".out"
92 def config_parser(testcase_cfg, parameters):
93 test_cfg = testcase_cfg['test_config']
94 stack_cfg = testcase_cfg['stack_config']
95 # TO-DO add cli parameters to stack_config.
96 return test_cfg, stack_cfg
99 def convert_docker_env(config, ip_type):
100 if ip_type is "dashboard":
101 config["contexts"]["dashboard_ip"] = \
102 docker_env.ELK_info["ip"] + ":9200"
103 elif ip_type is "yardstick":
104 config["contexts"]["yardstick_ip"] = \
105 docker_env.yardstick_info["ip"] + ":8888"
108 def ip_parser(ip_type):
110 GATEWAY_IP = ip.routes['default'].gateway
111 if ip_type is "dashboard":
112 TEST_IP = GATEWAY_IP + ":9200"
113 elif ip_type is "yardstick_test_ip":
114 TEST_IP = GATEWAY_IP + ":8888"
118 def result_to_file(data, file_name):
119 with open(file_name, "a") as f:
120 f.write(json.dumps(data, f))
124 class HeatTemplate_Parser():
125 """parser a Heat template and a method to deploy template to a stack"""
129 self.heat_date["resources"] = {}
130 self.heat_date["outputs"] = {}
131 self.heat_date["heat_template_version"] = "2013-05-23"
132 self.heat_date["description"] = {"Stack built by the bottlenecks"
133 " framework for root."}
135 def add_security_group(self, name):
136 """add to the template a Neutron SecurityGroup"""
137 security_name = name + "-security_group"
138 self.heat_date["resources"][security_name] = {
139 "type": "OS::Neutron::SecurityGroup",
141 "name": security_name,
142 "description": "Group allowing icmp and upd/tcp on all ports",
144 {"remote_ip_prefix": "0.0.0.0/0",
146 "port_range_min": "1",
147 "port_range_max": "65535"},
148 {"remote_ip_prefix": "0.0.0.0/0",
150 "port_range_min": "1",
151 "port_range_max": "65535"},
152 {"remote_ip_prefix": "0.0.0.0/0",
158 self.heat_date["outputs"][security_name] = {
159 "description": "ID of Security Group",
160 "value": {"get_resource": security_name}
163 def add_keypair(self, name):
164 """add to the template a Nova KeyPair"""
165 key_name = name + "key"
166 with open(Parser.root_dir +
168 "bottlenecks_key/bottlenecks_key.pub") as f:
169 key_content = f.read()
170 self.heat_date["resources"][key_name] = {
171 "type": "OS::Nova::KeyPair",
174 "public_key": key_content
178 def add_network(self, name):
179 """add to the template a Neutron Net"""
180 network_name = name + "-net"
181 self.heat_date["resources"][network_name] = {
182 "type": "OS::Neutron::Net",
183 "properties": {"name": network_name}
186 def add_subnet(self, name, cidr):
187 """add to the template a Neutron Subnet"""
188 network_name = name + "-net"
189 subnet_name = name + "-subnet"
190 self.heat_date["resources"][subnet_name] = {
191 "type": "OS::Neutron::Subnet",
192 "depends_on": network_name,
196 "network_id": {"get_resource": network_name}
200 self.heat_date["outputs"][subnet_name] = {
201 "description": "subnet %s ID" % subnet_name,
202 "value": {"get_resource": subnet_name}
205 def add_router(self, name, ext_gw_net):
206 """add to the template a Neutron Router and interface"""
207 router_name = name + "-route"
208 subnet_name = name + "-subnet"
210 self.heat_date["resources"][router_name] = {
211 "type": "OS::Neutron::Router",
212 "depends_on": [subnet_name],
215 "external_gateway_info": {
216 "network": ext_gw_net
221 def add_router_interface(self, name):
222 """add to the template a Neutron RouterInterface and interface"""
223 router_name = name + "-route"
224 subnet_name = name + "-subnet"
225 router_if_name = name + "-interface"
227 self.heat_date["resources"][router_if_name] = {
228 "type": "OS::Neutron::RouterInterface",
229 "depends_on": [router_name, subnet_name],
231 "router_id": {"get_resource": router_name},
232 "subnet_id": {"get_resource": subnet_name}
236 def add_server(self, name, image, flavor, user, ports=None):
237 """add to the template a Nova Server"""
239 key_name = "bottlenecks-poscakey"
240 port_name = name + "-port"
241 self.heat_date["resources"][name] = {
242 "type": "OS::Nova::Server",
247 "key_name": {"get_resource": key_name},
250 "port": {"get_resource": port_name}
255 self.heat_date["outputs"][name] = {
256 "description": "VM UUID",
257 "value": {"get_resource": name}
260 def add_port(self, name, stack_name=None):
261 """add to the template a named Neutron Port"""
262 network_name = stack_name + "-net"
263 subnet_name = stack_name + "-subnet"
264 port_name = name + "-port"
265 security_name = stack_name + "-security_group"
267 self.heat_date["resources"][port_name] = {
268 "type": "OS::Neutron::Port",
269 "depends_on": [subnet_name],
272 "fixed_ips": [{"subnet": {"get_resource": subnet_name}}],
273 "network_id": {"get_resource": network_name},
274 "replacement_policy": "AUTO",
275 "security_groups": [{"get_resource": security_name}]
279 self.heat_date["outputs"][port_name] = {
280 "description": "Address for interface %s" % port_name,
281 "value": {"get_attr": [port_name, "fixed_ips", 0, "ip_address"]}
284 def add_floating_ip(self, name, stack_name, network_ext):
285 """add to the template a Nova FloatingIP resource
286 see: https://bugs.launchpad.net/heat/+bug/1299259
288 port_name = name + "-port"
289 floating_ip_name = name + "-floating"
290 router_if_name = stack_name + "-interface"
292 self.heat_date["resources"][floating_ip_name] = {
293 "depends_on": [router_if_name, port_name],
294 "type": "OS::Nova::FloatingIP",
299 self.heat_date['outputs'][floating_ip_name] = {
300 'description': 'floating ip %s' % name,
301 'value': {'get_attr': [name, 'ip']}
304 def add_floating_ip_ass(self, name):
305 """add to the template a Nova FloatingIP resource
306 see: https://bugs.launchpad.net/heat/+bug/1299259
308 port_name = name + "-port"
309 floating_ip_name = name + "-floating"
310 floating_ass = name + "-floating_ass"
312 self.heat_date["resources"][floating_ass] = {
313 "type": 'OS::Neutron::FloatingIPAssociation',
314 "depends_on": [port_name, floating_ip_name],
316 "floatingip_id": {'get_resource': floating_ip_name},
317 "port_id": {"get_resource": port_name}
321 def get_template_date(self):
322 return self.heat_date