blob: b22ce380134e07aaa488e19a686b09622c4243dd [file] [log] [blame]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03001from __future__ import print_function
2
gstepanov023c1e42015-04-08 15:50:19 +03003import os
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08004import sys
koder aka kdanilove87ae652015-04-20 02:14:35 +03005import time
koder aka kdanilov2c473092015-03-29 17:12:13 +03006import Queue
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08007import pprint
koder aka kdanilove21d7472015-02-14 19:02:04 -08008import logging
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08009import argparse
koder aka kdanilov168f6092015-04-19 02:33:38 +030010import functools
koder aka kdanilov2c473092015-03-29 17:12:13 +030011import threading
koder aka kdanilov168f6092015-04-19 02:33:38 +030012import contextlib
koder aka kdanilov2c473092015-03-29 17:12:13 +030013import collections
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080014
koder aka kdanilov66839a92015-04-11 13:22:31 +030015import yaml
koder aka kdanilov2c473092015-03-29 17:12:13 +030016from concurrent.futures import ThreadPoolExecutor
koder aka kdanilov6c491062015-04-09 22:33:13 +030017
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030018from wally import pretty_yaml
koder aka kdanilove87ae652015-04-20 02:14:35 +030019from wally.sensors_utils import deploy_sensors_stage
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030020from wally.discover import discover, Node, undiscover
21from wally import utils, report, ssh_utils, start_vms
22from wally.suits.itest import IOPerfTest, PgBenchTest
23from wally.config import cfg_dict, load_config, setup_loggers
koder aka kdanilov2c473092015-03-29 17:12:13 +030024
25
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030026logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030027
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080028
Yulia Portnova7ddfa732015-02-24 17:32:58 +020029def format_result(res, formatter):
koder aka kdanilove21d7472015-02-14 19:02:04 -080030 data = "\n{0}\n".format("=" * 80)
31 data += pprint.pformat(res) + "\n"
32 data += "{0}\n".format("=" * 80)
koder aka kdanilovfe056622015-02-19 08:46:15 -080033 templ = "{0}\n\n====> {1}\n\n{2}\n\n"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020034 return templ.format(data, formatter(res), "=" * 80)
koder aka kdanilove21d7472015-02-14 19:02:04 -080035
36
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030037class Context(object):
38 def __init__(self):
39 self.build_meta = {}
40 self.nodes = []
41 self.clear_calls_stack = []
42 self.openstack_nodes_ids = []
koder aka kdanilov168f6092015-04-19 02:33:38 +030043 self.sensors_mon_q = None
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030044
45
koder aka kdanilov168f6092015-04-19 02:33:38 +030046def connect_one(node, vm=False):
koder aka kdanilov5d589b42015-03-26 12:25:51 +020047 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030048 ssh_pref = "ssh://"
49 if node.conn_url.startswith(ssh_pref):
50 url = node.conn_url[len(ssh_pref):]
koder aka kdanilov168f6092015-04-19 02:33:38 +030051
52 if vm:
53 ret_count = 24
54 log_warns = False
55 else:
56 ret_count = 3
57 log_warns = True
58
59 node.connection = ssh_utils.connect(url,
60 retry_count=ret_count,
61 log_warns=log_warns)
koder aka kdanilov2c473092015-03-29 17:12:13 +030062 else:
63 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030064 except Exception as exc:
65 # logger.exception("During connect to " + node.get_conn_id())
66 msg = "During connect to {0}: {1}".format(node.get_conn_id(),
67 exc.message)
68 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030069 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020070
71
koder aka kdanilov168f6092015-04-19 02:33:38 +030072def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030073 logger.info("Connecting to nodes")
74 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +030075 connect_one_f = functools.partial(connect_one, vm=vm)
76 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +030077
78
koder aka kdanilov652cd802015-04-13 12:21:07 +030079def test_thread(test, node, barrier, res_q):
koder aka kdanilov2c473092015-03-29 17:12:13 +030080 try:
81 logger.debug("Run preparation for {0}".format(node.conn_url))
82 test.pre_run(node.connection)
83 logger.debug("Run test for {0}".format(node.conn_url))
84 test.run(node.connection, barrier)
koder aka kdanilov652cd802015-04-13 12:21:07 +030085 except Exception as exc:
koder aka kdanilov2c473092015-03-29 17:12:13 +030086 logger.exception("In test {0} for node {1}".format(test, node))
koder aka kdanilov652cd802015-04-13 12:21:07 +030087 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +030088
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030089 try:
90 test.cleanup(node.connection)
91 except:
92 msg = "Duringf cleanup - in test {0} for node {1}"
93 logger.exception(msg.format(test, node))
94
koder aka kdanilov2c473092015-03-29 17:12:13 +030095
koder aka kdanilovcee43342015-04-14 22:52:53 +030096def run_tests(test_block, nodes):
koder aka kdanilov2c473092015-03-29 17:12:13 +030097 tool_type_mapper = {
98 "io": IOPerfTest,
99 "pgbench": PgBenchTest,
100 }
101
102 test_nodes = [node for node in nodes
103 if 'testnode' in node.roles]
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300104 test_number_per_type = {}
koder aka kdanilov2c473092015-03-29 17:12:13 +0300105 res_q = Queue.Queue()
106
koder aka kdanilovcee43342015-04-14 22:52:53 +0300107 for name, params in test_block.items():
108 logger.info("Starting {0} tests".format(name))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300109 test_num = test_number_per_type.get(name, 0)
110 test_number_per_type[name] = test_num + 1
koder aka kdanilovcee43342015-04-14 22:52:53 +0300111 threads = []
112 barrier = utils.Barrier(len(test_nodes))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300113
koder aka kdanilovcee43342015-04-14 22:52:53 +0300114 for node in test_nodes:
115 msg = "Starting {0} test on {1} node"
116 logger.debug(msg.format(name, node.conn_url))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300117
118 dr = os.path.join(
119 cfg_dict['test_log_directory'],
120 "{0}_{1}_{2}".format(name, test_num, node.get_ip())
121 )
122
123 if not os.path.exists(dr):
124 os.makedirs(dr)
125
126 test = tool_type_mapper[name](params, res_q.put, dr,
127 node=node.get_ip())
koder aka kdanilovcee43342015-04-14 22:52:53 +0300128 th = threading.Thread(None, test_thread, None,
129 (test, node, barrier, res_q))
130 threads.append(th)
131 th.daemon = True
132 th.start()
koder aka kdanilov2c473092015-03-29 17:12:13 +0300133
koder aka kdanilovcee43342015-04-14 22:52:53 +0300134 def gather_results(res_q, results):
135 while not res_q.empty():
136 val = res_q.get()
koder aka kdanilov66839a92015-04-11 13:22:31 +0300137
koder aka kdanilovcee43342015-04-14 22:52:53 +0300138 if isinstance(val, Exception):
139 msg = "Exception during test execution: {0}"
140 raise ValueError(msg.format(val.message))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300141
koder aka kdanilovcee43342015-04-14 22:52:53 +0300142 results.append(val)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300143
koder aka kdanilovcee43342015-04-14 22:52:53 +0300144 results = []
koder aka kdanilov652cd802015-04-13 12:21:07 +0300145
koder aka kdanilove87ae652015-04-20 02:14:35 +0300146 # MAX_WAIT_TIME = 10
147 # end_time = time.time() + MAX_WAIT_TIME
148
149 # while time.time() < end_time:
koder aka kdanilovcee43342015-04-14 22:52:53 +0300150 while True:
151 for th in threads:
152 th.join(1)
153 gather_results(res_q, results)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300154 # if time.time() > end_time:
155 # break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300156
koder aka kdanilovcee43342015-04-14 22:52:53 +0300157 if all(not th.is_alive() for th in threads):
158 break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300159
koder aka kdanilove87ae652015-04-20 02:14:35 +0300160 # if any(th.is_alive() for th in threads):
161 # logger.warning("Some test threads still running")
162
koder aka kdanilovcee43342015-04-14 22:52:53 +0300163 gather_results(res_q, results)
164 yield name, test.merge_results(results)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300165
166
koder aka kdanilovda45e882015-04-06 02:24:42 +0300167def log_nodes_statistic(_, ctx):
168 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300169 logger.info("Found {0} nodes total".format(len(nodes)))
170 per_role = collections.defaultdict(lambda: 0)
171 for node in nodes:
172 for role in node.roles:
173 per_role[role] += 1
174
175 for role, count in sorted(per_role.items()):
176 logger.debug("Found {0} nodes with role {1}".format(count, role))
177
178
koder aka kdanilovda45e882015-04-06 02:24:42 +0300179def connect_stage(cfg, ctx):
180 ctx.clear_calls_stack.append(disconnect_stage)
181 connect_all(ctx.nodes)
182
koder aka kdanilov168f6092015-04-19 02:33:38 +0300183 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300184
koder aka kdanilov168f6092015-04-19 02:33:38 +0300185 for node in ctx.nodes:
186 if node.connection is None:
187 if 'testnode' in node.roles:
188 msg = "Can't connect to testnode {0}"
189 raise RuntimeError(msg.format(node.get_conn_id()))
190 else:
191 msg = "Node {0} would be excluded - can't connect"
192 logger.warning(msg.format(node.get_conn_id()))
193 all_ok = False
194
195 if all_ok:
196 logger.info("All nodes connected successfully")
197
198 ctx.nodes = [node for node in ctx.nodes
199 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300200
201
koder aka kdanilovda45e882015-04-06 02:24:42 +0300202def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300203 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300204 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300205
koder aka kdanilove87ae652015-04-20 02:14:35 +0300206 nodes, clean_data = discover(ctx,
207 discover_objs,
208 cfg['clouds'],
209 cfg['var_dir'],
210 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300211
212 def undiscover_stage(cfg, ctx):
213 undiscover(clean_data)
214
215 ctx.clear_calls_stack.append(undiscover_stage)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300216 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300217
218 for url, roles in cfg.get('explicit_nodes', {}).items():
219 ctx.nodes.append(Node(url, roles.split(",")))
220
221
koder aka kdanilove87ae652015-04-20 02:14:35 +0300222def get_OS_credentials(cfg, ctx, creds_type):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300223 creds = None
koder aka kdanilovda45e882015-04-06 02:24:42 +0300224
koder aka kdanilovcee43342015-04-14 22:52:53 +0300225 if creds_type == 'clouds':
226 if 'openstack' in cfg['clouds']:
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300227 os_cfg = cfg['clouds']['openstack']
koder aka kdanilovcee43342015-04-14 22:52:53 +0300228
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300229 tenant = os_cfg['OS_TENANT_NAME'].strip()
230 user = os_cfg['OS_USERNAME'].strip()
231 passwd = os_cfg['OS_PASSWORD'].strip()
232 auth_url = os_cfg['OS_AUTH_URL'].strip()
233
koder aka kdanilovcee43342015-04-14 22:52:53 +0300234 elif 'fuel' in cfg['clouds'] and \
235 'openstack_env' in cfg['clouds']['fuel']:
236 creds = ctx.fuel_openstack_creds
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300237
koder aka kdanilovcee43342015-04-14 22:52:53 +0300238 elif creds_type == 'ENV':
239 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
240 elif os.path.isfile(creds_type):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300241 raise NotImplementedError()
242 # user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
koder aka kdanilovcee43342015-04-14 22:52:53 +0300243 else:
244 msg = "Creds {0!r} isn't supported".format(creds_type)
245 raise ValueError(msg)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300246
koder aka kdanilovcee43342015-04-14 22:52:53 +0300247 if creds is None:
248 creds = {'name': user,
249 'passwd': passwd,
250 'tenant': tenant,
251 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300252
koder aka kdanilovcee43342015-04-14 22:52:53 +0300253 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300254
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300255
koder aka kdanilov168f6092015-04-19 02:33:38 +0300256@contextlib.contextmanager
257def create_vms_ctx(ctx, cfg, config):
258 params = config['vm_params'].copy()
259 os_nodes_ids = []
260
261 os_creds_type = config['creds']
koder aka kdanilove87ae652015-04-20 02:14:35 +0300262 os_creds = get_OS_credentials(cfg, ctx, os_creds_type)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300263
264 start_vms.nova_connect(**os_creds)
265
266 logger.info("Preparing openstack")
267 start_vms.prepare_os_subpr(**os_creds)
268
269 new_nodes = []
270 try:
271 params['group_name'] = cfg_dict['run_uuid']
272 for new_node, node_id in start_vms.launch_vms(params):
273 new_node.roles.append('testnode')
274 ctx.nodes.append(new_node)
275 os_nodes_ids.append(node_id)
276 new_nodes.append(new_node)
277
278 store_nodes_in_log(cfg, os_nodes_ids)
279 ctx.openstack_nodes_ids = os_nodes_ids
280
281 yield new_nodes
282
283 finally:
284 if not cfg['keep_vm']:
285 shut_down_vms_stage(cfg, ctx)
286
287
koder aka kdanilovcee43342015-04-14 22:52:53 +0300288def run_tests_stage(cfg, ctx):
289 ctx.results = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300290
koder aka kdanilovcee43342015-04-14 22:52:53 +0300291 if 'tests' not in cfg:
292 return
gstepanov023c1e42015-04-08 15:50:19 +0300293
koder aka kdanilovcee43342015-04-14 22:52:53 +0300294 for group in cfg['tests']:
295
296 assert len(group.items()) == 1
297 key, config = group.items()[0]
298
299 if 'start_test_nodes' == key:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300300 with create_vms_ctx(ctx, cfg, config) as new_nodes:
301 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300302
koder aka kdanilov168f6092015-04-19 02:33:38 +0300303 for node in new_nodes:
304 if node.connection is None:
305 msg = "Failed to connect to vm {0}"
306 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300307
koder aka kdanilov168f6092015-04-19 02:33:38 +0300308 deploy_sensors_stage(cfg_dict,
309 ctx,
310 nodes=new_nodes,
311 undeploy=False)
koder aka kdanilov12ae0632015-04-15 01:13:43 +0300312
koder aka kdanilove87ae652015-04-20 02:14:35 +0300313 if not cfg['no_tests']:
314 for test_group in config.get('tests', []):
315 ctx.results.extend(run_tests(test_group, ctx.nodes))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300316 else:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300317 if not cfg['no_tests']:
318 ctx.results.extend(run_tests(group, ctx.nodes))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300319
gstepanov023c1e42015-04-08 15:50:19 +0300320
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300321def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300322 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300323 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300324 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300325 else:
326 nodes_ids = ctx.openstack_nodes_ids
327
koder aka kdanilov652cd802015-04-13 12:21:07 +0300328 if len(nodes_ids) != 0:
329 logger.info("Removing nodes")
330 start_vms.clear_nodes(nodes_ids)
331 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300332
koder aka kdanilov66839a92015-04-11 13:22:31 +0300333 if os.path.exists(vm_ids_fname):
334 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300335
koder aka kdanilov66839a92015-04-11 13:22:31 +0300336
337def store_nodes_in_log(cfg, nodes_ids):
338 with open(cfg['vm_ids_fname'], 'w') as fd:
339 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300340
341
342def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300343 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300344 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300345
346
koder aka kdanilovda45e882015-04-06 02:24:42 +0300347def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300348 ssh_utils.close_all_sessions()
349
koder aka kdanilovda45e882015-04-06 02:24:42 +0300350 for node in ctx.nodes:
351 if node.connection is not None:
352 node.connection.close()
353
354
koder aka kdanilov66839a92015-04-11 13:22:31 +0300355def store_raw_results_stage(cfg, ctx):
356
357 raw_results = os.path.join(cfg_dict['var_dir'], 'raw_results.yaml')
358
359 if os.path.exists(raw_results):
360 cont = yaml.load(open(raw_results).read())
361 else:
362 cont = []
363
koder aka kdanilov168f6092015-04-19 02:33:38 +0300364 cont.extend(utils.yamable(ctx.results))
koder aka kdanilov66839a92015-04-11 13:22:31 +0300365 raw_data = pretty_yaml.dumps(cont)
366
367 with open(raw_results, "w") as fd:
368 fd.write(raw_data)
369
370
371def console_report_stage(cfg, ctx):
372 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300373 if 'io' == tp and data is not None:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300374 print("\n")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300375 print(IOPerfTest.format_for_console(data))
koder aka kdanilove87ae652015-04-20 02:14:35 +0300376 print("\n")
koder aka kdanilov66839a92015-04-11 13:22:31 +0300377
378
koder aka kdanilove87ae652015-04-20 02:14:35 +0300379def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300380 html_rep_fname = cfg['html_report_file']
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300381
382 try:
383 fuel_url = cfg['clouds']['fuel']['url']
384 except KeyError:
385 fuel_url = None
386
387 try:
388 creds = cfg['clouds']['fuel']['creds']
389 except KeyError:
390 creds = None
391
gstepanov69339ac2015-04-16 20:09:33 +0300392 report.make_io_report(ctx.results, html_rep_fname, fuel_url, creds=creds)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300393
Yulia Portnova8ca20572015-04-14 14:09:39 +0300394 logger.info("Html report were stored in " + html_rep_fname)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300395
396 text_rep_fname = cfg_dict['text_report_file']
397 with open(text_rep_fname, "w") as fd:
398 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300399 if 'io' == tp and data is not None:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300400 fd.write(IOPerfTest.format_for_console(data))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300401 fd.write("\n")
402 fd.flush()
403
404 logger.info("Text report were stored in " + text_rep_fname)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300405
406
407def complete_log_nodes_statistic(cfg, ctx):
408 nodes = ctx.nodes
409 for node in nodes:
410 logger.debug(str(node))
411
412
koder aka kdanilov66839a92015-04-11 13:22:31 +0300413def load_data_from(var_dir):
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300414 def load_data_from_file(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300415 raw_results = os.path.join(var_dir, 'raw_results.yaml')
416 ctx.results = yaml.load(open(raw_results).read())
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300417 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300418
419
koder aka kdanilovcee43342015-04-14 22:52:53 +0300420def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300421 descr = "Disk io performance test suite"
422 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300423
424 parser.add_argument("-l", dest='extra_logs',
425 action='store_true', default=False,
426 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300427 parser.add_argument("-b", '--build_description',
428 type=str, default="Build info")
429 parser.add_argument("-i", '--build_id', type=str, default="id")
430 parser.add_argument("-t", '--build_type', type=str, default="GA")
431 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300432 parser.add_argument("-n", '--no-tests', action='store_true',
433 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300434 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
435 help="Only process data from previour run")
436 parser.add_argument("-k", '--keep-vm', action='store_true',
437 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300438 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
439 help="Don't connect/discover fuel nodes",
440 default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300441 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300442
443 return parser.parse_args(argv[1:])
444
445
koder aka kdanilov3f356262015-02-13 08:06:14 -0800446def main(argv):
koder aka kdanilove06762a2015-03-22 23:32:09 +0200447 opts = parse_args(argv)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300448
koder aka kdanilov66839a92015-04-11 13:22:31 +0300449 if opts.post_process_only is not None:
450 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300451 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300452 ]
453 else:
454 stages = [
455 discover_stage,
456 log_nodes_statistic,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300457 connect_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300458 deploy_sensors_stage,
459 run_tests_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300460 store_raw_results_stage
koder aka kdanilov66839a92015-04-11 13:22:31 +0300461 ]
462
koder aka kdanilove87ae652015-04-20 02:14:35 +0300463 report_stages = [
464 console_report_stage,
465 html_report_stage
466 ]
467
koder aka kdanilovcee43342015-04-14 22:52:53 +0300468 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300469
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300470 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
471 level = logging.DEBUG
472 else:
473 level = logging.WARNING
474
475 setup_loggers(level, cfg_dict['log_file'])
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300476
koder aka kdanilov652cd802015-04-13 12:21:07 +0300477 logger.info("All info would be stored into {0}".format(
478 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300479
koder aka kdanilovda45e882015-04-06 02:24:42 +0300480 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300481 ctx.build_meta['build_id'] = opts.build_id
482 ctx.build_meta['build_descrption'] = opts.build_description
483 ctx.build_meta['build_type'] = opts.build_type
484 ctx.build_meta['username'] = opts.username
koder aka kdanilove87ae652015-04-20 02:14:35 +0300485
koder aka kdanilov168f6092015-04-19 02:33:38 +0300486 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300487 cfg_dict['no_tests'] = opts.no_tests
488 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300489
koder aka kdanilovda45e882015-04-06 02:24:42 +0300490 try:
491 for stage in stages:
492 logger.info("Start {0.__name__} stage".format(stage))
493 stage(cfg_dict, ctx)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300494 except Exception as exc:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300495 emsg = exc.message
496 if emsg == "":
497 emsg = str(exc)
498 msg = "Exception during {0.__name__}: {1}".format(stage, emsg)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300499 logger.error(msg)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300500 finally:
501 exc, cls, tb = sys.exc_info()
502 for stage in ctx.clear_calls_stack[::-1]:
503 try:
504 logger.info("Start {0.__name__} stage".format(stage))
505 stage(cfg_dict, ctx)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300506 except Exception as exc:
507 logger.exception("During {0.__name__} stage".format(stage))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300508
koder aka kdanilovda45e882015-04-06 02:24:42 +0300509 if exc is not None:
510 raise exc, cls, tb
koder aka kdanilov2c473092015-03-29 17:12:13 +0300511
koder aka kdanilove87ae652015-04-20 02:14:35 +0300512 for report_stage in report_stages:
513 report_stage(cfg_dict, ctx)
514
515 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove06762a2015-03-22 23:32:09 +0200516 return 0