X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fbenchmark%2Fcontexts%2Fnode.py;h=d233e02aed6515de80b6991848fe487c3a5a6598;hb=b4dd62b673ea849fdbe6d54f3ab89b4f7a14c000;hp=78a2d1f46bb0bf4c20043048509b5207b18df6e9;hpb=5ce3b6f8c8b3217091e51a6041455738603d90b8;p=yardstick.git diff --git a/yardstick/benchmark/contexts/node.py b/yardstick/benchmark/contexts/node.py index 78a2d1f46..d233e02ae 100644 --- a/yardstick/benchmark/contexts/node.py +++ b/yardstick/benchmark/contexts/node.py @@ -7,30 +7,33 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from __future__ import absolute_import -import errno import subprocess import os import collections import logging +import tempfile -import yaml +import six import pkg_resources from yardstick import ssh +from yardstick.benchmark import contexts from yardstick.benchmark.contexts.base import Context from yardstick.common.constants import ANSIBLE_DIR, YARDSTICK_ROOT_PATH +from yardstick.common.ansible_common import AnsibleCommon +from yardstick.common.exceptions import ContextUpdateCollectdForNodeError LOG = logging.getLogger(__name__) +DEFAULT_DISPATCH = 'script' + class NodeContext(Context): """Class that handle nodes info""" - __context_type__ = "Node" + __context_type__ = contexts.CONTEXT_NODE def __init__(self): - self.name = None self.file_path = None self.nodes = [] self.networks = {} @@ -39,41 +42,17 @@ class NodeContext(Context): 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): - """Read from config file""" - - with open(self.file_path) as stream: - LOG.info("Parsing pod file: %s", self.file_path) - cfg = yaml.safe_load(stream) - return cfg - def init(self, attrs): """initializes itself from the supplied arguments""" - self.name = attrs["name"] - self.file_path = file_path = attrs.get("file", "pod.yaml") + super(NodeContext, self).init(attrs) - try: - cfg = self.read_config_file() - 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"]) - self.computes.extend([node for node in cfg["nodes"] - if node["role"] == "Compute"]) - self.baremetals.extend([node for node in cfg["nodes"] - if node["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) + cfg = self.read_pod_file(attrs) self.env = attrs.get('env', {}) self.attrs = attrs @@ -83,18 +62,12 @@ class NodeContext(Context): self.networks.update(cfg.get("networks", {})) def deploy(self): - config_type = self.env.get('type', '') - if config_type == 'ansible': - self._dispatch_ansible('setup') - elif config_type == 'script': - self._dispatch_script('setup') + config_type = self.env.get('type', DEFAULT_DISPATCH) + self.DISPATCH_TYPES[config_type]("setup") def undeploy(self): - config_type = self.env.get('type', '') - if config_type == 'ansible': - self._dispatch_ansible('teardown') - elif config_type == 'script': - self._dispatch_script('teardown') + config_type = self.env.get('type', DEFAULT_DISPATCH) + self.DISPATCH_TYPES[config_type]("teardown") super(NodeContext, self).undeploy() def _dispatch_script(self, key): @@ -105,22 +78,64 @@ class NodeContext(Context): def _dispatch_ansible(self, key): try: - step = self.env[key] + playbooks = self.env[key] except KeyError: pass else: - self._do_ansible_job(step) + 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_physical_nodes(self): + return self.nodes + + def _get_physical_node_for_server(self, server_name): + + node_name, context_name = self.split_host_name(server_name) + + if context_name is None or self.name != context_name: + return None + + for n in (n for n in self.nodes if n["name"] == node_name): + return "{}.{}".format(n["name"], self._name) + + return None + + def update_collectd_options_for_node(self, options, attr_name): + node_name, _ = self.split_host_name(attr_name) + + matching_nodes = (n for n in self.nodes if n["name"] == node_name) + try: + node = next(matching_nodes) + except StopIteration: + raise ContextUpdateCollectdForNodeError(attr_name=attr_name) - def _do_ansible_job(self, path): - cmd = 'ansible-playbook -i inventory.ini %s' % path - p = subprocess.Popen(cmd, shell=True, cwd=ANSIBLE_DIR) - p.communicate() + node["collectd"] = options 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 """ - node_name, name = self.split_name(attr_name) + node_name, name = self.split_host_name(attr_name) if name is None or self.name != name: return None @@ -138,7 +153,7 @@ class NodeContext(Context): except StopIteration: pass else: - raise ValueError("Duplicate nodes!!! Nodes: %s %s", + raise ValueError("Duplicate nodes!!! Nodes: %s %s" % (node, duplicate)) node["name"] = attr_name @@ -185,7 +200,7 @@ class NodeContext(Context): self.client._put_file_shell(script_file, '~/{}'.format(script)) cmd = 'sudo bash {} {}'.format(script, options) - status, stdout, stderr = self.client.execute(cmd) + status, _, stderr = self.client.execute(cmd) if status: raise RuntimeError(stderr)