1 # Copyright 2013: Mirantis Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # yardstick comment: this is a modified copy of rally/rally/common/utils.py
18 from __future__ import absolute_import
19 from __future__ import print_function
26 from functools import reduce
29 from six.moves import configparser
30 from oslo_utils import importutils
31 from oslo_serialization import jsonutils
35 logger = logging.getLogger(__name__)
36 logger.setLevel(logging.DEBUG)
39 # Decorator for cli-args
40 def cliargs(*args, **kwargs):
42 func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
47 def itersubclasses(cls, _seen=None):
48 """Generator over all subclasses of a given class in depth first order."""
50 if not isinstance(cls, type):
51 raise TypeError("itersubclasses must be called with "
52 "new-style classes, not %.100r" % cls)
53 _seen = _seen or set()
55 subs = cls.__subclasses__()
56 except TypeError: # fails only when cls is type
57 subs = cls.__subclasses__(cls)
62 for sub in itersubclasses(sub, _seen):
66 def try_append_module(name, modules):
67 if name not in modules:
68 modules[name] = importutils.import_module(name)
71 def import_modules_from_package(package):
72 """Import modules from package and append into sys.modules
74 :param: package - Full package name. For example: rally.deploy.engines
76 path = [os.path.dirname(yardstick.__file__), ".."] + package.split(".")
77 path = os.path.join(*path)
78 for root, dirs, files in os.walk(path):
79 for filename in files:
80 if filename.startswith("__") or not filename.endswith(".py"):
82 new_package = ".".join(root.split(os.sep)).split("....")[1]
83 module_name = "%s.%s" % (new_package, filename[:-3])
85 try_append_module(module_name, sys.modules)
87 logger.exception("unable to import %s", module_name)
90 def parse_yaml(file_path):
92 with open(file_path) as f:
93 value = yaml.safe_load(f)
97 if e.errno != errno.EEXIST:
103 def get_param(key, default=''):
105 conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml')
107 conf = parse_yaml(conf_file)
109 return reduce(lambda a, b: a[b], key.split('.'), conf)
120 if e.errno != errno.EEXIST:
124 def execute_command(cmd):
125 exec_msg = "Executing command: '%s'" % cmd
126 logger.debug(exec_msg)
128 output = subprocess.check_output(cmd.split()).split(os.linesep)
133 def source_env(env_file):
134 p = subprocess.Popen(". %s; env" % env_file, stdout=subprocess.PIPE,
136 output = p.communicate()[0]
137 env = dict((line.split('=', 1) for line in output.splitlines()))
138 os.environ.update(env)
142 def read_json_from_file(path):
143 with open(path, 'r') as f:
145 # don't use jsonutils.load() it conflicts with already decoded input
146 return jsonutils.loads(j)
149 def write_json_to_file(path, data, mode='w'):
150 with open(path, mode) as f:
151 jsonutils.dump(data, f)
154 def write_file(path, data, mode='w'):
155 with open(path, mode) as f:
159 def parse_ini_file(path):
160 parser = configparser.ConfigParser()
164 default = {k: v for k, v in parser.items('DEFAULT')}
165 except configparser.NoSectionError:
168 config = dict(DEFAULT=default,
169 **{s: {k: v for k, v in parser.items(
170 s)} for s in parser.sections()})
175 def get_port_mac(sshclient, port):
176 cmd = "ifconfig |grep HWaddr |grep %s |awk '{print $5}' " % port
177 status, stdout, stderr = sshclient.execute(cmd)
180 raise RuntimeError(stderr)
181 return stdout.rstrip()
184 def get_port_ip(sshclient, port):
185 cmd = "ifconfig %s |grep 'inet addr' |awk '{print $2}' " \
186 "|cut -d ':' -f2 " % port
187 status, stdout, stderr = sshclient.execute(cmd)
190 raise RuntimeError(stderr)
191 return stdout.rstrip()