X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fssh.py;h=cf9adf0dc6d5c994fbc57301b18e94a3fd459885;hb=92104932aa57b703d86926fc3aad13d742a41652;hp=cfbc3ca96239e084512c84671806b300390893b7;hpb=5c8396ba5bbeb12c70dae6ad3e777a28fefbcb4a;p=yardstick.git diff --git a/yardstick/ssh.py b/yardstick/ssh.py index cfbc3ca96..cf9adf0dc 100644 --- a/yardstick/ssh.py +++ b/yardstick/ssh.py @@ -70,13 +70,15 @@ import time import re import logging + import paramiko +from chainmap import ChainMap from oslo_utils import encodeutils from scp import SCPClient import six -DEFAULT_PORT = 22 +SSH_PORT = paramiko.config.SSH_PORT class SSHError(Exception): @@ -90,7 +92,7 @@ class SSHTimeout(SSHError): class SSH(object): """Represent ssh connection.""" - def __init__(self, user, host, port=DEFAULT_PORT, pkey=None, + def __init__(self, user, host, port=SSH_PORT, pkey=None, key_filename=None, password=None, name=None): """Initialize SSH client. @@ -109,6 +111,9 @@ class SSH(object): self.user = user self.host = host + # everybody wants to debug this in the caller, do it here instead + self.log.debug("user:%s host:%s", user, host) + # we may get text port from YAML, convert to int self.port = int(port) self.pkey = self._get_pkey(pkey) if pkey else None @@ -123,6 +128,23 @@ class SSH(object): else: logging.getLogger("paramiko").setLevel(logging.WARN) + @classmethod + def from_node(cls, node, overrides=None, defaults=None): + if overrides is None: + overrides = {} + if defaults is None: + defaults = {} + params = ChainMap(overrides, node, defaults) + return cls( + user=params['user'], + host=params['ip'], + # paramiko doesn't like None default, requires SSH_PORT default + port=params.get('ssh_port', SSH_PORT), + pkey=params.get('pkey'), + key_filename=params.get('key_filename'), + password=params.get('password'), + name=params.get('name')) + def _get_pkey(self, key): if isinstance(key, six.string_types): key = six.moves.StringIO(key)