def create_virtual_machine_dm_true(self, experiment_id, template_unit): cloud_service_name = template_unit.get_cloud_service_name() deployment_slot = template_unit.get_deployment_slot() deployment_name = template_unit.get_deployment_name() virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % ( template_unit.get_virtual_machine_name(), experiment_id) m = self.CREATE_DEPLOYMENT_INFO[0] % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot) commit_azure_deployment(deployment_name, deployment_slot, ADStatus.RUNNING, cloud_service_name, experiment_id) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 0) self.log.debug(m) # query virtual machine status run_job(MDL_CLS_FUNC[8], (self.azure_key_id, ), (cloud_service_name, deployment_name, virtual_machine_name, AVMStatus.READY_ROLE, MDL_CLS_FUNC[9], (self.azure_key_id, ), (experiment_id, template_unit)), VIRTUAL_MACHINE_TICK)
def create_virtual_machine_dm_true(self, experiment_id, template_unit): cloud_service_name = template_unit.get_cloud_service_name() deployment_slot = template_unit.get_deployment_slot() deployment_name = template_unit.get_deployment_name() virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(), experiment_id) m = self.CREATE_DEPLOYMENT_INFO[0] % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot) commit_azure_deployment(deployment_name, deployment_slot, ADStatus.RUNNING, cloud_service_name, experiment_id) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 0) self.log.debug(m) # query virtual machine status run_job(MDL_CLS_FUNC[8], (self.azure_key_id, ), (cloud_service_name, deployment_name, virtual_machine_name, AVMStatus.READY_ROLE, MDL_CLS_FUNC[9], (self.azure_key_id, ), (experiment_id, template_unit)), VIRTUAL_MACHINE_TICK)
def create_virtual_machine(self, experiment_id, template_unit): """ 0. Prerequisites: a. storage account and cloud service exist in both azure and database; b. input parameters are correct; 1. If deployment not exist in azure subscription, then create virtual machine with deployment Else reuse deployment in azure subscription 2. If virtual machine not exist in azure subscription, then add virtual machine to deployment Else reuse virtual machine in azure subscription :return: """ commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.START) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.START) deployment_slot = template_unit.get_deployment_slot() # avoid virtual machine name conflict on same name in template virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(), experiment_id) virtual_machine_size = template_unit.get_virtual_machine_size() if self.subscription.get_available_core_count() < self.SIZE_CORE_MAP[virtual_machine_size.lower()]: m = self.CREATE_DEPLOYMENT_ERROR[1] % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 1) self.log.error(m) m = self.CREATE_VIRTUAL_MACHINE_ERROR[1] % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 1) self.log.error(m) return False cloud_service_name = template_unit.get_cloud_service_name() vm_image_name = template_unit.get_vm_image_name() system_config = template_unit.get_system_config() os_virtual_hard_disk = template_unit.get_os_virtual_hard_disk() # avoid duplicate deployment in azure subscription if self.service.deployment_exists(cloud_service_name, deployment_slot): # use deployment name from azure subscription deployment_name = self.service.get_deployment_name(cloud_service_name, deployment_slot) if contain_azure_deployment(cloud_service_name, deployment_slot): m = self.CREATE_DEPLOYMENT_INFO[1] % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_name, AZURE_FORMATION) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 1) else: m = self.CREATE_DEPLOYMENT_INFO[2] % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_name, AZURE_FORMATION) commit_azure_deployment(deployment_name, deployment_slot, ADStatus.RUNNING, cloud_service_name, experiment_id) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 2) self.log.debug(m) # avoid duplicate virtual machine in azure subscription if self.service.virtual_machine_exists(cloud_service_name, deployment_name, virtual_machine_name): if contain_azure_virtual_machine(cloud_service_name, deployment_name, virtual_machine_name): m = self.CREATE_VIRTUAL_MACHINE_INFO[1] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.END, m, 1) self.log.debug(m) else: m = self.CREATE_VIRTUAL_MACHINE_ERROR[4] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 4) self.log.error(m) return False else: # delete old azure virtual machine, cascade delete old azure endpoint delete_azure_virtual_machine(cloud_service_name, deployment_name, virtual_machine_name) network_config = template_unit.get_network_config(self.service, False) try: result = self.service.add_virtual_machine(cloud_service_name, deployment_name, virtual_machine_name, system_config, os_virtual_hard_disk, network_config, virtual_machine_size, vm_image_name) except Exception as e: m = self.CREATE_VIRTUAL_MACHINE_ERROR[0] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, e.message) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 0) self.log.error(e) return False # query async operation status run_job(MDL_CLS_FUNC[2], (self.azure_key_id, ), (result.request_id, MDL_CLS_FUNC[6], (self.azure_key_id, ), (experiment_id, template_unit), MDL_CLS_FUNC[7], (self.azure_key_id, ), (experiment_id, template_unit))) else: # delete old azure deployment, cascade delete old azure virtual machine and azure endpoint delete_azure_deployment(cloud_service_name, deployment_slot) # use deployment name from template deployment_name = template_unit.get_deployment_name() virtual_machine_label = template_unit.get_virtual_machine_label() network_config = template_unit.get_network_config(self.service, False) try: result = self.service.create_virtual_machine_deployment(cloud_service_name, deployment_name, deployment_slot, virtual_machine_label, virtual_machine_name, system_config, os_virtual_hard_disk, network_config, virtual_machine_size, vm_image_name) except Exception as e: m = self.CREATE_DEPLOYMENT_ERROR[0] % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot, e.message) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 0) m = self.CREATE_VIRTUAL_MACHINE_ERROR[0] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, e.message) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 0) self.log.error(e) return False # query async operation status run_job(MDL_CLS_FUNC[2], (self.azure_key_id, ), (result.request_id, MDL_CLS_FUNC[13], (self.azure_key_id, ), (experiment_id, template_unit), MDL_CLS_FUNC[14], (self.azure_key_id, ), (experiment_id, template_unit))) return True
def create_virtual_machine(self, experiment_id, template_unit): """ 0. Prerequisites: a. storage account and cloud service exist in both azure and database; b. input parameters are correct; 1. If deployment not exist in azure subscription, then create virtual machine with deployment Else reuse deployment in azure subscription 2. If virtual machine not exist in azure subscription, then add virtual machine to deployment Else reuse virtual machine in azure subscription :return: """ commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.START) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.START) deployment_slot = template_unit.get_deployment_slot() # avoid virtual machine name conflict on same name in template virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % ( template_unit.get_virtual_machine_name(), experiment_id) virtual_machine_size = template_unit.get_virtual_machine_size() if self.subscription.get_available_core_count() < self.SIZE_CORE_MAP[ virtual_machine_size.lower()]: m = self.CREATE_DEPLOYMENT_ERROR[1] % ( AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 1) self.log.error(m) m = self.CREATE_VIRTUAL_MACHINE_ERROR[1] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 1) self.log.error(m) return False cloud_service_name = template_unit.get_cloud_service_name() vm_image_name = template_unit.get_vm_image_name() system_config = template_unit.get_system_config() os_virtual_hard_disk = template_unit.get_os_virtual_hard_disk() # avoid duplicate deployment in azure subscription if self.service.deployment_exists(cloud_service_name, deployment_slot): # use deployment name from azure subscription deployment_name = self.service.get_deployment_name( cloud_service_name, deployment_slot) if contain_azure_deployment(cloud_service_name, deployment_slot): m = self.CREATE_DEPLOYMENT_INFO[1] % ( AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_name, AZURE_FORMATION) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 1) else: m = self.CREATE_DEPLOYMENT_INFO[2] % ( AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_name, AZURE_FORMATION) commit_azure_deployment(deployment_name, deployment_slot, ADStatus.RUNNING, cloud_service_name, experiment_id) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 2) self.log.debug(m) # avoid duplicate virtual machine in azure subscription if self.service.virtual_machine_exists(cloud_service_name, deployment_name, virtual_machine_name): if contain_azure_virtual_machine(cloud_service_name, deployment_name, virtual_machine_name): m = self.CREATE_VIRTUAL_MACHINE_INFO[1] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.END, m, 1) self.log.debug(m) else: m = self.CREATE_VIRTUAL_MACHINE_ERROR[4] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 4) self.log.error(m) return False else: # delete old azure virtual machine, cascade delete old azure endpoint delete_azure_virtual_machine(cloud_service_name, deployment_name, virtual_machine_name) network_config = template_unit.get_network_config( self.service, False) try: result = self.service.add_virtual_machine( cloud_service_name, deployment_name, virtual_machine_name, system_config, os_virtual_hard_disk, network_config, virtual_machine_size, vm_image_name) except Exception as e: m = self.CREATE_VIRTUAL_MACHINE_ERROR[0] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, e.message) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 0) self.log.error(e) return False # query async operation status run_job( MDL_CLS_FUNC[2], (self.azure_key_id, ), (result.request_id, MDL_CLS_FUNC[6], (self.azure_key_id, ), (experiment_id, template_unit), MDL_CLS_FUNC[7], (self.azure_key_id, ), (experiment_id, template_unit))) else: # delete old azure deployment, cascade delete old azure virtual machine and azure endpoint delete_azure_deployment(cloud_service_name, deployment_slot) # use deployment name from template deployment_name = template_unit.get_deployment_name() virtual_machine_label = template_unit.get_virtual_machine_label() network_config = template_unit.get_network_config( self.service, False) try: result = self.service.create_virtual_machine_deployment( cloud_service_name, deployment_name, deployment_slot, virtual_machine_label, virtual_machine_name, system_config, os_virtual_hard_disk, network_config, virtual_machine_size, vm_image_name) except Exception as e: m = self.CREATE_DEPLOYMENT_ERROR[0] % ( AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot, e.message) commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 0) m = self.CREATE_VIRTUAL_MACHINE_ERROR[0] % ( AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, e.message) commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 0) self.log.error(e) return False # query async operation status run_job(MDL_CLS_FUNC[2], (self.azure_key_id, ), (result.request_id, MDL_CLS_FUNC[13], (self.azure_key_id, ), (experiment_id, template_unit), MDL_CLS_FUNC[14], (self.azure_key_id, ), (experiment_id, template_unit))) return True