Commit c6fe99b7 by Szabolcs Gelencser

Add VM shutdown, capture template, create from template

parent a8e2421f
...@@ -26,6 +26,7 @@ SECRET = os.getenv('SECRET') ...@@ -26,6 +26,7 @@ SECRET = os.getenv('SECRET')
TENANT = os.getenv('TENANT') TENANT = os.getenv('TENANT')
GROUP_NAME = os.getenv('GROUP_NAME') GROUP_NAME = os.getenv('GROUP_NAME')
REGION = os.getenv('REGION') REGION = os.getenv('REGION')
STORAGE_NAME = os.getenv('STORAGE_NAME')
credentials = ServicePrincipalCredentials( credentials = ServicePrincipalCredentials(
client_id = CLIENT_ID, client_id = CLIENT_ID,
...@@ -192,6 +193,26 @@ def create(vm_desc): ...@@ -192,6 +193,26 @@ def create(vm_desc):
) )
) )
vhd_uri = None
image_reference = None
os_disk_type = None
if vm_desc["vhd_uri"]:
#TODO: select OS Disk type based on OS
os_disk_type = azure.mgmt.compute.models.OperatingSystemTypes.linux
vhd_uri = azure.mgmt.compute.models.VirtualHardDisk(
vm_desc["vhd_uri"]
)
logging.info("creating vm from template")
else:
image_reference = azure.mgmt.compute.models.ImageReference(
publisher=vm_desc["os_publisher"],
offer=vm_desc["os_offer"],
sku=vm_desc["os_sku"],
version=vm_desc["os_version"],
)
logging.info("creating vm from Azure image")
poller = compute_client.virtual_machines.create_or_update( poller = compute_client.virtual_machines.create_or_update(
GROUP_NAME, GROUP_NAME,
vm_desc["name"], vm_desc["name"],
...@@ -210,6 +231,7 @@ def create(vm_desc): ...@@ -210,6 +231,7 @@ def create(vm_desc):
), ),
storage_profile=azure.mgmt.compute.models.StorageProfile( storage_profile=azure.mgmt.compute.models.StorageProfile(
os_disk=azure.mgmt.compute.models.OSDisk( os_disk=azure.mgmt.compute.models.OSDisk(
os_type=os_disk_type,
caching=azure.mgmt.compute.models.CachingTypes.none, caching=azure.mgmt.compute.models.CachingTypes.none,
create_option=\ create_option=\
azure.mgmt.compute.models.DiskCreateOptionTypes.from_image, azure.mgmt.compute.models.DiskCreateOptionTypes.from_image,
...@@ -220,13 +242,9 @@ def create(vm_desc): ...@@ -220,13 +242,9 @@ def create(vm_desc):
os_disk_name, os_disk_name,
), ),
), ),
image = vhd_uri,
), ),
image_reference = azure.mgmt.compute.models.ImageReference( image_reference = image_reference,
publisher=vm_desc["os_publisher"],
offer=vm_desc["os_offer"],
sku=vm_desc["os_sku"],
version=vm_desc["os_version"],
),
), ),
), ),
) )
...@@ -239,42 +257,46 @@ def create(vm_desc): ...@@ -239,42 +257,46 @@ def create(vm_desc):
except Exception, e: except Exception, e:
logging.error("cloud not create vm '%s'" % vm_desc["name"]) logging.error("cloud not create vm '%s'" % vm_desc["name"])
logging.error(str(e)) logging.error(str(e))
raise e
@celery.task
def shutdown(vm_name):
""" Shutdown a virtual machine in azure. """
poller = compute_client.virtual_machines.deallocate(GROUP_NAME, vm_name)
try:
poller.wait()
logging.info("shutdown vm: %s" % vm_name)
return "ok"
except Exception, e:
logging.error("cloud not stop vm '%s'" % vm_name)
return None return None
@celery.task
def save_as_template(vm_name, vm_pk):
""" Save virtual machine as template in azure. """
try:
compute_client.virtual_machines.generalize(
GROUP_NAME,
vm_name
)
class shutdown(AbortableTask): os_disk_name = "%s_os_disk_template" % vm_pk
""" Shutdown virtual machine (need ACPI support).
Return When domain is missiing.
This job is abortable:
AbortableAsyncResult(id="<<jobid>>").abort()
"""
time_limit = 120
@req_connection result = compute_client.virtual_machines.capture(
def run(self, args): GROUP_NAME,
from time import sleep vm_name,
name, = args azure.mgmt.compute.models.VirtualMachineCaptureParameters(
try: os_disk_name,
domain = lookupByName(name) "templates",
domain.shutdown() False
while True: )
try: )
Connection.get().lookupByName(name) result.wait()
except libvirt.libvirtError as e:
if e.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN:
return
else:
raise
else:
if self.is_aborted():
logging.info("Shutdown aborted on vm: %s", name)
return
sleep(5)
except libvirt.libvirtError as e:
new_e = Exception(e.get_error_message())
new_e.libvirtError = True
raise new_e
return result.result().output["resources"][0]["properties"]\
["storageProfile"]["osDisk"]["image"]["uri"]
except Exception, e:
raise
@celery.task @celery.task
@req_connection @req_connection
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment