repair .gitignore bug
[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 = list()
79     for line in output_lines:
80         if '=' in line:
81             env.append(tuple(line.split('=', 1)))
82
83     env = dict(env)
84 #    env = dict((line.split('=', 1) for line in output_lines))
85     os.environ.update(env)
86     return env
87
88
89 def _append_external_network(rc_file):
90     neutron_client = utils._get_neutron_client()
91     networks = neutron_client.list_networks()['networks']
92     try:
93         ext_network = next(n['name'] for n in networks if n['router:external'])
94     except StopIteration:
95         LOG.warning("Can't find external network")
96     else:
97         cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
98         try:
99             with open(rc_file, 'a') as f:
100                 f.write(cmd + '\n')
101         except OSError as e:
102             if e.errno != errno.EEXIST:
103                 raise
104
105
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)
110         return None
111     try:
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)
116         else:
117             LOG.info("Creating image '%s' from '%s'...", image_name, image_dir)
118
119             image = glance_client.images.create(
120                 name=image_name, visibility="public", disk_format="qcow2",
121                 container_format="bare")
122             image_id = image.id
123             with open(image_dir) as image_data:
124                 glance_client.images.upload(image_id, image_data)
125         return image_id
126     except Exception:  # pylint: disable=broad-except
127         LOG.error(
128             "Error [create_glance_image(glance_client, '%s', '%s')]",
129             image_name, image_dir)
130         return None