standardize ssh auth
[yardstick.git] / yardstick / benchmark / scenarios / availability / monitor / monitor_command.py
index c285024..ef07d94 100644 (file)
@@ -6,17 +6,19 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
+from __future__ import absolute_import
 import logging
 import subprocess
 import traceback
+
 import yardstick.ssh as ssh
-import basemonitor as basemonitor
+from yardstick.benchmark.scenarios.availability.monitor import basemonitor
 
 LOG = logging.getLogger(__name__)
 
 
 def _execute_shell_command(command):
-    '''execute shell script with error handling'''
+    """execute shell script with error handling"""
     exitcode = 0
     output = []
     try:
@@ -24,7 +26,7 @@ def _execute_shell_command(command):
     except Exception:
         exitcode = -1
         output = traceback.format_exc()
-        LOG.error("exec command '%s' error:\n " % command)
+        LOG.error("exec command '%s' error:\n ", command)
         LOG.error(traceback.format_exc())
 
     return exitcode, output
@@ -40,11 +42,9 @@ class MonitorOpenstackCmd(basemonitor.BaseMonitor):
         node_name = self._config.get("host", None)
         if node_name:
             host = self._context[node_name]
-            ip = host.get("ip", None)
-            user = host.get("user", "root")
-            key_filename = host.get("key_filename", "~/.ssh/id_rsa")
 
-            self.connection = ssh.SSH(user, ip, key_filename=key_filename)
+            self.connection = ssh.SSH.from_node(host,
+                                                defaults={"user": "root"})
             self.connection.wait(timeout=600)
             LOG.debug("ssh host success!")
 
@@ -56,12 +56,13 @@ class MonitorOpenstackCmd(basemonitor.BaseMonitor):
     def monitor_func(self):
         exit_status = 0
         if self.connection:
-            exit_status, stdout, stderr = self.connection.execute(
-                "/bin/bash -s '{0}'".format(self.cmd),
-                stdin=open(self.check_script, "r"))
+            with open(self.check_script, "r") as stdin_file:
+                exit_status, stdout, stderr = self.connection.execute(
+                    "/bin/bash -s '{0}'".format(self.cmd),
+                    stdin=stdin_file)
 
-            LOG.debug("the ret stats: %s stdout: %s stderr: %s" %
-                      (exit_status, stdout, stderr))
+            LOG.debug("the ret stats: %s stdout: %s stderr: %s",
+                      exit_status, stdout, stderr)
         else:
             exit_status, stdout = _execute_shell_command(self.cmd)
         if exit_status:
@@ -70,10 +71,10 @@ class MonitorOpenstackCmd(basemonitor.BaseMonitor):
 
     def verify_SLA(self):
         outage_time = self._result.get('outage_time', None)
-        LOG.debug("the _result:%s" % self._result)
+        LOG.debug("the _result:%s", self._result)
         max_outage_time = self._config["sla"]["max_outage_time"]
         if outage_time > max_outage_time:
-            LOG.info("SLA failure: %f > %f" % (outage_time, max_outage_time))
+            LOG.info("SLA failure: %f > %f", outage_time, max_outage_time)
             return False
         else:
             LOG.info("the sla is passed")