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