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
29 from functools import reduce
30 from contextlib import closing
34 from flask import jsonify
35 from six.moves import configparser
36 from oslo_utils import importutils
37 from oslo_serialization import jsonutils
41 logger = logging.getLogger(__name__)
42 logger.setLevel(logging.DEBUG)
45 # Decorator for cli-args
46 def cliargs(*args, **kwargs):
48 func.__dict__.setdefault('arguments', []).insert(0, (args, kwargs))
53 def itersubclasses(cls, _seen=None):
54 """Generator over all subclasses of a given class in depth first order."""
56 if not isinstance(cls, type):
57 raise TypeError("itersubclasses must be called with "
58 "new-style classes, not %.100r" % cls)
59 _seen = _seen or set()
61 subs = cls.__subclasses__()
62 except TypeError: # fails only when cls is type
63 subs = cls.__subclasses__(cls)
68 for sub in itersubclasses(sub, _seen):
72 def try_append_module(name, modules):
73 if name not in modules:
74 modules[name] = importutils.import_module(name)
77 def import_modules_from_package(package):
78 """Import modules from package and append into sys.modules
80 :param: package - Full package name. For example: rally.deploy.engines
82 path = [os.path.dirname(yardstick.__file__), ".."] + package.split(".")
83 path = os.path.join(*path)
84 for root, dirs, files in os.walk(path):
85 for filename in files:
86 if filename.startswith("__") or not filename.endswith(".py"):
88 new_package = ".".join(root.split(os.sep)).split("....")[1]
89 module_name = "%s.%s" % (new_package, filename[:-3])
91 try_append_module(module_name, sys.modules)
93 logger.exception("unable to import %s", module_name)
96 def parse_yaml(file_path):
98 with open(file_path) as f:
99 value = yaml.safe_load(f)
103 if e.errno != errno.EEXIST:
109 def get_param(key, default=''):
111 conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml')
113 conf = parse_yaml(conf_file)
115 return reduce(lambda a, b: a[b], key.split('.'), conf)
126 if e.errno != errno.EEXIST:
130 def remove_file(path):
134 if e.errno != errno.ENOENT:
138 def execute_command(cmd):
139 exec_msg = "Executing command: '%s'" % cmd
140 logger.debug(exec_msg)
142 output = subprocess.check_output(cmd.split()).split(os.linesep)
147 def source_env(env_file):
148 p = subprocess.Popen(". %s; env" % env_file, stdout=subprocess.PIPE,
150 output = p.communicate()[0]
151 env = dict((line.split('=', 1) for line in output.splitlines()))
152 os.environ.update(env)
156 def read_json_from_file(path):
157 with open(path, 'r') as f:
159 # don't use jsonutils.load() it conflicts with already decoded input
160 return jsonutils.loads(j)
163 def write_json_to_file(path, data, mode='w'):
164 with open(path, mode) as f:
165 jsonutils.dump(data, f)
168 def write_file(path, data, mode='w'):
169 with open(path, mode) as f:
173 def parse_ini_file(path):
174 parser = configparser.ConfigParser()
177 files = parser.read(path)
178 except configparser.MissingSectionHeaderError:
179 logger.exception('invalid file type')
183 raise RuntimeError('file not exist')
186 default = {k: v for k, v in parser.items('DEFAULT')}
187 except configparser.NoSectionError:
190 config = dict(DEFAULT=default,
191 **{s: {k: v for k, v in parser.items(
192 s)} for s in parser.sections()})
197 def get_port_mac(sshclient, port):
198 cmd = "ifconfig |grep HWaddr |grep %s |awk '{print $5}' " % port
199 status, stdout, stderr = sshclient.execute(cmd)
202 raise RuntimeError(stderr)
203 return stdout.rstrip()
206 def get_port_ip(sshclient, port):
207 cmd = "ifconfig %s |grep 'inet addr' |awk '{print $2}' " \
208 "|cut -d ':' -f2 " % port
209 status, stdout, stderr = sshclient.execute(cmd)
212 raise RuntimeError(stderr)
213 return stdout.rstrip()
216 def flatten_dict_key(data):
219 # use list, because iterable is too generic
220 if not any(isinstance(v, (collections.Mapping, list))
221 for v in data.values()):
224 for k, v in six.iteritems(data):
225 if isinstance(v, collections.Mapping):
226 for n_k, n_v in six.iteritems(v):
227 next_data["%s.%s" % (k, n_k)] = n_v
228 # use list because iterable is too generic
229 elif isinstance(v, list):
230 for index, item in enumerate(v):
231 next_data["%s%d" % (k, index)] = item
235 return flatten_dict_key(next_data)
238 def translate_to_str(obj):
239 if isinstance(obj, collections.Mapping):
240 return {str(k): translate_to_str(v) for k, v in obj.items()}
241 elif isinstance(obj, list):
242 return [translate_to_str(ele) for ele in obj]
243 elif isinstance(obj, six.text_type):
248 def result_handler(status, data):
253 return jsonify(result)
256 def change_obj_to_dict(obj):
258 for k, v in vars(obj).items():
266 def set_dict_value(dic, keys, value):
269 for key in keys.split('.'):
271 return_dic.setdefault(key, {})
272 if key == keys.split('.')[-1]:
273 return_dic[key] = value
275 return_dic = return_dic[key]
279 def get_free_port(ip):
280 with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
282 port = random.randint(5000, 10000)
283 if s.connect_ex((ip, port)) != 0: