Commit b24c1117 by Szabolcs Gelencser

Implement create task for Azure VM creation.

parent 13928bea
......@@ -16,6 +16,27 @@ from vm import VMInstance, VMDisk, VMNetwork
from vmcelery import celery, lib_connection, to_bool
from azure.common.credentials import ServicePrincipalCredentials
import azure.mgmt.compute
SUBSCRIPTION_ID = os.getenv('SUBSCRIPTION_ID')
CLIENT_ID = os.getenv('CLIENT_ID')
SECRET = os.getenv('SECRET')
TENANT = os.getenv('TENANT')
GROUP_NAME = os.getenv('GROUP_NAME')
REGION = os.getenv('REGION')
credentials = ServicePrincipalCredentials(
client_id = CLIENT_ID,
secret = SECRET,
tenant = TENANT,
)
compute_client = azure.mgmt.compute.ComputeManagementClient(
credentials,
SUBSCRIPTION_ID
)
sys.path.append(os.path.dirname(os.path.basename(__file__)))
vm_xml_dump = None
......@@ -30,7 +51,6 @@ state_dict = {0: 'NOSTATE',
7: 'PMSUSPENDED'
}
# class Singleton(type):
#
# """ Singleton class."""
......@@ -154,48 +174,66 @@ def define(vm):
@celery.task
@req_connection
@wrap_libvirtError
def create(vm_desc):
""" Create and start non-permanent virtual machine from xml.
Return the domain info dict.
flags can be:
VIR_DOMAIN_NONE = 0
VIR_DOMAIN_START_PAUSED = 1
VIR_DOMAIN_START_AUTODESTROY = 2
VIR_DOMAIN_START_BYPASS_CACHE = 4
VIR_DOMAIN_START_FORCE_BOOT = 8
""" Create and start a virtual machine in azure. """
os_disk_name = "%s_os_disk" % vm_desc["name"]
nic_references = []
for nic in vm_desc["nics"]:
nic_references.append(
azure.mgmt.compute.models.NetworkInterfaceReference(
id=nic,
)
)
poller = compute_client.virtual_machines.create_or_update(
GROUP_NAME,
vm_desc["name"],
azure.mgmt.compute.models.VirtualMachine(
location=REGION,
os_profile=azure.mgmt.compute.models.OSProfile(
admin_username=vm_desc["user"],
admin_password=vm_desc["pw"],
computer_name=vm_desc["name"],
),
hardware_profile=azure.mgmt.compute.models.HardwareProfile(
vm_size=vm_desc["vm_size"]
),
network_profile=azure.mgmt.compute.models.NetworkProfile(
network_interfaces=nic_references,
),
storage_profile=azure.mgmt.compute.models.StorageProfile(
os_disk=azure.mgmt.compute.models.OSDisk(
caching=azure.mgmt.compute.models.CachingTypes.none,
create_option=\
azure.mgmt.compute.models.DiskCreateOptionTypes.from_image,
name=os_disk_name,
vhd=azure.mgmt.compute.models.VirtualHardDisk(
uri='https://%s.blob.core.windows.net/vhds/%s.vhd' % (
vm_desc["datastore_name"],
os_disk_name,
),
),
),
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"],
),
),
),
)
"""
vm = VMInstance.deserialize(vm_desc)
# Setting proper hypervisor
vm.vm_type = os.getenv("HYPERVISOR_TYPE", "test")
if vm.vm_type == "test":
vm.arch = "i686"
vm_xml_dump = vm.dump_xml()
logging.info(vm_xml_dump)
# Emulating DOMAIN_START_PAUSED FLAG behaviour on test driver
if vm.vm_type == "test":
Connection.get().createXML(
vm_xml_dump, libvirt.VIR_DOMAIN_NONE)
domain = lookupByName(vm.name)
domain.suspend()
# Real driver create
else:
Connection.get().createXML(
vm_xml_dump, libvirt.VIR_DOMAIN_START_PAUSED)
logging.info("Virtual machine %s is created from xml", vm.name)
# context
try:
sock = socket.create_connection(('127.0.0.1', 1235), 3)
data = {'boot_token': vm.boot_token,
'socket': '/var/lib/libvirt/serial/%s' % vm.name}
sock.sendall(json.dumps(data))
sock.close()
except socket.error:
logging.error('Unable to connect to context server')
return vm_xml_dump
poller.wait()
azure_id = poller.result().id
logging.info("created vm with id: %s" % azure_id)
return azure_id
except Exception, e:
logging.error("cloud not create vm '%s'" % vm_desc["name"])
logging.error(str(e))
return None
class shutdown(AbortableTask):
......@@ -347,18 +385,11 @@ def restore(name, path):
@celery.task
@req_connection
@wrap_libvirtError
def resume(name):
""" Resume stopped virtual machines.
Return the domain info dict.
"""
domain = lookupByName(name)
domain.resume()
return _parse_info(domain.info())
#TODO: implement
return
@celery.task
......
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