blob: e0e37bcb01a7eed746220234e3b88a0110029f6c [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 kdanilov7acd6bd2015-02-12 14:28:30 -08008import argparse
koder aka kdanilov168f6092015-04-19 02:33:38 +03009import functools
koder aka kdanilov2c473092015-03-29 17:12:13 +030010import threading
koder aka kdanilov168f6092015-04-19 02:33:38 +030011import contextlib
koder aka kdanilov7306c642015-04-23 15:29:45 +030012import subprocess
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:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030057 conn_timeout = 240
koder aka kdanilov168f6092015-04-19 02:33:38 +030058 else:
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030059 conn_timeout = 30
koder aka kdanilov168f6092015-04-19 02:33:38 +030060
61 node.connection = ssh_utils.connect(url,
koder aka kdanilov6b1341a2015-04-21 22:44:21 +030062 conn_timeout=conn_timeout)
koder aka kdanilov2c473092015-03-29 17:12:13 +030063 else:
64 raise ValueError("Unknown url type {0}".format(node.conn_url))
koder aka kdanilove87ae652015-04-20 02:14:35 +030065 except Exception as exc:
66 # logger.exception("During connect to " + node.get_conn_id())
koder aka kdanilovec1b9732015-04-23 20:43:29 +030067 msg = "During connect to {0}: {1!s}".format(node.get_conn_id(),
68 exc)
koder aka kdanilove87ae652015-04-20 02:14:35 +030069 logger.error(msg)
koder aka kdanilov168f6092015-04-19 02:33:38 +030070 node.connection = None
koder aka kdanilov5d589b42015-03-26 12:25:51 +020071
72
koder aka kdanilov168f6092015-04-19 02:33:38 +030073def connect_all(nodes, vm=False):
koder aka kdanilov2c473092015-03-29 17:12:13 +030074 logger.info("Connecting to nodes")
75 with ThreadPoolExecutor(32) as pool:
koder aka kdanilov168f6092015-04-19 02:33:38 +030076 connect_one_f = functools.partial(connect_one, vm=vm)
77 list(pool.map(connect_one_f, nodes))
koder aka kdanilov2c473092015-03-29 17:12:13 +030078
79
koder aka kdanilov652cd802015-04-13 12:21:07 +030080def test_thread(test, node, barrier, res_q):
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030081 exc = None
koder aka kdanilov2c473092015-03-29 17:12:13 +030082 try:
koder aka kdanilova047e1b2015-04-21 23:16:59 +030083 logger.debug("Run preparation for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030084 test.pre_run()
koder aka kdanilova047e1b2015-04-21 23:16:59 +030085 logger.debug("Run test for {0}".format(node.get_conn_id()))
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030086 test.run(barrier)
koder aka kdanilov652cd802015-04-13 12:21:07 +030087 except Exception as exc:
koder aka kdanilov2c473092015-03-29 17:12:13 +030088 logger.exception("In test {0} for node {1}".format(test, node))
89
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030090 try:
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030091 test.cleanup()
koder aka kdanilov4500a5f2015-04-17 16:55:17 +030092 except:
93 msg = "Duringf cleanup - in test {0} for node {1}"
94 logger.exception(msg.format(test, node))
95
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030096 if exc is not None:
97 res_q.put(exc)
koder aka kdanilov2c473092015-03-29 17:12:13 +030098
koder aka kdanilov4d4771c2015-04-23 01:32:02 +030099
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300100def run_tests(cfg, 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 kdanilovec1b9732015-04-23 20:43:29 +0300117 coord_q = Queue.Queue()
118 test_cls = tool_type_mapper[name]
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300119 rem_folder = cfg['default_test_local_folder'].format(name=name)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300120
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300121 for idx, node in enumerate(test_nodes):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300122 msg = "Starting {0} test on {1} node"
123 logger.debug(msg.format(name, node.conn_url))
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300124
125 dr = os.path.join(
126 cfg_dict['test_log_directory'],
127 "{0}_{1}_{2}".format(name, test_num, node.get_ip())
128 )
129
130 if not os.path.exists(dr):
131 os.makedirs(dr)
132
koder aka kdanilovabd6ead2015-04-24 02:03:07 +0300133 test = test_cls(options=params,
134 is_primary=(idx == 0),
135 on_result_cb=res_q.put,
136 test_uuid=cfg['run_uuid'],
137 node=node,
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300138 remote_dir=rem_folder,
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300139 log_directory=dr,
140 coordination_queue=coord_q)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300141 th = threading.Thread(None, test_thread, None,
142 (test, node, barrier, res_q))
143 threads.append(th)
144 th.daemon = True
145 th.start()
koder aka kdanilov2c473092015-03-29 17:12:13 +0300146
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300147 th = threading.Thread(None, test_cls.coordination_th, None,
148 (coord_q, barrier, len(threads)))
149 threads.append(th)
150 th.daemon = True
151 th.start()
152
koder aka kdanilovcee43342015-04-14 22:52:53 +0300153 def gather_results(res_q, results):
154 while not res_q.empty():
155 val = res_q.get()
koder aka kdanilov66839a92015-04-11 13:22:31 +0300156
koder aka kdanilovcee43342015-04-14 22:52:53 +0300157 if isinstance(val, Exception):
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300158 msg = "Exception during test execution: {0!s}"
159 raise ValueError(msg.format(val))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300160
koder aka kdanilovcee43342015-04-14 22:52:53 +0300161 results.append(val)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300162
koder aka kdanilovcee43342015-04-14 22:52:53 +0300163 results = []
koder aka kdanilov652cd802015-04-13 12:21:07 +0300164
koder aka kdanilove87ae652015-04-20 02:14:35 +0300165 # MAX_WAIT_TIME = 10
166 # end_time = time.time() + MAX_WAIT_TIME
167
168 # while time.time() < end_time:
koder aka kdanilovcee43342015-04-14 22:52:53 +0300169 while True:
170 for th in threads:
171 th.join(1)
172 gather_results(res_q, results)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300173 # if time.time() > end_time:
174 # break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300175
koder aka kdanilovcee43342015-04-14 22:52:53 +0300176 if all(not th.is_alive() for th in threads):
177 break
koder aka kdanilov652cd802015-04-13 12:21:07 +0300178
koder aka kdanilove87ae652015-04-20 02:14:35 +0300179 # if any(th.is_alive() for th in threads):
180 # logger.warning("Some test threads still running")
181
koder aka kdanilovcee43342015-04-14 22:52:53 +0300182 gather_results(res_q, results)
183 yield name, test.merge_results(results)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300184
185
koder aka kdanilovda45e882015-04-06 02:24:42 +0300186def log_nodes_statistic(_, ctx):
187 nodes = ctx.nodes
koder aka kdanilov2c473092015-03-29 17:12:13 +0300188 logger.info("Found {0} nodes total".format(len(nodes)))
189 per_role = collections.defaultdict(lambda: 0)
190 for node in nodes:
191 for role in node.roles:
192 per_role[role] += 1
193
194 for role, count in sorted(per_role.items()):
195 logger.debug("Found {0} nodes with role {1}".format(count, role))
196
197
koder aka kdanilovda45e882015-04-06 02:24:42 +0300198def connect_stage(cfg, ctx):
199 ctx.clear_calls_stack.append(disconnect_stage)
200 connect_all(ctx.nodes)
201
koder aka kdanilov168f6092015-04-19 02:33:38 +0300202 all_ok = True
koder aka kdanilovda45e882015-04-06 02:24:42 +0300203
koder aka kdanilov168f6092015-04-19 02:33:38 +0300204 for node in ctx.nodes:
205 if node.connection is None:
206 if 'testnode' in node.roles:
207 msg = "Can't connect to testnode {0}"
208 raise RuntimeError(msg.format(node.get_conn_id()))
209 else:
210 msg = "Node {0} would be excluded - can't connect"
211 logger.warning(msg.format(node.get_conn_id()))
212 all_ok = False
213
214 if all_ok:
215 logger.info("All nodes connected successfully")
216
217 ctx.nodes = [node for node in ctx.nodes
218 if node.connection is not None]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300219
220
koder aka kdanilovda45e882015-04-06 02:24:42 +0300221def discover_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300222 if cfg.get('discover') is not None:
koder aka kdanilovda45e882015-04-06 02:24:42 +0300223 discover_objs = [i.strip() for i in cfg['discover'].strip().split(",")]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300224
koder aka kdanilove87ae652015-04-20 02:14:35 +0300225 nodes, clean_data = discover(ctx,
226 discover_objs,
227 cfg['clouds'],
228 cfg['var_dir'],
229 not cfg['dont_discover_nodes'])
koder aka kdanilov168f6092015-04-19 02:33:38 +0300230
231 def undiscover_stage(cfg, ctx):
232 undiscover(clean_data)
233
234 ctx.clear_calls_stack.append(undiscover_stage)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300235 ctx.nodes.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300236
237 for url, roles in cfg.get('explicit_nodes', {}).items():
238 ctx.nodes.append(Node(url, roles.split(",")))
239
240
koder aka kdanilove87ae652015-04-20 02:14:35 +0300241def get_OS_credentials(cfg, ctx, creds_type):
koder aka kdanilovcee43342015-04-14 22:52:53 +0300242 creds = None
koder aka kdanilovda45e882015-04-06 02:24:42 +0300243
koder aka kdanilovcee43342015-04-14 22:52:53 +0300244 if creds_type == 'clouds':
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300245 logger.info("Using OS credentials from 'cloud' section")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300246 if 'openstack' in cfg['clouds']:
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300247 os_cfg = cfg['clouds']['openstack']
koder aka kdanilovcee43342015-04-14 22:52:53 +0300248
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300249 tenant = os_cfg['OS_TENANT_NAME'].strip()
250 user = os_cfg['OS_USERNAME'].strip()
251 passwd = os_cfg['OS_PASSWORD'].strip()
252 auth_url = os_cfg['OS_AUTH_URL'].strip()
253
koder aka kdanilovcee43342015-04-14 22:52:53 +0300254 elif 'fuel' in cfg['clouds'] and \
255 'openstack_env' in cfg['clouds']['fuel']:
256 creds = ctx.fuel_openstack_creds
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300257
koder aka kdanilovcee43342015-04-14 22:52:53 +0300258 elif creds_type == 'ENV':
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300259 logger.info("Using OS credentials from shell environment")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300260 user, passwd, tenant, auth_url = start_vms.ostack_get_creds()
261 elif os.path.isfile(creds_type):
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300262 logger.info("Using OS credentials from " + creds_type)
koder aka kdanilov7306c642015-04-23 15:29:45 +0300263 fc = open(creds_type).read()
264
265 echo = 'echo "$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
266
267 p = subprocess.Popen(['/bin/bash'], shell=False,
268 stdout=subprocess.PIPE,
269 stdin=subprocess.PIPE,
270 stderr=subprocess.STDOUT)
271 p.stdin.write(fc + "\n" + echo)
272 p.stdin.close()
273 code = p.wait()
274 data = p.stdout.read().strip()
275
276 if code != 0:
277 msg = "Failed to get creads from openrc file: " + data
278 logger.error(msg)
279 raise RuntimeError(msg)
280
281 try:
282 user, tenant, passwd_auth_url = data.split(':', 2)
283 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
284 assert (auth_url.startswith("https://") or
285 auth_url.startswith("http://"))
286 except Exception:
287 msg = "Failed to get creads from openrc file: " + data
288 logger.exception(msg)
289 raise
290
koder aka kdanilovcee43342015-04-14 22:52:53 +0300291 else:
292 msg = "Creds {0!r} isn't supported".format(creds_type)
293 raise ValueError(msg)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300294
koder aka kdanilovcee43342015-04-14 22:52:53 +0300295 if creds is None:
296 creds = {'name': user,
297 'passwd': passwd,
298 'tenant': tenant,
299 'auth_url': auth_url}
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300300
koder aka kdanilov46d4f392015-04-24 11:35:00 +0300301 msg = "OS_CREDS: user={name} tenant={tenant} auth_url={auth_url}"
302 logger.debug(msg.format(**creds))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300303 return creds
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300304
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300305
koder aka kdanilov168f6092015-04-19 02:33:38 +0300306@contextlib.contextmanager
307def create_vms_ctx(ctx, cfg, config):
308 params = config['vm_params'].copy()
309 os_nodes_ids = []
310
311 os_creds_type = config['creds']
koder aka kdanilove87ae652015-04-20 02:14:35 +0300312 os_creds = get_OS_credentials(cfg, ctx, os_creds_type)
koder aka kdanilov168f6092015-04-19 02:33:38 +0300313
314 start_vms.nova_connect(**os_creds)
315
316 logger.info("Preparing openstack")
317 start_vms.prepare_os_subpr(**os_creds)
318
319 new_nodes = []
320 try:
321 params['group_name'] = cfg_dict['run_uuid']
322 for new_node, node_id in start_vms.launch_vms(params):
323 new_node.roles.append('testnode')
324 ctx.nodes.append(new_node)
325 os_nodes_ids.append(node_id)
326 new_nodes.append(new_node)
327
328 store_nodes_in_log(cfg, os_nodes_ids)
329 ctx.openstack_nodes_ids = os_nodes_ids
330
331 yield new_nodes
332
333 finally:
334 if not cfg['keep_vm']:
335 shut_down_vms_stage(cfg, ctx)
336
337
koder aka kdanilovcee43342015-04-14 22:52:53 +0300338def run_tests_stage(cfg, ctx):
339 ctx.results = []
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300340
koder aka kdanilovcee43342015-04-14 22:52:53 +0300341 if 'tests' not in cfg:
342 return
gstepanov023c1e42015-04-08 15:50:19 +0300343
koder aka kdanilovcee43342015-04-14 22:52:53 +0300344 for group in cfg['tests']:
345
346 assert len(group.items()) == 1
347 key, config = group.items()[0]
348
349 if 'start_test_nodes' == key:
koder aka kdanilov168f6092015-04-19 02:33:38 +0300350 with create_vms_ctx(ctx, cfg, config) as new_nodes:
351 connect_all(new_nodes, True)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300352
koder aka kdanilov168f6092015-04-19 02:33:38 +0300353 for node in new_nodes:
354 if node.connection is None:
355 msg = "Failed to connect to vm {0}"
356 raise RuntimeError(msg.format(node.get_conn_id()))
koder aka kdanilovcee43342015-04-14 22:52:53 +0300357
koder aka kdanilov168f6092015-04-19 02:33:38 +0300358 deploy_sensors_stage(cfg_dict,
359 ctx,
360 nodes=new_nodes,
361 undeploy=False)
koder aka kdanilov12ae0632015-04-15 01:13:43 +0300362
koder aka kdanilove87ae652015-04-20 02:14:35 +0300363 if not cfg['no_tests']:
364 for test_group in config.get('tests', []):
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300365 test_res = run_tests(cfg, test_group, ctx.nodes)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300366 ctx.results.extend(test_res)
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300367 else:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300368 if not cfg['no_tests']:
koder aka kdanilov2066daf2015-04-23 21:05:41 +0300369 test_res = run_tests(cfg, group, ctx.nodes)
koder aka kdanilov4d4771c2015-04-23 01:32:02 +0300370 ctx.results.extend(test_res)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300371
gstepanov023c1e42015-04-08 15:50:19 +0300372
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300373def shut_down_vms_stage(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300374 vm_ids_fname = cfg_dict['vm_ids_fname']
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300375 if ctx.openstack_nodes_ids is None:
koder aka kdanilov66839a92015-04-11 13:22:31 +0300376 nodes_ids = open(vm_ids_fname).read().split()
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300377 else:
378 nodes_ids = ctx.openstack_nodes_ids
379
koder aka kdanilov652cd802015-04-13 12:21:07 +0300380 if len(nodes_ids) != 0:
381 logger.info("Removing nodes")
382 start_vms.clear_nodes(nodes_ids)
383 logger.info("Nodes has been removed")
gstepanov023c1e42015-04-08 15:50:19 +0300384
koder aka kdanilov66839a92015-04-11 13:22:31 +0300385 if os.path.exists(vm_ids_fname):
386 os.remove(vm_ids_fname)
gstepanov023c1e42015-04-08 15:50:19 +0300387
koder aka kdanilov66839a92015-04-11 13:22:31 +0300388
389def store_nodes_in_log(cfg, nodes_ids):
390 with open(cfg['vm_ids_fname'], 'w') as fd:
391 fd.write("\n".join(nodes_ids))
gstepanov023c1e42015-04-08 15:50:19 +0300392
393
394def clear_enviroment(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300395 if os.path.exists(cfg_dict['vm_ids_fname']):
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300396 shut_down_vms_stage(cfg, ctx)
gstepanov023c1e42015-04-08 15:50:19 +0300397
398
koder aka kdanilovda45e882015-04-06 02:24:42 +0300399def disconnect_stage(cfg, ctx):
koder aka kdanilov652cd802015-04-13 12:21:07 +0300400 ssh_utils.close_all_sessions()
401
koder aka kdanilovda45e882015-04-06 02:24:42 +0300402 for node in ctx.nodes:
403 if node.connection is not None:
404 node.connection.close()
405
406
koder aka kdanilov66839a92015-04-11 13:22:31 +0300407def store_raw_results_stage(cfg, ctx):
408
409 raw_results = os.path.join(cfg_dict['var_dir'], 'raw_results.yaml')
410
411 if os.path.exists(raw_results):
412 cont = yaml.load(open(raw_results).read())
413 else:
414 cont = []
415
koder aka kdanilov168f6092015-04-19 02:33:38 +0300416 cont.extend(utils.yamable(ctx.results))
koder aka kdanilov66839a92015-04-11 13:22:31 +0300417 raw_data = pretty_yaml.dumps(cont)
418
419 with open(raw_results, "w") as fd:
420 fd.write(raw_data)
421
422
423def console_report_stage(cfg, ctx):
424 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300425 if 'io' == tp and data is not None:
koder aka kdanilove87ae652015-04-20 02:14:35 +0300426 print("\n")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300427 print(IOPerfTest.format_for_console(data))
koder aka kdanilove87ae652015-04-20 02:14:35 +0300428 print("\n")
koder aka kdanilov66839a92015-04-11 13:22:31 +0300429
430
koder aka kdanilove87ae652015-04-20 02:14:35 +0300431def html_report_stage(cfg, ctx):
Yulia Portnova8ca20572015-04-14 14:09:39 +0300432 html_rep_fname = cfg['html_report_file']
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300433
434 try:
435 fuel_url = cfg['clouds']['fuel']['url']
436 except KeyError:
437 fuel_url = None
438
439 try:
440 creds = cfg['clouds']['fuel']['creds']
441 except KeyError:
442 creds = None
443
gstepanov69339ac2015-04-16 20:09:33 +0300444 report.make_io_report(ctx.results, html_rep_fname, fuel_url, creds=creds)
koder aka kdanilov652cd802015-04-13 12:21:07 +0300445
koder aka kdanilov652cd802015-04-13 12:21:07 +0300446 text_rep_fname = cfg_dict['text_report_file']
447 with open(text_rep_fname, "w") as fd:
448 for tp, data in ctx.results:
koder aka kdanilov4500a5f2015-04-17 16:55:17 +0300449 if 'io' == tp and data is not None:
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300450 fd.write(IOPerfTest.format_for_console(data))
koder aka kdanilov652cd802015-04-13 12:21:07 +0300451 fd.write("\n")
452 fd.flush()
453
454 logger.info("Text report were stored in " + text_rep_fname)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300455
456
457def complete_log_nodes_statistic(cfg, ctx):
458 nodes = ctx.nodes
459 for node in nodes:
460 logger.debug(str(node))
461
462
koder aka kdanilov66839a92015-04-11 13:22:31 +0300463def load_data_from(var_dir):
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300464 def load_data_from_file(cfg, ctx):
koder aka kdanilov66839a92015-04-11 13:22:31 +0300465 raw_results = os.path.join(var_dir, 'raw_results.yaml')
466 ctx.results = yaml.load(open(raw_results).read())
koder aka kdanilov4e9f3ed2015-04-14 11:26:12 +0300467 return load_data_from_file
gstepanovcd256d62015-04-07 17:47:32 +0300468
469
koder aka kdanilovcee43342015-04-14 22:52:53 +0300470def parse_args(argv):
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300471 descr = "Disk io performance test suite"
472 parser = argparse.ArgumentParser(prog='wally', description=descr)
koder aka kdanilovcee43342015-04-14 22:52:53 +0300473
474 parser.add_argument("-l", dest='extra_logs',
475 action='store_true', default=False,
476 help="print some extra log info")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300477 parser.add_argument("-b", '--build_description',
478 type=str, default="Build info")
479 parser.add_argument("-i", '--build_id', type=str, default="id")
480 parser.add_argument("-t", '--build_type', type=str, default="GA")
481 parser.add_argument("-u", '--username', type=str, default="admin")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300482 parser.add_argument("-n", '--no-tests', action='store_true',
483 help="Don't run tests", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300484 parser.add_argument("-p", '--post-process-only', metavar="VAR_DIR",
485 help="Only process data from previour run")
486 parser.add_argument("-k", '--keep-vm', action='store_true',
487 help="Don't remove test vm's", default=False)
koder aka kdanilove87ae652015-04-20 02:14:35 +0300488 parser.add_argument("-d", '--dont-discover-nodes', action='store_true',
489 help="Don't connect/discover fuel nodes",
490 default=False)
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300491 parser.add_argument("-r", '--no-html-report', action='store_true',
492 help="Skip html report", default=False)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300493 parser.add_argument("config_file")
koder aka kdanilovcee43342015-04-14 22:52:53 +0300494
495 return parser.parse_args(argv[1:])
496
497
koder aka kdanilov3f356262015-02-13 08:06:14 -0800498def main(argv):
koder aka kdanilove06762a2015-03-22 23:32:09 +0200499 opts = parse_args(argv)
koder aka kdanilov2c473092015-03-29 17:12:13 +0300500
koder aka kdanilov66839a92015-04-11 13:22:31 +0300501 if opts.post_process_only is not None:
502 stages = [
koder aka kdanilove87ae652015-04-20 02:14:35 +0300503 load_data_from(opts.post_process_only)
koder aka kdanilov66839a92015-04-11 13:22:31 +0300504 ]
505 else:
506 stages = [
507 discover_stage,
508 log_nodes_statistic,
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300509 connect_stage,
koder aka kdanilov66839a92015-04-11 13:22:31 +0300510 deploy_sensors_stage,
511 run_tests_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300512 store_raw_results_stage
koder aka kdanilov66839a92015-04-11 13:22:31 +0300513 ]
514
koder aka kdanilove87ae652015-04-20 02:14:35 +0300515 report_stages = [
516 console_report_stage,
koder aka kdanilove87ae652015-04-20 02:14:35 +0300517 ]
518
koder aka kdanilova047e1b2015-04-21 23:16:59 +0300519 if not opts.no_html_report:
520 report_stages.append(html_report_stage)
521
koder aka kdanilovcee43342015-04-14 22:52:53 +0300522 load_config(opts.config_file, opts.post_process_only)
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300523
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300524 if cfg_dict.get('logging', {}).get("extra_logs", False) or opts.extra_logs:
525 level = logging.DEBUG
526 else:
527 level = logging.WARNING
528
529 setup_loggers(level, cfg_dict['log_file'])
koder aka kdanilovf4b82c22015-04-11 13:35:25 +0300530
koder aka kdanilov652cd802015-04-13 12:21:07 +0300531 logger.info("All info would be stored into {0}".format(
532 cfg_dict['var_dir']))
gstepanovcd256d62015-04-07 17:47:32 +0300533
koder aka kdanilovda45e882015-04-06 02:24:42 +0300534 ctx = Context()
gstepanovaffcdb12015-04-07 17:18:29 +0300535 ctx.build_meta['build_id'] = opts.build_id
536 ctx.build_meta['build_descrption'] = opts.build_description
537 ctx.build_meta['build_type'] = opts.build_type
538 ctx.build_meta['username'] = opts.username
koder aka kdanilove87ae652015-04-20 02:14:35 +0300539
koder aka kdanilov168f6092015-04-19 02:33:38 +0300540 cfg_dict['keep_vm'] = opts.keep_vm
koder aka kdanilove87ae652015-04-20 02:14:35 +0300541 cfg_dict['no_tests'] = opts.no_tests
542 cfg_dict['dont_discover_nodes'] = opts.dont_discover_nodes
koder aka kdanilov6c491062015-04-09 22:33:13 +0300543
koder aka kdanilovda45e882015-04-06 02:24:42 +0300544 try:
545 for stage in stages:
546 logger.info("Start {0.__name__} stage".format(stage))
547 stage(cfg_dict, ctx)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300548 except Exception as exc:
koder aka kdanilovec1b9732015-04-23 20:43:29 +0300549 msg = "Exception during {0.__name__}: {1!s}".format(stage, exc)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +0300550 logger.error(msg)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300551 finally:
552 exc, cls, tb = sys.exc_info()
553 for stage in ctx.clear_calls_stack[::-1]:
554 try:
555 logger.info("Start {0.__name__} stage".format(stage))
556 stage(cfg_dict, ctx)
koder aka kdanilov1c2b5112015-04-10 16:53:51 +0300557 except Exception as exc:
558 logger.exception("During {0.__name__} stage".format(stage))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300559
koder aka kdanilovda45e882015-04-06 02:24:42 +0300560 if exc is not None:
561 raise exc, cls, tb
koder aka kdanilov2c473092015-03-29 17:12:13 +0300562
koder aka kdanilove87ae652015-04-20 02:14:35 +0300563 for report_stage in report_stages:
564 report_stage(cfg_dict, ctx)
565
566 logger.info("All info stored in {0} folder".format(cfg_dict['var_dir']))
koder aka kdanilove06762a2015-03-22 23:32:09 +0200567 return 0