From 46754323aeac6193dd9b451a6525a478fdb0887b Mon Sep 17 00:00:00 2001 From: xudan Date: Tue, 29 May 2018 02:42:11 -0400 Subject: Replace openstack commands with shade Use openstack shade to get endpoint infomation rather than executing openstack commands directly. The next step can replace openstack commands by shade for some test cases that need to create openstack resources (images, flavors) if we agree to use shade. JIRA: DOVETAIL-659 Change-Id: I72b302fb2d4a61e9249a7e53f81ea9464944dcd3 Signed-off-by: xudan --- dovetail/utils/dovetail_config.py | 15 +++-------- dovetail/utils/dovetail_utils.py | 53 ++++++++------------------------------- dovetail/utils/openstack_utils.py | 20 +++++++++++++++ 3 files changed, 34 insertions(+), 54 deletions(-) create mode 100644 dovetail/utils/openstack_utils.py (limited to 'dovetail/utils') diff --git a/dovetail/utils/dovetail_config.py b/dovetail/utils/dovetail_config.py index 394bcf73..67ad9612 100644 --- a/dovetail/utils/dovetail_config.py +++ b/dovetail/utils/dovetail_config.py @@ -21,17 +21,10 @@ class DovetailConfig(object): cls.dovetail_config = yaml.safe_load(f) for extra_config_file in cls.dovetail_config['include_config']: - - # The yardstick and bottlenecks config files are with Jinja2. - # They need to be parsed later. - # All other config files should be transfer to like this gradually. - if extra_config_file.startswith(("yardstick", "bottlenecks")): - continue - else: - file_path = os.path.join(conf_path, extra_config_file) - with open(file_path) as f: - extra_config = yaml.safe_load(f) - cls.dovetail_config.update(extra_config) + file_path = os.path.join(conf_path, extra_config_file) + with open(file_path) as f: + extra_config = yaml.safe_load(f) + cls.dovetail_config.update(extra_config) path = os.path.join(conf_path, cls.dovetail_config['cli_file_name']) with open(path) as f: diff --git a/dovetail/utils/dovetail_utils.py b/dovetail/utils/dovetail_utils.py index 02ffa798..e758a84b 100644 --- a/dovetail/utils/dovetail_utils.py +++ b/dovetail/utils/dovetail_utils.py @@ -22,6 +22,7 @@ import python_hosts from dovetail import constants from dovetail_config import DovetailConfig as dt_cfg +from openstack_utils import OS_Utils def exec_log(verbose, logger, msg, level, flush=False): @@ -132,38 +133,6 @@ def check_https_enabled(logger=None): return False -def get_ext_net_name(env_file, logger=None): - ext_net = os.getenv('EXTERNAL_NETWORK') - if ext_net: - return ext_net - else: - https_enabled = check_https_enabled(logger) - insecure_option = '' - insecure = os.getenv('OS_INSECURE') - if https_enabled: - logger.debug("https enabled...") - if insecure: - if insecure.lower() == "true": - insecure_option = ' --insecure ' - else: - logger.warn("Env variable OS_INSECURE is {}, if https + " - "no credential used, should be set as True." - .format(insecure)) - - cmd_check = "openstack %s network list" % insecure_option - ret, msg = exec_cmd(cmd_check, logger) - if ret: - logger.error("The credentials info in {} is invalid." - .format(env_file)) - return None - cmd = "openstack %s network list --long | grep 'External' | head -1 | \ - awk '{print $4}'" % insecure_option - ret, msg = exec_cmd(cmd, logger) - if not ret: - return msg - return None - - def get_duration(start_date, stop_date, logger): fmt = '%Y-%m-%d %H:%M:%S' try: @@ -296,17 +265,15 @@ def combine_files(file_path, result_file, logger=None): def get_openstack_endpoint(logger=None): https_enabled = check_https_enabled(logger) - insecure_option = '' insecure = os.getenv('OS_INSECURE') - if https_enabled: - if insecure: - if insecure.lower() == "true": - insecure_option = ' --insecure ' - cmd = ("openstack {} endpoint list --interface admin -f json" - .format(insecure_option)) - ret, msg = exec_cmd(cmd, logger, verbose=False) - if ret != 0: - logger.error("Failed to get the endpoint info.") + if https_enabled and insecure and insecure.lower() == "true": + os_utils = OS_Utils(verify=False) + else: + os_utils = OS_Utils() + res, msg = os_utils.list_endpoints() + if not res: + logger.error("Failed to get admin endpoints. Exception message, {}" + .format(msg)) return None result_file = os.path.join(dt_cfg.dovetail_config['result_dir'], 'endpoint_info.json') @@ -315,7 +282,7 @@ def get_openstack_endpoint(logger=None): f.write(msg) logger.debug("Record all endpoint info into file {}." .format(result_file)) - return result_file + return msg except Exception: logger.exception("Failed to write endpoint info into file.") return None diff --git a/dovetail/utils/openstack_utils.py b/dovetail/utils/openstack_utils.py new file mode 100644 index 00000000..c4dfabf9 --- /dev/null +++ b/dovetail/utils/openstack_utils.py @@ -0,0 +1,20 @@ +import json +import os_client_config +import shade +from shade import exc + + +class OS_Utils(object): + + def __init__(self, **kwargs): + self.cloud = shade.OperatorCloud(os_client_config.get_config(**kwargs)) + self.images = [] + self.flavors = [] + + def list_endpoints(self): + try: + res = self.cloud.search_endpoints() + endpoints = json.dumps(res) + return True, endpoints + except exc.OpenStackCloudException as o_exc: + return False, o_exc.orig_message -- cgit 1.2.3-korg