- start = time()
- pkey = None
- if 'priv_key' in node:
- pkey = RSAKey.from_private_key(
- StringIO.StringIO(node['priv_key']))
-
- self._ssh = paramiko.SSHClient()
- self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-
- self._ssh.connect(node['host'], username=node['username'],
- password=node.get('password'), pkey=pkey,
- port=node['port'])
-
- self._ssh.get_transport().set_keepalive(10)
-
- SSH.__existing_connections[node_hash] = self._ssh
-
- logger.trace('connect took {} seconds'.format(time() - start))
- logger.debug('new ssh: {0}'.format(self._ssh))
-
- logger.debug('Connect peer: {0}'.
- format(self._ssh.get_transport().getpeername()))
- logger.debug('Connections: {0}'.format(str(SSH.__existing_connections)))
-
- def disconnect(self, node):
+ try:
+ start = time()
+ pkey = None
+ if 'priv_key' in node:
+ pkey = RSAKey.from_private_key(
+ StringIO.StringIO(node['priv_key']))
+
+ self._ssh = SSHClient()
+ self._ssh.set_missing_host_key_policy(AutoAddPolicy())
+
+ self._ssh.connect(node['host'], username=node['username'],
+ password=node.get('password'), pkey=pkey,
+ port=node['port'])
+
+ self._ssh.get_transport().set_keepalive(10)
+
+ SSH.__existing_connections[node_hash] = self._ssh
+ logger.debug('New SSH to {peer} took {total} seconds: {ssh}'.
+ format(
+ peer=self._ssh.get_transport().getpeername(),
+ total=(time() - start),
+ ssh=self._ssh))
+ except SSHException as exc:
+ raise_from(IOError('Cannot connect to {host}'.format(
+ host=node['host'])), exc)
+ except NoValidConnectionsError as err:
+ raise_from(IOError(
+ 'Unable to connect to port {port} on {host}'.format(
+ port=node['port'], host=node['host'])), err)
+
+ def disconnect(self, node=None):