summaryrefslogtreecommitdiff
path: root/pygerrit/ssh.py
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2012-08-24 13:57:25 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2012-09-03 17:53:08 +0900
commit424415197211d32eaeb84630542bcab3cc6521b0 (patch)
tree7224108a1d43047c8e4304a184c6eb52bbb13698 /pygerrit/ssh.py
parent26bc11340590b0b863527fa12da03cea528feb46 (diff)
downloadpygerrit-424415197211d32eaeb84630542bcab3cc6521b0.tar.gz
Add GerritSSHClient
Add initial version of GerritSSHClient, which is a wrapper for paramiko's SSHClient. The class derives from SSHClient and configures the ssh connection from the user's ssh config file. Change-Id: I54a193e41356de272c95c5288ed2c9e50a776cdf
Diffstat (limited to 'pygerrit/ssh.py')
-rw-r--r--pygerrit/ssh.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/pygerrit/ssh.py b/pygerrit/ssh.py
new file mode 100644
index 0000000..35de375
--- /dev/null
+++ b/pygerrit/ssh.py
@@ -0,0 +1,53 @@
+""" Gerrit SSH Client. """
+
+from os.path import abspath, expanduser, isfile
+
+from pygerrit.error import GerritError
+
+from paramiko import SSHClient, SSHConfig
+
+
+class GerritSSHClient(SSHClient):
+
+ """ Gerrit SSH Client, wrapping the paramiko SSH Client. """
+
+ def __init__(self, hostname):
+ """ Initialise and connect to SSH. """
+ super(GerritSSHClient, self).__init__()
+ self.load_system_host_keys()
+
+ configfile = expanduser("~/.ssh/config")
+ if not isfile(configfile):
+ raise GerritError("ssh config file '%s' does not exist" %
+ configfile)
+
+ config = SSHConfig()
+ config.parse(open(configfile))
+ data = config.lookup(hostname)
+ if not data:
+ raise GerritError("No ssh config for host %s" % hostname)
+ if not 'hostname' in data or not 'port' in data or not 'user' in data:
+ raise GerritError("Missing configuration data in %s" % configfile)
+ key_filename = None
+ if 'identityfile' in data:
+ key_filename = abspath(expanduser(data['identityfile']))
+ if not isfile(key_filename):
+ raise GerritError("Identity file '%s' does not exist" %
+ key_filename)
+ self.connect(hostname=data['hostname'],
+ port=int(data['port']),
+ username=data['user'],
+ key_filename=key_filename)
+
+ def run_gerrit_command(self, command):
+ """ Run the given command.
+
+ Run `command` and return a tuple of stdin, stdout, and stderr.
+
+ """
+ gerrit_command = ["gerrit"]
+ if isinstance(command, list):
+ gerrit_command += command
+ else:
+ gerrit_command.append(command)
+ return self.exec_command(" ".join(gerrit_command))