summaryrefslogtreecommitdiffstats
path: root/func/driver.py
blob: 47d00f1f54022a78be3fb0b319e278284270a70a (plain)
1
2
3
4
5
6
7
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Num
##############################################################################
# Copyright (c) 2015 Dell Inc, ZTE  and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from utils import logger_utils
from operator import add
from ansible_api import AnsibleApi


logger = logger_utils.QtipLogger('driver').get


class Driver:

    def __init__(self):

        logger.info("Class driver initialized\n")
        self.installer_username = {'fuel': 'root',
                                   'joid': 'ubuntu',
                                   'apex': 'heat-admin'}

    @staticmethod
    def merge_two_dicts(x, y):
        '''
        It is from http://stackoverflow.com/questions/38987/
        how-can-i-merge-two-python-dictionaries-in-a-single-expression
        '''
        z = x.copy()
        z.update(y)
        return z

    def get_common_var_json(self, installer_type, pwd, benchmark_fname,
                            benchmark_detail, pip_dict, proxy_info):
        common_json = {'Dest_dir': 'results',
                       'ip1': '',
                       'ip2': '',
                       'installer': str(installer_type),
                       'workingdir': str(pwd),
                       'fname': str(benchmark_fname),
                       'username': self.installer_username[str(installer_type)]}
        common_json.update(benchmark_detail) if benchmark_detail else None
        common_json.update(proxy_info) if proxy_info else None
        return common_json

    def get_special_var_json(self, role, roles, benchmark_detail, pip_dict):
        special_json = {}
        index = roles.index(role) + 1
        private_ip = pip_dict[0][1] if pip_dict[0][1][0] else 'NONE'
        map(lambda x: special_json.update({'ip' + str(index): x}), role[1])\
            if benchmark_detail and (role[0] == '1-server') else None
        map(lambda x: special_json.update({'privateip' + str(index): private_ip}), role[1])\
            if benchmark_detail and (role[0] == '1-server') else None
        special_json = self.get_special_var_json(filter(lambda x: x[0] == '1-server', roles)[0],
                                                 roles,
                                                 benchmark_detail,
                                                 pip_dict) if role[0] == '2-host' else special_json
        special_json.update({'role': role[0]})
        return special_json

    def run_ansible_playbook(self, benchmark, extra_vars):
        logger.info(extra_vars)
        ansible_api = AnsibleApi()
        ansible_api.execute_playbook('./config/hosts',
                                     './benchmarks/perftest/{0}.yaml'.format(benchmark),
                                     './config/QtipKey', extra_vars)
        return self.get_ansible_result(extra_vars['role'], ansible_api.get_detail_playbook_stats())

    def drive_bench(self, installer_type, pwd, benchmark, roles, benchmark_fname,
                    benchmark_detail=None, pip_dict=None, proxy_info=None):
        roles = sorted(roles)
        pip_dict = sorted(pip_dict)
        var_json = self.get_common_var_json(installer_type, pwd, benchmark_fname,
                                            benchmark_detail, pip_dict, proxy_info)
        result = map(lambda role: self.run_ansible_playbook
                     (benchmark, self.merge_two_dicts(var_json,
                                                      self.get_special_var_json(role, roles,
                                                                                benchmark_detail,
                                                                                pip_dict))), roles)
        return reduce(self._merge_ansible_result, result)

    def get_ansible_result(self, role, stats):
        result = reduce(add, map(lambda x: x[1]['failures'] + x[1]['unreachable'], stats))
        return {'result': result,
                'detail': {role: stats}}

    def _merge_ansible_result(self, result_1, result_2):
        return {'result': result_1['result'] + result_2['result'],
                'detail': self.merge_two_dicts(result_1['detail'], result_2['detail'])}