bugfix: push_result_to_db exception show
[functest.git] / testcases / OpenStack / examples / create_instance_and_ip.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 # This script boots an instance and assigns a floating ip
11 #
12
13 import argparse
14 import os
15 import sys
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
19
20 parser = argparse.ArgumentParser()
21
22 parser.add_argument("-r", "--report",
23                     help="Create json result file",
24                     action="store_true")
25
26 args = parser.parse_args()
27
28 """ logging configuration """
29 logger = ft_logger.Logger("create_instance_and_ip").getLogger()
30
31 REPO_PATH = os.environ['repos_dir'] + '/functest/'
32 HOME = os.environ['HOME'] + "/"
33
34 VM_BOOT_TIMEOUT = 180
35
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
45
46 # NEUTRON Private Network parameters
47
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")
56
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")
61
62 TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
63
64
65 def main():
66
67     nova_client = os_utils.get_nova_client()
68     neutron_client = os_utils.get_neutron_client()
69     glance_client = os_utils.get_glance_client()
70
71     image_id = os_utils.create_glance_image(glance_client,
72                                             IMAGE_NAME,
73                                             IMAGE_PATH,
74                                             disk=IMAGE_FORMAT,
75                                             container="bare",
76                                             public=True)
77
78     network_dic = os_utils.create_network_full(neutron_client,
79                                                NET_NAME,
80                                                SUBNET_NAME,
81                                                ROUTER_NAME,
82                                                SUBNET_CIDR)
83     if not network_dic:
84         logger.error(
85             "There has been a problem when creating the neutron network")
86         sys.exit(-1)
87
88     network_id = network_dic["net_id"]
89
90     sg_id = os_utils.create_security_group_full(neutron_client,
91                                                 SECGROUP_NAME, SECGROUP_DESCR)
92
93     # boot INTANCE
94     logger.info("Creating instance '%s'..." % INSTANCE_NAME)
95     logger.debug(
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,
99                                                             image_id,
100                                                             network_id,
101                                                             INSTANCE_NAME)
102
103     if instance is None:
104         logger.error("Error while booting instance.")
105         sys.exit(-1)
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))
110
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)
114
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']
119
120     if floatip is None:
121         logger.error("Cannot create floating IP.")
122         sys.exit(-1)
123     logger.info("Floating IP created: '%s'" % floatip)
124
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.")
129         sys.exit(-1)
130
131     sys.exit(0)
132
133 if __name__ == '__main__':
134     main()