summaryrefslogtreecommitdiff
path: root/pygerrit/ssh.py
diff options
context:
space:
mode:
authorDavid Pursehouse <david.pursehouse@sonymobile.com>2013-09-12 18:21:45 +0900
committerDavid Pursehouse <david.pursehouse@sonymobile.com>2013-09-12 18:21:45 +0900
commit730b6b8bc3697f4294edbf3b73cec47b64c625c0 (patch)
tree6523edb2b9b74fa715351650efdb10fdf235daff /pygerrit/ssh.py
parent7fc93f60775f252278af535ade9a7c6cae17a3d1 (diff)
downloadpygerrit-730b6b8bc3697f4294edbf3b73cec47b64c625c0.tar.gz
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
Diffstat (limited to 'pygerrit/ssh.py')
-rw-r--r--pygerrit/ssh.py30
1 files changed, 20 insertions, 10 deletions
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)