3 # Author: Jose Lausuch (jose.lausuch@ericsson.com)
5 # Installs the Functest framework within the Docker container
6 # and run the tests automatically
9 # All rights reserved. This program and the accompanying materials
10 # are made available under the terms of the Apache License, Version 2.0
11 # which accompanies this distribution, and is available at
12 # http://www.apache.org/licenses/LICENSE-2.0
22 import functest.utils.functest_logger as ft_logger
23 import functest.utils.functest_utils as ft_utils
24 import functest.utils.generate_defaults as gen_def
25 import functest.utils.openstack_utils as os_utils
29 actions = ['start', 'check']
30 parser = argparse.ArgumentParser()
31 parser.add_argument("action", help="Possible actions are: "
32 "'{d[0]}|{d[1]}' ".format(d=actions))
33 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
34 args = parser.parse_args()
37 """ logging configuration """
38 logger = ft_logger.Logger("prepare_env").getLogger()
41 """ global variables """
42 INSTALLERS = ['fuel', 'compass', 'apex', 'joid']
43 CI_INSTALLER_TYPE = ""
47 REPOS_DIR = os.getenv('repos_dir')
48 FUNCTEST_REPO = REPOS_DIR + '/functest/'
50 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
51 functest_yaml = yaml.safe_load(f)
53 FUNCTEST_CONF_DIR = functest_yaml.get("general").get(
54 "directories").get("dir_functest_conf")
56 FUNCTEST_DATA_DIR = functest_yaml.get("general").get(
57 "directories").get("dir_functest_data")
58 FUNCTEST_RESULTS_DIR = functest_yaml.get("general").get(
59 "directories").get("dir_results")
60 DEPLOYMENT_MAME = functest_yaml.get("rally").get("deployment_name")
61 TEMPEST_REPO_DIR = functest_yaml.get("general").get(
62 "directories").get("dir_repo_tempest")
64 ENV_FILE = FUNCTEST_CONF_DIR + "/env_active"
67 def print_separator():
68 logger.info("==============================================")
71 def check_env_variables():
73 logger.info("Checking environment variables...")
74 global CI_INSTALLER_TYPE
75 global CI_INSTALLER_IP
78 CI_INSTALLER_TYPE = os.getenv('INSTALLER_TYPE')
79 CI_INSTALLER_IP = os.getenv('INSTALLER_IP')
80 CI_SCENARIO = os.getenv('DEPLOY_SCENARIO')
81 CI_NODE = os.getenv('NODE_NAME')
82 CI_BUILD_TAG = os.getenv('BUILD_TAG')
83 CI_DEBUG = os.getenv('CI_DEBUG')
85 if CI_INSTALLER_TYPE is None:
86 logger.warning("The env variable 'INSTALLER_TYPE' is not defined.")
87 CI_INSTALLER_TYPE = "undefined"
89 if os.getenv('INSTALLER_TYPE') not in INSTALLERS:
90 logger.warning("INSTALLER_TYPE=%s is not a valid OPNFV installer. "
91 "Available OPNFV Installers are : %s."
92 "Setting INSTALLER_TYPE=undefined." % INSTALLERS)
93 CI_INSTALLER_TYPE = "undefined"
95 logger.info(" INSTALLER_TYPE=%s" % CI_INSTALLER_TYPE)
97 if CI_INSTALLER_IP is None:
98 logger.warning("The env variable 'INSTALLER_IP' is not defined. "
99 "It is needed to fetch the OpenStack credentials. "
100 "If the credentials are not provided to the "
101 "container as a volume, please add this env variable "
102 "to the 'docker run' command.")
104 logger.info(" INSTALLER_IP=%s" % CI_INSTALLER_IP)
106 if CI_SCENARIO is None:
107 logger.warning("The env variable 'DEPLOY_SCENARIO' is not defined. "
108 "Setting CE_SCENARIO=undefined.")
109 CI_SCENARIO = "undefined"
111 logger.info(" DEPLOY_SCENARIO=%s" % CI_SCENARIO)
113 logger.info(" CI_DEBUG=%s" % CI_DEBUG)
116 logger.info(" NODE_NAME=%s" % CI_NODE)
119 logger.info(" BUILD_TAG=%s" % CI_BUILD_TAG)
122 def create_directories():
124 logger.info("Creating needed directories...")
125 if not os.path.exists(FUNCTEST_CONF_DIR):
126 os.makedirs(FUNCTEST_CONF_DIR)
127 logger.info(" %s created." % FUNCTEST_CONF_DIR)
129 logger.debug(" %s already exists." % FUNCTEST_CONF_DIR)
131 if not os.path.exists(FUNCTEST_DATA_DIR):
132 os.makedirs(FUNCTEST_DATA_DIR)
133 logger.info(" %s created." % FUNCTEST_DATA_DIR)
135 logger.debug(" %s already exists." % FUNCTEST_DATA_DIR)
137 ODL_RESULTS_DIR = FUNCTEST_RESULTS_DIR + "/ODL/"
138 if not os.path.exists(ODL_RESULTS_DIR):
139 os.makedirs(ODL_RESULTS_DIR)
140 logger.info(" %s created." % ODL_RESULTS_DIR)
142 logger.debug(" %s already exists." % ODL_RESULTS_DIR)
145 def source_rc_file():
147 logger.info("Fetching RC file...")
148 rc_file = os.getenv('creds')
150 logger.warning("The environment variable 'creds' must be set and"
151 "pointing to the local RC file. Using default: "
152 "/home/opnfv/functest/conf/openstack.creds ...")
153 rc_file = "/home/opnfv/functest/conf/openstack.creds ..."
155 if not os.path.isfile(rc_file):
156 logger.info("RC file not provided. "
157 "Fetching it from the installer...")
158 if CI_INSTALLER_IP is None:
159 logger.error("The env variable CI_INSTALLER_IP must be provided in"
160 " order to fetch the credentials from the installer.")
161 sys.exit("Missing CI_INSTALLER_IP.")
162 if CI_INSTALLER_TYPE not in INSTALLERS:
163 logger.error("Cannot fetch credentials. INSTALLER_TYPE=%s is "
164 "not a valid OPNFV installer. Available "
165 "installers are : %s." % INSTALLERS)
166 sys.exit("Wrong INSTALLER_TYPE.")
168 cmd = ("/home/opnfv/repos/releng/utils/fetch_os_creds.sh "
170 % (rc_file, CI_INSTALLER_TYPE, CI_INSTALLER_IP))
171 logger.debug("Executing command: %s" % cmd)
172 p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
173 output = p.communicate()[0]
174 logger.debug("\n%s" % output)
175 if p.returncode != 0:
176 logger.error("Failed to fetch credentials from installer.")
179 logger.info("RC file provided in %s." % rc_file)
180 if os.path.getsize(rc_file) == 0:
181 logger.error("The file %s is empty." % rc_file)
184 logger.info("Sourcing the OpenStack RC file...")
185 creds = os_utils.source_credentials(rc_file)
187 for key, value in creds.iteritems():
188 if re.search("OS_", key):
189 str += "\n\t\t\t\t\t\t " + key + "=" + value
190 logger.debug("Used credentials: %s" % str)
193 def verify_deployment():
195 logger.info("Verifying OpenStack services...")
196 cmd = ("%s/ci/check_os.sh" % FUNCTEST_REPO)
198 logger.debug("Executing command: %s" % cmd)
199 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
201 while p.poll() is None:
202 line = p.stdout.readline().rstrip()
205 sys.exit("Problem while running 'check_os.sh'.")
211 logger.info("Creating Rally environment...")
213 cmd = "rally deployment destroy opnfv-rally"
214 ft_utils.execute_command(cmd, logger=logger, exit_on_error=False,
215 error_msg=("Deployment %s does not exist."
216 % DEPLOYMENT_MAME), verbose=False)
218 cmd = "rally deployment create --fromenv --name=" + DEPLOYMENT_MAME
219 ft_utils.execute_command(cmd, logger,
220 error_msg="Problem creating Rally deployment")
222 logger.info("Installing tempest from existing repo...")
223 cmd = ("rally verify install --source " + TEMPEST_REPO_DIR +
225 ft_utils.execute_command(cmd, logger,
226 error_msg="Problem installing Tempest.")
228 cmd = "rally deployment check"
229 ft_utils.execute_command(cmd, logger,
230 error_msg=("OpenStack not responding or "
231 "faulty Rally deployment."))
233 cmd = "rally show images"
234 ft_utils.execute_command(cmd, logger,
235 error_msg=("Problem while listing "
236 "OpenStack images."))
238 cmd = "rally show flavors"
239 ft_utils.execute_command(cmd, logger,
240 error_msg=("Problem while showing "
241 "OpenStack flavors."))
244 def generate_os_defaults():
246 logger.info("Generating OpenStack defaults...")
250 def check_environment():
251 msg_not_active = "The Functest environment is not installed."
252 if not os.path.isfile(ENV_FILE):
253 logger.error(msg_not_active)
256 with open(ENV_FILE, "r") as env_file:
258 if not re.search("1", s):
259 logger.error(msg_not_active)
262 logger.info("Functest environment installed.")
266 if not (args.action in actions):
267 logger.error('Argument not valid.')
270 if args.action == "start":
271 logger.info("\n######### Preparing Functest environment #########\n")
272 check_env_variables()
277 generate_os_defaults()
279 with open(ENV_FILE, "w") as env_file:
284 if args.action == "check":
289 if __name__ == '__main__':