3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # pylint: disable=missing-docstring
12 from __future__ import print_function
18 LOGGER = logging.getLogger(__name__)
21 def execute_command_raise(cmd, info=False, error_msg="",
22 verbose=True, output_file=None):
23 ret = execute_command(cmd, info, error_msg, verbose, output_file)
25 raise Exception(error_msg)
28 def execute_command(cmd, info=False, error_msg="",
29 verbose=True, output_file=None):
31 error_msg = ("The command '%s' failed." % cmd)
32 msg_exec = ("Executing command: '%s'" % cmd)
37 LOGGER.debug(msg_exec)
38 popen = subprocess.Popen(
39 cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
41 ofd = open(output_file, "w")
42 for line in iter(popen.stdout.readline, b''):
46 line = line.replace('\n', '')
52 returncode = popen.wait()
55 LOGGER.error(error_msg)
60 def get_parameter_from_yaml(parameter, yfile):
62 Returns the value of a given parameter in file.yaml
63 parameter must be given in string format with dots
64 Example: general.openstack.image_name
66 with open(yfile) as yfd:
67 file_yaml = yaml.safe_load(yfd)
69 for element in parameter.split("."):
70 value = value.get(element)
72 raise ValueError("The parameter %s is not defined in"
73 " %s" % (parameter, yfile))