Add frame support of elk one docker support
[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         cmd = "cp %s %s" % (rc_file,
36                             config.bottlenecks_config["yardstick_rc_dir"])
37         yardstick_contain = docker_env.yardstick_info["container"]
38         docker_env.docker_exec_cmd(yardstick_contain,
39                                    cmd)
40
41     # update the external_network
42     _source_file(rc_file)
43
44
45 def file_copy(src_file, dest_file):
46     src = file(src_file, "r+")
47     des = file(dest_file, "w+")
48     des.writelines(src.read())
49     src.close()
50     des.close()
51
52
53 def _get_remote_rc_file(rc_file, installer_ip, installer_type):
54
55     RELENG_DIR = config.bottlenecks_config["releng_dir"]
56     OS_FETCH_SCRIPT = config.bottlenecks_config["fetch_os"]
57     os_fetch_script = os.path.join(RELENG_DIR, OS_FETCH_SCRIPT)
58
59     try:
60         cmd = [os_fetch_script, '-d', rc_file, '-i', installer_type,
61                '-a', installer_ip]
62         p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
63         p.communicate()[0]
64
65         if p.returncode != 0:
66             LOG.debug('Failed to fetch credentials from installer')
67     except OSError as e:
68         if e.errno != errno.EEXIST:
69             raise
70
71
72 def _source_file(rc_file):
73     p = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
74                          shell=True)
75     output = p.communicate()[0]
76     env = dict((line.split('=', 1) for line in output.splitlines()))
77     os.environ.update(env)
78     return env
79
80
81 def _append_external_network(rc_file):
82     neutron_client = utils._get_neutron_client()
83     networks = neutron_client.list_networks()['networks']
84     try:
85         ext_network = next(n['name'] for n in networks if n['router:external'])
86     except StopIteration:
87         LOG.warning("Can't find external network")
88     else:
89         cmd = 'export EXTERNAL_NETWORK=%s' % ext_network
90         try:
91             with open(rc_file, 'a') as f:
92                 f.write(cmd + '\n')
93         except OSError as e:
94             if e.errno != errno.EEXIST:
95                 raise