aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/ssh.py
diff options
context:
space:
mode:
authorRoss Brattain <ross.b.brattain@intel.com>2017-03-01 17:28:46 -0800
committerRoss Brattain <ross.b.brattain@intel.com>2017-04-11 21:58:20 -0700
commit99abbb424007da2e01762f3c040a39c0157cbe1f (patch)
treebaab901a9e7444c9fd36aa4a19c1e51d03cb8e7f /yardstick/ssh.py
parent2240fcc201fa9665e42e92c29e201cb62490acfa (diff)
standardize ssh auth
we need to be following defautl paramiko rules, first use pkey, then key_filenames (autodetecting ~/.ssh/ keys), then password We have too much boilerplate redudant code everywhere, we need to standardize on a factory function that takes a node dict. Using Python3 ChainMap we can layer overrides and defaults. VNF descriptors have to default key_filename, password to Python None. The only way to do this is to omit key values if the variable is not defined, this way the dict will not have the value and it will default to Python None Add python2 chainmap backport Updated unittest mocking to use ssh.SSH.from_node Change-Id: I80b0cb606e593b33e317c9e5e8ed0b74da591514 Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
Diffstat (limited to 'yardstick/ssh.py')
-rw-r--r--yardstick/ssh.py26
1 files changed, 24 insertions, 2 deletions
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)