2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
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
9 ##############################################################################
13 from utils.logger import Logger
14 from utils.parser import Parser as config
15 import utils.infra_setup.heat.manager as utils
16 import utils.infra_setup.runner.docker_env as docker_env
17 import utils.infra_setup.heat.manager as client_manager
19 LOG = Logger(__name__).getLogger()
22 def _prepare_env_daemon(test_yardstick):
24 rc_file = config.bottlenecks_config["rc_dir"]
26 if not os.path.exists(rc_file):
27 installer_ip = os.environ.get('INSTALLER_IP', 'undefined')
28 installer_type = os.environ.get('INSTALLER_TYPE', 'undefined')
29 _get_remote_rc_file(rc_file, installer_ip, installer_type)
33 if not os.environ.get("EXTERNAL_NETWORK"):
34 _append_external_network(rc_file)
36 yardstick_contain = docker_env.yardstick_info["container"]
37 cmd = "cp %s %s" % (rc_file,
38 config.bottlenecks_config["yardstick_rc_dir"])
39 docker_env.docker_exec_cmd(yardstick_contain,
42 # update the external_network
46 def file_copy(src_file, dest_file):
47 src = file(src_file, "r+")
48 des = file(dest_file, "w+")
49 des.writelines(src.read())
54 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
56 RELENG_DIR = config.bottlenecks_config["releng_dir"]
57 OS_FETCH_SCRIPT = config.bottlenecks_config["fetch_os"]
58 os_fetch_script = os.path.join(RELENG_DIR, OS_FETCH_SCRIPT)
61 cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
63 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
67 LOG.debug('Failed to fetch credentials from installer')
69 if e.errno != errno.EEXIST:
73 def _source_file(rc_file):
74 p = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
76 output = p.communicate()[0]
77 output_lines = output.splitlines()
79 for line in output_lines:
81 env.append(tuple(line.split('=', 1)))
84 # env = dict((line.split('=', 1) for line in output_lines))
85 os.environ.update(env)
89 def _append_external_network(rc_file):
90 neutron_client = utils._get_neutron_client()
91 networks = neutron_client.list_networks()['networks']
93 ext_network = next(n['name'] for n in networks if n['router:external'])
95 LOG.warning("Can't find external network")
97 cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
99 with open(rc_file, 'a') as f:
102 if e.errno != errno.EEXIST:
106 def prepare_image(image_name, image_dir):
107 glance_client = client_manager._get_glance_client()
108 if not os.path.isfile(image_dir):
109 LOG.error("Error: file %s does not exist.", image_dir)
112 images = glance_client.images.list()
113 image_id = next((i.id for i in images if i.name == image_name), None)
114 if image_id is not None:
115 LOG.info("Image %s already exists.", image_name)
117 LOG.info("Creating image '%s' from '%s'...", image_name, image_dir)
119 image = glance_client.images.create(
120 name=image_name, visibility="public", disk_format="qcow2",
121 container_format="bare")
123 with open(image_dir) as image_data:
124 glance_client.images.upload(image_id, image_data)
126 except Exception: # pylint: disable=broad-except
128 "Error [create_glance_image(glance_client, '%s', '%s')]",
129 image_name, image_dir)