X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=yardstick%2Fcommon%2Futils.py;h=92bb7b7d35c2e87af61f1020af831020df2f87b7;hb=146e193c209fe654c4aeb88e9a2564df9960a8e4;hp=473bbf540aed087ab0b72b30c8547a0f9d88a5ae;hpb=5bd185f75c1c9f886c0125e8bc9d2d6f8bced138;p=yardstick.git diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 473bbf540..92bb7b7d3 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -23,12 +23,16 @@ import logging import os import subprocess import sys +import collections +import socket +import random from functools import reduce +from contextlib import closing import yaml -from keystoneauth1 import identity -from keystoneauth1 import session -from neutronclient.v2_0 import client +import six +from flask import jsonify +from six.moves import configparser from oslo_utils import importutils from oslo_serialization import jsonutils @@ -83,29 +87,36 @@ def import_modules_from_package(package): continue new_package = ".".join(root.split(os.sep)).split("....")[1] module_name = "%s.%s" % (new_package, filename[:-3]) - try_append_module(module_name, sys.modules) + try: + try_append_module(module_name, sys.modules) + except ImportError: + logger.exception("unable to import %s", module_name) -def get_para_from_yaml(file_path, args): - - def func(a, b): - if a is None: - return None - return a.get(b) - - if os.path.exists(file_path): +def parse_yaml(file_path): + try: with open(file_path) as f: value = yaml.safe_load(f) - value = reduce(func, args.split('.'), value) + except IOError: + return {} + except OSError as e: + if e.errno != errno.EEXIST: + raise + else: + return value - if value is None: - print('parameter not found') - return None - return value - else: - print('file not exist') - return None +def get_param(key, default=''): + + conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml') + + conf = parse_yaml(conf_file) + try: + return reduce(lambda a, b: a[b], key.split('.'), conf) + except KeyError: + if not default: + raise + return default def makedirs(d): @@ -116,6 +127,14 @@ def makedirs(d): raise +def remove_file(path): + try: + os.remove(path) + except OSError as e: + if e.errno != errno.ENOENT: + raise + + def execute_command(cmd): exec_msg = "Executing command: '%s'" % cmd logger.debug(exec_msg) @@ -134,24 +153,124 @@ def source_env(env_file): return env -def get_openstack_session(): - auth = identity.Password(auth_url=os.environ.get('OS_AUTH_URL'), - username=os.environ.get('OS_USERNAME'), - password=os.environ.get('OS_PASSWORD'), - tenant_name=os.environ.get('OS_TENANT_NAME')) - return session.Session(auth=auth) - - -def get_neutron_client(): - sess = get_openstack_session() - neutron_client = client.Client(session=sess) - return neutron_client +def read_json_from_file(path): + with open(path, 'r') as f: + j = f.read() + # don't use jsonutils.load() it conflicts with already decoded input + return jsonutils.loads(j) def write_json_to_file(path, data, mode='w'): - write_file(path, jsonutils.dump_as_bytes(data), mode) + with open(path, mode) as f: + jsonutils.dump(data, f) def write_file(path, data, mode='w'): with open(path, mode) as f: f.write(data) + + +def parse_ini_file(path): + parser = configparser.ConfigParser() + parser.read(path) + + try: + default = {k: v for k, v in parser.items('DEFAULT')} + except configparser.NoSectionError: + default = {} + + config = dict(DEFAULT=default, + **{s: {k: v for k, v in parser.items( + s)} for s in parser.sections()}) + + return config + + +def get_port_mac(sshclient, port): + cmd = "ifconfig |grep HWaddr |grep %s |awk '{print $5}' " % port + status, stdout, stderr = sshclient.execute(cmd) + + if status: + raise RuntimeError(stderr) + return stdout.rstrip() + + +def get_port_ip(sshclient, port): + cmd = "ifconfig %s |grep 'inet addr' |awk '{print $2}' " \ + "|cut -d ':' -f2 " % port + status, stdout, stderr = sshclient.execute(cmd) + + if status: + raise RuntimeError(stderr) + return stdout.rstrip() + + +def flatten_dict_key(data): + next_data = {} + + # use list, because iterable is too generic + if not any(isinstance(v, (collections.Mapping, list)) + for v in data.values()): + return data + + for k, v in six.iteritems(data): + if isinstance(v, collections.Mapping): + for n_k, n_v in six.iteritems(v): + next_data["%s.%s" % (k, n_k)] = n_v + # use list because iterable is too generic + elif isinstance(v, list): + for index, item in enumerate(v): + next_data["%s%d" % (k, index)] = item + else: + next_data[k] = v + + return flatten_dict_key(next_data) + + +def translate_to_str(obj): + if isinstance(obj, collections.Mapping): + return {str(k): translate_to_str(v) for k, v in obj.items()} + elif isinstance(obj, list): + return [translate_to_str(ele) for ele in obj] + elif isinstance(obj, six.text_type): + return str(obj) + return obj + + +def result_handler(status, data): + result = { + 'status': status, + 'result': data + } + return jsonify(result) + + +def change_obj_to_dict(obj): + dic = {} + for k, v in vars(obj).items(): + try: + vars(v) + except TypeError: + dic.update({k: v}) + return dic + + +def set_dict_value(dic, keys, value): + return_dic = dic + + for key in keys.split('.'): + + return_dic.setdefault(key, {}) + if key == keys.split('.')[-1]: + return_dic[key] = value + else: + return_dic = return_dic[key] + return dic + + +def get_free_port(ip): + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: + while True: + port = random.randint(5000, 10000) + if s.connect_ex((ip, port)) != 0: + return port