Merge "Unified way to provide configurations and env variables(proposal 1)"
[functest.git] / functest / opnfv_tests / openstack / tempest / conf_utils.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 import ConfigParser
11 import os
12 import re
13 import shutil
14
15 import functest.utils.functest_constants as ft_constants
16 import functest.utils.functest_utils as ft_utils
17 import opnfv.utils.constants as releng_constants
18
19
20 IMAGE_ID_ALT = None
21 FLAVOR_ID_ALT = None
22 REPO_PATH = ft_constants.FUNCTEST_REPO_DIR
23 GLANCE_IMAGE_PATH = os.path.join(ft_constants.FUNCTEST_DATA_DIR,
24                                  ft_constants.GLANCE_IMAGE_FILENAME)
25 TEMPEST_TEST_LIST_DIR = ft_constants.TEMPEST_TEST_LIST_DIR
26 TEMPEST_RESULTS_DIR = os.path.join(ft_constants.FUNCTEST_RESULTS_DIR,
27                                    'tempest')
28 TEMPEST_CUSTOM = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
29                               'test_list.txt')
30 TEMPEST_BLACKLIST = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
31                                  'blacklist.txt')
32 TEMPEST_DEFCORE = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
33                                'defcore_req.txt')
34 TEMPEST_RAW_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_raw_list.txt')
35 TEMPEST_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_list.txt')
36
37 CI_INSTALLER_TYPE = ft_constants.CI_INSTALLER_TYPE
38 CI_INSTALLER_IP = ft_constants.CI_INSTALLER_IP
39
40
41 def configure_tempest(logger, deployment_dir, IMAGE_ID=None, FLAVOR_ID=None):
42     """
43     Add/update needed parameters into tempest.conf file generated by Rally
44     """
45     tempest_conf_file = deployment_dir + "/tempest.conf"
46     if os.path.isfile(tempest_conf_file):
47         logger.debug("Deleting old tempest.conf file...")
48         os.remove(tempest_conf_file)
49
50     logger.debug("Generating new tempest.conf file...")
51     cmd = "rally verify genconfig"
52     ft_utils.execute_command(cmd)
53
54     logger.debug("Finding tempest.conf file...")
55     if not os.path.isfile(tempest_conf_file):
56         logger.error("Tempest configuration file %s NOT found."
57                      % tempest_conf_file)
58         return releng_constants.EXIT_RUN_ERROR
59
60     logger.debug("Updating selected tempest.conf parameters...")
61     config = ConfigParser.RawConfigParser()
62     config.read(tempest_conf_file)
63     config.set(
64         'compute',
65         'fixed_network_name',
66         ft_constants.TEMPEST_PRIVATE_NET_NAME)
67     if ft_constants.TEMPEST_USE_CUSTOM_IMAGES:
68         if IMAGE_ID is not None:
69             config.set('compute', 'image_ref', IMAGE_ID)
70         if IMAGE_ID_ALT is not None:
71             config.set('compute', 'image_ref_alt', IMAGE_ID_ALT)
72     if ft_constants.TEMPEST_USE_CUSTOM_FLAVORS:
73         if FLAVOR_ID is not None:
74             config.set('compute', 'flavor_ref', FLAVOR_ID)
75         if FLAVOR_ID_ALT is not None:
76             config.set('compute', 'flavor_ref_alt', FLAVOR_ID_ALT)
77     config.set('identity', 'tenant_name', ft_constants.TEMPEST_TENANT_NAME)
78     config.set('identity', 'username', ft_constants.TEMPEST_USER_NAME)
79     config.set('identity', 'password', ft_constants.TEMPEST_USER_PASSWORD)
80     config.set(
81         'validation', 'ssh_timeout', ft_constants.TEMPEST_SSH_TIMEOUT)
82
83     if ft_constants.OS_ENDPOINT_TYPE is not None:
84         services_list = ['compute',
85                          'volume',
86                          'image',
87                          'network',
88                          'data-processing',
89                          'object-storage',
90                          'orchestration']
91         sections = config.sections()
92         for service in services_list:
93             if service not in sections:
94                 config.add_section(service)
95             config.set(service, 'endpoint_type',
96                        ft_constants.OS_ENDPOINT_TYPE)
97
98     with open(tempest_conf_file, 'wb') as config_file:
99         config.write(config_file)
100
101     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
102     shutil.copyfile(
103         tempest_conf_file, TEMPEST_RESULTS_DIR + '/tempest.conf')
104
105     return releng_constants.EXIT_OK
106
107
108 def configure_tempest_multisite(logger, deployment_dir):
109     """
110     Add/update needed parameters into tempest.conf file generated by Rally
111     """
112     logger.debug("configure the tempest")
113     configure_tempest(logger, deployment_dir)
114
115     logger.debug("Finding tempest.conf file...")
116     tempest_conf_old = os.path.join(deployment_dir, '/tempest.conf')
117     if not os.path.isfile(tempest_conf_old):
118         logger.error("Tempest configuration file %s NOT found."
119                      % tempest_conf_old)
120         return releng_constants.EXIT_RUN_ERROR
121
122     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
123     cur_path = os.path.split(os.path.realpath(__file__))[0]
124     tempest_conf_file = os.path.join(cur_path, '/tempest_multisite.conf')
125     shutil.copyfile(tempest_conf_old, tempest_conf_file)
126
127     logger.debug("Updating selected tempest.conf parameters...")
128     config = ConfigParser.RawConfigParser()
129     config.read(tempest_conf_file)
130
131     config.set('service_available', 'kingbird', 'true')
132     cmd = ("openstack endpoint show kingbird | grep publicurl |"
133            "awk '{print $4}' | awk -F '/' '{print $4}'")
134     kingbird_api_version = os.popen(cmd).read()
135     if CI_INSTALLER_TYPE == 'fuel':
136         # For MOS based setup, the service is accessible
137         # via bind host
138         kingbird_conf_path = "/etc/kingbird/kingbird.conf"
139         installer_type = CI_INSTALLER_TYPE
140         installer_ip = CI_INSTALLER_IP
141         installer_username = ft_utils.get_functest_config(
142             "multisite." + installer_type +
143             "_environment.installer_username")
144         installer_password = ft_utils.get_functest_config(
145             "multisite." + installer_type +
146             "_environment.installer_password")
147
148         ssh_options = ("-o UserKnownHostsFile=/dev/null -o "
149                        "StrictHostKeyChecking=no")
150
151         # Get the controller IP from the fuel node
152         cmd = 'sshpass -p %s ssh 2>/dev/null %s %s@%s \
153                 \'fuel node --env 1| grep controller | grep "True\|  1" \
154                 | awk -F\| "{print \$5}"\'' % (installer_password,
155                                                ssh_options,
156                                                installer_username,
157                                                installer_ip)
158         multisite_controller_ip = "".join(os.popen(cmd).read().split())
159
160         # Login to controller and get bind host details
161         cmd = 'sshpass -p %s ssh 2>/dev/null  %s %s@%s "ssh %s \\" \
162             grep -e "^bind_" %s  \\""' % (installer_password,
163                                           ssh_options,
164                                           installer_username,
165                                           installer_ip,
166                                           multisite_controller_ip,
167                                           kingbird_conf_path)
168         bind_details = os.popen(cmd).read()
169         bind_details = "".join(bind_details.split())
170         # Extract port number from the bind details
171         bind_port = re.findall(r"\D(\d{4})", bind_details)[0]
172         # Extract ip address from the bind details
173         bind_host = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",
174                                bind_details)[0]
175         kingbird_endpoint_url = "http://%s:%s/" % (bind_host, bind_port)
176     else:
177         cmd = "openstack endpoint show kingbird | grep publicurl |\
178                awk '{print $4}' | awk -F '/' '{print $3}'"
179         kingbird_endpoint_url = os.popen(cmd).read()
180
181     try:
182         config.add_section("kingbird")
183     except Exception:
184         logger.info('kingbird section exist')
185     config.set('kingbird', 'endpoint_type', 'publicURL')
186     config.set('kingbird', 'TIME_TO_SYNC', '20')
187     config.set('kingbird', 'endpoint_url', kingbird_endpoint_url)
188     config.set('kingbird', 'api_version', kingbird_api_version)
189     with open(tempest_conf_file, 'wb') as config_file:
190         config.write(config_file)
191
192     return releng_constants.EXIT_OK