Merge "Fix instance boot when metadata exists"
[sdnvpn.git] / sdnvpn / lib / utils.py
index e241d79..67b75d0 100644 (file)
@@ -247,13 +247,10 @@ def get_instance_ip(instance):
     return instance_ip
 
 
-def wait_for_instance(instance):
-    logger.info("Waiting for instance %s to get a DHCP lease and "
-                "prompt for login..." % instance.id)
-    # The sleep this function replaced waited for 80s
+def wait_for_instance(instance, pattern=".* login:"):
+    logger.info("Waiting for instance %s to boot up" % instance.id)
     tries = 40
     sleep_time = 2
-    pattern = ".* login:"
     expected_regex = re.compile(pattern)
     console_log = ""
     while tries > 0 and not expected_regex.search(console_log):
@@ -262,14 +259,20 @@ def wait_for_instance(instance):
         tries -= 1
 
     if not expected_regex.search(console_log):
-        logger.error("Instance %s seems not to boot up properly."
+        logger.error("Instance %s does not boot up properly."
                      % instance.id)
         return False
     return True
 
 
-def wait_for_instances_up(*args):
-    check = [wait_for_instance(instance) for instance in args]
+def wait_for_instances_up(*instances):
+    check = [wait_for_instance(instance) for instance in instances]
+    return all(check)
+
+
+def wait_for_instances_get_dhcp(*instances):
+    check = [wait_for_instance(instance, "Lease of .* obtained")
+             for instance in instances]
     return all(check)
 
 
@@ -592,7 +595,7 @@ def cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids, interfaces,
     return True
 
 
-def cleanup_nova(nova_client, instance_ids, image_ids):
+def cleanup_nova(nova_client, instance_ids):
     if len(instance_ids) != 0:
         for instance_id in instance_ids:
             if not os_utils.delete_instance(nova_client, instance_id):
@@ -600,10 +603,13 @@ def cleanup_nova(nova_client, instance_ids, image_ids):
                               'Instance with id {} was not deleted.'.
                               format(instance_id))
                 return False
+    return True
+
 
+def cleanup_glance(glance_client, image_ids):
     if len(image_ids) != 0:
         for image_id in image_ids:
-            if not os_utils.delete_glance_image(nova_client, image_id):
+            if not os_utils.delete_glance_image(glance_client, image_id):
                 logging.error('Fail to delete all images. '
                               'Image with id {} was not deleted.'.
                               format(image_id))
@@ -681,3 +687,66 @@ def is_fail_mode_secure():
                               'in {} node'.format(openstack_node.name))
                 is_secure[openstack_node.name] = False
     return is_secure
+
+
+def update_nw_subnet_port_quota(neutron_client, tenant_id, nw_quota,
+                                subnet_quota, port_quota):
+    json_body = {"quota": {
+        "network": nw_quota,
+        "subnet": subnet_quota,
+        "port": port_quota
+    }}
+
+    try:
+        neutron_client.update_quota(tenant_id=tenant_id,
+                                    body=json_body)
+        return True
+    except Exception as e:
+        logger.error("Error [update_nw_subnet_port_quota(neutron_client,"
+                     " '%s', '%s', '%s', '%s')]: %s" %
+                     (tenant_id, nw_quota, subnet_quota, port_quota, e))
+        return False
+
+
+def update_instance_quota_class(nova_client, instances_quota):
+    try:
+        nova_client.quota_classes.update("default", instances=instances_quota)
+        return True
+    except Exception as e:
+        logger.error("Error [update_instance_quota_class(nova_client,"
+                     " '%s' )]: %s" % (instances_quota, e))
+        return False
+
+
+def get_neutron_quota(neutron_client, tenant_id):
+    try:
+        return neutron_client.show_quota(tenant_id=tenant_id)['quota']
+    except Exception as e:
+        logger.error("Error in getting neutron quota for tenant "
+                     " '%s' )]: %s" % (tenant_id, e))
+        raise
+
+
+def get_nova_instances_quota(nova_client):
+    try:
+        return nova_client.quota_classes.get("default").instances
+    except Exception as e:
+        logger.error("Error in getting nova instances quota: %s" % e)
+        raise
+
+
+def get_ovs_groups(compute_node_list, ovs_br_list, of_protocol="OpenFlow13"):
+    """
+    Gets, as input, a list of compute nodes and a list of OVS bridges
+    and returns the command console output, as a list of lines, that
+    contains all the OVS groups from all bridges and nodes in lists.
+    """
+    cmd_out_lines = []
+    for compute_node in compute_node_list:
+        for ovs_br in ovs_br_list:
+            if ovs_br in compute_node.run_cmd("sudo ovs-vsctl show"):
+                ovs_groups_cmd = ("sudo ovs-ofctl dump-groups {} -O {} | "
+                                  "grep group".format(ovs_br, of_protocol))
+                cmd_out_lines += (compute_node.run_cmd(ovs_groups_cmd).strip().
+                                  split("\n"))
+    return cmd_out_lines