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