Ejemplo n.º 1
0
 def start_virtual_machine_vm_true(self, experiment_id, template_unit):
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     self.__start_virtual_machine_helper(experiment_id, template_unit)
     m = self.START_VIRTUAL_MACHINE_INFO[0] % (VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.END, m, 0)
     log.debug(m)
Ejemplo n.º 2
0
 def stop_virtual_machine_vm_true(self, experiment_id, template_unit, need_status):
     cloud_service_name = template_unit.get_cloud_service_name()
     deployment_slot = template_unit.get_deployment_slot()
     deployment_name = self.service.get_deployment_name(cloud_service_name, deployment_slot)
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     self.__stop_virtual_machine_helper(experiment_id, template_unit, need_status)
     m = self.STOP_VIRTUAL_MACHINE_INFO[0] % (VIRTUAL_MACHINE, virtual_machine_name, need_status)
     commit_azure_log(experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.END, m, 0)
     log.debug(m)
Ejemplo n.º 3
0
 def create_virtual_machine_async_false_3(self, experiment_id, template_unit):
     deployment_slot = template_unit.get_deployment_slot()
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     m = self.CREATE_DEPLOYMENT_ERROR[2] % (DEPLOYMENT, deployment_slot)
     commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 2)
     log.error(m)
     m = self.CREATE_VIRTUAL_MACHINE_ERROR[2] % (VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 2)
     log.error(m)
Ejemplo n.º 4
0
 def __create_virtual_machine_helper(self, experiment_id, template_unit):
     cloud_service_name = template_unit.get_cloud_service_name()
     deployment_slot = template_unit.get_deployment_slot()
     deployment_name = self.service.get_deployment_name(cloud_service_name, deployment_slot)
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     public_ip = self.service.get_virtual_machine_public_ip(cloud_service_name,
                                                            deployment_name,
                                                            virtual_machine_name)
     remote_port_name = template_unit.get_remote_port_name()
     remote_port = self.service.get_virtual_machine_public_endpoint(cloud_service_name,
                                                                    deployment_name,
                                                                    virtual_machine_name,
                                                                    remote_port_name)
     remote_paras = template_unit.get_remote_paras(virtual_machine_name,
                                                   public_ip,
                                                   remote_port)
     virtual_environment = commit_virtual_environment(VEProvider.AzureVM,
                                                      virtual_machine_name,
                                                      template_unit.get_image_name(),
                                                      VEStatus.Running,
                                                      VERemoteProvider.Guacamole,
                                                      json.dumps(remote_paras),
                                                      experiment_id)
     dns = self.service.get_deployment_dns(cloud_service_name, deployment_slot)
     private_ip = self.service.get_virtual_machine_private_ip(cloud_service_name,
                                                              deployment_name,
                                                              virtual_machine_name)
     virtual_machine_label = template_unit.get_virtual_machine_label()
     virtual_machine = commit_azure_virtual_machine(virtual_machine_name,
                                                    virtual_machine_label,
                                                    AVMStatus.READY_ROLE,
                                                    dns,
                                                    public_ip,
                                                    private_ip,
                                                    cloud_service_name,
                                                    deployment_name,
                                                    experiment_id,
                                                    virtual_environment)
     network_config = self.service.get_virtual_machine_network_config(cloud_service_name,
                                                                      deployment_name,
                                                                      virtual_machine_name)
     for input_endpoint in network_config.input_endpoints.input_endpoints:
         commit_azure_endpoint(input_endpoint.name,
                               input_endpoint.protocol,
                               input_endpoint.port,
                               input_endpoint.local_port,
                               virtual_machine)
     m = self.CREATE_VIRTUAL_MACHINE_INFO[0] % (VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.END, m, 0)
     log.debug(m)
Ejemplo n.º 5
0
 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] % (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)
     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)
Ejemplo n.º 6
0
 def start_virtual_machine(self, experiment_id, template_unit):
     """
     0. Prerequisites: a. virtual machine exist in both azure and database
                       b. input parameters are correct
     :param experiment_id:
     :param template_unit:
     :return:
     """
     commit_azure_log(experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.START)
     cloud_service_name = template_unit.get_cloud_service_name()
     deployment_slot = template_unit.get_deployment_slot()
     deployment_name = self.service.get_deployment_name(cloud_service_name, deployment_slot)
     deployment = self.service.get_deployment_by_name(cloud_service_name, deployment_name)
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     status = self.service.get_virtual_machine_instance_status(deployment, virtual_machine_name)
     if status == AVMStatus.READY_ROLE:
         db_status = get_azure_virtual_machine_status(cloud_service_name, deployment_name, virtual_machine_name)
         if db_status == status:
             m = self.START_VIRTUAL_MACHINE_INFO[1] % (VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION)
             commit_azure_log(experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.END, m, 1)
         else:
             m = self.START_VIRTUAL_MACHINE_INFO[2] % (VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION)
             self.__start_virtual_machine_helper(experiment_id, template_unit)
             commit_azure_log(experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.END, m, 2)
         log.debug(m)
     else:
         try:
             result = self.service.start_virtual_machine(cloud_service_name,
                                                         deployment_name,
                                                         virtual_machine_name)
         except Exception as e:
             m = self.START_VIRTUAL_MACHINE_ERROR[0] % (VIRTUAL_MACHINE, virtual_machine_name, e.message)
             commit_azure_log(experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.FAIL, 0)
             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[22], (self.azure_key_id, ), (experiment_id, template_unit),
                  MDL_CLS_FUNC[23], (self.azure_key_id, ), (experiment_id, template_unit)))
     return True
Ejemplo n.º 7
0
 def create_cloud_service(self, experiment_id, template_unit):
     """
     If cloud service not exist in azure subscription, then create it
     Else reuse cloud service in azure subscription
     :return:
     """
     name = template_unit.get_cloud_service_name()
     label = template_unit.get_cloud_service_label()
     location = template_unit.get_cloud_service_location()
     commit_azure_log(experiment_id, ALOperation.CREATE_CLOUD_SERVICE,
                      ALStatus.START)
     # avoid duplicate cloud service in azure subscription
     if not self.service.cloud_service_exists(name):
         # avoid name already taken by other azure subscription
         if not self.service.check_hosted_service_name_availability(
                 name).result:
             m = self.CREATE_CLOUD_SERVICE_ERROR[1] % (CLOUD_SERVICE, name)
             commit_azure_log(experiment_id,
                              ALOperation.CREATE_CLOUD_SERVICE,
                              ALStatus.FAIL, m, 1)
             log.error(m)
             return False
         # avoid no available subscription remained
         if self.subscription.get_available_cloud_service_count(
         ) < self.NEED_COUNT:
             m = self.CREATE_CLOUD_SERVICE_ERROR[2] % (CLOUD_SERVICE, name)
             commit_azure_log(experiment_id,
                              ALOperation.CREATE_CLOUD_SERVICE,
                              ALStatus.FAIL, m, 2)
             log.error(m)
             return False
         # delete old azure cloud service in database, cascade delete old azure deployment,
         # old azure virtual machine and old azure end point
         delete_azure_cloud_service(name)
         try:
             self.service.create_hosted_service(name=name,
                                                label=label,
                                                location=location)
         except Exception as e:
             m = self.CREATE_CLOUD_SERVICE_ERROR[0] % (CLOUD_SERVICE, name,
                                                       e.message)
             commit_azure_log(experiment_id,
                              ALOperation.CREATE_CLOUD_SERVICE,
                              ALStatus.FAIL, m, 0)
             log.error(e)
             return False
         # make sure cloud service is created
         if not self.service.cloud_service_exists(name):
             m = self.CREATE_CLOUD_SERVICE_ERROR[3] % (CLOUD_SERVICE, name)
             commit_azure_log(experiment_id,
                              ALOperation.CREATE_CLOUD_SERVICE,
                              ALStatus.FAIL, m, 3)
             log.error(m)
             return False
         else:
             m = self.CREATE_CLOUD_SERVICE_INFO[0] % (CLOUD_SERVICE, name)
             commit_azure_cloud_service(name, label, location,
                                        ACSStatus.CREATED, experiment_id)
             commit_azure_log(experiment_id,
                              ALOperation.CREATE_CLOUD_SERVICE,
                              ALStatus.END, m, 0)
             log.debug(m)
     else:
         # check whether cloud service created by azure formation before
         if contain_azure_cloud_service(name):
             m = self.CREATE_CLOUD_SERVICE_INFO[1] % (CLOUD_SERVICE, name,
                                                      AZURE_FORMATION)
             commit_azure_log(experiment_id,
                              ALOperation.CREATE_CLOUD_SERVICE,
                              ALStatus.END, m, 1)
         else:
             m = self.CREATE_CLOUD_SERVICE_INFO[2] % (CLOUD_SERVICE, name,
                                                      AZURE_FORMATION)
             commit_azure_cloud_service(name, label, location,
                                        ACSStatus.CREATED, experiment_id)
             commit_azure_log(experiment_id,
                              ALOperation.CREATE_CLOUD_SERVICE,
                              ALStatus.END, m, 2)
         log.debug(m)
     # create virtual machine
     run_job(MDL_CLS_FUNC[5], (self.azure_key_id, ),
             (experiment_id, template_unit))
     return True
Ejemplo n.º 8
0
 def start_virtual_machine_async_false(self, experiment_id, template_unit):
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     m = self.START_VIRTUAL_MACHINE_ERROR[1] % (VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.FAIL, 1)
     log.error(m)
Ejemplo n.º 9
0
 def stop_virtual_machine(self, experiment_id, template_unit, action):
     """
     0. Prerequisites: a. virtual machine exist in both azure and database
                       b. input parameters are correct
     :param experiment_id:
     :param template_unit:
     :param action: AVMStatus.STOPPED or AVMStatus.STOPPED_DEALLOCATED
     :return:
     """
     commit_azure_log(experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.START)
     # need_status: AVMStatus.STOPPED_VM or AVMStatus.STOPPED_DEALLOCATED
     need_status = AVMStatus.STOPPED_VM if action == AVMStatus.STOPPED else AVMStatus.STOPPED_DEALLOCATED
     cloud_service_name = template_unit.get_cloud_service_name()
     deployment_slot = template_unit.get_deployment_slot()
     deployment_name = self.service.get_deployment_name(cloud_service_name, deployment_slot)
     deployment = self.service.get_deployment_by_name(cloud_service_name, deployment_name)
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     now_status = self.service.get_virtual_machine_instance_status(deployment, virtual_machine_name)
     if need_status == AVMStatus.STOPPED_VM and now_status == AVMStatus.STOPPED_DEALLOCATED:
         m = self.STOP_VIRTUAL_MACHINE_ERROR[1] % (VIRTUAL_MACHINE,
                                                   virtual_machine_name,
                                                   AVMStatus.STOPPED_VM,
                                                   AVMStatus.STOPPED_DEALLOCATED)
         commit_azure_log(experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.FAIL, m, 1)
         log.error(m)
         return False
     elif need_status == now_status:
         db_status = get_azure_virtual_machine_status(cloud_service_name, deployment_name, virtual_machine_name)
         if db_status == need_status:
             m = self.STOP_VIRTUAL_MACHINE_INFO[1] % (VIRTUAL_MACHINE,
                                                      virtual_machine_name,
                                                      need_status,
                                                      AZURE_FORMATION)
             commit_azure_log(experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.END, m, 1)
         else:
             m = self.STOP_VIRTUAL_MACHINE_INFO[2] % (VIRTUAL_MACHINE,
                                                      virtual_machine_name,
                                                      need_status,
                                                      AZURE_FORMATION)
             self.__stop_virtual_machine_helper(experiment_id, template_unit, need_status)
             commit_azure_log(experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.END, m, 2)
         log.debug(m)
     else:
         try:
             result = self.service.stop_virtual_machine(cloud_service_name,
                                                        deployment_name,
                                                        virtual_machine_name,
                                                        action)
         except Exception as e:
             m = self.STOP_VIRTUAL_MACHINE_ERROR[0] % (VIRTUAL_MACHINE, virtual_machine_name, e.message)
             commit_azure_log(experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.FAIL, 0)
             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[18], (self.azure_key_id, ), (experiment_id, template_unit, need_status),
                  MDL_CLS_FUNC[19], (self.azure_key_id, ), (experiment_id, template_unit, need_status)))
     return True
Ejemplo n.º 10
0
 def create_virtual_machine_async_false_2(self, experiment_id, template_unit):
     virtual_machine_name = self.VIRTUAL_MACHINE_NAME_BASE % (template_unit.get_virtual_machine_name(),
                                                              experiment_id)
     m = self.CREATE_VIRTUAL_MACHINE_ERROR[3] % (VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 3)
     log.error(m)
Ejemplo n.º 11
0
 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] % (DEPLOYMENT, deployment_slot)
         commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 1)
         log.error(m)
         m = self.CREATE_VIRTUAL_MACHINE_ERROR[1] % (VIRTUAL_MACHINE, virtual_machine_name)
         commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 1)
         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] % (DEPLOYMENT, deployment_name, AZURE_FORMATION)
             commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 1)
         else:
             m = self.CREATE_DEPLOYMENT_INFO[2] % (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)
         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] % (VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION)
                 commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.END, m, 1)
                 log.debug(m)
             else:
                 m = self.CREATE_VIRTUAL_MACHINE_ERROR[4] % (VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION)
                 commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 4)
                 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] % (VIRTUAL_MACHINE, virtual_machine_name, e.message)
                 commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 0)
                 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] % (DEPLOYMENT, deployment_slot, e.message)
             commit_azure_log(experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 0)
             m = self.CREATE_VIRTUAL_MACHINE_ERROR[0] % (VIRTUAL_MACHINE, virtual_machine_name, e.message)
             commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 0)
             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