Merge "Reverted the file permission"
[functest-xtesting.git] / functest / opnfv_tests / 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
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
20
21 parser = argparse.ArgumentParser()
22
23 parser.add_argument("-r", "--report",
24                     help="Create json result file",
25                     action="store_true")
26
27 args = parser.parse_args()
28
29 """ logging configuration """
30 logger = ft_logger.Logger("create_instance_and_ip").getLogger()
31
32 HOME = CONST.dir_home + "/"
33
34 VM_BOOT_TIMEOUT = 180
35
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)
42
43 # NEUTRON Private Network parameters
44
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
49
50 EXAMPLE_SECGROUP_NAME = CONST.example_sg_name
51 EXAMPLE_SECGROUP_DESCR = CONST.example_sg_desc
52
53
54 def main():
55
56     nova_client = os_utils.get_nova_client()
57     neutron_client = os_utils.get_neutron_client()
58     glance_client = os_utils.get_glance_client()
59
60     image_id = os_utils.create_glance_image(glance_client,
61                                             EXAMPLE_IMAGE_NAME,
62                                             IMAGE_PATH,
63                                             disk=IMAGE_FORMAT,
64                                             container="bare",
65                                             public=True)
66
67     network_dic = os_utils.create_network_full(
68                     neutron_client,
69                     EXAMPLE_PRIVATE_NET_NAME,
70                     EXAMPLE_PRIVATE_SUBNET_NAME,
71                     EXAMPLE_ROUTER_NAME,
72                     EXAMPLE_PRIVATE_SUBNET_CIDR)
73     if not network_dic:
74         logger.error(
75             "There has been a problem when creating the neutron network")
76         sys.exit(-1)
77
78     network_id = network_dic["net_id"]
79
80     sg_id = os_utils.create_security_group_full(neutron_client,
81                                                 EXAMPLE_SECGROUP_NAME,
82                                                 EXAMPLE_SECGROUP_DESCR)
83
84     # boot INTANCE
85     logger.info("Creating instance '%s'..." % EXAMPLE_INSTANCE_NAME)
86     logger.debug(
87         "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
88         "network=%s \n"
89         % (EXAMPLE_INSTANCE_NAME, EXAMPLE_FLAVOR, image_id, network_id))
90     instance = os_utils.create_instance_and_wait_for_active(
91                 EXAMPLE_FLAVOR,
92                 image_id,
93                 network_id,
94                 EXAMPLE_INSTANCE_NAME)
95
96     if instance is None:
97         logger.error("Error while booting instance.")
98         sys.exit(-1)
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))
103
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)
107
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']
112
113     if floatip is None:
114         logger.error("Cannot create floating IP.")
115         sys.exit(-1)
116     logger.info("Floating IP created: '%s'" % floatip)
117
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.")
122         sys.exit(-1)
123
124     sys.exit(0)
125
126
127 if __name__ == '__main__':
128     main()