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
17 from functest.utils.constants import CONST
18 import functest.utils.functest_logger as ft_logger
19 import functest.utils.openstack_utils as os_utils
21 parser = argparse.ArgumentParser()
23 parser.add_argument("-r", "--report",
24 help="Create json result file",
27 args = parser.parse_args()
29 """ logging configuration """
30 logger = ft_logger.Logger("create_instance_and_ip").getLogger()
32 HOME = CONST.dir_home + "/"
36 EXAMPLE_INSTANCE_NAME = CONST.example_vm_name
37 EXAMPLE_FLAVOR = CONST.example_flavor
38 EXAMPLE_IMAGE_NAME = CONST.example_image_name
39 IMAGE_FILENAME = CONST.openstack_image_file_name
40 IMAGE_FORMAT = CONST.openstack_image_disk_format
41 IMAGE_PATH = os.path.join(CONST.dir_functest_data, IMAGE_FILENAME)
43 # NEUTRON Private Network parameters
45 EXAMPLE_PRIVATE_NET_NAME = CONST.example_private_net_name
46 EXAMPLE_PRIVATE_SUBNET_NAME = CONST.example_private_subnet_name
47 EXAMPLE_PRIVATE_SUBNET_CIDR = CONST.example_private_subnet_cidr
48 EXAMPLE_ROUTER_NAME = CONST.example_router_name
50 EXAMPLE_SECGROUP_NAME = CONST.example_sg_name
51 EXAMPLE_SECGROUP_DESCR = CONST.example_sg_desc
56 nova_client = os_utils.get_nova_client()
57 neutron_client = os_utils.get_neutron_client()
58 glance_client = os_utils.get_glance_client()
60 image_id = os_utils.create_glance_image(glance_client,
67 network_dic = os_utils.create_network_full(
69 EXAMPLE_PRIVATE_NET_NAME,
70 EXAMPLE_PRIVATE_SUBNET_NAME,
72 EXAMPLE_PRIVATE_SUBNET_CIDR)
75 "There has been a problem when creating the neutron network")
78 network_id = network_dic["net_id"]
80 sg_id = os_utils.create_security_group_full(neutron_client,
81 EXAMPLE_SECGROUP_NAME,
82 EXAMPLE_SECGROUP_DESCR)
85 logger.info("Creating instance '%s'..." % EXAMPLE_INSTANCE_NAME)
87 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
89 % (EXAMPLE_INSTANCE_NAME, EXAMPLE_FLAVOR, image_id, network_id))
90 instance = os_utils.create_instance_and_wait_for_active(
94 EXAMPLE_INSTANCE_NAME)
97 logger.error("Error while booting instance.")
99 # Retrieve IP of INSTANCE
100 instance_ip = instance.networks.get(EXAMPLE_PRIVATE_NET_NAME)[0]
101 logger.debug("Instance '%s' got private ip '%s'." %
102 (EXAMPLE_INSTANCE_NAME, instance_ip))
104 logger.info("Adding '%s' to security group '%s'..."
105 % (EXAMPLE_INSTANCE_NAME, EXAMPLE_SECGROUP_NAME))
106 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
108 logger.info("Creating floating IP for VM '%s'..." % EXAMPLE_INSTANCE_NAME)
109 floatip_dic = os_utils.create_floating_ip(neutron_client)
110 floatip = floatip_dic['fip_addr']
111 # floatip_id = floatip_dic['fip_id']
114 logger.error("Cannot create floating IP.")
116 logger.info("Floating IP created: '%s'" % floatip)
118 logger.info("Associating floating ip: '%s' to VM '%s' "
119 % (floatip, EXAMPLE_INSTANCE_NAME))
120 if not os_utils.add_floating_ip(nova_client, instance.id, floatip):
121 logger.error("Cannot associate floating IP to VM.")
127 if __name__ == '__main__':