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