aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark
diff options
context:
space:
mode:
authorRoss Brattain <ross.b.brattain@intel.com>2017-03-13 22:08:06 -0700
committerRoss Brattain <ross.b.brattain@intel.com>2017-08-11 21:09:17 -0700
commitc2f99db8b4d8f021b29a4e3aae483ba715936a66 (patch)
treece5cbf8443c14d1078aef5ae870235c7f706d0ad /yardstick/benchmark
parentae6f51c15a61e345cdc609f372ad04859d2e999d (diff)
Add Ansible executor class for node context
import the AnsibleCommon class to execute Ansible playbooks Update node context support to use AnsibleCommon needs unittests We must call ansible-playbook as an executable, so we must create temp files for inventory, and for the playbooks. AnsibleCommon has evolved to be quite flexible, it auto-generates the inventory from the context['nodes'] and generates groups from the node Role. We also support either a single playbook filename, or a list of filenames. If given a list we dynamically generate a playbook that includes the other playbooks. We support adding any number of extra_vars using a temp JSON file. Also designed to be extended by subclassing. Change-Id: I5bd0a2b4547feaadd70b7e2b8801f19371b99df0 Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
Diffstat (limited to 'yardstick/benchmark')
-rw-r--r--yardstick/benchmark/contexts/node.py55
1 files changed, 37 insertions, 18 deletions
diff --git a/yardstick/benchmark/contexts/node.py b/yardstick/benchmark/contexts/node.py
index 78a2d1f46..35c64335d 100644
--- a/yardstick/benchmark/contexts/node.py
+++ b/yardstick/benchmark/contexts/node.py
@@ -13,16 +13,21 @@ import subprocess
import os
import collections
import logging
+import tempfile
-import yaml
+import six
import pkg_resources
+import yaml
from yardstick import ssh
from yardstick.benchmark.contexts.base import Context
from yardstick.common.constants import ANSIBLE_DIR, YARDSTICK_ROOT_PATH
+from yardstick.common.ansible_common import AnsibleCommon
LOG = logging.getLogger(__name__)
+DEFAULT_DISPATCH = 'script'
+
class NodeContext(Context):
"""Class that handle nodes info"""
@@ -39,6 +44,10 @@ 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):
@@ -83,18 +92,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,16 +108,32 @@ 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)
-
- 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()
+ 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