3 # Copyright (c) 2015 Ericsson
4 # jose.lausuch@ericsson.com
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
11 import re, json, os, urllib2, argparse, logging, shutil, subprocess, yaml, sys, getpass
15 from pwd import getpwuid
16 from neutronclient.v2_0 import client as neutronclient
18 actions = ['start', 'check', 'clean']
19 parser = argparse.ArgumentParser()
20 parser.add_argument("action", help="Possible actions are: '{d[0]}|{d[1]}|{d[2]}' ".format(d=actions))
21 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
22 parser.add_argument("-f", "--force", help="Force", action="store_true")
23 args = parser.parse_args()
26 """ logging configuration """
27 logger = logging.getLogger('config_functest')
28 logger.setLevel(logging.DEBUG)
30 ch = logging.StreamHandler()
32 ch.setLevel(logging.DEBUG)
34 ch.setLevel(logging.INFO)
36 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
37 ch.setFormatter(formatter)
40 REPO_PATH=os.environ['repos_dir']+'/functest/'
41 if not os.path.exists(REPO_PATH):
42 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
44 sys.path.append(REPO_PATH + "testcases/")
46 with open(REPO_PATH+"testcases/config_functest.yaml") as f:
47 functest_yaml = yaml.safe_load(f)
51 """ global variables """
53 RALLY_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_rally")
54 RALLY_REPO_DIR = functest_yaml.get("general").get("directories").get("dir_repo_rally")
55 RALLY_INSTALLATION_DIR = functest_yaml.get("general").get("directories").get("dir_rally_inst")
56 RALLY_RESULT_DIR = functest_yaml.get("general").get("directories").get("dir_rally_res")
57 VPING_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_vping")
58 ODL_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_odl")
59 DATA_DIR = functest_yaml.get("general").get("directories").get("dir_functest_data")
61 # Tempest/Rally configuration details
62 DEPLOYMENT_MAME = functest_yaml.get("rally").get("deployment_name")
63 RALLY_COMMIT = functest_yaml.get("general").get("repositories").get("rally_commit")
66 IMAGE_FILE_NAME = functest_yaml.get("general").get("openstack").get("image_file_name")
67 IMAGE_PATH = DATA_DIR + "/" + IMAGE_FILE_NAME
69 # NEUTRON Private Network parameters
70 NEUTRON_PRIVATE_NET_NAME = functest_yaml.get("general"). \
71 get("openstack").get("neutron_private_net_name")
72 NEUTRON_PRIVATE_SUBNET_NAME = functest_yaml.get("general"). \
73 get("openstack").get("neutron_private_subnet_name")
74 NEUTRON_PRIVATE_SUBNET_CIDR = functest_yaml.get("general"). \
75 get("openstack").get("neutron_private_subnet_cidr")
76 NEUTRON_ROUTER_NAME = functest_yaml.get("general"). \
77 get("openstack").get("neutron_router_name")
79 creds_neutron = functest_utils.get_credentials("neutron")
80 neutron_client = neutronclient.Client(**creds_neutron)
84 Start the functest environment installation
86 if not functest_utils.check_internet_connectivity():
87 logger.error("There is no Internet connectivity. Please check the network configuration.")
91 logger.info("Functest environment already installed. Nothing to do.")
95 # Clean in case there are left overs
96 logger.debug("Cleaning possible functest environment leftovers.")
98 logger.info("Starting installation of functest environment")
100 private_net = functest_utils.get_private_net(neutron_client)
101 if private_net is None:
102 # If there is no private network in the deployment we create one
103 if not create_private_neutron_net(neutron_client):
104 logger.error("There has been a problem while creating the functest network.")
108 logger.info("Private network '%s' already existing in the deployment."
109 % private_net['name'])
111 logger.info("Installing Rally...")
112 if not install_rally():
113 logger.error("There has been a problem while installing Rally.")
117 # Create result folder under functest if necessary
118 if not os.path.exists(RALLY_RESULT_DIR):
119 os.makedirs(RALLY_RESULT_DIR)
126 Check if the functest environment is properly installed
130 logger.info("Checking current functest configuration...")
132 logger.debug("Checking script directories...")
134 dirs = [RALLY_DIR, RALLY_INSTALLATION_DIR, VPING_DIR, ODL_DIR]
136 if not os.path.exists(dir):
137 logger.debug("The directory '%s' does NOT exist." % dir)
141 logger.debug(" %s found" % dir)
143 logger.debug("...OK")
145 logger.debug("...FAIL")
148 logger.debug("Checking Rally deployment...")
149 if not check_rally():
150 logger.debug(" Rally deployment NOT installed.")
152 logger.debug("...FAIL")
154 logger.debug("...OK")
156 logger.debug("Checking Image...")
158 if not os.path.isfile(IMAGE_PATH):
159 logger.debug(" Image file '%s' NOT found." % IMAGE_PATH)
163 logger.debug(" Image file found in %s" % IMAGE_PATH)
167 logger.debug("...OK")
169 logger.debug("...FAIL")
171 #TODO: check OLD environment setup
172 return not errors_all
178 Clean the existing functest environment
180 logger.info("Removing current functest environment...")
181 if os.path.exists(RALLY_INSTALLATION_DIR):
182 logger.debug("Removing Rally installation directory %s" % RALLY_INSTALLATION_DIR)
183 shutil.rmtree(RALLY_INSTALLATION_DIR,ignore_errors=True)
185 if os.path.exists(RALLY_RESULT_DIR):
186 logger.debug("Removing Result directory")
187 shutil.rmtree(RALLY_RESULT_DIR,ignore_errors=True)
189 logger.debug("Cleaning up the OpenStack deployment...")
190 cmd='python ' + REPO_PATH + \
191 '/testcases/VIM/OpenStack/CI/libraries/clean_openstack.py -d '
192 functest_utils.execute_command(cmd,logger)
193 logger.info("Functest environment clean!")
199 logger.info("Rally is already installed.")
201 logger.debug("Executing %s/install_rally.sh..." %RALLY_REPO_DIR)
202 install_script = RALLY_REPO_DIR + "/install_rally.sh --yes"
203 cmd = 'sudo ' + install_script
204 functest_utils.execute_command(cmd,logger)
206 logger.debug("Creating Rally environment...")
207 cmd = "rally deployment create --fromenv --name="+DEPLOYMENT_MAME
208 functest_utils.execute_command(cmd,logger)
210 logger.debug("Installing tempest...")
211 cmd = "rally verify install"
212 functest_utils.execute_command(cmd,logger)
214 cmd = "rally deployment check"
215 functest_utils.execute_command(cmd,logger)
216 #TODO: check that everything is 'Available' and warn if not
218 cmd = "rally show images"
219 functest_utils.execute_command(cmd,logger)
221 cmd = "rally show flavors"
222 functest_utils.execute_command(cmd,logger)
229 Check if Rally is installed and properly configured
231 if os.path.exists(RALLY_INSTALLATION_DIR):
232 logger.debug(" Rally installation directory found in %s" % RALLY_INSTALLATION_DIR)
233 FNULL = open(os.devnull, 'w');
234 cmd="rally deployment list | grep "+DEPLOYMENT_MAME
235 logger.debug(' Executing command : {}'.format(cmd))
236 p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=FNULL);
237 #if the command does not exist or there is no deployment
238 line = p.stdout.readline()
240 logger.debug(" Rally deployment NOT found")
242 logger.debug(" Rally deployment found")
248 def create_private_neutron_net(neutron):
249 neutron.format = 'json'
250 logger.info('Creating neutron network %s...' % NEUTRON_PRIVATE_NET_NAME)
251 network_id = functest_utils. \
252 create_neutron_net(neutron, NEUTRON_PRIVATE_NET_NAME)
256 logger.debug("Network '%s' created successfully" % network_id)
257 logger.debug('Creating Subnet....')
258 subnet_id = functest_utils. \
259 create_neutron_subnet(neutron,
260 NEUTRON_PRIVATE_SUBNET_NAME,
261 NEUTRON_PRIVATE_SUBNET_CIDR,
265 logger.debug("Subnet '%s' created successfully" % subnet_id)
266 logger.debug('Creating Router...')
267 router_id = functest_utils. \
268 create_neutron_router(neutron, NEUTRON_ROUTER_NAME)
273 logger.debug("Router '%s' created successfully" % router_id)
274 logger.debug('Adding router to subnet...')
276 result = functest_utils.add_interface_router(neutron, router_id, subnet_id)
281 logger.debug("Interface added successfully.")
282 network_dic = {'net_id': network_id,
283 'subnet_id': subnet_id,
284 'router_id': router_id}
289 if not (args.action in actions):
290 logger.error('argument not valid')
294 if not functest_utils.check_credentials():
295 logger.error("Please source the openrc credentials and run the script again.")
296 #TODO: source the credentials in this script
300 if args.action == "start":
303 if args.action == "check":
305 logger.info("Functest environment correctly installed")
307 logger.info("Functest environment not found or faulty")
309 if args.action == "clean":
314 print("Are you sure? [y|n]")
315 answer = raw_input("")
322 print("Invalid option.")
326 if __name__ == '__main__':