koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 1 | import re |
| 2 | import Queue |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 3 | import logging |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 4 | import traceback |
| 5 | import threading |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 6 | from concurrent.futures import ThreadPoolExecutor |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 7 | |
| 8 | from utils import ssh_connect |
| 9 | |
| 10 | import itest |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 11 | from utils import get_barrier, log_error, wait_on_barrier |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 12 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 13 | |
| 14 | logger = logging.getLogger("io-perf-tool") |
| 15 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 16 | conn_uri_attrs = ("user", "passwd", "host", "port", "path") |
| 17 | |
| 18 | |
| 19 | class ConnCreds(object): |
| 20 | def __init__(self): |
| 21 | for name in conn_uri_attrs: |
| 22 | setattr(self, name, None) |
| 23 | |
| 24 | |
| 25 | uri_reg_exprs = [] |
| 26 | |
| 27 | |
| 28 | class URIsNamespace(object): |
| 29 | class ReParts(object): |
| 30 | user_rr = "[^:]*?" |
| 31 | host_rr = "[^:]*?" |
| 32 | port_rr = "\\d+" |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 33 | key_file_rr = "[^:@]*" |
| 34 | passwd_rr = ".*?" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 35 | |
| 36 | re_dct = ReParts.__dict__ |
| 37 | |
| 38 | for attr_name, val in re_dct.items(): |
| 39 | if attr_name.endswith('_rr'): |
| 40 | new_rr = "(?P<{0}>{1})".format(attr_name[:-3], val) |
| 41 | setattr(ReParts, attr_name, new_rr) |
| 42 | |
| 43 | re_dct = ReParts.__dict__ |
| 44 | |
| 45 | templs = [ |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 46 | "^{host_rr}$", |
| 47 | "^{user_rr}@{host_rr}::{key_file_rr}$", |
| 48 | "^{user_rr}@{host_rr}:{port_rr}:{key_file_rr}$", |
| 49 | "^{user_rr}:{passwd_rr}@@{host_rr}$", |
| 50 | "^{user_rr}:{passwd_rr}@@{host_rr}:{port_rr}$", |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 51 | ] |
| 52 | |
| 53 | for templ in templs: |
| 54 | uri_reg_exprs.append(templ.format(**re_dct)) |
| 55 | |
| 56 | |
| 57 | def parse_ssh_uri(uri): |
| 58 | # user:passwd@ip_host:port |
| 59 | # user:passwd@ip_host |
| 60 | # user@ip_host:port |
| 61 | # user@ip_host |
| 62 | # ip_host:port |
| 63 | # ip_host |
| 64 | # user@ip_host:port:path_to_key_file |
| 65 | # user@ip_host::path_to_key_file |
| 66 | # ip_host:port:path_to_key_file |
| 67 | # ip_host::path_to_key_file |
| 68 | |
| 69 | res = ConnCreds() |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 70 | res.port = "22" |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 71 | |
| 72 | for rr in uri_reg_exprs: |
| 73 | rrm = re.match(rr, uri) |
| 74 | if rrm is not None: |
| 75 | res.__dict__.update(rrm.groupdict()) |
| 76 | return res |
| 77 | raise ValueError("Can't parse {0!r} as ssh uri value".format(uri)) |
| 78 | |
| 79 | |
| 80 | def connect(uri): |
| 81 | creds = parse_ssh_uri(uri) |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 82 | creds.port = int(creds.port) |
| 83 | return ssh_connect(creds) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def conn_func(obj, barrier, latest_start_time, conn): |
| 87 | try: |
| 88 | test_iter = itest.run_test_iter(obj, conn) |
| 89 | next(test_iter) |
| 90 | |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 91 | wait_on_barrier(barrier, latest_start_time) |
| 92 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 93 | with log_error("!Run test"): |
| 94 | return next(test_iter) |
| 95 | except: |
| 96 | print traceback.format_exc() |
| 97 | raise |
| 98 | |
| 99 | |
| 100 | def get_ssh_runner(uris, |
| 101 | latest_start_time=None, |
| 102 | keep_temp_files=False): |
| 103 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 104 | logger.debug("Connecting to servers") |
| 105 | |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 106 | with ThreadPoolExecutor(max_workers=16) as executor: |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 107 | connections = list(executor.map(connect, uris)) |
koder aka kdanilov | 97644f9 | 2015-02-13 11:11:08 -0800 | [diff] [blame] | 108 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 109 | result_queue = Queue.Queue() |
| 110 | barrier = get_barrier(len(uris), threaded=True) |
| 111 | |
| 112 | def closure(obj): |
| 113 | ths = [] |
| 114 | obj.set_result_cb(result_queue.put) |
| 115 | |
| 116 | params = (obj, barrier, latest_start_time) |
| 117 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 118 | logger.debug("Start tests") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 119 | for conn in connections: |
| 120 | th = threading.Thread(None, conn_func, None, |
| 121 | params + (conn,)) |
| 122 | th.daemon = True |
| 123 | th.start() |
| 124 | ths.append(th) |
| 125 | |
| 126 | for th in ths: |
| 127 | th.join() |
| 128 | |
| 129 | test_result = [] |
| 130 | while not result_queue.empty(): |
| 131 | test_result.append(result_queue.get()) |
| 132 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 133 | logger.debug("Done. Closing connection") |
koder aka kdanilov | 3f35626 | 2015-02-13 08:06:14 -0800 | [diff] [blame] | 134 | for conn in connections: |
| 135 | conn.close() |
| 136 | |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 137 | return test_result |
| 138 | |
| 139 | return closure |