Bug fix: Bottlenecks sometimes deadlock
[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
18 LOG = Logger(__name__).getLogger()
19
20
21 def _prepare_env_daemon(test_yardstick):
22
23     rc_file = config.bottlenecks_config["rc_dir"]
24
25     if not os.path.exists(rc_file):
26         installer_ip = os.environ.get('INSTALLER_IP', 'undefined')
27         installer_type = os.environ.get('INSTALLER_TYPE', 'undefined')
28         _get_remote_rc_file(rc_file, installer_ip, installer_type)
29
30     _source_file(rc_file)
31
32     if not os.environ.get("EXTERNAL_NETWORK"):
33         _append_external_network(rc_file)
34     if test_yardstick:
35         yardstick_contain = docker_env.yardstick_info["container"]
36         cmd = "cp %s %s" % (rc_file,
37                             config.bottlenecks_config["yardstick_rc_dir"])
38         docker_env.docker_exec_cmd(yardstick_contain,
39                                    cmd)
40         file_orig = ("/home/opnfv/repos/yardstick/etc"
41                      "/yardstick/yardstick.conf.sample")
42         file_after = "/etc/yardstick/yardstick.conf"
43         cmd = "cp %s %s" % (file_orig,
44                             file_after)
45         docker_env.docker_exec_cmd(yardstick_contain,
46                                    cmd)
47         cmd = "sed -i '12s/http/file/g' /etc/yardstick/yardstick.conf"
48         docker_env.docker_exec_cmd(yardstick_contain,
49                                    cmd)
50
51     # update the external_network
52     _source_file(rc_file)
53
54
55 def file_copy(src_file, dest_file):
56     src = file(src_file, "r+")
57     des = file(dest_file, "w+")
58     des.writelines(src.read())
59     src.close()
60     des.close()
61
62
63 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
64
65     RELENG_DIR = config.bottlenecks_config["releng_dir"]
66     OS_FETCH_SCRIPT = config.bottlenecks_config["fetch_os"]
67     os_fetch_script = os.path.join(RELENG_DIR, OS_FETCH_SCRIPT)
68
69     try:
70         cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
71                '-a', installer_ip]
72         p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
73         p.communicate()[0]
74
75         if p.returncode != 0:
76             LOG.debug('Failed to fetch credentials from installer')
77     except OSError as e:
78         if e.errno != errno.EEXIST:
79             raise
80
81
82 def _source_file(rc_file):
83     p = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
84                          shell=True)
85     output = p.communicate()[0]
86     env = dict((line.split('=', 1) for line in output.splitlines()))
87     os.environ.update(env)
88     return env
89
90
91 def _append_external_network(rc_file):
92     neutron_client = utils._get_neutron_client()
93     networks = neutron_client.list_networks()['networks']
94     try:
95         ext_network = next(n['name'] for n in networks if n['router:external'])
96     except StopIteration:
97         LOG.warning("Can't find external network")
98     else:
99         cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
100         try:
101             with open(rc_file, 'a') as f:
102                 f.write(cmd + '\n')
103         except OSError as e:
104             if e.errno != errno.EEXIST:
105                 raise