Merge "Disable spoof check on vfs in sriov setup"
[yardstick.git] / yardstick / benchmark / contexts / node.py
index f8c38cb..ffc82c8 100644 (file)
@@ -13,16 +13,21 @@ import subprocess
 import os
 import collections
 import logging
+import tempfile
 
-import yaml
+import six
 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.constants import ANSIBLE_DIR, YARDSTICK_ROOT_PATH
+from yardstick.common.ansible_common import AnsibleCommon
+from yardstick.common.yaml_loader import yaml_load
 
 LOG = logging.getLogger(__name__)
 
+DEFAULT_DISPATCH = 'script'
+
 
 class NodeContext(Context):
     """Class that handle nodes info"""
@@ -33,10 +38,16 @@ class NodeContext(Context):
         self.name = None
         self.file_path = None
         self.nodes = []
+        self.networks = {}
         self.controllers = []
         self.computes = []
         self.baremetals = []
         self.env = {}
+        self.attrs = {}
+        self.DISPATCH_TYPES = {
+            "ansible": self._dispatch_ansible,
+            "script": self._dispatch_script,
+        }
         super(NodeContext, self).__init__()
 
     def read_config_file(self):
@@ -44,63 +55,94 @@ class NodeContext(Context):
 
         with open(self.file_path) as stream:
             LOG.info("Parsing pod file: %s", self.file_path)
-            cfg = yaml.load(stream)
+            cfg = yaml_load(stream)
         return cfg
 
     def init(self, attrs):
         """initializes itself from the supplied arguments"""
         self.name = attrs["name"]
-        self.file_path = attrs.get("file", "pod.yaml")
+        self.file_path = file_path = attrs.get("file", "pod.yaml")
 
         try:
             cfg = self.read_config_file()
-        except IOError as ioerror:
-            if ioerror.errno == errno.ENOENT:
-                self.file_path = \
-                    os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
-                cfg = self.read_config_file()
-            else:
+        except IOError as io_error:
+            if io_error.errno != errno.ENOENT:
                 raise
 
+            self.file_path = os.path.join(YARDSTICK_ROOT_PATH, file_path)
+            cfg = self.read_config_file()
+
         self.nodes.extend(cfg["nodes"])
         self.controllers.extend([node for node in cfg["nodes"]
-                                 if node["role"] == "Controller"])
+                                 if node.get("role") == "Controller"])
         self.computes.extend([node for node in cfg["nodes"]
-                              if node["role"] == "Compute"])
+                              if node.get("role") == "Compute"])
         self.baremetals.extend([node for node in cfg["nodes"]
-                                if node["role"] == "Baremetal"])
+                                if node.get("role") == "Baremetal"])
         LOG.debug("Nodes: %r", self.nodes)
         LOG.debug("Controllers: %r", self.controllers)
         LOG.debug("Computes: %r", self.computes)
         LOG.debug("BareMetals: %r", self.baremetals)
 
         self.env = attrs.get('env', {})
+        self.attrs = attrs
         LOG.debug("Env: %r", self.env)
 
+        # add optional static network definition
+        self.networks.update(cfg.get("networks", {}))
+
     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', DEFAULT_DISPATCH)
+        self.DISPATCH_TYPES[config_type]("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', DEFAULT_DISPATCH)
+        self.DISPATCH_TYPES[config_type]("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)
 
-        super(NodeContext, self).undeploy()
+    def _dispatch_ansible(self, key):
+        try:
+            playbooks = self.env[key]
+        except KeyError:
+            pass
+        else:
+            self._do_ansible_job(playbooks)
+
+    def _do_ansible_job(self, playbooks):
+        self.ansible_exec = AnsibleCommon(nodes=self.nodes,
+                                          test_vars=self.env)
+        # playbooks relative to ansible dir
+        # playbooks can also be a list of playbooks
+        self.ansible_exec.gen_inventory_ini_dict()
+        if isinstance(playbooks, six.string_types):
+            playbooks = [playbooks]
+        playbooks = [self.fix_ansible_path(playbook) for playbook in playbooks]
+
+        tmpdir = tempfile.mkdtemp(prefix='ansible-')
+        self.ansible_exec.execute_ansible(playbooks, tmpdir,
+                                          verbose=self.env.get("verbose",
+                                                               False))
+
+    def fix_ansible_path(self, playbook):
+        if not os.path.isabs(playbook):
+            #  make relative paths absolute in ANSIBLE_DIR
+            playbook = os.path.join(ANSIBLE_DIR, playbook)
+        return playbook
 
     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
         """
-        if isinstance(attr_name, collections.Mapping):
+        node_name, name = self.split_name(attr_name)
+        if name is None or self.name != name:
             return None
 
-        if self.name != attr_name.split(".")[1]:
-            return None
-        node_name = attr_name.split(".")[0]
         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
 
         try:
@@ -116,11 +158,36 @@ class NodeContext(Context):
             pass
         else:
             raise ValueError("Duplicate nodes!!! Nodes: %s %s",
-                             (matching_nodes, duplicate))
+                             (node, duplicate))
 
         node["name"] = attr_name
+        node.setdefault("interfaces", {})
         return node
 
+    def _get_network(self, attr_name):
+        if not isinstance(attr_name, collections.Mapping):
+            network = self.networks.get(attr_name)
+
+        else:
+            # Don't generalize too much  Just support vld_id
+            vld_id = attr_name.get('vld_id', {})
+            # for node context networks are dicts
+            iter1 = (n for n in self.networks.values() if n.get('vld_id') == vld_id)
+            network = next(iter1, None)
+
+        if network is None:
+            return None
+
+        result = {
+            # name is required
+            "name": network["name"],
+            "vld_id": network.get("vld_id"),
+            "segmentation_id": network.get("segmentation_id"),
+            "network_type": network.get("network_type"),
+            "physical_network": network.get("physical_network"),
+        }
+        return result
+
     def _execute_script(self, node_name, info):
         if node_name == 'local':
             self._execute_local_script(info)
@@ -158,21 +225,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)