koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 1 | import time |
koder aka kdanilov | 4ec0f71 | 2015-02-19 19:19:27 -0800 | [diff] [blame] | 2 | import socket |
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 threading |
| 5 | import contextlib |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 6 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 7 | |
| 8 | logger = logging.getLogger("io-perf-tool") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 9 | |
| 10 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 11 | def parse_creds(creds): |
| 12 | # parse user:passwd@host |
| 13 | user, passwd_host = creds.split(":", 1) |
| 14 | |
| 15 | if '@' not in passwd_host: |
| 16 | passwd, host = passwd_host, None |
| 17 | else: |
| 18 | passwd, host = passwd_host.rsplit('@', 1) |
| 19 | |
| 20 | return user, passwd, host |
| 21 | |
| 22 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 23 | class TaksFinished(Exception): |
| 24 | pass |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 25 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 26 | |
| 27 | class Barrier(object): |
| 28 | def __init__(self, count): |
| 29 | self.count = count |
| 30 | self.curr_count = 0 |
| 31 | self.cond = threading.Condition() |
| 32 | self.exited = False |
| 33 | |
| 34 | def wait(self, timeout=None): |
| 35 | with self.cond: |
| 36 | if self.exited: |
| 37 | raise TaksFinished() |
| 38 | |
| 39 | self.curr_count += 1 |
| 40 | if self.curr_count == self.count: |
| 41 | self.curr_count = 0 |
| 42 | self.cond.notify_all() |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 43 | else: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 44 | self.cond.wait(timeout=timeout) |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 45 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 46 | def exit(self): |
| 47 | with self.cond: |
| 48 | self.exited = True |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 49 | |
| 50 | |
| 51 | @contextlib.contextmanager |
| 52 | def log_error(action, types=(Exception,)): |
| 53 | if not action.startswith("!"): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 54 | logger.debug("Starts : " + action) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 55 | else: |
| 56 | action = action[1:] |
| 57 | |
| 58 | try: |
| 59 | yield |
| 60 | except Exception as exc: |
| 61 | if isinstance(exc, types) and not isinstance(exc, StopIteration): |
| 62 | templ = "Error during {0} stage: {1}" |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 63 | logger.debug(templ.format(action, exc.message)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 64 | raise |
| 65 | |
| 66 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 67 | def run_over_ssh(conn, cmd, stdin_data=None, exec_timeout=60): |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 68 | "should be replaces by normal implementation, with select" |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 69 | transport = conn.get_transport() |
| 70 | session = transport.open_session() |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 71 | try: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 72 | session.set_combine_stderr(True) |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 73 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 74 | stime = time.time() |
| 75 | session.exec_command(cmd) |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 76 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 77 | if stdin_data is not None: |
| 78 | session.sendall(stdin_data) |
koder aka kdanilov | 83cd713 | 2015-02-14 21:37:14 -0800 | [diff] [blame] | 79 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 80 | session.settimeout(1) |
| 81 | session.shutdown_write() |
| 82 | output = "" |
| 83 | |
| 84 | while True: |
| 85 | try: |
| 86 | ndata = session.recv(1024) |
| 87 | output += ndata |
| 88 | if "" == ndata: |
| 89 | break |
| 90 | except socket.timeout: |
| 91 | pass |
| 92 | if time.time() - stime > exec_timeout: |
| 93 | return 1, output + "\nExecution timeout" |
| 94 | code = session.recv_exit_status() |
| 95 | finally: |
| 96 | session.close() |
| 97 | |
| 98 | return code, output |
| 99 | |
| 100 | |
| 101 | SMAP = dict(k=1024, m=1024 ** 2, g=1024 ** 3, t=1024 ** 4) |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def ssize_to_b(ssize): |
| 105 | try: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 106 | ssize = ssize.lower() |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 107 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 108 | if ssize.endswith("b"): |
| 109 | ssize = ssize[:-1] |
| 110 | if ssize[-1] in SMAP: |
| 111 | return int(ssize[:-1]) * SMAP[ssize[-1]] |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 112 | return int(ssize) |
| 113 | except (ValueError, TypeError, AttributeError): |
| 114 | tmpl = "Unknow size format {0!r} (or size not multiples 1024)" |
| 115 | raise ValueError(tmpl.format(ssize)) |