From e0095c0f7bc359910633fda491013e9c1fae3408 Mon Sep 17 00:00:00 2001 From: Romanos Skiadas Date: Wed, 7 Sep 2016 12:35:52 +0000 Subject: Refactor some common methods into utils - generete_ping_userdata(): This method was exactly the same in two tests - create_instance(): The various implementations of this method had almost the same signature, but not quite. This was fixed by adding extra arguments as needed. Also a global variable was used in the function, which was converted into a parameter. - create_network(): The three implementations of create_network had three different signatures and returned different variables. They were all unified in a single create_network class and the calls to create_network were adjusted accordingly. Revision 1: Fix flake8 violations Revision 2: Rename logger in utils Change-Id: Ib6cfe798dc561a69eb50c901e3aa71c19d465821 Signed-off-by: Romanos Skiadas (cherry picked from commit 4c2e5fb06be31502ab3476f25f6d1c2ef8d7b0f1) --- test/functest/testcase_1.py | 176 +++++++++++++--------------------------- test/functest/testcase_2.py | 191 +++++++++++++++----------------------------- test/functest/testcase_4.py | 179 ++++++++++++++--------------------------- test/functest/utils.py | 112 ++++++++++++++++++++++++++ 4 files changed, 293 insertions(+), 365 deletions(-) create mode 100644 test/functest/utils.py diff --git a/test/functest/testcase_1.py b/test/functest/testcase_1.py index 6a252af..d37f196 100644 --- a/test/functest/testcase_1.py +++ b/test/functest/testcase_1.py @@ -18,6 +18,7 @@ import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils +import utils as test_utils parser = argparse.ArgumentParser() @@ -94,77 +95,6 @@ NUM_TESTS = 0 NUM_TESTS_FAILED = 0 -def create_network(neutron_client, net, subnet, router, cidr): - network_dic = os_utils.create_network_full(neutron_client, - net, - subnet, - router, - cidr) - if not network_dic: - logger.error( - "There has been a problem when creating the neutron network") - sys.exit(-1) - return network_dic["net_id"] - - -def create_instance(nova_client, - name, - flavor, - image_id, - network_id, - sg_id, - compute_node='', - userdata=None): - logger.info("Creating instance '%s'..." % name) - logger.debug( - "Configuration:\n name=%s \n flavor=%s \n image=%s \n " - "network=%s \n secgroup=%s \n hypervisor=%s \n userdata=%s\n" - % (name, flavor, image_id, network_id, sg_id, compute_node, userdata)) - instance = os_utils.create_instance_and_wait_for_active( - flavor, - image_id, - network_id, - name, - config_drive=True, - userdata=userdata, - av_zone=compute_node) - - if instance is None: - logger.error("Error while booting instance.") - sys.exit(-1) - # Retrieve IP of INSTANCE - # instance_ip = instance.networks.get(network_id)[0] - - logger.debug("Adding '%s' to security group '%s'..." - % (name, SECGROUP_NAME)) - os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id) - - return instance - - -def generate_ping_userdata(ips_array): - ips = "" - for ip in ips_array: - ips = ("%s %s" % (ips, ip)) - - ips = ips.replace(' ', ' ') - return ("#!/bin/sh\n" - "set%s\n" - "while true; do\n" - " for i do\n" - " ip=$i\n" - " ping -c 1 $ip 2>&1 >/dev/null\n" - " RES=$?\n" - " if [ \"Z$RES\" = \"Z0\" ] ; then\n" - " echo ping $ip OK\n" - " else echo ping $ip KO\n" - " fi\n" - " done\n" - " sleep 1\n" - "done\n" - % ips) - - def get_ping_status(vm_source, ip_source, vm_target, ip_target, expected="PASS", timeout=30): @@ -258,16 +188,16 @@ def main(): disk=IMAGE_FORMAT, container="bare", public=True) - network_1_id = create_network(neutron_client, - NET_1_NAME, - SUBNET_1_NAME, - ROUTER_1_NAME, - SUBNET_1_CIDR) - network_2_id = create_network(neutron_client, - NET_2_NAME, - SUBNET_2_NAME, - ROUTER_2_NAME, - SUBNET_2_CIDR) + network_1_id, _, _ = test_utils.create_network(neutron_client, + NET_1_NAME, + SUBNET_1_NAME, + SUBNET_1_CIDR, + ROUTER_1_NAME) + network_2_id, _, _ = test_utils.create_network(neutron_client, + NET_2_NAME, + SUBNET_2_NAME, + SUBNET_2_CIDR, + ROUTER_2_NAME) sg_id = os_utils.create_security_group_full(neutron_client, SECGROUP_NAME, SECGROUP_DESCR) @@ -285,63 +215,71 @@ def main(): av_zone_2 = "nova:" + compute_nodes[1] # boot INTANCES - vm_2 = create_instance(nova_client, - INSTANCE_2_NAME, - FLAVOR, - image_id, - network_1_id, - sg_id, - av_zone_1) + vm_2 = test_utils.create_instance(nova_client, + INSTANCE_2_NAME, + FLAVOR, + image_id, + network_1_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1) vm_2_ip = vm_2.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_2_NAME, vm_2_ip)) - vm_3 = create_instance(nova_client, - INSTANCE_3_NAME, - FLAVOR, image_id, - network_1_id, - sg_id, - av_zone_2) + vm_3 = test_utils.create_instance(nova_client, + INSTANCE_3_NAME, + FLAVOR, image_id, + network_1_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_2) vm_3_ip = vm_3.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_3_NAME, vm_3_ip)) - vm_5 = create_instance(nova_client, - INSTANCE_5_NAME, - FLAVOR, - image_id, - network_2_id, - sg_id, - av_zone_2) + vm_5 = test_utils.create_instance(nova_client, + INSTANCE_5_NAME, + FLAVOR, + image_id, + network_2_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_2) vm_5_ip = vm_5.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_5_NAME, vm_5_ip)) # We boot vm5 first because we need vm5_ip for vm4 userdata - u4 = generate_ping_userdata([vm_5_ip]) - vm_4 = create_instance(nova_client, - INSTANCE_4_NAME, - FLAVOR, - image_id, - network_2_id, - sg_id, - av_zone_1, - userdata=u4) + u4 = test_utils.generate_ping_userdata([vm_5_ip]) + vm_4 = test_utils.create_instance(nova_client, + INSTANCE_4_NAME, + FLAVOR, + image_id, + network_2_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1, + userdata=u4) vm_4_ip = vm_4.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_4_NAME, vm_4_ip)) # We boot VM1 at the end because we need to get the IPs first to generate # the userdata - u1 = generate_ping_userdata([vm_2_ip, vm_3_ip, vm_4_ip, vm_5_ip]) - vm_1 = create_instance(nova_client, - INSTANCE_1_NAME, - FLAVOR, - image_id, - network_1_id, - sg_id, - av_zone_1, - userdata=u1) + u1 = test_utils.generate_ping_userdata([vm_2_ip, + vm_3_ip, + vm_4_ip, + vm_5_ip]) + vm_1 = test_utils.create_instance(nova_client, + INSTANCE_1_NAME, + FLAVOR, + image_id, + network_1_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1, + userdata=u1) vm_1_ip = vm_1.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_1_NAME, vm_1_ip)) diff --git a/test/functest/testcase_2.py b/test/functest/testcase_2.py index e319333..f5e5097 100644 --- a/test/functest/testcase_2.py +++ b/test/functest/testcase_2.py @@ -18,6 +18,7 @@ import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils +import utils as test_utils parser = argparse.ArgumentParser() @@ -111,71 +112,6 @@ NUM_TESTS = 0 NUM_TESTS_FAILED = 0 -def create_network(neutron_client, net, subnet1, cidr1, - router, subnet2=None, cidr2=None): - network_dic = os_utils.create_network_full(neutron_client, - net, - subnet1, - router, - cidr1) - if not network_dic: - logger.error( - "There has been a problem when creating the neutron network") - sys.exit(-1) - net_id = network_dic["net_id"] - if subnet2 is not None: - logger.debug("Creating and attaching a second subnet...") - subnet_id = os_utils.create_neutron_subnet( - neutron_client, subnet2, cidr2, net_id) - if not subnet_id: - logger.error( - "There has been a problem when creating the second subnet") - sys.exit(-1) - logger.debug("Subnet '%s' created successfully" % subnet_id) - return net_id - - -def create_instance(nova_client, - name, - flavor, - image_id, - network_id, - sg_id, - fixed_ip, - compute_node='', - userdata=None, - files=None): - logger.info("Creating instance '%s'..." % name) - logger.debug( - "Configuration:\n name=%s \n flavor=%s \n image=%s \n" - " network=%s\n secgroup=%s \n hypervisor=%s \n" - " fixed_ip=%s\n files=%s\n userdata=\n%s\n" - % (name, flavor, image_id, network_id, sg_id, - compute_node, fixed_ip, files, userdata)) - instance = os_utils.create_instance_and_wait_for_active( - flavor, - image_id, - network_id, - name, - config_drive=True, - userdata=userdata, - av_zone=compute_node, - fixed_ip=fixed_ip, - files=files) - - if instance is None: - logger.error("Error while booting instance.") - sys.exit(-1) - # Retrieve IP of INSTANCE - # instance_ip = instance.networks.get(network_id)[0] - - logger.debug("Adding '%s' to security group '%s'..." - % (name, SECGROUP_NAME)) - os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id) - - return instance - - def generate_userdata_common(): return ("#!/bin/sh\n" "sudo mkdir -p /home/cirros/.ssh/\n" @@ -305,20 +241,20 @@ def main(): disk=IMAGE_FORMAT, container="bare", public=True) - network_1_id = create_network(neutron_client, - NET_1_NAME, - SUBNET_1a_NAME, - SUBNET_1a_CIDR, - ROUTER_1_NAME, - SUBNET_1b_NAME, - SUBNET_1b_CIDR) - network_2_id = create_network(neutron_client, - NET_2_NAME, - SUBNET_2a_NAME, - SUBNET_2a_CIDR, - ROUTER_2_NAME, - SUBNET_2b_NAME, - SUBNET_2b_CIDR) + network_1_id, _, _ = test_utils.create_network(neutron_client, + NET_1_NAME, + SUBNET_1a_NAME, + SUBNET_1a_CIDR, + ROUTER_1_NAME, + SUBNET_1b_NAME, + SUBNET_1b_CIDR) + network_2_id, _, _ = test_utils.create_network(neutron_client, + NET_2_NAME, + SUBNET_2a_NAME, + SUBNET_2a_CIDR, + ROUTER_2_NAME, + SUBNET_2b_NAME, + SUBNET_2b_CIDR) sg_id = os_utils.create_security_group_full(neutron_client, SECGROUP_NAME, SECGROUP_DESCR) @@ -337,40 +273,43 @@ def main(): # boot INTANCES userdata_common = generate_userdata_common() - vm_2 = create_instance(nova_client, - INSTANCE_2_NAME, - FLAVOR, - image_id, - network_1_id, - sg_id, - INSTANCE_2_IP, - compute_node=av_zone_1, - userdata=userdata_common) + vm_2 = test_utils.create_instance(nova_client, + INSTANCE_2_NAME, + FLAVOR, + image_id, + network_1_id, + sg_id, + fixed_ip=INSTANCE_2_IP, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1, + userdata=userdata_common) vm_2_ip = vm_2.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_2_NAME, vm_2_ip)) - vm_3 = create_instance(nova_client, - INSTANCE_3_NAME, - FLAVOR, image_id, - network_1_id, - sg_id, - INSTANCE_3_IP, - compute_node=av_zone_2, - userdata=userdata_common) + vm_3 = test_utils.create_instance(nova_client, + INSTANCE_3_NAME, + FLAVOR, image_id, + network_1_id, + sg_id, + fixed_ip=INSTANCE_3_IP, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_2, + userdata=userdata_common) vm_3_ip = vm_3.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_3_NAME, vm_3_ip)) - vm_5 = create_instance(nova_client, - INSTANCE_5_NAME, - FLAVOR, - image_id, - network_2_id, - sg_id, - fixed_ip=INSTANCE_5_IP, - compute_node=av_zone_2, - userdata=userdata_common) + vm_5 = test_utils.create_instance(nova_client, + INSTANCE_5_NAME, + FLAVOR, + image_id, + network_2_id, + sg_id, + fixed_ip=INSTANCE_5_IP, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_2, + userdata=userdata_common) vm_5_ip = vm_5.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_5_NAME, vm_5_ip)) @@ -378,16 +317,17 @@ def main(): # We boot vm5 first because we need vm5_ip for vm4 userdata u4 = generate_userdata_with_ssh( [INSTANCE_1_IP, INSTANCE_3_IP, INSTANCE_5_IP]) - vm_4 = create_instance(nova_client, - INSTANCE_4_NAME, - FLAVOR, - image_id, - network_2_id, - sg_id, - fixed_ip=INSTANCE_4_IP, - compute_node=av_zone_1, - userdata=u4, - files=files) + vm_4 = test_utils.create_instance(nova_client, + INSTANCE_4_NAME, + FLAVOR, + image_id, + network_2_id, + sg_id, + fixed_ip=INSTANCE_4_IP, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1, + userdata=u4, + files=files) vm_4_ip = vm_4.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_4_NAME, vm_4_ip)) @@ -396,16 +336,17 @@ def main(): # the userdata u1 = generate_userdata_with_ssh( [INSTANCE_2_IP, INSTANCE_3_IP, INSTANCE_4_IP, INSTANCE_5_IP]) - vm_1 = create_instance(nova_client, - INSTANCE_1_NAME, - FLAVOR, - image_id, - network_1_id, - sg_id, - fixed_ip=INSTANCE_1_IP, - compute_node=av_zone_1, - userdata=u1, - files=files) + vm_1 = test_utils.create_instance(nova_client, + INSTANCE_1_NAME, + FLAVOR, + image_id, + network_1_id, + sg_id, + fixed_ip=INSTANCE_1_IP, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1, + userdata=u1, + files=files) vm_1_ip = vm_1.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_1_NAME, vm_1_ip)) diff --git a/test/functest/testcase_4.py b/test/functest/testcase_4.py index c7d222e..cc4237c 100644 --- a/test/functest/testcase_4.py +++ b/test/functest/testcase_4.py @@ -18,6 +18,7 @@ import functest.utils.functest_logger as ft_logger import functest.utils.functest_utils as ft_utils import functest.utils.openstack_utils as os_utils +import utils as test_utils parser = argparse.ArgumentParser() @@ -94,79 +95,6 @@ NUM_TESTS = 0 NUM_TESTS_FAILED = 0 -def create_network(neutron_client, net, subnet, router, cidr): - network_dic = os_utils.create_network_full(neutron_client, - net, - subnet, - router, - cidr) - if not network_dic: - logger.error( - "There has been a problem when creating the neutron network") - sys.exit(-1) - return network_dic["net_id"], \ - network_dic["subnet_id"], \ - network_dic["router_id"] - - -def create_instance(nova_client, - name, - flavor, - image_id, - network_id, - sg_id, - compute_node='', - userdata=None): - logger.info("Creating instance '%s'..." % name) - logger.debug( - "Configuration:\n name=%s \n flavor=%s \n image=%s \n " - "network=%s \n secgroup=%s \n hypervisor=%s \n userdata=%s\n" - % (name, flavor, image_id, network_id, sg_id, compute_node, userdata)) - instance = os_utils.create_instance_and_wait_for_active( - flavor, - image_id, - network_id, - name, - config_drive=True, - userdata=userdata, - av_zone=compute_node) - - if instance is None: - logger.error("Error while booting instance.") - sys.exit(-1) - # Retrieve IP of INSTANCE - # instance_ip = instance.networks.get(network_id)[0] - - logger.debug("Adding '%s' to security group '%s'..." - % (name, SECGROUP_NAME)) - os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id) - - return instance - - -def generate_ping_userdata(ips_array): - ips = "" - for ip in ips_array: - ips = ("%s %s" % (ips, ip)) - - ips = ips.replace(' ', ' ') - return ("#!/bin/sh\n" - "set%s\n" - "while true; do\n" - " for i do\n" - " ip=$i\n" - " ping -c 1 $ip 2>&1 >/dev/null\n" - " RES=$?\n" - " if [ \"Z$RES\" = \"Z0\" ] ; then\n" - " echo ping $ip OK\n" - " else echo ping $ip KO\n" - " fi\n" - " done\n" - " sleep 1\n" - "done\n" - % ips) - - def get_ping_status(vm_source, ip_source, vm_target, ip_target, expected="PASS", timeout=30): @@ -260,16 +188,16 @@ def main(): disk=IMAGE_FORMAT, container="bare", public=True) - network_1_id, _, router_1_id = create_network(neutron_client, - NET_1_NAME, - SUBNET_1_NAME, - ROUTER_1_NAME, - SUBNET_1_CIDR) - network_2_id, _, router_2_id = create_network(neutron_client, - NET_2_NAME, - SUBNET_2_NAME, - ROUTER_2_NAME, - SUBNET_2_CIDR) + network_1_id, _, router_1_id = test_utils.create_network(neutron_client, + NET_1_NAME, + SUBNET_1_NAME, + SUBNET_1_CIDR, + ROUTER_1_NAME) + network_2_id, _, router_2_id = test_utils.create_network(neutron_client, + NET_2_NAME, + SUBNET_2_NAME, + SUBNET_2_CIDR, + ROUTER_2_NAME) sg_id = os_utils.create_security_group_full(neutron_client, SECGROUP_NAME, SECGROUP_DESCR) @@ -287,63 +215,72 @@ def main(): av_zone_2 = "nova:" + compute_nodes[1] # boot INTANCES - vm_2 = create_instance(nova_client, - INSTANCE_2_NAME, - FLAVOR, - image_id, - network_1_id, - sg_id, - av_zone_1) + vm_2 = test_utils.create_instance(nova_client, + INSTANCE_2_NAME, + FLAVOR, + image_id, + network_1_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1) vm_2_ip = vm_2.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_2_NAME, vm_2_ip)) - vm_3 = create_instance(nova_client, - INSTANCE_3_NAME, - FLAVOR, image_id, - network_1_id, - sg_id, - av_zone_2) + vm_3 = test_utils.create_instance(nova_client, + INSTANCE_3_NAME, + FLAVOR, + image_id, + network_1_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_2) vm_3_ip = vm_3.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_3_NAME, vm_3_ip)) - vm_5 = create_instance(nova_client, - INSTANCE_5_NAME, - FLAVOR, - image_id, - network_2_id, - sg_id, - av_zone_2) + vm_5 = test_utils.create_instance(nova_client, + INSTANCE_5_NAME, + FLAVOR, + image_id, + network_2_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_2) vm_5_ip = vm_5.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_5_NAME, vm_5_ip)) # We boot vm5 first because we need vm5_ip for vm4 userdata - u4 = generate_ping_userdata([vm_5_ip]) - vm_4 = create_instance(nova_client, - INSTANCE_4_NAME, - FLAVOR, - image_id, - network_2_id, - sg_id, - av_zone_1, - userdata=u4) + u4 = test_utils.generate_ping_userdata([vm_5_ip]) + vm_4 = test_utils.create_instance(nova_client, + INSTANCE_4_NAME, + FLAVOR, + image_id, + network_2_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1, + userdata=u4) vm_4_ip = vm_4.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_4_NAME, vm_4_ip)) # We boot VM1 at the end because we need to get the IPs first to generate # the userdata - u1 = generate_ping_userdata([vm_2_ip, vm_3_ip, vm_4_ip, vm_5_ip]) - vm_1 = create_instance(nova_client, - INSTANCE_1_NAME, - FLAVOR, - image_id, - network_1_id, - sg_id, - av_zone_1, - userdata=u1) + u1 = test_utils.generate_ping_userdata([vm_2_ip, + vm_3_ip, + vm_4_ip, + vm_5_ip]) + vm_1 = test_utils.create_instance(nova_client, + INSTANCE_1_NAME, + FLAVOR, + image_id, + network_1_id, + sg_id, + secgroup_name=SECGROUP_NAME, + compute_node=av_zone_1, + userdata=u1) vm_1_ip = vm_1.networks.itervalues().next()[0] logger.debug("Instance '%s' booted successfully. IP='%s'." % (INSTANCE_1_NAME, vm_1_ip)) diff --git a/test/functest/utils.py b/test/functest/utils.py new file mode 100644 index 0000000..4d921c8 --- /dev/null +++ b/test/functest/utils.py @@ -0,0 +1,112 @@ +#!/usr/bin/python +# +# Copyright (c) 2015 All rights reserved +# This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +import sys + +import functest.utils.functest_logger as ft_logger +import functest.utils.openstack_utils as os_utils + + +logger = ft_logger.Logger("sndvpn_test_utils").getLogger() + + +def create_network(neutron_client, net, subnet1, cidr1, + router, subnet2=None, cidr2=None): + network_dic = os_utils.create_network_full(neutron_client, + net, + subnet1, + router, + cidr1) + if not network_dic: + logger.error( + "There has been a problem when creating the neutron network") + sys.exit(-1) + net_id = network_dic["net_id"] + subnet_id = network_dic["subnet_id"] + router_id = network_dic["router_id"] + + if subnet2 is not None: + logger.debug("Creating and attaching a second subnet...") + subnet_id = os_utils.create_neutron_subnet( + neutron_client, subnet2, cidr2, net_id) + if not subnet_id: + logger.error( + "There has been a problem when creating the second subnet") + sys.exit(-1) + logger.debug("Subnet '%s' created successfully" % subnet_id) + return net_id, subnet_id, router_id + + +def create_instance(nova_client, + name, + flavor, + image_id, + network_id, + sg_id, + secgroup_name=None, + fixed_ip=None, + compute_node='', + userdata=None, + files=None): + logger.info("Creating instance '%s'..." % name) + logger.debug( + "Configuration:\n name=%s \n flavor=%s \n image=%s \n" + " network=%s\n secgroup=%s \n hypervisor=%s \n" + " fixed_ip=%s\n files=%s\n userdata=\n%s\n" + % (name, flavor, image_id, network_id, sg_id, + compute_node, fixed_ip, files, userdata)) + instance = os_utils.create_instance_and_wait_for_active( + flavor, + image_id, + network_id, + name, + config_drive=True, + userdata=userdata, + av_zone=compute_node, + fixed_ip=fixed_ip, + files=files) + + if instance is None: + logger.error("Error while booting instance.") + sys.exit(-1) + # Retrieve IP of INSTANCE + # instance_ip = instance.networks.get(network_id)[0] + + if secgroup_name: + logger.debug("Adding '%s' to security group '%s'..." + % (name, secgroup_name)) + else: + logger.debug("Adding '%s' to security group '%s'..." + % (name, sg_id)) + os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id) + + return instance + + +def generate_ping_userdata(ips_array): + ips = "" + for ip in ips_array: + ips = ("%s %s" % (ips, ip)) + + ips = ips.replace(' ', ' ') + return ("#!/bin/sh\n" + "set%s\n" + "while true; do\n" + " for i do\n" + " ip=$i\n" + " ping -c 1 $ip 2>&1 >/dev/null\n" + " RES=$?\n" + " if [ \"Z$RES\" = \"Z0\" ] ; then\n" + " echo ping $ip OK\n" + " else echo ping $ip KO\n" + " fi\n" + " done\n" + " sleep 1\n" + "done\n" + % ips) -- cgit 1.2.3-korg