Merge "standardize ssh auth"
[yardstick.git] / yardstick / benchmark / contexts / node.py
index 6fa9aa9..baa1cf5 100644 (file)
@@ -19,7 +19,7 @@ import pkg_resources
 
 from yardstick import ssh
 from yardstick.benchmark.contexts.base import Context
-from yardstick.common.constants import YARDSTICK_ROOT_PATH
+from yardstick.common import constants as consts
 
 LOG = logging.getLogger(__name__)
 
@@ -57,7 +57,7 @@ class NodeContext(Context):
         except IOError as ioerror:
             if ioerror.errno == errno.ENOENT:
                 self.file_path = \
-                    os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
+                    os.path.join(consts.YARDSTICK_ROOT_PATH, self.file_path)
                 cfg = self.read_config_file()
             else:
                 raise
@@ -78,17 +78,39 @@ class NodeContext(Context):
         LOG.debug("Env: %r", self.env)
 
     def deploy(self):
-        setups = self.env.get('setup', [])
-        for setup in setups:
-            for host, info in setup.items():
-                self._execute_script(host, info)
+        config_type = self.env.get('type', '')
+        if config_type == 'ansible':
+            self._dispatch_ansible('setup')
+        elif config_type == 'script':
+            self._dispatch_script('setup')
 
     def undeploy(self):
-        teardowns = self.env.get('teardown', [])
-        for teardown in teardowns:
-            for host, info in teardown.items():
+        config_type = self.env.get('type', '')
+        if config_type == 'ansible':
+            self._dispatch_ansible('teardown')
+        elif config_type == 'script':
+            self._dispatch_script('teardown')
+        super(NodeContext, self).undeploy()
+
+    def _dispatch_script(self, key):
+        steps = self.env.get(key, [])
+        for step in steps:
+            for host, info in step.items():
                 self._execute_script(host, info)
 
+    def _dispatch_ansible(self, key):
+        try:
+            step = self.env[key]
+        except KeyError:
+            pass
+        else:
+            self._do_ansible_job(step)
+
+    def _do_ansible_job(self, path):
+        cmd = 'ansible-playbook -i inventory.ini %s' % path
+        p = subprocess.Popen(cmd, shell=True, cwd=consts.ANSIBLE_DIR)
+        p.communicate()
+
     def _get_server(self, attr_name):
         """lookup server info by name from context
         attr_name: a name for a server listed in nodes config file
@@ -141,7 +163,7 @@ class NodeContext(Context):
 
     def _execute_local_script(self, info):
         script, options = self._get_script(info)
-        script = os.path.join(YARDSTICK_ROOT_PATH, script)
+        script = os.path.join(consts.YARDSTICK_ROOT_PATH, script)
         cmd = ['bash', script, options]
 
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
@@ -156,21 +178,7 @@ class NodeContext(Context):
         if node is None:
             raise SystemExit('No such node')
 
-        user = node.get('user', 'ubuntu')
-        ssh_port = node.get("ssh_port", ssh.DEFAULT_PORT)
-        ip = node.get('ip')
-        pwd = node.get('password')
-        key_fname = node.get('key_filename', '/root/.ssh/id_rsa')
-
-        if pwd is not None:
-            LOG.debug("Log in via pw, user:%s, host:%s, password:%s",
-                      user, ip, pwd)
-            self.client = ssh.SSH(user, ip, password=pwd, port=ssh_port)
-        else:
-            LOG.debug("Log in via key, user:%s, host:%s, key_filename:%s",
-                      user, ip, key_fname)
-            self.client = ssh.SSH(user, ip, key_filename=key_fname,
-                                  port=ssh_port)
+        self.client = ssh.SSH.from_node(node, defaults={'user': 'ubuntu'})
 
         self.client.wait(timeout=600)