Commit b24c1117 by Szabolcs Gelencser

Implement create task for Azure VM creation.

parent 13928bea
...@@ -16,6 +16,27 @@ from vm import VMInstance, VMDisk, VMNetwork ...@@ -16,6 +16,27 @@ from vm import VMInstance, VMDisk, VMNetwork
from vmcelery import celery, lib_connection, to_bool 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__))) sys.path.append(os.path.dirname(os.path.basename(__file__)))
vm_xml_dump = None vm_xml_dump = None
...@@ -30,7 +51,6 @@ state_dict = {0: 'NOSTATE', ...@@ -30,7 +51,6 @@ state_dict = {0: 'NOSTATE',
7: 'PMSUSPENDED' 7: 'PMSUSPENDED'
} }
# class Singleton(type): # class Singleton(type):
# #
# """ Singleton class.""" # """ Singleton class."""
...@@ -154,48 +174,66 @@ def define(vm): ...@@ -154,48 +174,66 @@ def define(vm):
@celery.task @celery.task
@req_connection
@wrap_libvirtError
def create(vm_desc): def create(vm_desc):
""" Create and start non-permanent virtual machine from xml. """ Create and start a virtual machine in azure. """
os_disk_name = "%s_os_disk" % vm_desc["name"]
Return the domain info dict.
flags can be: nic_references = []
VIR_DOMAIN_NONE = 0 for nic in vm_desc["nics"]:
VIR_DOMAIN_START_PAUSED = 1 nic_references.append(
VIR_DOMAIN_START_AUTODESTROY = 2 azure.mgmt.compute.models.NetworkInterfaceReference(
VIR_DOMAIN_START_BYPASS_CACHE = 4 id=nic,
VIR_DOMAIN_START_FORCE_BOOT = 8 )
)
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: try:
sock = socket.create_connection(('127.0.0.1', 1235), 3) poller.wait()
data = {'boot_token': vm.boot_token, azure_id = poller.result().id
'socket': '/var/lib/libvirt/serial/%s' % vm.name} logging.info("created vm with id: %s" % azure_id)
sock.sendall(json.dumps(data)) return azure_id
sock.close() except Exception, e:
except socket.error: logging.error("cloud not create vm '%s'" % vm_desc["name"])
logging.error('Unable to connect to context server') logging.error(str(e))
return vm_xml_dump return None
class shutdown(AbortableTask): class shutdown(AbortableTask):
...@@ -347,18 +385,11 @@ def restore(name, path): ...@@ -347,18 +385,11 @@ def restore(name, path):
@celery.task @celery.task
@req_connection
@wrap_libvirtError
def resume(name): def resume(name):
""" Resume stopped virtual machines. """ Resume stopped virtual machines.
Return the domain info dict.
""" """
#TODO: implement
domain = lookupByName(name) return
domain.resume()
return _parse_info(domain.info())
@celery.task @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