import os
import subprocess
import sys
+import collections
+import six
from functools import reduce
import yaml
-from keystoneauth1 import identity
-from keystoneauth1 import session
-from neutronclient.v2_0 import client
+from six.moves import configparser
from oslo_utils import importutils
+from oslo_serialization import jsonutils
import yardstick
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):
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 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'):
+ 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
-def get_neutron_client():
- sess = get_openstack_session()
- neutron_client = client.Client(session=sess)
- return neutron_client
+ return flatten_dict_key(next_data)