+def create_instance(flavor_name,
+ image_id,
+ network_id,
+ instance_name="",
+ config_drive=False,
+ userdata=""):
+ nova_client = get_nova_client()
+ try:
+ flavor = nova_client.flavors.find(name=flavor_name)
+ except:
+ print("Error: Flavor '%s' not found. Available flavors are:" %
+ flavor_name)
+ print(nova_client.flavor.list())
+ return -1
+
+ return nova_client.servers.create(
+ name=instance_name,
+ flavor=flavor,
+ image=image_id,
+ config_drive=config_drive,
+ nics=[{"net-id": network_id}]
+ )
+
+
+def create_instance_and_wait_for_active(flavor_name,
+ image_id,
+ network_id,
+ instance_name="",
+ config_drive=False,
+ userdata=""):
+ SLEEP = 3
+ VM_BOOT_TIMEOUT = 180
+ nova_client = get_nova_client()
+ instance = create_instance(flavor_name,
+ image_id,
+ network_id,
+ instance_name="",
+ config_drive=False,
+ userdata="")
+
+ count = VM_BOOT_TIMEOUT / SLEEP
+ for n in range(count, -1, -1):
+ status = get_instance_status(nova_client, instance)
+ if status.lower() == "active":
+ return instance
+ elif status.lower() == "error":
+ print("The instance %s went to ERROR status." % instance_name)
+ return None
+ time.sleep(SLEEP)
+ print("Timeout booting the instance %s." % instance_name)
+ return None
+
+