[multisite] refactor the scripts of multiste
[functest.git] / testcases / OpenStack / tempest / gen_tempest_conf.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Execute Multisite Tempest test cases
11 ##
12
13 import ConfigParser
14 import os
15 import re
16 import shutil
17 import functest.utils.functest_utils as ft_utils
18 import functest.utils.functest_logger as ft_logger
19 from run_tempest import configure_tempest
20
21 logger = ft_logger.Logger("multisite").getLogger()
22
23
24 def configure_tempest_multisite(deployment_dir):
25     """
26     Add/update needed parameters into tempest.conf file generated by Rally
27     """
28     logger.debug("configure the tempest")
29     configure_tempest(deployment_dir)
30
31     logger.debug("Finding tempest.conf file...")
32     tempest_conf_file = deployment_dir + "/tempest.conf"
33     if not os.path.isfile(tempest_conf_file):
34         logger.error("Tempest configuration file %s NOT found."
35                      % tempest_conf_file)
36         exit(-1)
37
38     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
39     cur_path = os.path.split(os.path.realpath(__file__))[0]
40     shutil.copyfile(tempest_conf_file, cur_path + '/tempest_multisite.conf')
41     tempest_conf_file = cur_path + "/tempest_multisite.conf"
42
43     logger.debug("Updating selected tempest.conf parameters...")
44     config = ConfigParser.RawConfigParser()
45     config.read(tempest_conf_file)
46
47     config.set('service_available', 'kingbird', 'true')
48     cmd = "openstack endpoint show kingbird | grep publicurl |\
49            awk '{print $4}' | awk -F '/' '{print $4}'"
50     kingbird_api_version = os.popen(cmd).read()
51     if os.environ.get("INSTALLER_TYPE") == 'fuel':
52         # For MOS based setup, the service is accessible
53         # via bind host
54         kingbird_conf_path = "/etc/kingbird/kingbird.conf"
55         installer_type = os.getenv('INSTALLER_TYPE', 'Unknown')
56         installer_ip = os.getenv('INSTALLER_IP', 'Unknown')
57         installer_username = ft_utils.get_parameter_from_yaml(
58             "multisite." + installer_type +
59             "_environment.installer_username")
60         installer_password = ft_utils.get_parameter_from_yaml(
61             "multisite." + installer_type +
62             "_environment.installer_password")
63
64         ssh_options = "-o UserKnownHostsFile=/dev/null -o \
65             StrictHostKeyChecking=no"
66
67         # Get the controller IP from the fuel node
68         cmd = 'sshpass -p %s ssh 2>/dev/null %s %s@%s \
69                 \'fuel node --env 1| grep controller | grep "True\|  1" \
70                 | awk -F\| "{print \$5}"\'' % (installer_password,
71                                                ssh_options,
72                                                installer_username,
73                                                installer_ip)
74         multisite_controller_ip = \
75             "".join(os.popen(cmd).read().split())
76
77         # Login to controller and get bind host details
78         cmd = 'sshpass -p %s ssh 2>/dev/null  %s %s@%s "ssh %s \\" \
79             grep -e "^bind_" %s  \\""' % (installer_password,
80                                           ssh_options,
81                                           installer_username,
82                                           installer_ip,
83                                           multisite_controller_ip,
84                                           kingbird_conf_path)
85         bind_details = os.popen(cmd).read()
86         bind_details = "".join(bind_details.split())
87         # Extract port number from the bind details
88         bind_port = re.findall(r"\D(\d{4})", bind_details)[0]
89         # Extract ip address from the bind details
90         bind_host = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",
91                                bind_details)[0]
92         kingbird_endpoint_url = "http://" + bind_host + ":" + bind_port + \
93                                 "/"
94     else:
95         cmd = "openstack endpoint show kingbird | grep publicurl |\
96                awk '{print $4}' | awk -F '/' '{print $3}'"
97         kingbird_endpoint_url = os.popen(cmd).read()
98
99     try:
100         config.add_section("kingbird")
101     except Exception:
102         logger.info('kingbird section exist')
103     config.set('kingbird', 'endpoint_type', 'publicURL')
104     config.set('kingbird', 'TIME_TO_SYNC', '20')
105     config.set('kingbird', 'endpoint_url', kingbird_endpoint_url)
106     config.set('kingbird', 'api_version', kingbird_api_version)
107     with open(tempest_conf_file, 'wb') as config_file:
108         config.write(config_file)
109
110     return True
111
112
113 def main():
114
115     deployment_dir = ft_utils.get_deployment_dir(logger)
116     configure_tempest_multisite(deployment_dir)
117
118
119 if __name__ == '__main__':
120     main()