From 57bcc8497c3f9ca4d5d7b1728afe5805c1516cd6 Mon Sep 17 00:00:00 2001 From: ahothan Date: Thu, 19 Mar 2015 08:50:07 -0700 Subject: [PATCH] Add support for $HOME/.vmtp.yaml Change-Id: I9bdba6a76491867d05ef4a961a5bd6b191cde309 --- doc/source/usage.rst | 16 ++++++++++++---- vmtp.py | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/doc/source/usage.rst b/doc/source/usage.rst index 9cb9d4a..2c6a18b 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -21,7 +21,7 @@ VMTP Usage [--stop-on-error] [--vm_image_url ] [--test_description ] - OpenStack VM Throughput V2.0.3 + OpenStack VM Throughput V2.0.4 optional arguments: -h, --help show this help message and exit @@ -77,13 +77,21 @@ VMTP Usage Configuration File ^^^^^^^^^^^^^^^^^^ -VMTP configuration files follow the yaml syntax and contain variables used by VMTP to run and collect performance data. The default configuration is stored in the cfg.default.yaml file. +VMTP configuration files follow the yaml syntax and contain variables used by VMTP to run and collect performance data. +The default configuration is stored in the cfg.default.yaml file. -Default values should be overwritten for any cloud under test by defining new variable values in a new configuration file that follows the same format. Variables that are not defined in the new configuration file will retain their default values. +Default values should be overwritten for any cloud under test by defining new variable values in a new configuration file that follows the same format. +Variables that are not defined in the new configuration file will retain their default values. +The precedence order for configuration files is as follows: +- the command line argument "-c " has highest precedence +- $HOME/.vmtp.yaml if the file exists in the user home directory +- cfg.default.yaml has the lowest precedence (always exists in the VMTP package root directory) + +To override a default value set in cfg.default.yaml, simply redefine that value in the configuration file passed in -c or in the $HOME/.vmtp.yaml file. Check the content of cfg.default.yaml file as it contains the list of configuration variables and instructions on how to set them. -**Note:** the configuration file is not needed if the VMTP only runs the native host throughput option (*--host*) +**Note:** the configuration file is not needed if VMTP only runs the native host throughput option (*--host*) OpenStack openrc File diff --git a/vmtp.py b/vmtp.py index 6799129..0b72050 100755 --- a/vmtp.py +++ b/vmtp.py @@ -40,7 +40,7 @@ from neutronclient.v2_0 import client as neutronclient from novaclient.client import Client from novaclient.exceptions import ClientException -__version__ = '2.0.3' +__version__ = '2.0.4' from perf_instance import PerfInstance as PerfInstance @@ -511,6 +511,30 @@ def extract_user_host_pwd(user_host_pwd): sys.exit(4) return match.groups() +def _merge_config(file, source_config, required=False): + ''' + returns the merged config or exits if the file does not exist and is required + ''' + dest_config = source_config + + fullname = os.path.expanduser(file) + if os.path.isfile(fullname): + print('Loading ' + fullname + '...') + try: + alt_config = configure.Configuration.from_file(fullname).configure() + dest_config = source_config.merge(alt_config) + + except configure.ConfigurationError: + # this is in most cases when the config file passed is empty + # configure.ConfigurationError: unconfigured + # in case of syntax error, another exception is thrown: + # TypeError: string indices must be integers, not str + pass + elif required: + print('Error: configration file %s does not exist' % (fullname)) + sys.exit(1) + return dest_config + if __name__ == '__main__': fpr = FlowPrinter() @@ -655,10 +679,15 @@ if __name__ == '__main__': default_cfg_file = get_absolute_path_for_file("cfg.default.yaml") # read the default configuration file and possibly an override config file + # the precedence order is as follows: + # $HOME/.vmtp.yaml if exists + # -c from command line if provided + # cfg.default.yaml config = configure.Configuration.from_file(default_cfg_file).configure() + config = _merge_config('~/.vmtp.yaml', config) + if opts.config: - alt_config = configure.Configuration.from_file(opts.config).configure() - config = config.merge(alt_config) + config = _merge_config(opts.config, config, required=True) if opts.version: print('Version ' + __version__)