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
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # This script boots an instance and assigns a floating ip
16 import functest.utils.functest_logger as ft_logger
17 import functest.utils.functest_utils as ft_utils
18 import functest.utils.openstack_utils as os_utils
20 parser = argparse.ArgumentParser()
22 parser.add_argument("-r", "--report",
23 help="Create json result file",
26 args = parser.parse_args()
28 """ logging configuration """
29 logger = ft_logger.Logger("create_instance_and_ip").getLogger()
31 REPO_PATH = os.environ['repos_dir'] + '/functest/'
32 HOME = os.environ['HOME'] + "/"
36 INSTANCE_NAME = ft_utils.get_parameter_from_yaml("example.example_vm_name")
37 FLAVOR = ft_utils.get_parameter_from_yaml("example.example_flavor")
38 IMAGE_NAME = ft_utils.get_parameter_from_yaml("example.example_image_name")
39 IMAGE_FILENAME = ft_utils.get_parameter_from_yaml(
40 "general.openstack.image_file_name")
41 IMAGE_FORMAT = ft_utils.get_parameter_from_yaml(
42 "general.openstack.image_disk_format")
43 IMAGE_PATH = ft_utils.get_parameter_from_yaml(
44 "general.directories.dir_functest_data") + "/" + IMAGE_FILENAME
46 # NEUTRON Private Network parameters
48 NET_NAME = ft_utils.get_parameter_from_yaml(
49 "example.example_private_net_name")
50 SUBNET_NAME = ft_utils.get_parameter_from_yaml(
51 "example.example_private_subnet_name")
52 SUBNET_CIDR = ft_utils.get_parameter_from_yaml(
53 "example.example_private_subnet_cidr")
54 ROUTER_NAME = ft_utils.get_parameter_from_yaml(
55 "example.example_router_name")
57 SECGROUP_NAME = ft_utils.get_parameter_from_yaml(
58 "example.example_sg_name")
59 SECGROUP_DESCR = ft_utils.get_parameter_from_yaml(
60 "example.example_sg_descr")
62 TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
67 nova_client = os_utils.get_nova_client()
68 neutron_client = os_utils.get_neutron_client()
69 glance_client = os_utils.get_glance_client()
71 image_id = os_utils.create_glance_image(glance_client,
78 network_dic = os_utils.create_network_full(neutron_client,
85 "There has been a problem when creating the neutron network")
88 network_id = network_dic["net_id"]
90 sg_id = os_utils.create_security_group_full(neutron_client,
91 SECGROUP_NAME, SECGROUP_DESCR)
94 logger.info("Creating instance '%s'..." % INSTANCE_NAME)
96 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
97 "network=%s \n" % (INSTANCE_NAME, FLAVOR, image_id, network_id))
98 instance = os_utils.create_instance_and_wait_for_active(FLAVOR,
104 logger.error("Error while booting instance.")
106 # Retrieve IP of INSTANCE
107 instance_ip = instance.networks.get(NET_NAME)[0]
108 logger.debug("Instance '%s' got private ip '%s'." %
109 (INSTANCE_NAME, instance_ip))
111 logger.info("Adding '%s' to security group '%s'..."
112 % (INSTANCE_NAME, SECGROUP_NAME))
113 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
115 logger.info("Creating floating IP for VM '%s'..." % INSTANCE_NAME)
116 floatip_dic = os_utils.create_floating_ip(neutron_client)
117 floatip = floatip_dic['fip_addr']
118 # floatip_id = floatip_dic['fip_id']
121 logger.error("Cannot create floating IP.")
123 logger.info("Floating IP created: '%s'" % floatip)
125 logger.info("Associating floating ip: '%s' to VM '%s' "
126 % (floatip, INSTANCE_NAME))
127 if not os_utils.add_floating_ip(nova_client, instance.id, floatip):
128 logger.error("Cannot associate floating IP to VM.")
133 if __name__ == '__main__':