239
239
def connect_ssh(self, username, password, host, port, command):
240
240
"""Make an SSH connection.
242
:returns: something with a `close` method, and a `get_filelike_channels`
243
method that returns a pair of (read, write) filelike objects.
242
:returns: an SSHConnection.
245
244
raise NotImplementedError(self.connect_ssh)
269
268
register_ssh_vendor('loopback', LoopbackVendor())
272
class _ParamikoSSHConnection(object):
273
def __init__(self, channel):
274
self.channel = channel
276
def get_filelike_channels(self):
277
return self.channel.makefile('rb'), self.channel.makefile('wb')
280
return self.channel.close()
283
271
class ParamikoVendor(SSHVendor):
284
272
"""Vendor that uses paramiko."""
363
351
"""Abstract base class for vendors that use pipes to a subprocess."""
365
353
def _connect(self, argv):
366
proc = subprocess.Popen(argv,
367
stdin=subprocess.PIPE,
368
stdout=subprocess.PIPE,
354
# Attempt to make a socketpair to use as stdin/stdout for the SSH
355
# subprocess. We prefer sockets to pipes because they support
356
# non-blocking short reads, allowing us to optimistically read 64k (or
359
my_sock, subproc_sock = socket.socketpair()
360
except (AttributeError, socket.error):
361
# This platform doesn't support socketpair(), so just use ordinary
363
stdin = stdout = subprocess.PIPE
366
stdin = stdout = subproc_sock
368
proc = subprocess.Popen(argv, stdin=stdin, stdout=stdout,
369
369
**os_specific_subprocess_params())
370
return SSHSubprocess(proc)
370
return SSHSubprocessConnection(proc, sock=sock)
372
372
def connect_sftp(self, username, password, host, port):
377
377
return SFTPClient(SocketAsChannelAdapter(sock))
378
378
except _sftp_connection_errors, e:
379
379
self._raise_connection_error(host, port=port, orig_error=e)
380
except (OSError, IOError), e:
380
except (OSError, IOError, socket.error), e:
381
381
# If the machine is fast enough, ssh can actually exit
382
382
# before we try and send it the sftp request, which
383
383
# raises a Broken Pipe
392
392
return self._connect(argv)
393
393
except (EOFError), e:
394
394
self._raise_connection_error(host, port=port, orig_error=e)
395
except (OSError, IOError), e:
395
except (OSError, IOError, socket.error), e:
396
396
# If the machine is fast enough, ssh can actually exit
397
397
# before we try and send it the sftp request, which
398
398
# raises a Broken Pipe
645
645
_subproc_weakrefs = set()
647
647
def _close_ssh_proc(proc):
648
for func in [proc.stdin.close, proc.stdout.close, proc.wait]:
648
"""Carefully close stdin/stdout and reap the SSH process.
650
If the pipes are already closed and/or the process has already been
651
wait()ed on, that's ok, and no error is raised. The goal is to do our best
652
to clean up (whether or not a clean up was already tried).
654
dotted_names = ['stdin.close', 'stdout.close', 'wait']
655
for dotted_name in dotted_names:
656
attrs = dotted_name.split('.')
660
obj = getattr(obj, attr)
661
except AttributeError:
662
# It's ok for proc.stdin or proc.stdout to be None.
655
class SSHSubprocess(object):
656
"""A socket-like object that talks to an ssh subprocess via pipes."""
658
def __init__(self, proc):
667
# It's ok for the pipe to already be closed, or the process to
668
# already be finished.
672
class SSHConnection(object):
673
"""Abstract base class for SSH connections."""
675
def get_sock_or_pipes(self):
676
"""Returns a (kind, io_object) pair.
678
If kind == 'socket', then io_object is a socket.
680
If kind == 'pipes', then io_object is a pair of file-like objects
681
(read_from, write_to).
683
raise NotImplementedError(self.get_sock_or_pipes)
686
raise NotImplementedError(self.close)
689
class SSHSubprocessConnection(SSHConnection):
690
"""A connection to an ssh subprocess via pipes or a socket.
692
This class is also socket-like enough to be used with
693
SocketAsChannelAdapter (it has 'send' and 'recv' methods).
696
def __init__(self, proc, sock=None):
699
:param proc: a subprocess.Popen
700
:param sock: if proc.stdin/out is a socket from a socketpair, then sock
701
should bzrlib's half of that socketpair. If not passed, proc's
702
stdin/out is assumed to be ordinary pipes.
660
706
# Add a weakref to proc that will attempt to do the same as self.close
661
707
# to avoid leaving processes lingering indefinitely.
662
708
def terminate(ref):
665
711
_subproc_weakrefs.add(weakref.ref(self, terminate))
667
713
def send(self, data):
668
return os.write(self.proc.stdin.fileno(), data)
714
if self._sock is not None:
715
return self._sock.send(data)
717
return os.write(self.proc.stdin.fileno(), data)
670
719
def recv(self, count):
671
return os.read(self.proc.stdout.fileno(), count)
720
if self._sock is not None:
721
return self._sock.recv(count)
723
return os.read(self.proc.stdout.fileno(), count)
674
726
_close_ssh_proc(self.proc)
676
def get_filelike_channels(self):
677
return (self.proc.stdout, self.proc.stdin)
728
def get_sock_or_pipes(self):
729
if self._sock is not None:
730
return 'socket', self._sock
732
return 'pipes', (self.proc.stdout, self.proc.stdin)
735
class _ParamikoSSHConnection(SSHConnection):
736
"""An SSH connection via paramiko."""
738
def __init__(self, channel):
739
self.channel = channel
741
def get_sock_or_pipes(self):
742
return ('socket', self.channel)
745
return self.channel.close()