From 730b6b8bc3697f4294edbf3b73cec47b64c625c0 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 12 Sep 2013 18:21:45 +0900 Subject: Fix #10: Allow to manually specify ssh username and port If the username and port are specified when constructing the client, they will be used instead of attempting to fetch from the ssh config file. In this case the full hostname of the server is required, rather than only the name as listed in the ssh config. Change-Id: I82b8638d99922c9b40a54a8275ffc085f505696f --- pygerrit/ssh.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'pygerrit/ssh.py') diff --git a/pygerrit/ssh.py b/pygerrit/ssh.py index c235999..e779715 100644 --- a/pygerrit/ssh.py +++ b/pygerrit/ssh.py @@ -65,17 +65,19 @@ class GerritSSHClient(SSHClient): """ Gerrit SSH Client, wrapping the paramiko SSH Client. """ - def __init__(self, hostname): + def __init__(self, hostname, username=None, port=None): """ Initialise and connect to SSH. """ super(GerritSSHClient, self).__init__() self.remote_version = None self.hostname = hostname + self.username = username + self.key_filename = None + self.port = port self.connected = Event() self.lock = Lock() - def _do_connect(self): - """ Connect to the remote. """ - self.load_system_host_keys() + def _configure(self): + """ Configure the ssh parameters from the config file. """ configfile = expanduser("~/.ssh/config") if not isfile(configfile): raise GerritError("ssh config file '%s' does not exist" % @@ -88,21 +90,29 @@ class GerritSSHClient(SSHClient): raise GerritError("No ssh config for host %s" % self.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 + self.hostname = data['hostname'] + self.username = data['user'] if 'identityfile' in data: key_filename = abspath(expanduser(data['identityfile'][0])) if not isfile(key_filename): raise GerritError("Identity file '%s' does not exist" % key_filename) + self.key_filename = key_filename try: - port = int(data['port']) + self.port = int(data['port']) except ValueError: raise GerritError("Invalid port: %s" % data['port']) + + def _do_connect(self): + """ Connect to the remote. """ + self.load_system_host_keys() + if self.username is None or self.port is None: + self._configure() try: - self.connect(hostname=data['hostname'], - port=port, - username=data['user'], - key_filename=key_filename) + self.connect(hostname=self.hostname, + port=self.port, + username=self.username, + key_filename=self.key_filename) except socket.error as e: raise GerritError("Failed to connect to server: %s" % e) -- cgit v1.2.1