blob: 01bb8f60da296e19aa510596d418a43e91684a6b [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 kdanilov2c473092015-03-29 17:12:13 +03005import Queue
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08006import pprint
koder aka kdanilove21d7472015-02-14 19:02:04 -08007import logging
koder aka kdanilov7306c642015-04-23 15:29:45 +03008import StringIO
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 kdanilov7306c642015-04-23 15:29:45 +030013import subprocess
koder aka kdanilov2c473092015-03-29 17:12:13 +030014import collections
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080015
koder aka kdanilov66839a92015-04-11 13:22:31 +030016import yaml
koder aka kdanilov2c473092015-03-29 17:12:13 +030017from concurrent.futures import ThreadPoolExecutor
koder aka kdanilov6c491062015-04-09 22:33:13 +030018
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030019from wally import pretty_yaml
koder aka kdanilove87ae652015-04-20 02:14:35 +030020from wally.sensors_utils import deploy_sensors_stage
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030021from wally.discover import discover, Node, undiscover
22from wally import utils, report, ssh_utils, start_vms
23from wally.suits.itest import IOPerfTest, PgBenchTest
24from wally.config import cfg_dict, load_config, setup_loggers
koder aka kdanilov2c473092015-03-29 17:12:13 +030025
26
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030027logger = logging.getLogger("wally")
koder aka kdanilovcee43342015-04-14 22:52:53 +030028
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -080029
Yulia Portnova7ddfa732015-02-24 17:32:58 +020030def format_result(res, formatter):
koder aka kdanilove21d7472015-02-14 19:02:04 -080031 data = "\n{0}\n".format("=" * 80)
32 data += pprint.pformat(res) + "\n"
33 data += "{0}\n".format("=" * 80)
koder aka kdanilovfe056622015-02-19 08:46:15 -080034 templ = "{0}\n\n====> {1}\n\n{2}\n\n"
Yulia Portnova7ddfa732015-02-24 17:32:58 +020035 return templ.format(data, formatter(res), "=" * 80)
koder aka kdanilove21d7472015-02-14 19:02:04 -080036
37
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030038class Context(object):
39 def __init__(self):
40 self.build_meta = {}
41 self.nodes = []
42 self.clear_calls_stack = []
43 self.openstack_nodes_ids = []
koder aka kdanilov168f6092015-04-19 02:33:38 +030044 self.sensors_mon_q = None
koder aka kdanilov1c2b5112015-04-10 16:53:51 +030045
46
koder aka kdanilov168f6092015-04-19 02:33:38 +030047def connect_one(node, vm=False):
koder aka kdanilov0c598a12015-04-21 03:01:40 +030048 if node.conn_url == 'local':
49 node.connection = ssh_utils.connect(node.conn_url)
50 return
51
koder aka kdanilov5d589b42015-03-26 12:25:51 +020052 try:
koder aka kdanilov2c473092015-03-29 17:12:13 +030053 ssh_pref = "ssh://"
54 if node.conn_url.startswith(ssh_pref):
55 url = node.conn_url[len(ssh_pref):]
koder aka kdanilov168f6092015-04-19 02:33:38 +030056
57 if vm:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030058 conn_timeout = 240
koder aka kdanilov168f6092015-04-19 02:33:38 +030059 else:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030060 conn_timeout = 30
koder aka kdanilov168f6092015-04-19 02:33:38 +030061
62 node.connection = ssh_utils.connect(url,
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030063 conn_timeout=conn_timeout)
koder aka kdanilov2c473092015-03-29 17:12:13 +030064 else:
65 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030066 except Exception as exc:
67 # logger.exception("During connect to " + node.get_conn_id())
68 msg = "During connect to {0}: {1}".format(node.get_conn_id(),
69 exc.message)
70 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030071 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020072
73
koder aka kdanilov168f6092015-04-19 02:33:38 +030074def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030075 logger.info("Connecting to nodes")
76 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +030077 connect_one_f = functools.partial(connect_one, vm=vm)
78 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +030079
80
koder aka kdanilov652cd802015-04-13 12:21:07 +030081def test_thread(test, node, barrier, res_q):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030082 exc = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030083 try:
koder aka kdanilova047e1b2015-04-21 23:16:59 +030084 logger.debug("Run preparation for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030085 test.pre_run()
koder aka kdanilova047e1b2015-04-21 23:16:59 +030086 logger.debug("Run test for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030087 test.run(barrier)
koder aka kdanilov652cd802015-04-13 12:21:07 +030088 except Exception as exc:
koder aka kdanilov2c473092015-03-29 17:12:13 +030089 logger.exception("In test {0} for node {1}".format(test, node))
90
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030091 try:
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030092 test.cleanup()
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030093 except:
94 msg = "Duringf cleanup - in test {0} for node {1}"
95 logger.exception(msg.format(test, node))
96
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030097 if exc is not None:
98 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +030099
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300100
101def run_tests(test_block, nodes, test_uuid):
koder aka kdanilov2c473092015-03-29 17:12:13 +0300102 tool_type_mapper = {
103 "io": IOPerfTest,
104 "pgbench": PgBenchTest,
105 }
106
107 test_nodes = [node for node in nodes
108 if 'testnode' in node.roles]
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300109 test_number_per_type = {}
koder aka kdanilov2c473092015-03-29 17:12:13 +0300110 res_q = Queue.Queue()
111
koder aka kdanilovcee43342015-04-14 22:52:53 +0300112 for name, params in test_block.items():
113 logger.info("Starting {0} tests".format(name))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300114 test_num = test_number_per_type.get(name, 0)
115 test_number_per_type[name] = test_num + 1
koder aka kdanilovcee43342015-04-14 22:52:53 +0300116 threads = []
117 barrier = utils.Barrier(len(test_nodes))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300118
koder aka kdanilovcee43342015-04-14 22:52:53 +0300119 for node in test_nodes:
120 msg = "Starting {0} test on {1} node"
121 logger.debug(msg.format(name, node.conn_url))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300122
123 dr = os.path.join(
124 cfg_dict['test_log_directory'],
125 "{0}_{1}_{2}".format(name, test_num, node.get_ip())
126 )
127
128 if not os.path.exists(dr):
129 os.makedirs(dr)
130
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300131 test = tool_type_mapper[name](params, res_q.put, test_uuid, node,
132 log_directory=dr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300133 th = threading.Thread(None, test_thread, None,
134 (test, node, barrier, res_q))
135 threads.append(th)
136 th.daemon = True
137 th.start()
koder aka kdanilov2c473092015-03-29 17:12:13 +0300138
koder aka kdanilovcee43342015-04-14 22:52:53 +0300139 def gather_results(res_q, results):
140 while not res_q.empty():
141 val = res_q.get()
koder aka kdanilov66839a92015-04-11 13:22:31 +0300142
koder aka kdanilovcee43342015-04-14 22:52:53 +0300143 if isinstance(val, Exception):
144 msg = "Exception during test execution: {0}"
145 raise ValueError(msg.format(val.message))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300146
koder aka kdanilovcee43342015-04-14 22:52:53 +0300147 results.append(val)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300148
koder aka kdanilovcee43342015-04-14 22:52:53 +0300149 results = []
koder aka kdanilov652cd802015-04-13 12:21:07 +0300150
koder aka kdanilove87ae652015-04-20 02:14:35 +0300151 # MAX_WAIT_TIME = 10
152 # end_time = time.time() + MAX_WAIT_TIME
153
154 # while time.time() < end_time:
koder aka kdanilovcee43342015-04-14 22:52:53 +0300155 while True:
156 for th in threads:
157 th.join(1)
158 gather_results(res_q, results)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300159 # if time.time() > end_time:
160 # break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300161
koder aka kdanilovcee43342015-04-14 22:52:53 +0300162 if all(not th.is_alive() for th in threads):
163 break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300164
koder aka kdanilove87ae652015-04-20 02:14:35 +0300165 # if any(th.is_alive() for th in threads):
166 # logger.warning("Some test threads still running")
167
koder aka kdanilovcee43342015-04-14 22:52:53 +0300168 gather_results(res_q, results)
169 yield name, test.merge_results(results)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300170
171
koder aka kdanilovda45e882015-04-06 02:24:42 +0300172def log_nodes_statistic(_, ctx):
173 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300174 logger.info("Found {0} nodes total".format(len(nodes)))
175 per_role = collections.defaultdict(lambda: 0)
176 for node in nodes:
177 for role in node.roles:
178 per_role[role] += 1
179
180 for role, count in sorted(per_role.items()):
181 logger.debug("Found {0} nodes with role {1}".format(count, role))
182
183
koder aka kdanilovda45e882015-04-06 02:24:42 +0300184def connect_stage(cfg, ctx):
185 ctx.clear_calls_stack.append(disconnect_stage)
186 connect_all(ctx.nodes)
187
koder aka kdanilov168f6092015-04-19 02:33:38 +0300188 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300189
koder aka kdanilov168f6092015-04-19 02:33:38 +0300190 for node in ctx.nodes:
191 if node.connection is None:
192 if 'testnode' in node.roles:
193 msg = "Can't connect to testnode {0}"
194 raise RuntimeError(msg.format(node.get_conn_id()))
195 else:
196 msg = "Node {0} would be excluded - can't connect"
197 logger.warning(msg.format(node.get_conn_id()))
198 all_ok = False
199
200 if all_ok:
201 logger.info("All nodes connected successfully")
202
203 ctx.nodes = [node for node in ctx.nodes
204 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300205
206
koder aka kdanilovda45e882015-04-06 02:24:42 +0300207def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300208 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300209 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300210
koder aka kdanilove87ae652015-04-20 02:14:35 +0300211 nodes, clean_data = discover(ctx,
212 discover_objs,
213 cfg['clouds'],
214 cfg['var_dir'],
215 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300216
217 def undiscover_stage(cfg, ctx):
218 undiscover(clean_data)
219
220 ctx.clear_calls_stack.append(undiscover_stage)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300221 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300222
223 for url, roles in cfg.get('explicit_nodes', {}).items():
224 ctx.nodes.append(Node(url, roles.split(",")))
225
226
koder aka kdanilove87ae652015-04-20 02:14:35 +0300227def get_OS_credentials(cfg, ctx, creds_type):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300228 creds = None
koder aka kdanilovda45e882015-04-06 02:24:42 +0300229
koder aka kdanilovcee43342015-04-14 22:52:53 +0300230 if creds_type == 'clouds':
231 if 'openstack' in cfg['clouds']:
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300232 os_cfg = cfg['clouds']['openstack']
koder aka kdanilovcee43342015-04-14 22:52:53 +0300233
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300234 tenant = os_cfg['OS_TENANT_NAME'].strip()
235 user = os_cfg['OS_USERNAME'].strip()
236 passwd = os_cfg['OS_PASSWORD'].strip()
237 auth_url = os_cfg['OS_AUTH_URL'].strip()
238
koder aka kdanilovcee43342015-04-14 22:52:53 +0300239 elif 'fuel' in cfg['clouds'] and \
240 'openstack_env' in cfg['clouds']['fuel']:
241 creds = ctx.fuel_openstack_creds
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300242
koder aka kdanilovcee43342015-04-14 22:52:53 +0300243 elif creds_type == 'ENV':
244 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
245 elif os.path.isfile(creds_type):
koder aka kdanilov7306c642015-04-23 15:29:45 +0300246 fc = open(creds_type).read()
247
248 echo = 'echo "$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
249
250 p = subprocess.Popen(['/bin/bash'], shell=False,
251 stdout=subprocess.PIPE,
252 stdin=subprocess.PIPE,
253 stderr=subprocess.STDOUT)
254 p.stdin.write(fc + "\n" + echo)
255 p.stdin.close()
256 code = p.wait()
257 data = p.stdout.read().strip()
258
259 if code != 0:
260 msg = "Failed to get creads from openrc file: " + data
261 logger.error(msg)
262 raise RuntimeError(msg)
263
264 try:
265 user, tenant, passwd_auth_url = data.split(':', 2)
266 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
267 assert (auth_url.startswith("https://") or
268 auth_url.startswith("http://"))
269 except Exception:
270 msg = "Failed to get creads from openrc file: " + data
271 logger.exception(msg)
272 raise
273
koder aka kdanilovcee43342015-04-14 22:52:53 +0300274 else:
275 msg = "Creds {0!r} isn't supported".format(creds_type)
276 raise ValueError(msg)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300277
koder aka kdanilovcee43342015-04-14 22:52:53 +0300278 if creds is None:
279 creds = {'name': user,
280 'passwd': passwd,
281 'tenant': tenant,
282 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300283
koder aka kdanilovcee43342015-04-14 22:52:53 +0300284 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300285
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300286
koder aka kdanilov168f6092015-04-19 02:33:38 +0300287@contextlib.contextmanager
288def create_vms_ctx(ctx, cfg, config):
289 params = config['vm_params'].copy()
290 os_nodes_ids = []
291
292 os_creds_type = config['creds']
koder aka kdanilove87ae652015-04-20 02:14:35 +0300293 os_creds = get_OS_credentials(cfg, ctx, os_creds_type)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300294
295 start_vms.nova_connect(**os_creds)
296
297 logger.info("Preparing openstack")
298 start_vms.prepare_os_subpr(**os_creds)
299
300 new_nodes = []
301 try:
302 params['group_name'] = cfg_dict['run_uuid']
303 for new_node, node_id in start_vms.launch_vms(params):
304 new_node.roles.append('testnode')
305 ctx.nodes.append(new_node)
306 os_nodes_ids.append(node_id)
307 new_nodes.append(new_node)
308
309 store_nodes_in_log(cfg, os_nodes_ids)
310 ctx.openstack_nodes_ids = os_nodes_ids
311
312 yield new_nodes
313
314 finally:
315 if not cfg['keep_vm']:
316 shut_down_vms_stage(cfg, ctx)
317
318
koder aka kdanilovcee43342015-04-14 22:52:53 +0300319def run_tests_stage(cfg, ctx):
320 ctx.results = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300321
koder aka kdanilovcee43342015-04-14 22:52:53 +0300322 if 'tests' not in cfg:
323 return
gstepanov023c1e42015-04-08 15:50:19 +0300324
koder aka kdanilovcee43342015-04-14 22:52:53 +0300325 for group in cfg['tests']:
326
327 assert len(group.items()) == 1
328 key, config = group.items()[0]
329
330 if 'start_test_nodes' == key:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300331 with create_vms_ctx(ctx, cfg, config) as new_nodes:
332 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300333
koder aka kdanilov168f6092015-04-19 02:33:38 +0300334 for node in new_nodes:
335 if node.connection is None:
336 msg = "Failed to connect to vm {0}"
337 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300338
koder aka kdanilov168f6092015-04-19 02:33:38 +0300339 deploy_sensors_stage(cfg_dict,
340 ctx,
341 nodes=new_nodes,
342 undeploy=False)
koder aka kdanilov12ae0632015-04-15 01:13:43 +0300343
koder aka kdanilove87ae652015-04-20 02:14:35 +0300344 if not cfg['no_tests']:
345 for test_group in config.get('tests', []):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300346 test_res = run_tests(test_group, ctx.nodes,
347 cfg['run_uuid'])
348 ctx.results.extend(test_res)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300349 else:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300350 if not cfg['no_tests']:
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300351 test_res = run_tests(group, ctx.nodes, cfg['run_uuid'])
352 ctx.results.extend(test_res)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300353
gstepanov023c1e42015-04-08 15:50:19 +0300354
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300355def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300356 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300357 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300358 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300359 else:
360 nodes_ids = ctx.openstack_nodes_ids
361
koder aka kdanilov652cd802015-04-13 12:21:07 +0300362 if len(nodes_ids) != 0:
363 logger.info("Removing nodes")
364 start_vms.clear_nodes(nodes_ids)
365 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300366
koder aka kdanilov66839a92015-04-11 13:22:31 +0300367 if os.path.exists(vm_ids_fname):
368 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300369
koder aka kdanilov66839a92015-04-11 13:22:31 +0300370
371def store_nodes_in_log(cfg, nodes_ids):
372 with open(cfg['vm_ids_fname'], 'w') as fd:
373 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300374
375
376def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300377 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300378 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300379
380
koder aka kdanilovda45e882015-04-06 02:24:42 +0300381def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300382 ssh_utils.close_all_sessions()
383
koder aka kdanilovda45e882015-04-06 02:24:42 +0300384 for node in ctx.nodes:
385 if node.connection is not None:
386 node.connection.close()
387
388
koder aka kdanilov66839a92015-04-11 13:22:31 +0300389def store_raw_results_stage(cfg, ctx):
390
391 raw_results = os.path.join(cfg_dict['var_dir'], 'raw_results.yaml')
392
393 if os.path.exists(raw_results):
394 cont = yaml.load(open(raw_results).read())
395 else:
396 cont = []
397
koder aka kdanilov168f6092015-04-19 02:33:38 +0300398 cont.extend(utils.yamable(ctx.results))
koder aka kdanilov66839a92015-04-11 13:22:31 +0300399 raw_data = pretty_yaml.dumps(cont)
400
401 with open(raw_results, "w") as fd:
402 fd.write(raw_data)
403
404
405def console_report_stage(cfg, ctx):
406 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300407 if 'io' == tp and data is not None:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300408 print("\n")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300409 print(IOPerfTest.format_for_console(data))
koder aka kdanilove87ae652015-04-20 02:14:35 +0300410 print("\n")
koder aka kdanilov66839a92015-04-11 13:22:31 +0300411
412
koder aka kdanilove87ae652015-04-20 02:14:35 +0300413def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300414 html_rep_fname = cfg['html_report_file']
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300415
416 try:
417 fuel_url = cfg['clouds']['fuel']['url']
418 except KeyError:
419 fuel_url = None
420
421 try:
422 creds = cfg['clouds']['fuel']['creds']
423 except KeyError:
424 creds = None
425
gstepanov69339ac2015-04-16 20:09:33 +0300426 report.make_io_report(ctx.results, html_rep_fname, fuel_url, creds=creds)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300427
koder aka kdanilov652cd802015-04-13 12:21:07 +0300428 text_rep_fname = cfg_dict['text_report_file']
429 with open(text_rep_fname, "w") as fd:
430 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300431 if 'io' == tp and data is not None:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300432 fd.write(IOPerfTest.format_for_console(data))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300433 fd.write("\n")
434 fd.flush()
435
436 logger.info("Text report were stored in " + text_rep_fname)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300437
438
439def complete_log_nodes_statistic(cfg, ctx):
440 nodes = ctx.nodes
441 for node in nodes:
442 logger.debug(str(node))
443
444
koder aka kdanilov66839a92015-04-11 13:22:31 +0300445def load_data_from(var_dir):
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300446 def load_data_from_file(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300447 raw_results = os.path.join(var_dir, 'raw_results.yaml')
448 ctx.results = yaml.load(open(raw_results).read())
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300449 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300450
451
koder aka kdanilovcee43342015-04-14 22:52:53 +0300452def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300453 descr = "Disk io performance test suite"
454 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300455
456 parser.add_argument("-l", dest='extra_logs',
457 action='store_true', default=False,
458 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300459 parser.add_argument("-b", '--build_description',
460 type=str, default="Build info")
461 parser.add_argument("-i", '--build_id', type=str, default="id")
462 parser.add_argument("-t", '--build_type', type=str, default="GA")
463 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300464 parser.add_argument("-n", '--no-tests', action='store_true',
465 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300466 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
467 help="Only process data from previour run")
468 parser.add_argument("-k", '--keep-vm', action='store_true',
469 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300470 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
471 help="Don't connect/discover fuel nodes",
472 default=False)
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300473 parser.add_argument("-r", '--no-html-report', action='store_true',
474 help="Skip html report", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300475 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300476
477 return parser.parse_args(argv[1:])
478
479
koder aka kdanilov3f356262015-02-13 08:06:14 -0800480def main(argv):
koder aka kdanilove06762a2015-03-22 23:32:09 +0200481 opts = parse_args(argv)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300482
koder aka kdanilov66839a92015-04-11 13:22:31 +0300483 if opts.post_process_only is not None:
484 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300485 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300486 ]
487 else:
488 stages = [
489 discover_stage,
490 log_nodes_statistic,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300491 connect_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300492 deploy_sensors_stage,
493 run_tests_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300494 store_raw_results_stage
koder aka kdanilov66839a92015-04-11 13:22:31 +0300495 ]
496
koder aka kdanilove87ae652015-04-20 02:14:35 +0300497 report_stages = [
498 console_report_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300499 ]
500
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300501 if not opts.no_html_report:
502 report_stages.append(html_report_stage)
503
koder aka kdanilovcee43342015-04-14 22:52:53 +0300504 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300505
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300506 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
507 level = logging.DEBUG
508 else:
509 level = logging.WARNING
510
511 setup_loggers(level, cfg_dict['log_file'])
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300512
koder aka kdanilov652cd802015-04-13 12:21:07 +0300513 logger.info("All info would be stored into {0}".format(
514 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300515
koder aka kdanilovda45e882015-04-06 02:24:42 +0300516 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300517 ctx.build_meta['build_id'] = opts.build_id
518 ctx.build_meta['build_descrption'] = opts.build_description
519 ctx.build_meta['build_type'] = opts.build_type
520 ctx.build_meta['username'] = opts.username
koder aka kdanilove87ae652015-04-20 02:14:35 +0300521
koder aka kdanilov168f6092015-04-19 02:33:38 +0300522 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300523 cfg_dict['no_tests'] = opts.no_tests
524 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300525
koder aka kdanilovda45e882015-04-06 02:24:42 +0300526 try:
527 for stage in stages:
528 logger.info("Start {0.__name__} stage".format(stage))
529 stage(cfg_dict, ctx)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300530 except Exception as exc:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300531 emsg = exc.message
532 if emsg == "":
533 emsg = str(exc)
534 msg = "Exception during {0.__name__}: {1}".format(stage, emsg)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300535 logger.error(msg)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300536 finally:
537 exc, cls, tb = sys.exc_info()
538 for stage in ctx.clear_calls_stack[::-1]:
539 try:
540 logger.info("Start {0.__name__} stage".format(stage))
541 stage(cfg_dict, ctx)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300542 except Exception as exc:
543 logger.exception("During {0.__name__} stage".format(stage))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300544
koder aka kdanilovda45e882015-04-06 02:24:42 +0300545 if exc is not None:
546 raise exc, cls, tb
koder aka kdanilov2c473092015-03-29 17:12:13 +0300547
koder aka kdanilove87ae652015-04-20 02:14:35 +0300548 for report_stage in report_stages:
549 report_stage(cfg_dict, ctx)
550
551 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove06762a2015-03-22 23:32:09 +0200552 return 0