koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 1 | import time |
| 2 | import json |
| 3 | import os.path |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 4 | import logging |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 5 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 6 | from concurrent.futures import ThreadPoolExecutor, wait |
| 7 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 8 | from disk_perf_test_tool.ssh_utils import connect, copy_paths |
| 9 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 10 | logger = logging.getLogger('io-perf-tool') |
| 11 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 12 | |
| 13 | def wait_all_ok(futures): |
| 14 | return all(future.result() for future in futures) |
| 15 | |
| 16 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 17 | def deploy_and_start_sensors(monitor_uri, config, |
| 18 | remote_path='/tmp/sensors', |
| 19 | connected_config=None): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 20 | paths = {os.path.dirname(__file__): remote_path} |
| 21 | with ThreadPoolExecutor(max_workers=32) as executor: |
| 22 | futures = [] |
| 23 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 24 | if connected_config is not None: |
| 25 | assert config is None |
| 26 | node_iter = connected_config |
| 27 | else: |
| 28 | node_iter = config.items() |
| 29 | |
| 30 | for uri_or_conn, config in node_iter: |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 31 | futures.append(executor.submit(deploy_and_start_sensor, |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 32 | paths, uri_or_conn, |
| 33 | monitor_uri, |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 34 | config, remote_path)) |
| 35 | |
| 36 | if not wait_all_ok(futures): |
| 37 | raise RuntimeError("Sensor deployment fails on some nodes") |
| 38 | |
| 39 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 40 | def deploy_and_start_sensor(paths, uri_or_conn, monitor_uri, config, |
| 41 | remote_path): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 42 | try: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 43 | if isinstance(uri_or_conn, basestring): |
| 44 | conn = connect(uri_or_conn) |
| 45 | else: |
| 46 | conn = uri_or_conn |
| 47 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 48 | copy_paths(conn, paths) |
| 49 | sftp = conn.open_sftp() |
| 50 | |
| 51 | config_remote_path = os.path.join(remote_path, "conf.json") |
| 52 | main_remote_path = os.path.join(remote_path, "main.py") |
| 53 | |
| 54 | with sftp.open(config_remote_path, "w") as fd: |
| 55 | fd.write(json.dumps(config)) |
| 56 | |
| 57 | cmd_templ = "python {0} -d start -u {1} {2}" |
| 58 | cmd = cmd_templ.format(main_remote_path, |
| 59 | monitor_uri, |
| 60 | config_remote_path) |
| 61 | conn.exec_command(cmd) |
| 62 | sftp.close() |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 63 | |
| 64 | if isinstance(uri_or_conn, basestring): |
| 65 | conn.close() |
koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 66 | except: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 67 | logger.exception("During deploing sensors in {0}".format(uri_or_conn)) |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 68 | return False |
| 69 | return True |
| 70 | |
| 71 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 72 | def stop_and_remove_sensor(uri_or_conn, remote_path): |
| 73 | if isinstance(uri_or_conn, basestring): |
| 74 | conn = connect(uri_or_conn) |
| 75 | else: |
| 76 | conn = uri_or_conn |
| 77 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 78 | main_remote_path = os.path.join(remote_path, "main.py") |
| 79 | |
| 80 | cmd_templ = "python {0} -d stop" |
| 81 | conn.exec_command(cmd_templ.format(main_remote_path)) |
koder aka kdanilov | e4ade1a | 2015-03-16 20:44:16 +0200 | [diff] [blame] | 82 | |
| 83 | # some magic |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 84 | time.sleep(0.3) |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 85 | |
| 86 | conn.exec_command("rm -rf {0}".format(remote_path)) |
| 87 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 88 | if isinstance(uri_or_conn, basestring): |
| 89 | conn.close() |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 90 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 91 | logger.debug("Sensors stopped and removed") |
| 92 | |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 93 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 94 | def stop_and_remove_sensors(config, remote_path='/tmp/sensors', |
| 95 | connected_config=None): |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 96 | with ThreadPoolExecutor(max_workers=32) as executor: |
| 97 | futures = [] |
| 98 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 99 | if connected_config is not None: |
| 100 | assert config is None |
| 101 | conf_iter = connected_config |
| 102 | else: |
| 103 | conf_iter = config.items() |
| 104 | |
| 105 | for uri_or_conn, config in conf_iter: |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 106 | futures.append(executor.submit(stop_and_remove_sensor, |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 107 | uri_or_conn, remote_path)) |
koder aka kdanilov | dda86d3 | 2015-03-16 11:20:04 +0200 | [diff] [blame] | 108 | |
| 109 | wait(futures) |