blob: 44b65c01f178d6d4e7bf52f50d380d8486c4d9af [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 kdanilov0c598a12015-04-21 03:01:40 +030047 if node.conn_url == 'local':
48 node.connection = ssh_utils.connect(node.conn_url)
49 return
50
koder aka kdanilov5d589b42015-03-26 12:25:51 +020051 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030052 ssh_pref = "ssh://"
53 if node.conn_url.startswith(ssh_pref):
54 url = node.conn_url[len(ssh_pref):]
koder aka kdanilov168f6092015-04-19 02:33:38 +030055
56 if vm:
57 ret_count = 24
58 log_warns = False
59 else:
60 ret_count = 3
61 log_warns = True
62
63 node.connection = ssh_utils.connect(url,
64 retry_count=ret_count,
65 log_warns=log_warns)
koder aka kdanilov2c473092015-03-29 17:12:13 +030066 else:
67 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030068 except Exception as exc:
69 # logger.exception("During connect to " + node.get_conn_id())
70 msg = "During connect to {0}: {1}".format(node.get_conn_id(),
71 exc.message)
72 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030073 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020074
75
koder aka kdanilov168f6092015-04-19 02:33:38 +030076def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030077 logger.info("Connecting to nodes")
78 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +030079 connect_one_f = functools.partial(connect_one, vm=vm)
80 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +030081
82
koder aka kdanilov652cd802015-04-13 12:21:07 +030083def test_thread(test, node, barrier, res_q):
koder aka kdanilov2c473092015-03-29 17:12:13 +030084 try:
85 logger.debug("Run preparation for {0}".format(node.conn_url))
86 test.pre_run(node.connection)
87 logger.debug("Run test for {0}".format(node.conn_url))
88 test.run(node.connection, barrier)
koder aka kdanilov652cd802015-04-13 12:21:07 +030089 except Exception as exc:
koder aka kdanilov2c473092015-03-29 17:12:13 +030090 logger.exception("In test {0} for node {1}".format(test, node))
koder aka kdanilov652cd802015-04-13 12:21:07 +030091 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +030092
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030093 try:
94 test.cleanup(node.connection)
95 except:
96 msg = "Duringf cleanup - in test {0} for node {1}"
97 logger.exception(msg.format(test, node))
98
koder aka kdanilov2c473092015-03-29 17:12:13 +030099
koder aka kdanilovcee43342015-04-14 22:52:53 +0300100def run_tests(test_block, nodes):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300101 tool_type_mapper = {
102 "io": IOPerfTest,
103 "pgbench": PgBenchTest,
104 }
105
106 test_nodes = [node for node in nodes
107 if 'testnode' in node.roles]
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300108 test_number_per_type = {}
koder aka kdanilov2c473092015-03-29 17:12:13 +0300109 res_q = Queue.Queue()
110
koder aka kdanilovcee43342015-04-14 22:52:53 +0300111 for name, params in test_block.items():
112 logger.info("Starting {0} tests".format(name))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300113 test_num = test_number_per_type.get(name, 0)
114 test_number_per_type[name] = test_num + 1
koder aka kdanilovcee43342015-04-14 22:52:53 +0300115 threads = []
116 barrier = utils.Barrier(len(test_nodes))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300117
koder aka kdanilovcee43342015-04-14 22:52:53 +0300118 for node in test_nodes:
119 msg = "Starting {0} test on {1} node"
120 logger.debug(msg.format(name, node.conn_url))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300121
122 dr = os.path.join(
123 cfg_dict['test_log_directory'],
124 "{0}_{1}_{2}".format(name, test_num, node.get_ip())
125 )
126
127 if not os.path.exists(dr):
128 os.makedirs(dr)
129
130 test = tool_type_mapper[name](params, res_q.put, dr,
131 node=node.get_ip())
koder aka kdanilovcee43342015-04-14 22:52:53 +0300132 th = threading.Thread(None, test_thread, None,
133 (test, node, barrier, res_q))
134 threads.append(th)
135 th.daemon = True
136 th.start()
koder aka kdanilov2c473092015-03-29 17:12:13 +0300137
koder aka kdanilovcee43342015-04-14 22:52:53 +0300138 def gather_results(res_q, results):
139 while not res_q.empty():
140 val = res_q.get()
koder aka kdanilov66839a92015-04-11 13:22:31 +0300141
koder aka kdanilovcee43342015-04-14 22:52:53 +0300142 if isinstance(val, Exception):
143 msg = "Exception during test execution: {0}"
144 raise ValueError(msg.format(val.message))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300145
koder aka kdanilovcee43342015-04-14 22:52:53 +0300146 results.append(val)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300147
koder aka kdanilovcee43342015-04-14 22:52:53 +0300148 results = []
koder aka kdanilov652cd802015-04-13 12:21:07 +0300149
koder aka kdanilove87ae652015-04-20 02:14:35 +0300150 # MAX_WAIT_TIME = 10
151 # end_time = time.time() + MAX_WAIT_TIME
152
153 # while time.time() < end_time:
koder aka kdanilovcee43342015-04-14 22:52:53 +0300154 while True:
155 for th in threads:
156 th.join(1)
157 gather_results(res_q, results)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300158 # if time.time() > end_time:
159 # break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300160
koder aka kdanilovcee43342015-04-14 22:52:53 +0300161 if all(not th.is_alive() for th in threads):
162 break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300163
koder aka kdanilove87ae652015-04-20 02:14:35 +0300164 # if any(th.is_alive() for th in threads):
165 # logger.warning("Some test threads still running")
166
koder aka kdanilovcee43342015-04-14 22:52:53 +0300167 gather_results(res_q, results)
168 yield name, test.merge_results(results)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300169
170
koder aka kdanilovda45e882015-04-06 02:24:42 +0300171def log_nodes_statistic(_, ctx):
172 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300173 logger.info("Found {0} nodes total".format(len(nodes)))
174 per_role = collections.defaultdict(lambda: 0)
175 for node in nodes:
176 for role in node.roles:
177 per_role[role] += 1
178
179 for role, count in sorted(per_role.items()):
180 logger.debug("Found {0} nodes with role {1}".format(count, role))
181
182
koder aka kdanilovda45e882015-04-06 02:24:42 +0300183def connect_stage(cfg, ctx):
184 ctx.clear_calls_stack.append(disconnect_stage)
185 connect_all(ctx.nodes)
186
koder aka kdanilov168f6092015-04-19 02:33:38 +0300187 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300188
koder aka kdanilov168f6092015-04-19 02:33:38 +0300189 for node in ctx.nodes:
190 if node.connection is None:
191 if 'testnode' in node.roles:
192 msg = "Can't connect to testnode {0}"
193 raise RuntimeError(msg.format(node.get_conn_id()))
194 else:
195 msg = "Node {0} would be excluded - can't connect"
196 logger.warning(msg.format(node.get_conn_id()))
197 all_ok = False
198
199 if all_ok:
200 logger.info("All nodes connected successfully")
201
202 ctx.nodes = [node for node in ctx.nodes
203 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300204
205
koder aka kdanilovda45e882015-04-06 02:24:42 +0300206def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300207 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300208 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300209
koder aka kdanilove87ae652015-04-20 02:14:35 +0300210 nodes, clean_data = discover(ctx,
211 discover_objs,
212 cfg['clouds'],
213 cfg['var_dir'],
214 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300215
216 def undiscover_stage(cfg, ctx):
217 undiscover(clean_data)
218
219 ctx.clear_calls_stack.append(undiscover_stage)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300220 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300221
222 for url, roles in cfg.get('explicit_nodes', {}).items():
223 ctx.nodes.append(Node(url, roles.split(",")))
224
225
koder aka kdanilove87ae652015-04-20 02:14:35 +0300226def get_OS_credentials(cfg, ctx, creds_type):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300227 creds = None
koder aka kdanilovda45e882015-04-06 02:24:42 +0300228
koder aka kdanilovcee43342015-04-14 22:52:53 +0300229 if creds_type == 'clouds':
230 if 'openstack' in cfg['clouds']:
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300231 os_cfg = cfg['clouds']['openstack']
koder aka kdanilovcee43342015-04-14 22:52:53 +0300232
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300233 tenant = os_cfg['OS_TENANT_NAME'].strip()
234 user = os_cfg['OS_USERNAME'].strip()
235 passwd = os_cfg['OS_PASSWORD'].strip()
236 auth_url = os_cfg['OS_AUTH_URL'].strip()
237
koder aka kdanilovcee43342015-04-14 22:52:53 +0300238 elif 'fuel' in cfg['clouds'] and \
239 'openstack_env' in cfg['clouds']['fuel']:
240 creds = ctx.fuel_openstack_creds
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300241
koder aka kdanilovcee43342015-04-14 22:52:53 +0300242 elif creds_type == 'ENV':
243 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
244 elif os.path.isfile(creds_type):
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300245 raise NotImplementedError()
246 # user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
koder aka kdanilovcee43342015-04-14 22:52:53 +0300247 else:
248 msg = "Creds {0!r} isn't supported".format(creds_type)
249 raise ValueError(msg)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300250
koder aka kdanilovcee43342015-04-14 22:52:53 +0300251 if creds is None:
252 creds = {'name': user,
253 'passwd': passwd,
254 'tenant': tenant,
255 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300256
koder aka kdanilovcee43342015-04-14 22:52:53 +0300257 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300258
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300259
koder aka kdanilov168f6092015-04-19 02:33:38 +0300260@contextlib.contextmanager
261def create_vms_ctx(ctx, cfg, config):
262 params = config['vm_params'].copy()
263 os_nodes_ids = []
264
265 os_creds_type = config['creds']
koder aka kdanilove87ae652015-04-20 02:14:35 +0300266 os_creds = get_OS_credentials(cfg, ctx, os_creds_type)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300267
268 start_vms.nova_connect(**os_creds)
269
270 logger.info("Preparing openstack")
271 start_vms.prepare_os_subpr(**os_creds)
272
273 new_nodes = []
274 try:
275 params['group_name'] = cfg_dict['run_uuid']
276 for new_node, node_id in start_vms.launch_vms(params):
277 new_node.roles.append('testnode')
278 ctx.nodes.append(new_node)
279 os_nodes_ids.append(node_id)
280 new_nodes.append(new_node)
281
282 store_nodes_in_log(cfg, os_nodes_ids)
283 ctx.openstack_nodes_ids = os_nodes_ids
284
285 yield new_nodes
286
287 finally:
288 if not cfg['keep_vm']:
289 shut_down_vms_stage(cfg, ctx)
290
291
koder aka kdanilovcee43342015-04-14 22:52:53 +0300292def run_tests_stage(cfg, ctx):
293 ctx.results = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300294
koder aka kdanilovcee43342015-04-14 22:52:53 +0300295 if 'tests' not in cfg:
296 return
gstepanov023c1e42015-04-08 15:50:19 +0300297
koder aka kdanilovcee43342015-04-14 22:52:53 +0300298 for group in cfg['tests']:
299
300 assert len(group.items()) == 1
301 key, config = group.items()[0]
302
303 if 'start_test_nodes' == key:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300304 with create_vms_ctx(ctx, cfg, config) as new_nodes:
305 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300306
koder aka kdanilov168f6092015-04-19 02:33:38 +0300307 for node in new_nodes:
308 if node.connection is None:
309 msg = "Failed to connect to vm {0}"
310 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300311
koder aka kdanilov168f6092015-04-19 02:33:38 +0300312 deploy_sensors_stage(cfg_dict,
313 ctx,
314 nodes=new_nodes,
315 undeploy=False)
koder aka kdanilov12ae0632015-04-15 01:13:43 +0300316
koder aka kdanilove87ae652015-04-20 02:14:35 +0300317 if not cfg['no_tests']:
318 for test_group in config.get('tests', []):
319 ctx.results.extend(run_tests(test_group, ctx.nodes))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300320 else:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300321 if not cfg['no_tests']:
322 ctx.results.extend(run_tests(group, ctx.nodes))
koder aka kdanilovda45e882015-04-06 02:24:42 +0300323
gstepanov023c1e42015-04-08 15:50:19 +0300324
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300325def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300326 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300327 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300328 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300329 else:
330 nodes_ids = ctx.openstack_nodes_ids
331
koder aka kdanilov652cd802015-04-13 12:21:07 +0300332 if len(nodes_ids) != 0:
333 logger.info("Removing nodes")
334 start_vms.clear_nodes(nodes_ids)
335 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300336
koder aka kdanilov66839a92015-04-11 13:22:31 +0300337 if os.path.exists(vm_ids_fname):
338 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300339
koder aka kdanilov66839a92015-04-11 13:22:31 +0300340
341def store_nodes_in_log(cfg, nodes_ids):
342 with open(cfg['vm_ids_fname'], 'w') as fd:
343 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300344
345
346def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300347 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300348 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300349
350
koder aka kdanilovda45e882015-04-06 02:24:42 +0300351def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300352 ssh_utils.close_all_sessions()
353
koder aka kdanilovda45e882015-04-06 02:24:42 +0300354 for node in ctx.nodes:
355 if node.connection is not None:
356 node.connection.close()
357
358
koder aka kdanilov66839a92015-04-11 13:22:31 +0300359def store_raw_results_stage(cfg, ctx):
360
361 raw_results = os.path.join(cfg_dict['var_dir'], 'raw_results.yaml')
362
363 if os.path.exists(raw_results):
364 cont = yaml.load(open(raw_results).read())
365 else:
366 cont = []
367
koder aka kdanilov168f6092015-04-19 02:33:38 +0300368 cont.extend(utils.yamable(ctx.results))
koder aka kdanilov66839a92015-04-11 13:22:31 +0300369 raw_data = pretty_yaml.dumps(cont)
370
371 with open(raw_results, "w") as fd:
372 fd.write(raw_data)
373
374
375def console_report_stage(cfg, ctx):
376 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300377 if 'io' == tp and data is not None:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300378 print("\n")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300379 print(IOPerfTest.format_for_console(data))
koder aka kdanilove87ae652015-04-20 02:14:35 +0300380 print("\n")
koder aka kdanilov66839a92015-04-11 13:22:31 +0300381
382
koder aka kdanilove87ae652015-04-20 02:14:35 +0300383def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300384 html_rep_fname = cfg['html_report_file']
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300385
386 try:
387 fuel_url = cfg['clouds']['fuel']['url']
388 except KeyError:
389 fuel_url = None
390
391 try:
392 creds = cfg['clouds']['fuel']['creds']
393 except KeyError:
394 creds = None
395
gstepanov69339ac2015-04-16 20:09:33 +0300396 report.make_io_report(ctx.results, html_rep_fname, fuel_url, creds=creds)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300397
Yulia Portnova8ca20572015-04-14 14:09:39 +0300398 logger.info("Html report were stored in " + html_rep_fname)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300399
400 text_rep_fname = cfg_dict['text_report_file']
401 with open(text_rep_fname, "w") as fd:
402 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300403 if 'io' == tp and data is not None:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300404 fd.write(IOPerfTest.format_for_console(data))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300405 fd.write("\n")
406 fd.flush()
407
408 logger.info("Text report were stored in " + text_rep_fname)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300409
410
411def complete_log_nodes_statistic(cfg, ctx):
412 nodes = ctx.nodes
413 for node in nodes:
414 logger.debug(str(node))
415
416
koder aka kdanilov66839a92015-04-11 13:22:31 +0300417def load_data_from(var_dir):
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300418 def load_data_from_file(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300419 raw_results = os.path.join(var_dir, 'raw_results.yaml')
420 ctx.results = yaml.load(open(raw_results).read())
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300421 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300422
423
koder aka kdanilovcee43342015-04-14 22:52:53 +0300424def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300425 descr = "Disk io performance test suite"
426 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300427
428 parser.add_argument("-l", dest='extra_logs',
429 action='store_true', default=False,
430 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300431 parser.add_argument("-b", '--build_description',
432 type=str, default="Build info")
433 parser.add_argument("-i", '--build_id', type=str, default="id")
434 parser.add_argument("-t", '--build_type', type=str, default="GA")
435 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300436 parser.add_argument("-n", '--no-tests', action='store_true',
437 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300438 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
439 help="Only process data from previour run")
440 parser.add_argument("-k", '--keep-vm', action='store_true',
441 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300442 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
443 help="Don't connect/discover fuel nodes",
444 default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300445 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300446
447 return parser.parse_args(argv[1:])
448
449
koder aka kdanilov3f356262015-02-13 08:06:14 -0800450def main(argv):
koder aka kdanilove06762a2015-03-22 23:32:09 +0200451 opts = parse_args(argv)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300452
koder aka kdanilov66839a92015-04-11 13:22:31 +0300453 if opts.post_process_only is not None:
454 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300455 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300456 ]
457 else:
458 stages = [
459 discover_stage,
460 log_nodes_statistic,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300461 connect_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300462 deploy_sensors_stage,
463 run_tests_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300464 store_raw_results_stage
koder aka kdanilov66839a92015-04-11 13:22:31 +0300465 ]
466
koder aka kdanilove87ae652015-04-20 02:14:35 +0300467 report_stages = [
468 console_report_stage,
469 html_report_stage
470 ]
471
koder aka kdanilovcee43342015-04-14 22:52:53 +0300472 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300473
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300474 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
475 level = logging.DEBUG
476 else:
477 level = logging.WARNING
478
479 setup_loggers(level, cfg_dict['log_file'])
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300480
koder aka kdanilov652cd802015-04-13 12:21:07 +0300481 logger.info("All info would be stored into {0}".format(
482 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300483
koder aka kdanilovda45e882015-04-06 02:24:42 +0300484 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300485 ctx.build_meta['build_id'] = opts.build_id
486 ctx.build_meta['build_descrption'] = opts.build_description
487 ctx.build_meta['build_type'] = opts.build_type
488 ctx.build_meta['username'] = opts.username
koder aka kdanilove87ae652015-04-20 02:14:35 +0300489
koder aka kdanilov168f6092015-04-19 02:33:38 +0300490 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300491 cfg_dict['no_tests'] = opts.no_tests
492 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300493
koder aka kdanilovda45e882015-04-06 02:24:42 +0300494 try:
495 for stage in stages:
496 logger.info("Start {0.__name__} stage".format(stage))
497 stage(cfg_dict, ctx)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300498 except Exception as exc:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300499 emsg = exc.message
500 if emsg == "":
501 emsg = str(exc)
502 msg = "Exception during {0.__name__}: {1}".format(stage, emsg)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300503 logger.error(msg)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300504 finally:
505 exc, cls, tb = sys.exc_info()
506 for stage in ctx.clear_calls_stack[::-1]:
507 try:
508 logger.info("Start {0.__name__} stage".format(stage))
509 stage(cfg_dict, ctx)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300510 except Exception as exc:
511 logger.exception("During {0.__name__} stage".format(stage))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300512
koder aka kdanilovda45e882015-04-06 02:24:42 +0300513 if exc is not None:
514 raise exc, cls, tb
koder aka kdanilov2c473092015-03-29 17:12:13 +0300515
koder aka kdanilove87ae652015-04-20 02:14:35 +0300516 for report_stage in report_stages:
517 report_stage(cfg_dict, ctx)
518
519 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove06762a2015-03-22 23:32:09 +0200520 return 0