c1ac395ad1889ba001268f5910ce9b2220d12def
[bottlenecks.git] / utils / env_prepare / stack_prepare.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
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 ##############################################################################
10 import os
11 import subprocess
12 import errno
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
18
19 LOG = Logger(__name__).getLogger()
20
21
22 def _prepare_env_daemon(test_yardstick):
23
24     rc_file = config.bottlenecks_config["rc_dir"]
25
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)
30
31     _source_file(rc_file)
32
33     if not os.environ.get("EXTERNAL_NETWORK"):
34         _append_external_network(rc_file)
35     if test_yardstick:
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,
40                                    cmd)
41
42     # update the external_network
43     _source_file(rc_file)
44
45
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())
50     src.close()
51     des.close()
52
53
54 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
55
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)
59
60     try:
61         cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
62                '-a', installer_ip]
63         p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
64         p.communicate()[0]
65
66         if p.returncode != 0:
67             LOG.debug('Failed to fetch credentials from installer')
68     except OSError as e:
69         if e.errno != errno.EEXIST:
70             raise
71
72
73 def _source_file(rc_file):
74     p = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
75                          shell=True)
76     output = p.communicate()[0]
77     output_lines = output.splitlines()
78     env = dict((line.split('=', 1) for line in output_lines))
79     os.environ.update(env)
80     return env
81
82
83 def _append_external_network(rc_file):
84     neutron_client = utils._get_neutron_client()
85     networks = neutron_client.list_networks()['networks']
86     try:
87         ext_network = next(n['name'] for n in networks if n['router:external'])
88     except StopIteration:
89         LOG.warning("Can't find external network")
90     else:
91         cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
92         try:
93             with open(rc_file, 'a') as f:
94                 f.write(cmd + '\n')
95         except OSError as e:
96             if e.errno != errno.EEXIST:
97                 raise
98
99
100 def prepare_image(image_name, image_dir):
101     glance_client = client_manager._get_glance_client()
102     if not os.path.isfile(image_dir):
103         LOG.error("Error: file %s does not exist.", image_dir)
104         return None
105     try:
106         images = glance_client.images.list()
107         image_id = next((i.id for i in images if i.name == image_name), None)
108         if image_id is not None:
109             LOG.info("Image %s already exists.", image_name)
110         else:
111             LOG.info("Creating image '%s' from '%s'...", image_name, image_dir)
112
113             image = glance_client.images.create(
114                 name=image_name, visibility="public", disk_format="qcow2",
115                 container_format="bare")
116             image_id = image.id
117             with open(image_dir) as image_data:
118                 glance_client.images.upload(image_id, image_data)
119         return image_id
120     except Exception:  # pylint: disable=broad-except
121         LOG.error(
122             "Error [create_glance_image(glance_client, '%s', '%s')]",
123             image_name, image_dir)
124         return None