koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 1 | import re |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 2 | import os |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 3 | import time |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 4 | import psutil |
koder aka kdanilov | afd9874 | 2015-04-24 01:27:22 +0300 | [diff] [blame] | 5 | import socket |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 6 | import logging |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 7 | import threading |
| 8 | import contextlib |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 9 | import subprocess |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 10 | |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 11 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 12 | logger = logging.getLogger("wally") |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 13 | |
| 14 | |
koder aka kdanilov | 209e85d | 2015-04-27 23:11:05 +0300 | [diff] [blame] | 15 | def is_ip(data): |
| 16 | if data.count('.') != 3: |
| 17 | return False |
| 18 | |
| 19 | try: |
| 20 | for part in map(int, data.split('.')): |
| 21 | if part > 255 or part < 0: |
| 22 | raise ValueError() |
| 23 | except ValueError: |
| 24 | return False |
| 25 | return True |
| 26 | |
| 27 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 28 | class StopTestError(RuntimeError): |
| 29 | def __init__(self, reason, orig_exc=None): |
| 30 | RuntimeError.__init__(self, reason) |
| 31 | self.orig_exc = orig_exc |
| 32 | |
| 33 | |
| 34 | def check_input_param(is_ok, message): |
| 35 | if not is_ok: |
| 36 | logger.error(message) |
| 37 | raise StopTestError(message) |
| 38 | |
| 39 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 40 | def parse_creds(creds): |
| 41 | # parse user:passwd@host |
| 42 | user, passwd_host = creds.split(":", 1) |
| 43 | |
| 44 | if '@' not in passwd_host: |
| 45 | passwd, host = passwd_host, None |
| 46 | else: |
| 47 | passwd, host = passwd_host.rsplit('@', 1) |
| 48 | |
| 49 | return user, passwd, host |
| 50 | |
| 51 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 52 | class TaksFinished(Exception): |
| 53 | pass |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 54 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 55 | |
| 56 | class Barrier(object): |
| 57 | def __init__(self, count): |
| 58 | self.count = count |
| 59 | self.curr_count = 0 |
| 60 | self.cond = threading.Condition() |
| 61 | self.exited = False |
| 62 | |
| 63 | def wait(self, timeout=None): |
| 64 | with self.cond: |
| 65 | if self.exited: |
| 66 | raise TaksFinished() |
| 67 | |
| 68 | self.curr_count += 1 |
| 69 | if self.curr_count == self.count: |
| 70 | self.curr_count = 0 |
| 71 | self.cond.notify_all() |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 72 | return True |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 73 | else: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 74 | self.cond.wait(timeout=timeout) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 75 | return False |
koder aka kdanilov | 4643fd6 | 2015-02-10 16:20:13 -0800 | [diff] [blame] | 76 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 77 | def exit(self): |
| 78 | with self.cond: |
| 79 | self.exited = True |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 80 | |
| 81 | |
| 82 | @contextlib.contextmanager |
| 83 | def log_error(action, types=(Exception,)): |
| 84 | if not action.startswith("!"): |
koder aka kdanilov | e21d747 | 2015-02-14 19:02:04 -0800 | [diff] [blame] | 85 | logger.debug("Starts : " + action) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 86 | else: |
| 87 | action = action[1:] |
| 88 | |
| 89 | try: |
| 90 | yield |
| 91 | except Exception as exc: |
| 92 | if isinstance(exc, types) and not isinstance(exc, StopIteration): |
koder aka kdanilov | ec1b973 | 2015-04-23 20:43:29 +0300 | [diff] [blame] | 93 | templ = "Error during {0} stage: {1!s}" |
| 94 | logger.debug(templ.format(action, exc)) |
koder aka kdanilov | 7acd6bd | 2015-02-12 14:28:30 -0800 | [diff] [blame] | 95 | raise |
| 96 | |
| 97 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 98 | 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] | 99 | |
| 100 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 101 | def ssize2b(ssize): |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 102 | try: |
koder aka kdanilov | 63e9c5a | 2015-04-28 23:06:07 +0300 | [diff] [blame] | 103 | if isinstance(ssize, (int, long)): |
| 104 | return ssize |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 105 | |
koder aka kdanilov | 63e9c5a | 2015-04-28 23:06:07 +0300 | [diff] [blame] | 106 | ssize = ssize.lower() |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 107 | if ssize[-1] in SMAP: |
| 108 | return int(ssize[:-1]) * SMAP[ssize[-1]] |
koder aka kdanilov | 8ad6e81 | 2015-03-22 14:42:18 +0200 | [diff] [blame] | 109 | return int(ssize) |
| 110 | except (ValueError, TypeError, AttributeError): |
koder aka kdanilov | 2e92802 | 2015-04-08 13:47:15 +0300 | [diff] [blame] | 111 | raise ValueError("Unknow size format {0!r}".format(ssize)) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 112 | |
| 113 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 114 | RSMAP = [('K', 1024), |
| 115 | ('M', 1024 ** 2), |
| 116 | ('G', 1024 ** 3), |
| 117 | ('T', 1024 ** 4)] |
| 118 | |
| 119 | |
| 120 | def b2ssize(size): |
| 121 | if size < 1024: |
| 122 | return str(size) |
| 123 | |
| 124 | for name, scale in RSMAP: |
| 125 | if size < 1024 * scale: |
| 126 | if size % scale == 0: |
| 127 | return "{0} {1}i".format(size // scale, name) |
| 128 | else: |
| 129 | return "{0:.1f} {1}i".format(float(size) / scale, name) |
| 130 | |
| 131 | return "{0}{1}i".format(size // scale, name) |
| 132 | |
| 133 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 134 | RSMAP_10 = [('k', 1000), |
| 135 | ('m', 1000 ** 2), |
| 136 | ('g', 1000 ** 3), |
| 137 | ('t', 1000 ** 4)] |
| 138 | |
| 139 | |
| 140 | def b2ssize_10(size): |
| 141 | if size < 1000: |
| 142 | return str(size) |
| 143 | |
| 144 | for name, scale in RSMAP_10: |
| 145 | if size < 1000 * scale: |
| 146 | if size % scale == 0: |
| 147 | return "{0} {1}".format(size // scale, name) |
| 148 | else: |
| 149 | return "{0:.1f} {1}".format(float(size) / scale, name) |
| 150 | |
| 151 | return "{0}{1}".format(size // scale, name) |
| 152 | |
| 153 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 154 | def run_locally(cmd, input_data="", timeout=20): |
| 155 | shell = isinstance(cmd, basestring) |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 156 | proc = subprocess.Popen(cmd, |
| 157 | shell=shell, |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 158 | stdin=subprocess.PIPE, |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 159 | stdout=subprocess.PIPE, |
| 160 | stderr=subprocess.PIPE) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 161 | res = [] |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 162 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 163 | def thread_func(): |
| 164 | rr = proc.communicate(input_data) |
| 165 | res.extend(rr) |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 166 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 167 | thread = threading.Thread(target=thread_func) |
| 168 | thread.daemon = True |
| 169 | thread.start() |
| 170 | thread.join(timeout) |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 171 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 172 | if thread.is_alive(): |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 173 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 174 | parent = psutil.Process(proc.pid) |
| 175 | for child in parent.children(recursive=True): |
| 176 | child.kill() |
| 177 | parent.kill() |
| 178 | thread.join() |
| 179 | raise RuntimeError("Local process timeout: " + str(cmd)) |
| 180 | |
| 181 | out, err = res |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 182 | if 0 != proc.returncode: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 183 | raise subprocess.CalledProcessError(proc.returncode, |
| 184 | cmd, out + err) |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 185 | |
| 186 | return out |
| 187 | |
| 188 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 189 | def get_ip_for_target(target_ip): |
koder aka kdanilov | 209e85d | 2015-04-27 23:11:05 +0300 | [diff] [blame] | 190 | if not is_ip(target_ip): |
koder aka kdanilov | afd9874 | 2015-04-24 01:27:22 +0300 | [diff] [blame] | 191 | target_ip = socket.gethostbyname(target_ip) |
| 192 | |
koder aka kdanilov | 209e85d | 2015-04-27 23:11:05 +0300 | [diff] [blame] | 193 | first_dig = map(int, target_ip.split(".")) |
| 194 | if first_dig == 127: |
koder aka kdanilov | afd9874 | 2015-04-24 01:27:22 +0300 | [diff] [blame] | 195 | return '127.0.0.1' |
| 196 | |
koder aka kdanilov | d5ed4da | 2015-05-07 23:33:23 +0300 | [diff] [blame] | 197 | data = run_locally('ip route get to'.split(" ") + [target_ip]) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 198 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 199 | rr1 = r'{0} via [.0-9]+ dev (?P<dev>.*?) src (?P<ip>[.0-9]+)$' |
| 200 | rr1 = rr1.replace(" ", r'\s+') |
| 201 | rr1 = rr1.format(target_ip.replace('.', r'\.')) |
| 202 | |
| 203 | rr2 = r'{0} dev (?P<dev>.*?) src (?P<ip>[.0-9]+)$' |
| 204 | rr2 = rr2.replace(" ", r'\s+') |
| 205 | rr2 = rr2.format(target_ip.replace('.', r'\.')) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 206 | |
| 207 | data_line = data.split("\n")[0].strip() |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 208 | res1 = re.match(rr1, data_line) |
| 209 | res2 = re.match(rr2, data_line) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 210 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 211 | if res1 is not None: |
| 212 | return res1.group('ip') |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 213 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 214 | if res2 is not None: |
| 215 | return res2.group('ip') |
| 216 | |
| 217 | raise OSError("Can't define interface for {0}".format(target_ip)) |
| 218 | |
| 219 | |
| 220 | def open_for_append_or_create(fname): |
| 221 | if not os.path.exists(fname): |
| 222 | return open(fname, "w") |
| 223 | |
| 224 | fd = open(fname, 'r+') |
| 225 | fd.seek(0, os.SEEK_END) |
| 226 | return fd |
| 227 | |
| 228 | |
| 229 | def sec_to_str(seconds): |
| 230 | h = seconds // 3600 |
| 231 | m = (seconds % 3600) // 60 |
| 232 | s = seconds % 60 |
| 233 | return "{0}:{1:02d}:{2:02d}".format(h, m, s) |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 234 | |
| 235 | |
| 236 | def yamable(data): |
| 237 | if isinstance(data, (tuple, list)): |
| 238 | return map(yamable, data) |
| 239 | |
| 240 | if isinstance(data, unicode): |
| 241 | return str(data) |
| 242 | |
| 243 | if isinstance(data, dict): |
| 244 | res = {} |
| 245 | for k, v in data.items(): |
| 246 | res[yamable(k)] = yamable(v) |
| 247 | return res |
| 248 | |
| 249 | return data |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 250 | |
| 251 | |
| 252 | CLEANING = [] |
| 253 | |
| 254 | |
| 255 | def clean_resource(func, *args, **kwargs): |
| 256 | CLEANING.append((func, args, kwargs)) |
| 257 | |
| 258 | |
| 259 | def iter_clean_func(): |
| 260 | while CLEANING != []: |
| 261 | yield CLEANING.pop() |