koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 1 | import re |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 2 | import time |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 3 | import errno |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 4 | import random |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 5 | import socket |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 6 | import shutil |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 7 | import logging |
| 8 | import os.path |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 9 | import getpass |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 10 | import StringIO |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 11 | import threading |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 12 | import subprocess |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 13 | |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 14 | import paramiko |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 15 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 16 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 17 | logger = logging.getLogger("wally") |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 18 | |
| 19 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 20 | class Local(object): |
koder aka kdanilov | 7647164 | 2015-08-14 11:44:43 +0300 | [diff] [blame] | 21 | "simulate ssh connection to local" |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 22 | @classmethod |
| 23 | def open_sftp(cls): |
koder aka kdanilov | afd9874 | 2015-04-24 01:27:22 +0300 | [diff] [blame] | 24 | return cls() |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 25 | |
| 26 | @classmethod |
| 27 | def mkdir(cls, remotepath, mode=None): |
| 28 | os.mkdir(remotepath) |
| 29 | if mode is not None: |
| 30 | os.chmod(remotepath, mode) |
| 31 | |
| 32 | @classmethod |
| 33 | def put(cls, localfile, remfile): |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 34 | dirname = os.path.dirname(remfile) |
| 35 | if not os.path.exists(dirname): |
| 36 | os.makedirs(dirname) |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 37 | shutil.copyfile(localfile, remfile) |
| 38 | |
| 39 | @classmethod |
koder aka kdanilov | a8253ce | 2015-06-13 03:10:21 +0300 | [diff] [blame] | 40 | def get(cls, remfile, localfile): |
| 41 | dirname = os.path.dirname(localfile) |
| 42 | if not os.path.exists(dirname): |
| 43 | os.makedirs(dirname) |
| 44 | shutil.copyfile(remfile, localfile) |
| 45 | |
| 46 | @classmethod |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 47 | def chmod(cls, path, mode): |
| 48 | os.chmod(path, mode) |
| 49 | |
| 50 | @classmethod |
| 51 | def copytree(cls, src, dst): |
| 52 | shutil.copytree(src, dst) |
| 53 | |
| 54 | @classmethod |
| 55 | def remove(cls, path): |
| 56 | os.unlink(path) |
| 57 | |
| 58 | @classmethod |
| 59 | def close(cls): |
| 60 | pass |
| 61 | |
| 62 | @classmethod |
| 63 | def open(cls, *args, **kwarhgs): |
| 64 | return open(*args, **kwarhgs) |
| 65 | |
koder aka kdanilov | e2de58c | 2015-04-24 22:59:36 +0300 | [diff] [blame] | 66 | @classmethod |
| 67 | def stat(cls, path): |
| 68 | return os.stat(path) |
| 69 | |
koder aka kdanilov | 783b454 | 2015-04-23 18:57:04 +0300 | [diff] [blame] | 70 | def __enter__(self): |
| 71 | return self |
| 72 | |
| 73 | def __exit__(self, x, y, z): |
| 74 | return False |
| 75 | |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 76 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 77 | NODE_KEYS = {} |
| 78 | |
| 79 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 80 | def exists(sftp, path): |
| 81 | """os.path.exists for paramiko's SCP object |
| 82 | """ |
| 83 | try: |
| 84 | sftp.stat(path) |
| 85 | return True |
| 86 | except IOError as e: |
| 87 | if e.errno == errno.ENOENT: |
| 88 | return False |
| 89 | raise |
| 90 | |
| 91 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 92 | def set_key_for_node(host_port, key): |
| 93 | sio = StringIO.StringIO(key) |
| 94 | NODE_KEYS[host_port] = paramiko.RSAKey.from_private_key(sio) |
| 95 | sio.close() |
| 96 | |
| 97 | |
koder aka kdanilov | bb5fe07 | 2015-05-21 02:50:23 +0300 | [diff] [blame] | 98 | def ssh_connect(creds, conn_timeout=60, reuse_conn=None): |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 99 | if creds == 'local': |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 100 | return Local() |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 101 | |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 102 | tcp_timeout = 15 |
| 103 | banner_timeout = 30 |
| 104 | |
koder aka kdanilov | bb5fe07 | 2015-05-21 02:50:23 +0300 | [diff] [blame] | 105 | if reuse_conn is None: |
| 106 | ssh = paramiko.SSHClient() |
| 107 | ssh.load_host_keys('/dev/null') |
| 108 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 109 | ssh.known_hosts = None |
| 110 | else: |
| 111 | ssh = reuse_conn |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 112 | |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 113 | etime = time.time() + conn_timeout |
| 114 | |
| 115 | while True: |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 116 | try: |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 117 | tleft = etime - time.time() |
| 118 | c_tcp_timeout = min(tcp_timeout, tleft) |
koder aka kdanilov | 8bc4802 | 2015-07-15 11:54:47 +0300 | [diff] [blame] | 119 | |
| 120 | if paramiko.__version_info__ >= (1, 15, 2): |
| 121 | banner_timeout = {'banner_timeout': min(banner_timeout, tleft)} |
| 122 | else: |
| 123 | banner_timeout = {} |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 124 | |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 125 | if creds.passwd is not None: |
| 126 | ssh.connect(creds.host, |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 127 | timeout=c_tcp_timeout, |
koder aka kdanilov | a4a570f | 2015-04-23 22:11:40 +0300 | [diff] [blame] | 128 | username=creds.user, |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 129 | password=creds.passwd, |
| 130 | port=creds.port, |
| 131 | allow_agent=False, |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 132 | look_for_keys=False, |
koder aka kdanilov | 8bc4802 | 2015-07-15 11:54:47 +0300 | [diff] [blame] | 133 | **banner_timeout) |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 134 | elif creds.key_file is not None: |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 135 | ssh.connect(creds.host, |
koder aka kdanilov | a4a570f | 2015-04-23 22:11:40 +0300 | [diff] [blame] | 136 | username=creds.user, |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 137 | timeout=c_tcp_timeout, |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 138 | key_filename=creds.key_file, |
| 139 | look_for_keys=False, |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 140 | port=creds.port, |
koder aka kdanilov | 8bc4802 | 2015-07-15 11:54:47 +0300 | [diff] [blame] | 141 | **banner_timeout) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 142 | elif (creds.host, creds.port) in NODE_KEYS: |
| 143 | ssh.connect(creds.host, |
| 144 | username=creds.user, |
| 145 | timeout=c_tcp_timeout, |
| 146 | pkey=NODE_KEYS[(creds.host, creds.port)], |
| 147 | look_for_keys=False, |
| 148 | port=creds.port, |
koder aka kdanilov | 8bc4802 | 2015-07-15 11:54:47 +0300 | [diff] [blame] | 149 | **banner_timeout) |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 150 | else: |
| 151 | key_file = os.path.expanduser('~/.ssh/id_rsa') |
| 152 | ssh.connect(creds.host, |
| 153 | username=creds.user, |
| 154 | timeout=c_tcp_timeout, |
| 155 | key_filename=key_file, |
| 156 | look_for_keys=False, |
| 157 | port=creds.port, |
koder aka kdanilov | 8bc4802 | 2015-07-15 11:54:47 +0300 | [diff] [blame] | 158 | **banner_timeout) |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 159 | return ssh |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 160 | except paramiko.PasswordRequiredException: |
| 161 | raise |
koder aka kdanilov | 46d4f39 | 2015-04-24 11:35:00 +0300 | [diff] [blame] | 162 | except (socket.error, paramiko.SSHException): |
koder aka kdanilov | 6b1341a | 2015-04-21 22:44:21 +0300 | [diff] [blame] | 163 | if time.time() > etime: |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 164 | raise |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 165 | time.sleep(1) |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 166 | |
| 167 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 168 | def save_to_remote(sftp, path, content): |
| 169 | with sftp.open(path, "wb") as fd: |
| 170 | fd.write(content) |
| 171 | |
| 172 | |
| 173 | def read_from_remote(sftp, path): |
| 174 | with sftp.open(path, "rb") as fd: |
| 175 | return fd.read() |
| 176 | |
| 177 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 178 | def normalize_dirpath(dirpath): |
| 179 | while dirpath.endswith("/"): |
| 180 | dirpath = dirpath[:-1] |
| 181 | return dirpath |
| 182 | |
| 183 | |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 184 | ALL_RWX_MODE = ((1 << 9) - 1) |
| 185 | |
| 186 | |
| 187 | def ssh_mkdir(sftp, remotepath, mode=ALL_RWX_MODE, intermediate=False): |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 188 | remotepath = normalize_dirpath(remotepath) |
| 189 | if intermediate: |
| 190 | try: |
| 191 | sftp.mkdir(remotepath, mode=mode) |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 192 | except (IOError, OSError): |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 193 | upper_dir = remotepath.rsplit("/", 1)[0] |
| 194 | |
| 195 | if upper_dir == '' or upper_dir == '/': |
| 196 | raise |
| 197 | |
| 198 | ssh_mkdir(sftp, upper_dir, mode=mode, intermediate=True) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 199 | return sftp.mkdir(remotepath, mode=mode) |
| 200 | else: |
| 201 | sftp.mkdir(remotepath, mode=mode) |
| 202 | |
| 203 | |
| 204 | def ssh_copy_file(sftp, localfile, remfile, preserve_perm=True): |
| 205 | sftp.put(localfile, remfile) |
| 206 | if preserve_perm: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 207 | sftp.chmod(remfile, os.stat(localfile).st_mode & ALL_RWX_MODE) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 208 | |
| 209 | |
| 210 | def put_dir_recursively(sftp, localpath, remotepath, preserve_perm=True): |
| 211 | "upload local directory to remote recursively" |
| 212 | |
| 213 | # hack for localhost connection |
| 214 | if hasattr(sftp, "copytree"): |
| 215 | sftp.copytree(localpath, remotepath) |
| 216 | return |
| 217 | |
| 218 | assert remotepath.startswith("/"), "%s must be absolute path" % remotepath |
| 219 | |
| 220 | # normalize |
| 221 | localpath = normalize_dirpath(localpath) |
| 222 | remotepath = normalize_dirpath(remotepath) |
| 223 | |
| 224 | try: |
| 225 | sftp.chdir(remotepath) |
| 226 | localsuffix = localpath.rsplit("/", 1)[1] |
| 227 | remotesuffix = remotepath.rsplit("/", 1)[1] |
| 228 | if localsuffix != remotesuffix: |
| 229 | remotepath = os.path.join(remotepath, localsuffix) |
| 230 | except IOError: |
| 231 | pass |
| 232 | |
| 233 | for root, dirs, fls in os.walk(localpath): |
| 234 | prefix = os.path.commonprefix([localpath, root]) |
| 235 | suffix = root.split(prefix, 1)[1] |
| 236 | if suffix.startswith("/"): |
| 237 | suffix = suffix[1:] |
| 238 | |
| 239 | remroot = os.path.join(remotepath, suffix) |
| 240 | |
| 241 | try: |
| 242 | sftp.chdir(remroot) |
| 243 | except IOError: |
| 244 | if preserve_perm: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 245 | mode = os.stat(root).st_mode & ALL_RWX_MODE |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 246 | else: |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 247 | mode = ALL_RWX_MODE |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 248 | ssh_mkdir(sftp, remroot, mode=mode, intermediate=True) |
| 249 | sftp.chdir(remroot) |
| 250 | |
| 251 | for f in fls: |
| 252 | remfile = os.path.join(remroot, f) |
| 253 | localfile = os.path.join(root, f) |
| 254 | ssh_copy_file(sftp, localfile, remfile, preserve_perm) |
| 255 | |
| 256 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 257 | def delete_file(conn, path): |
| 258 | sftp = conn.open_sftp() |
| 259 | sftp.remove(path) |
| 260 | sftp.close() |
| 261 | |
| 262 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 263 | def copy_paths(conn, paths): |
| 264 | sftp = conn.open_sftp() |
| 265 | try: |
| 266 | for src, dst in paths.items(): |
| 267 | try: |
| 268 | if os.path.isfile(src): |
| 269 | ssh_copy_file(sftp, src, dst) |
| 270 | elif os.path.isdir(src): |
| 271 | put_dir_recursively(sftp, src, dst) |
| 272 | else: |
| 273 | templ = "Can't copy {0!r} - " + \ |
| 274 | "it neither a file not a directory" |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 275 | raise OSError(templ.format(src)) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 276 | except Exception as exc: |
| 277 | tmpl = "Scp {0!r} => {1!r} failed - {2!r}" |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 278 | raise OSError(tmpl.format(src, dst, exc)) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 279 | finally: |
| 280 | sftp.close() |
| 281 | |
| 282 | |
| 283 | class ConnCreds(object): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 284 | conn_uri_attrs = ("user", "passwd", "host", "port", "path") |
| 285 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 286 | def __init__(self): |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 287 | for name in self.conn_uri_attrs: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 288 | setattr(self, name, None) |
| 289 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 290 | def __str__(self): |
| 291 | return str(self.__dict__) |
| 292 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 293 | |
| 294 | uri_reg_exprs = [] |
| 295 | |
| 296 | |
| 297 | class URIsNamespace(object): |
| 298 | class ReParts(object): |
| 299 | user_rr = "[^:]*?" |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 300 | host_rr = "[^:@]*?" |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 301 | port_rr = "\\d+" |
| 302 | key_file_rr = "[^:@]*" |
| 303 | passwd_rr = ".*?" |
| 304 | |
| 305 | re_dct = ReParts.__dict__ |
| 306 | |
| 307 | for attr_name, val in re_dct.items(): |
| 308 | if attr_name.endswith('_rr'): |
| 309 | new_rr = "(?P<{0}>{1})".format(attr_name[:-3], val) |
| 310 | setattr(ReParts, attr_name, new_rr) |
| 311 | |
| 312 | re_dct = ReParts.__dict__ |
| 313 | |
| 314 | templs = [ |
| 315 | "^{host_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 316 | "^{host_rr}:{port_rr}$", |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 317 | "^{host_rr}::{key_file_rr}$", |
| 318 | "^{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 319 | "^{user_rr}@{host_rr}$", |
| 320 | "^{user_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 321 | "^{user_rr}@{host_rr}::{key_file_rr}$", |
| 322 | "^{user_rr}@{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 323 | "^{user_rr}:{passwd_rr}@{host_rr}$", |
| 324 | "^{user_rr}:{passwd_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 325 | ] |
| 326 | |
| 327 | for templ in templs: |
| 328 | uri_reg_exprs.append(templ.format(**re_dct)) |
| 329 | |
| 330 | |
| 331 | def parse_ssh_uri(uri): |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 332 | # user:passwd@ip_host:port |
| 333 | # user:passwd@ip_host |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 334 | # user@ip_host:port |
| 335 | # user@ip_host |
| 336 | # ip_host:port |
| 337 | # ip_host |
| 338 | # user@ip_host:port:path_to_key_file |
| 339 | # user@ip_host::path_to_key_file |
| 340 | # ip_host:port:path_to_key_file |
| 341 | # ip_host::path_to_key_file |
| 342 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 343 | if uri.startswith("ssh://"): |
| 344 | uri = uri[len("ssh://"):] |
| 345 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 346 | res = ConnCreds() |
| 347 | res.port = "22" |
| 348 | res.key_file = None |
| 349 | res.passwd = None |
koder aka kdanilov | a4a570f | 2015-04-23 22:11:40 +0300 | [diff] [blame] | 350 | res.user = getpass.getuser() |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 351 | |
| 352 | for rr in uri_reg_exprs: |
| 353 | rrm = re.match(rr, uri) |
| 354 | if rrm is not None: |
| 355 | res.__dict__.update(rrm.groupdict()) |
| 356 | return res |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 357 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 358 | raise ValueError("Can't parse {0!r} as ssh uri value".format(uri)) |
| 359 | |
| 360 | |
koder aka kdanilov | bb5fe07 | 2015-05-21 02:50:23 +0300 | [diff] [blame] | 361 | def reconnect(conn, uri, **params): |
| 362 | if uri == 'local': |
| 363 | return conn |
| 364 | |
| 365 | creds = parse_ssh_uri(uri) |
| 366 | creds.port = int(creds.port) |
| 367 | return ssh_connect(creds, reuse_conn=conn, **params) |
| 368 | |
| 369 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 370 | def connect(uri, **params): |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 371 | if uri == 'local': |
koder aka kdanilov | 0fdaaee | 2015-06-30 11:10:48 +0300 | [diff] [blame] | 372 | res = Local() |
| 373 | else: |
| 374 | creds = parse_ssh_uri(uri) |
| 375 | creds.port = int(creds.port) |
| 376 | res = ssh_connect(creds, **params) |
| 377 | return res |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 378 | |
| 379 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 380 | all_sessions_lock = threading.Lock() |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 381 | all_sessions = {} |
| 382 | |
| 383 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 384 | class BGSSHTask(object): |
koder aka kdanilov | 7647164 | 2015-08-14 11:44:43 +0300 | [diff] [blame] | 385 | CHECK_RETRY = 5 |
| 386 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 387 | def __init__(self, node, use_sudo): |
| 388 | self.node = node |
| 389 | self.pid = None |
| 390 | self.use_sudo = use_sudo |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 391 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 392 | def start(self, orig_cmd, **params): |
| 393 | uniq_name = 'test' |
| 394 | cmd = "screen -S {0} -d -m {1}".format(uniq_name, orig_cmd) |
| 395 | run_over_ssh(self.node.connection, cmd, |
| 396 | timeout=10, node=self.node.get_conn_id(), |
| 397 | **params) |
| 398 | processes = run_over_ssh(self.node.connection, "ps aux", nolog=True) |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 399 | |
koder aka kdanilov | 7647164 | 2015-08-14 11:44:43 +0300 | [diff] [blame] | 400 | for iter in range(self.CHECK_RETRY): |
| 401 | for proc in processes.split("\n"): |
| 402 | if orig_cmd in proc and "SCREEN" not in proc: |
| 403 | self.pid = proc.split()[1] |
| 404 | break |
| 405 | if self.pid is not None: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 406 | break |
koder aka kdanilov | 7647164 | 2015-08-14 11:44:43 +0300 | [diff] [blame] | 407 | time.sleep(1) |
| 408 | |
| 409 | if self.pid is None: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 410 | self.pid = -1 |
| 411 | |
| 412 | def check_running(self): |
| 413 | assert self.pid is not None |
koder aka kdanilov | 7647164 | 2015-08-14 11:44:43 +0300 | [diff] [blame] | 414 | if -1 == self.pid: |
| 415 | return False |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 416 | try: |
| 417 | run_over_ssh(self.node.connection, |
| 418 | "ls /proc/{0}".format(self.pid), |
| 419 | timeout=10, nolog=True) |
| 420 | return True |
| 421 | except OSError: |
| 422 | return False |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 423 | |
| 424 | def kill(self, soft=True, use_sudo=True): |
| 425 | assert self.pid is not None |
koder aka kdanilov | 7647164 | 2015-08-14 11:44:43 +0300 | [diff] [blame] | 426 | if self.pid == -1: |
| 427 | return True |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 428 | try: |
| 429 | if soft: |
| 430 | cmd = "kill {0}" |
| 431 | else: |
| 432 | cmd = "kill -9 {0}" |
| 433 | |
| 434 | if self.use_sudo: |
| 435 | cmd = "sudo " + cmd |
| 436 | |
| 437 | run_over_ssh(self.node.connection, |
| 438 | cmd.format(self.pid), nolog=True) |
| 439 | return True |
| 440 | except OSError: |
| 441 | return False |
| 442 | |
| 443 | def wait(self, soft_timeout, timeout): |
| 444 | end_of_wait_time = timeout + time.time() |
| 445 | soft_end_of_wait_time = soft_timeout + time.time() |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 446 | |
koder aka kdanilov | a8253ce | 2015-06-13 03:10:21 +0300 | [diff] [blame] | 447 | # time_till_check = random.randint(5, 10) |
| 448 | time_till_check = 2 |
| 449 | |
| 450 | # time_till_first_check = random.randint(2, 6) |
| 451 | time_till_first_check = 2 |
koder aka kdanilov | fd2cfa5 | 2015-05-20 03:17:42 +0300 | [diff] [blame] | 452 | time.sleep(time_till_first_check) |
| 453 | if not self.check_running(): |
| 454 | return True |
| 455 | |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 456 | while self.check_running() and time.time() < soft_end_of_wait_time: |
koder aka kdanilov | 7647164 | 2015-08-14 11:44:43 +0300 | [diff] [blame] | 457 | # time.sleep(soft_end_of_wait_time - time.time()) |
koder aka kdanilov | a94dfe1 | 2015-08-19 13:04:51 +0300 | [diff] [blame] | 458 | time.sleep(time_till_check) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 459 | |
| 460 | while end_of_wait_time > time.time(): |
| 461 | time.sleep(time_till_check) |
| 462 | if not self.check_running(): |
| 463 | break |
| 464 | else: |
| 465 | self.kill() |
koder aka kdanilov | a8253ce | 2015-06-13 03:10:21 +0300 | [diff] [blame] | 466 | time.sleep(1) |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 467 | if self.check_running(): |
| 468 | self.kill(soft=False) |
| 469 | return False |
| 470 | return True |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 471 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 472 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 473 | def run_over_ssh(conn, cmd, stdin_data=None, timeout=60, |
| 474 | nolog=False, node=None): |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 475 | "should be replaces by normal implementation, with select" |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 476 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 477 | if isinstance(conn, Local): |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 478 | if not nolog: |
| 479 | logger.debug("SSH:local Exec {0!r}".format(cmd)) |
| 480 | proc = subprocess.Popen(cmd, shell=True, |
| 481 | stdin=subprocess.PIPE, |
| 482 | stdout=subprocess.PIPE, |
| 483 | stderr=subprocess.STDOUT) |
| 484 | |
| 485 | stdoutdata, _ = proc.communicate(input=stdin_data) |
koder aka kdanilov | 0c598a1 | 2015-04-21 03:01:40 +0300 | [diff] [blame] | 486 | if proc.returncode != 0: |
| 487 | templ = "SSH:{0} Cmd {1!r} failed with code {2}. Output: {3}" |
| 488 | raise OSError(templ.format(node, cmd, proc.returncode, stdoutdata)) |
| 489 | |
| 490 | return stdoutdata |
| 491 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 492 | transport = conn.get_transport() |
| 493 | session = transport.open_session() |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 494 | |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 495 | if node is None: |
| 496 | node = "" |
| 497 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 498 | with all_sessions_lock: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 499 | all_sessions[id(session)] = session |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 500 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 501 | try: |
| 502 | session.set_combine_stderr(True) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 503 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 504 | stime = time.time() |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 505 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 506 | if not nolog: |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 507 | logger.debug("SSH:{0} Exec {1!r}".format(node, cmd)) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 508 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 509 | session.exec_command(cmd) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 510 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 511 | if stdin_data is not None: |
| 512 | session.sendall(stdin_data) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 513 | |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 514 | session.settimeout(1) |
| 515 | session.shutdown_write() |
| 516 | output = "" |
| 517 | |
| 518 | while True: |
| 519 | try: |
| 520 | ndata = session.recv(1024) |
| 521 | output += ndata |
| 522 | if "" == ndata: |
| 523 | break |
| 524 | except socket.timeout: |
| 525 | pass |
| 526 | |
| 527 | if time.time() - stime > timeout: |
| 528 | raise OSError(output + "\nExecution timeout") |
| 529 | |
| 530 | code = session.recv_exit_status() |
| 531 | finally: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 532 | found = False |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 533 | with all_sessions_lock: |
koder aka kdanilov | 4af1c1d | 2015-05-18 15:48:58 +0300 | [diff] [blame] | 534 | if id(session) in all_sessions: |
| 535 | found = True |
| 536 | del all_sessions[id(session)] |
| 537 | |
| 538 | if found: |
| 539 | session.close() |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 540 | |
| 541 | if code != 0: |
koder aka kdanilov | 4500a5f | 2015-04-17 16:55:17 +0300 | [diff] [blame] | 542 | templ = "SSH:{0} Cmd {1!r} failed with code {2}. Output: {3}" |
| 543 | raise OSError(templ.format(node, cmd, code, output)) |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 544 | |
| 545 | return output |
| 546 | |
| 547 | |
| 548 | def close_all_sessions(): |
| 549 | with all_sessions_lock: |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 550 | for session in all_sessions.values(): |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 551 | try: |
| 552 | session.sendall('\x03') |
| 553 | session.close() |
| 554 | except: |
| 555 | pass |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 556 | all_sessions.clear() |