def create_storage_account_async_true(self, context):
     name = context.template_unit.get_storage_account_name()
     description = context.template_unit.get_storage_account_description()
     label = context.template_unit.get_storage_account_label()
     location = context.template_unit.get_storage_account_location()
     # make sure storage account exist
     if not self.azure_adapter.storage_account_exists(
             context.azure_key_id, name):
         m = '%s [%s] created but not exist' % (
             AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, name)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL,
                          m, 4)
         self.log.error(m)
     else:
         m = '%s [%s] created' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, name)
         commit_azure_storage_account(name, description, label, location,
                                      ASAStatus.ONLINE,
                                      context.experiment_id)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.END,
                          m, 0)
         self.log.debug(m)
         # create cloud service
         self.scheduler.add_once(feature='azure_cloud_service',
                                 method='create_cloud_service',
                                 context=context,
                                 seconds=3)
 def __create_storage_account_in_azure_service(self, context, args_context):
     try:
         commit_azure_log(args_context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT,
                          ALStatus.START)
         result = self.azure_adapter.create_storage_account(
             args_context.azure_key_id, args_context.name,
             args_context.description, args_context.label,
             args_context.location)
     except Exception as e:
         m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT,
                             args_context.name, e.message)
         commit_azure_log(args_context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL,
                          m, 0)
         self.log.error(e)
         raise InternalServerError(
             "azure service raised a exception when create storage account")
     # query async operation status
     query_context = Context(
         request_id=result.id,
         azure_key_id=context.azure_key_id,
         feature='azure_storage_account_service',
         true_method='create_storage_account_async_true',
         false_method='create_storage_account_async_false',
         method_args_context=context)
     self.scheduler.add_once(feature='azure_adapter',
                             method='query_async_operation_status',
                             context=query_context,
                             seconds=3)
 def create_virtual_machine_dm_true(self, context):
     cloud_service_name = context.template_unit.get_cloud_service_name()
     deployment_slot = context.template_unit.get_deployment_slot()
     deployment_name = context.template_unit.get_deployment_name()
     virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
     m = '%s [%s] created' % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot)
     commit_azure_deployment(deployment_name,
                             deployment_slot,
                             ADStatus.RUNNING,
                             cloud_service_name,
                             context.experiment_id)
     commit_azure_log(context.experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 0)
     self.log.debug(m)
     # query virtual machine status
     args_context = Context(
         cloud_service_name=cloud_service_name,
         deployment_name=deployment_name,
         virtual_machine_name=virtual_machine_name,
         status=AVMStatus.READY_ROLE,
         feature='azure_vm_service',
         true_method='create_virtual_machine_async_true_2',
         method_args_context=context
     )
     self.scheduler.add_once(feature='azure_service',
                             method='query_virtual_machine_status',
                             context=args_context,
                             seconds=3)
 def create_virtual_machine_dm_true(self, context):
     cloud_service_name = context.template_unit.get_cloud_service_name()
     deployment_slot = context.template_unit.get_deployment_slot()
     deployment_name = context.template_unit.get_deployment_name()
     virtual_machine_name = '%s-%d' % (
         context.template_unit.get_virtual_machine_name(),
         context.experiment_id)
     m = '%s [%s] created' % (AZURE_RESOURCE_TYPE.DEPLOYMENT,
                              deployment_slot)
     commit_azure_deployment(deployment_name, deployment_slot,
                             ADStatus.RUNNING, cloud_service_name,
                             context.experiment_id)
     commit_azure_log(context.experiment_id, ALOperation.CREATE_DEPLOYMENT,
                      ALStatus.END, m, 0)
     self.log.debug(m)
     # query virtual machine status
     args_context = Context(
         cloud_service_name=cloud_service_name,
         deployment_name=deployment_name,
         virtual_machine_name=virtual_machine_name,
         status=AVMStatus.READY_ROLE,
         feature='azure_vm_service',
         true_method='create_virtual_machine_async_true_2',
         method_args_context=context)
     self.scheduler.add_once(feature='azure_service',
                             method='query_virtual_machine_status',
                             context=args_context,
                             seconds=3)
 def __create_storage_account_in_azure_service(self, context, args_context):
     try:
         commit_azure_log(args_context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.START)
         result = self.azure_adapter.create_storage_account(args_context.azure_key_id,
                                                            args_context.name,
                                                            args_context.description,
                                                            args_context.label,
                                                            args_context.location)
     except Exception as e:
         m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, args_context.name, e.message)
         commit_azure_log(args_context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL, m, 0)
         self.log.error(e)
         raise InternalServerError("azure service raised a exception when create storage account")
     # query async operation status
     query_context = Context(
         request_id=result.id,
         azure_key_id=context.azure_key_id,
         feature='azure_storage_account_service',
         true_method='create_storage_account_async_true',
         false_method='create_storage_account_async_false',
         method_args_context=context
     )
     self.scheduler.add_once(feature='azure_adapter',
                             method='query_async_operation_status',
                             context=query_context,
                             seconds=3)
 def __azure_service_create_vm(self, deployment_name, context):
     commit_azure_log(context.experiment_id,
                      ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.START)
     try:
         result = self.azure_adapter.add_virtual_machine(
             context.cloud_service_name, deployment_name,
             context.virtual_machine_name, context.system_config,
             context.os_virtual_hard_disk, context.network_config,
             context.virtual_machine_size, context.vm_image_name)
     except Exception as e:
         m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                             context.virtual_machine_name, e.message)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL,
                          m, 0)
         self.log.error(e)
         raise InternalServerError("Azure service create vm failed")
     # query async operation status
     query_context = Context(
         request_id=result.id,
         azure_key_id=context.azure_key_id,
         feature='azure_vm_service',
         true_method='create_virtual_machine_async_true_1',
         false_method='create_virtual_machine_async_false_1',
         method_args_context=self.__generate_base_context(
             context.azure_key_id, context.experiment_id,
             context.template_unit))
     self.scheduler.add_once(feature='azure_service',
                             method='query_async_operation_status',
                             context=query_context,
                             seconds=3)
 def __azure_service_create_vm(self, deployment_name, context):
     commit_azure_log(context.experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.START)
     try:
         result = self.azure_adapter.add_virtual_machine(context.cloud_service_name,
                                                         deployment_name,
                                                         context.virtual_machine_name,
                                                         context.system_config,
                                                         context.os_virtual_hard_disk,
                                                         context.network_config,
                                                         context.virtual_machine_size,
                                                         context.vm_image_name)
     except Exception as e:
         m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, context.virtual_machine_name, e.message)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 0)
         self.log.error(e)
         raise InternalServerError("Azure service create vm failed")
     # query async operation status
     query_context = Context(
         request_id=result.id,
         azure_key_id=context.azure_key_id,
         feature='azure_vm_service',
         true_method='create_virtual_machine_async_true_1',
         false_method='create_virtual_machine_async_false_1',
         method_args_context=self.__generate_base_context(context.azure_key_id, context.experiment_id,
                                                          context.template_unit)
     )
     self.scheduler.add_once(feature='azure_service',
                             method='query_async_operation_status',
                             context=query_context,
                             seconds=3)
 def create_storage_account_async_false(self, context):
     name = context.template_unit.get_storage_account_name()
     m = '%s [%s] wait for async fail' % (
         AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, name)
     commit_azure_log(context.experiment_id,
                      ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL, m,
                      3)
     self.log.error(m)
 def create_virtual_machine_async_false_3(self, context):
     deployment_slot = context.template_unit.get_deployment_slot()
     virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
     m = '%s [%s] wait for async fail' % (AZURE_RESOURCE_TYPE.DEPLOYMENT, deployment_slot)
     commit_azure_log(context.experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 2)
     self.log.error(m)
     m = '%s [%s] wait for async fail' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(context.experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 2)
     self.log.error(m)
示例#10
0
 def start_virtual_machine_async_false(self, context):
     virtual_machine_name = '%s-%d' % (
         context.template_unit.get_virtual_machine_name(),
         context.experiment_id)
     m = '%s [%s] wait for async fail' % (
         AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(context.experiment_id,
                      ALOperation.START_VIRTUAL_MACHINE, ALStatus.FAIL, 1)
     self.log.error(m)
示例#11
0
 def __check_available_cores(self, context):
     if self.subscription.get_available_core_count(context.azure_key_id) < self.SIZE_CORE_MAP[context.virtual_machine_size.lower()]:
         m = '%s [%s] subscription not enough' % (AZURE_RESOURCE_TYPE.DEPLOYMENT, context.deployment_slot)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m, 1)
         self.log.error(m)
         m = '%s [%s] subscription not enough' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, context.virtual_machine_name)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 1)
         self.log.error(m)
         raise BadRequest("available cores is not enough")
示例#12
0
 def create_virtual_machine_async_false_2(self, context):
     virtual_machine_name = '%s-%d' % (
         context.template_unit.get_virtual_machine_name(),
         context.experiment_id)
     m = '%s [%s] wait for async fail (update network config)' % (
         AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(context.experiment_id,
                      ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m,
                      3)
     self.log.error(m)
示例#13
0
 def __create_virtual_machine_helper(self, azure_key_id, experiment_id, template_unit):
     cloud_service_name = template_unit.get_cloud_service_name()
     deployment_slot = template_unit.get_deployment_slot()
     deployment_name = self.azure_adapter.get_deployment_name(azure_key_id, cloud_service_name, deployment_slot)
     virtual_machine_name = '%s-%d' % (template_unit.get_virtual_machine_name(), experiment_id)
     public_ip = self.azure_adapter.get_virtual_machine_public_ip(azure_key_id,
                                                                  cloud_service_name,
                                                                  deployment_name,
                                                                  virtual_machine_name)
     remote_port_name = template_unit.get_remote_port_name()
     remote_port = self.azure_adapter.get_virtual_machine_public_endpoint(azure_key_id,
                                                                          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(VE_PROVIDER.AZURE,
                                                      virtual_machine_name,
                                                      template_unit.get_image_name(),
                                                      VEStatus.RUNNING,
                                                      VERemoteProvider.Guacamole,
                                                      json.dumps(remote_paras),
                                                      experiment_id)
     dns = self.azure_adapter.get_deployment_dns(azure_key_id, cloud_service_name, deployment_slot)
     private_ip = self.azure_adapter.get_virtual_machine_private_ip(azure_key_id,
                                                                    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.azure_adapter.get_virtual_machine_network_config(azure_key_id,
                                                                            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 = '%s [%s] created' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.END, m, 0)
     self.log.debug(m)
示例#14
0
    def stop_virtual_machine_vm_true(self, context):
        """

        :param context : contains azure_key_id, experiment_id, template_unit, need_status
        :return:
        """
        virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
        self.__stop_virtual_machine_helper(context.experiment_id, context.template_unit, context.need_status)
        m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, context.need_status)
        commit_azure_log(context.experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.END, m, 0)
        self.log.debug(m)
示例#15
0
    def stop_virtual_machine_async_false(self, context):
        """

        :param context : contains azure_key_id, experiment_id, template_unit, need_status
        :return:
        """
        virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
        m = '%s [%s] %s wait for async fail' % (
            AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, context.need_status)
        commit_azure_log(context.experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.FAIL, 2)
        self.log.error(m)
示例#16
0
 def start_virtual_machine_vm_true(self, context):
     virtual_machine_name = '%s-%d' % (
         context.template_unit.get_virtual_machine_name(),
         context.experiment_id)
     self.__start_virtual_machine_helper(context.experiment_id,
                                         context.template_unit)
     m = '%s [%s] started' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                              virtual_machine_name)
     commit_azure_log(context.experiment_id,
                      ALOperation.START_VIRTUAL_MACHINE, ALStatus.END, m, 0)
     self.log.debug(m)
 def __create_cloud_service(self, context):
     try:
         commit_azure_log(context.experiment_id, ALOperation.CREATE_CLOUD_SERVICE, ALStatus.START)
         self.azure_adapter.create_hosted_service(azure_key_id=context.azure_key_id,
                                                  name=context.name,
                                                  label=context.label,
                                                  location=context.location)
     except Exception as e:
         m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name, e.message)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL, m, 0)
         self.log.error(e)
         raise InternalServerError("azure service raised a exception when create cloud service")
 def __check_creation_requirement(self, args_context):
     # avoid name already taken by other azure subscription
     if not self.azure_adapter.check_storage_account_name_availability(args_context.azure_key_id,
                                                                       args_context.name).result:
         m = '%s [%s] name not available' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, args_context.name)
         commit_azure_log(args_context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL, m, 1)
         self.log.error(m)
         raise BadRequest("storage account name not available")
     # avoid no available subscription remained
     if self.subscription.get_available_storage_account_count(args_context.azure_key_id) < 1:
         m = '%s [%s] subscription not enough' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, args_context.name)
         commit_azure_log(args_context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL, m, 2)
         self.log.error(m)
         raise BadRequest("storage account subscription not enough")
 def __check_creation_requirement(self, context):
     # avoid name already taken by other azure subscription
     if not self.azure_adapter.check_hosted_service_name_availability(context.azure_key_id, context.name).result:
         m = '%s [%s] name not available' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL, m, 1)
         self.log.error(m)
         raise BadRequest("cloud service name not available")
     # avoid no available subscription remained
     if self.subscription.get_available_cloud_service_count(context.azure_key_id) < 1:
         m = '%s [%s] subscription not enough' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL, m, 2)
         self.log.error(m)
         raise BadRequest("cloud service subscription not enough")
     delete_azure_cloud_service(context.name)
示例#20
0
    def stop_virtual_machine_async_false(self, context):
        """

        :param context : contains azure_key_id, experiment_id, template_unit, need_status
        :return:
        """
        virtual_machine_name = '%s-%d' % (
            context.template_unit.get_virtual_machine_name(),
            context.experiment_id)
        m = '%s [%s] %s wait for async fail' % (
            AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name,
            context.need_status)
        commit_azure_log(context.experiment_id,
                         ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.FAIL, 2)
        self.log.error(m)
示例#21
0
 def __check_vm_exist_in_db(self, context):
     if contain_azure_virtual_machine(context.cloud_service_name, context.deployment_name,
                                      context.virtual_machine_name):
         m = '%s [%s] exist and created by %s before' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                                         context.virtual_machine_name,
                                                         AZURE_FORMATION)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.END, m, 1)
         self.log.debug(m)
     else:
         m = '%s [%s] exist but not created by %s before' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                                             context.virtual_machine_name,
                                                             AZURE_FORMATION)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 4)
         self.log.error(m)
         raise BadRequest("VM already exist")
 def __check_cloud_service_in_db(self, context):
     # check whether cloud service created by azure formation before
     if contain_azure_cloud_service(context.name):
         m = '%s [%s] exist and created by %s before' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE,
                                                         context.name,
                                                         AZURE_FORMATION)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_CLOUD_SERVICE, ALStatus.END, m, 1)
     else:
         m = '%s [%s] exist but not created by %s before' % (
         AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name, AZURE_FORMATION)
         commit_azure_cloud_service(context.name, context.label,
                                    context.location,
                                    ACSStatus.CREATED,
                                    context.experiment_id)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_CLOUD_SERVICE, ALStatus.END, m, 2)
     self.log.debug(m)
示例#23
0
 def create_virtual_machine_async_false_3(self, context):
     deployment_slot = context.template_unit.get_deployment_slot()
     virtual_machine_name = '%s-%d' % (
         context.template_unit.get_virtual_machine_name(),
         context.experiment_id)
     m = '%s [%s] wait for async fail' % (AZURE_RESOURCE_TYPE.DEPLOYMENT,
                                          deployment_slot)
     commit_azure_log(context.experiment_id, ALOperation.CREATE_DEPLOYMENT,
                      ALStatus.FAIL, m, 2)
     self.log.error(m)
     m = '%s [%s] wait for async fail' % (
         AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(context.experiment_id,
                      ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m,
                      2)
     self.log.error(m)
示例#24
0
    def stop_virtual_machine_vm_true(self, context):
        """

        :param context : contains azure_key_id, experiment_id, template_unit, need_status
        :return:
        """
        virtual_machine_name = '%s-%d' % (
            context.template_unit.get_virtual_machine_name(),
            context.experiment_id)
        self.__stop_virtual_machine_helper(context.experiment_id,
                                           context.template_unit,
                                           context.need_status)
        m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                            virtual_machine_name, context.need_status)
        commit_azure_log(context.experiment_id,
                         ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.END, m, 0)
        self.log.debug(m)
 def __check_exist_after_creation(self, context):
     if not self.azure_adapter.cloud_service_exists(context.azure_key_id, context.name):
         m = '%s [%s] created but not exist' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL, m, 3)
         self.log.error(m)
         return False
     else:
         m = '%s [%s] created' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name)
         commit_azure_cloud_service(context.name,
                                    context.label,
                                    context.location,
                                    ACSStatus.CREATED,
                                    context.experiment_id)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE,
                          ALStatus.END, m, 0)
         self.log.debug(m)
示例#26
0
 def __create_virtual_machine_helper(self, azure_key_id, experiment_id,
                                     template_unit):
     cloud_service_name = template_unit.get_cloud_service_name()
     deployment_slot = template_unit.get_deployment_slot()
     deployment_name = self.azure_adapter.get_deployment_name(
         azure_key_id, cloud_service_name, deployment_slot)
     virtual_machine_name = '%s-%d' % (
         template_unit.get_virtual_machine_name(), experiment_id)
     public_ip = self.azure_adapter.get_virtual_machine_public_ip(
         azure_key_id, cloud_service_name, deployment_name,
         virtual_machine_name)
     remote_port_name = template_unit.get_remote_port_name()
     remote_port = self.azure_adapter.get_virtual_machine_public_endpoint(
         azure_key_id, 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(
         VE_PROVIDER.AZURE, virtual_machine_name,
         template_unit.get_image_name(),
         VEStatus.RUNNING, VERemoteProvider.Guacamole,
         json.dumps(remote_paras), experiment_id)
     dns = self.azure_adapter.get_deployment_dns(azure_key_id,
                                                 cloud_service_name,
                                                 deployment_slot)
     private_ip = self.azure_adapter.get_virtual_machine_private_ip(
         azure_key_id, 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.azure_adapter.get_virtual_machine_network_config(
         azure_key_id, 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 = '%s [%s] created' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                              virtual_machine_name)
     commit_azure_log(experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE,
                      ALStatus.END, m, 0)
     self.log.debug(m)
示例#27
0
 def __check_available_cores(self, context):
     if self.subscription.get_available_core_count(
             context.azure_key_id) < self.SIZE_CORE_MAP[
                 context.virtual_machine_size.lower()]:
         m = '%s [%s] subscription not enough' % (
             AZURE_RESOURCE_TYPE.DEPLOYMENT, context.deployment_slot)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_DEPLOYMENT, ALStatus.FAIL, m,
                          1)
         self.log.error(m)
         m = '%s [%s] subscription not enough' % (
             AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
             context.virtual_machine_name)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL,
                          m, 1)
         self.log.error(m)
         raise BadRequest("available cores is not enough")
 def __create_cloud_service(self, context):
     try:
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.START)
         self.azure_adapter.create_hosted_service(
             azure_key_id=context.azure_key_id,
             name=context.name,
             label=context.label,
             location=context.location)
     except Exception as e:
         m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE,
                             context.name, e.message)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL,
                          m, 0)
         self.log.error(e)
         raise InternalServerError(
             "azure service raised a exception when create cloud service")
示例#29
0
 def __check_deployment_in_db(self, context):
     # use deployment name from azure subscription
     if contain_azure_deployment(context.cloud_service_name, context.deployment_slot):
         m = '%s [%s] exist and created by %s before' % (AZURE_RESOURCE_TYPE.DEPLOYMENT,
                                                         context.deployment_name,
                                                         AZURE_FORMATION)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 1)
     else:
         m = '%s [%s] exist but not created by %s before' % (AZURE_RESOURCE_TYPE.DEPLOYMENT,
                                                             context.deployment_name,
                                                             AZURE_FORMATION)
         commit_azure_deployment(context.deployment_name,
                                 context.deployment_slot,
                                 ADStatus.RUNNING,
                                 context.cloud_service_name,
                                 context.experiment_id)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 2)
     self.log.debug(m)
示例#30
0
    def start_virtual_machine(self, context):
        """
        0. Prerequisites: a. virtual machine exist in both azure and database
                          b. input parameters are correct
        :param context : contains azure_key_id, experiment_id, template_unit

        :return:
        """
        commit_azure_log(context.experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.START)
        cloud_service_name = context.template_unit.get_cloud_service_name()
        deployment_slot = context.template_unit.get_deployment_slot()
        deployment_name = self.azure_adapter.get_deployment_name(cloud_service_name, deployment_slot)
        deployment = self.azure_adapter.get_deployment_by_name(cloud_service_name, deployment_name)
        virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
        status = self.azure_adapter.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 = '%s [%s] started by %s before' % (
                    AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION)
                commit_azure_log(context.experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.END, m, 1)
            else:
                m = '%s [%s] started but not by %s before' % (
                    AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, AZURE_FORMATION)
                self.__start_virtual_machine_helper(context.experiment_id, context.template_unit)
                commit_azure_log(context.experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.END, m, 2)
            self.log.debug(m)
        else:
            try:
                result = self.azure_adapter.start_virtual_machine(cloud_service_name,
                                                                  deployment_name,
                                                                  virtual_machine_name)
            except Exception as e:
                m = '%s [%s] %s' % (
                    AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, e.message)
                commit_azure_log(context.experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.FAIL, 0)
                self.log.error(e)
                self.log.error(m)
                return False

            # query async operation status
            query_context = Context(
                request_id=result.id,
                azure_key_id=context.azure_key_id,
                feature='azure_vm_service',
                true_method='start_virtual_machine_async_true',
                false_method='start_virtual_machine_async_false',
                method_args_context=context
            )
            self.scheduler.add_once(feature='azure_service',
                                    method='query_async_operation_status',
                                    context=query_context,
                                    seconds=3)
        return True
 def __check_storage_account_in_db(self, args_context):
     # check whether storage account created by azure formation before
     if contain_azure_storage_account(args_context.name):
         m = '%s [%s] exist and created by %s before' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT,
                                                         args_context.name,
                                                         AZURE_FORMATION)
         commit_azure_log(args_context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.END, m, 1)
     else:
         m = '%s [%s] exist but not created by %s before' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT,
                                                             args_context.name,
                                                             AZURE_FORMATION)
         commit_azure_storage_account(args_context.name,
                                      args_context.description,
                                      args_context.label,
                                      args_context.location,
                                      ASAStatus.ONLINE,
                                      args_context.experiment_id)
         commit_azure_log(args_context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.END, m, 2)
     self.log.debug(m)
示例#32
0
 def __check_deployment_in_db(self, context):
     # use deployment name from azure subscription
     if contain_azure_deployment(context.cloud_service_name,
                                 context.deployment_slot):
         m = '%s [%s] exist and created by %s before' % (
             AZURE_RESOURCE_TYPE.DEPLOYMENT, context.deployment_name,
             AZURE_FORMATION)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 1)
     else:
         m = '%s [%s] exist but not created by %s before' % (
             AZURE_RESOURCE_TYPE.DEPLOYMENT, context.deployment_name,
             AZURE_FORMATION)
         commit_azure_deployment(context.deployment_name,
                                 context.deployment_slot, ADStatus.RUNNING,
                                 context.cloud_service_name,
                                 context.experiment_id)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_DEPLOYMENT, ALStatus.END, m, 2)
     self.log.debug(m)
 def __check_exist_after_creation(self, context):
     if not self.azure_adapter.cloud_service_exists(context.azure_key_id,
                                                    context.name):
         m = '%s [%s] created but not exist' % (
             AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL,
                          m, 3)
         self.log.error(m)
         return False
     else:
         m = '%s [%s] created' % (AZURE_RESOURCE_TYPE.CLOUD_SERVICE,
                                  context.name)
         commit_azure_cloud_service(context.name, context.label,
                                    context.location, ACSStatus.CREATED,
                                    context.experiment_id)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.END, m,
                          0)
         self.log.debug(m)
 def create_storage_account_async_true(self, context):
     name = context.template_unit.get_storage_account_name()
     description = context.template_unit.get_storage_account_description()
     label = context.template_unit.get_storage_account_label()
     location = context.template_unit.get_storage_account_location()
     # make sure storage account exist
     if not self.azure_adapter.storage_account_exists(context.azure_key_id, name):
         m = '%s [%s] created but not exist' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, name)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL, m, 4)
         self.log.error(m)
     else:
         m = '%s [%s] created' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, name)
         commit_azure_storage_account(name, description, label, location, ASAStatus.ONLINE, context.experiment_id)
         commit_azure_log(context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.END, m, 0)
         self.log.debug(m)
         # create cloud service
         self.scheduler.add_once(feature='azure_cloud_service',
                                 method='create_cloud_service',
                                 context=context,
                                 seconds=3)
 def __check_cloud_service_in_db(self, context):
     # check whether cloud service created by azure formation before
     if contain_azure_cloud_service(context.name):
         m = '%s [%s] exist and created by %s before' % (
             AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name,
             AZURE_FORMATION)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.END, m,
                          1)
     else:
         m = '%s [%s] exist but not created by %s before' % (
             AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name,
             AZURE_FORMATION)
         commit_azure_cloud_service(context.name, context.label,
                                    context.location, ACSStatus.CREATED,
                                    context.experiment_id)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.END, m,
                          2)
     self.log.debug(m)
示例#36
0
 def __check_vm_exist_in_db(self, context):
     if contain_azure_virtual_machine(context.cloud_service_name,
                                      context.deployment_name,
                                      context.virtual_machine_name):
         m = '%s [%s] exist and created by %s before' % (
             AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
             context.virtual_machine_name, AZURE_FORMATION)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.END,
                          m, 1)
         self.log.debug(m)
     else:
         m = '%s [%s] exist but not created by %s before' % (
             AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
             context.virtual_machine_name, AZURE_FORMATION)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL,
                          m, 4)
         self.log.error(m)
         raise BadRequest("VM already exist")
 def __check_creation_requirement(self, args_context):
     # avoid name already taken by other azure subscription
     if not self.azure_adapter.check_storage_account_name_availability(
             args_context.azure_key_id, args_context.name).result:
         m = '%s [%s] name not available' % (
             AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, args_context.name)
         commit_azure_log(args_context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL,
                          m, 1)
         self.log.error(m)
         raise BadRequest("storage account name not available")
     # avoid no available subscription remained
     if self.subscription.get_available_storage_account_count(
             args_context.azure_key_id) < 1:
         m = '%s [%s] subscription not enough' % (
             AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, args_context.name)
         commit_azure_log(args_context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL,
                          m, 2)
         self.log.error(m)
         raise BadRequest("storage account subscription not enough")
 def __check_creation_requirement(self, context):
     # avoid name already taken by other azure subscription
     if not self.azure_adapter.check_hosted_service_name_availability(
             context.azure_key_id, context.name).result:
         m = '%s [%s] name not available' % (
             AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL,
                          m, 1)
         self.log.error(m)
         raise BadRequest("cloud service name not available")
     # avoid no available subscription remained
     if self.subscription.get_available_cloud_service_count(
             context.azure_key_id) < 1:
         m = '%s [%s] subscription not enough' % (
             AZURE_RESOURCE_TYPE.CLOUD_SERVICE, context.name)
         commit_azure_log(context.experiment_id,
                          ALOperation.CREATE_CLOUD_SERVICE, ALStatus.FAIL,
                          m, 2)
         self.log.error(m)
         raise BadRequest("cloud service subscription not enough")
     delete_azure_cloud_service(context.name)
 def __check_storage_account_in_db(self, args_context):
     # check whether storage account created by azure formation before
     if contain_azure_storage_account(args_context.name):
         m = '%s [%s] exist and created by %s before' % (
             AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, args_context.name,
             AZURE_FORMATION)
         commit_azure_log(args_context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.END,
                          m, 1)
     else:
         m = '%s [%s] exist but not created by %s before' % (
             AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, args_context.name,
             AZURE_FORMATION)
         commit_azure_storage_account(args_context.name,
                                      args_context.description,
                                      args_context.label,
                                      args_context.location,
                                      ASAStatus.ONLINE,
                                      args_context.experiment_id)
         commit_azure_log(args_context.experiment_id,
                          ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.END,
                          m, 2)
     self.log.debug(m)
 def create_storage_account_async_false(self, context):
     name = context.template_unit.get_storage_account_name()
     m = '%s [%s] wait for async fail' % (AZURE_RESOURCE_TYPE.STORAGE_ACCOUNT, name)
     commit_azure_log(context.experiment_id, ALOperation.CREATE_STORAGE_ACCOUNT, ALStatus.FAIL, m, 3)
     self.log.error(m)
示例#41
0
 def start_virtual_machine_async_false(self, context):
     virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
     m = '%s [%s] wait for async fail' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(context.experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.FAIL, 1)
     self.log.error(m)
示例#42
0
 def stop_virtual_machine(self, context):
     """
     0. Prerequisites: a. virtual machine exist in both azure and database
                       b. input parameters are correct
     :param context :
                     azure_key_id,
                     experiment_id,
                     template_unit,
                     action: AVMStatus.STOPPED or AVMStatus.STOPPED_DEALLOCATED
     :return:
     """
     commit_azure_log(context.experiment_id,
                      ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.START)
     # need_status: AVMStatus.STOPPED_VM or AVMStatus.STOPPED_DEALLOCATED
     need_status = AVMStatus.STOPPED_VM if context.action == AVMStatus.STOPPED else AVMStatus.STOPPED_DEALLOCATED
     cloud_service_name = context.template_unit.get_cloud_service_name()
     deployment_slot = context.template_unit.get_deployment_slot()
     deployment_name = self.azure_adapter.get_deployment_name(
         cloud_service_name, deployment_slot)
     deployment = self.azure_adapter.get_deployment_by_name(
         cloud_service_name, deployment_name)
     virtual_machine_name = '%s-%d' % (
         context.template_unit.get_virtual_machine_name(),
         context.experiment_id)
     now_status = self.azure_adapter.get_virtual_machine_instance_status(
         deployment, virtual_machine_name)
     if need_status == AVMStatus.STOPPED_VM and now_status == AVMStatus.STOPPED_DEALLOCATED:
         m = '%s [%s] need status %s but now status %s' % (
             AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name,
             AVMStatus.STOPPED_VM, AVMStatus.STOPPED_DEALLOCATED)
         commit_azure_log(context.experiment_id,
                          ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.FAIL,
                          m, 1)
         self.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 = '%s [%s] %s and by %s before' % (
                 AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name,
                 need_status, AZURE_FORMATION)
             commit_azure_log(context.experiment_id,
                              ALOperation.STOP_VIRTUAL_MACHINE,
                              ALStatus.END, m, 1)
         else:
             m = '%s [%s] %s but not by %s before' % (
                 AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name,
                 need_status, AZURE_FORMATION)
             self.__stop_virtual_machine_helper(context.experiment_id,
                                                context.template_unit,
                                                need_status)
             commit_azure_log(context.experiment_id,
                              ALOperation.STOP_VIRTUAL_MACHINE,
                              ALStatus.END, m, 2)
         self.log.debug(m)
     else:
         try:
             result = self.azure_adapter.stop_virtual_machine(
                 cloud_service_name, deployment_name, virtual_machine_name,
                 context.action)
         except Exception as e:
             m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                 virtual_machine_name, e.message)
             commit_azure_log(context.experiment_id,
                              ALOperation.STOP_VIRTUAL_MACHINE,
                              ALStatus.FAIL, 0)
             self.log.error(m)
             self.log.error(e)
             return False
         # query async operation status
         method_args_context = self.__generate_base_context(
             context.azure_key_id, context.experiment_id,
             context.template_unit)
         method_args_context.need_status = need_status
         query_context = Context(
             request_id=result.id,
             azure_key_id=context.azure_key_id,
             feature='azure_vm_service',
             true_method='stop_virtual_machine_async_true',
             false_method='stop_virtual_machine_async_false',
             method_args_context=method_args_context)
         self.scheduler.add_once(feature='azure_service',
                                 method='query_async_operation_status',
                                 context=query_context,
                                 seconds=3)
     return True
示例#43
0
    def start_virtual_machine(self, context):
        """
        0. Prerequisites: a. virtual machine exist in both azure and database
                          b. input parameters are correct
        :param context : contains azure_key_id, experiment_id, template_unit

        :return:
        """
        commit_azure_log(context.experiment_id,
                         ALOperation.START_VIRTUAL_MACHINE, ALStatus.START)
        cloud_service_name = context.template_unit.get_cloud_service_name()
        deployment_slot = context.template_unit.get_deployment_slot()
        deployment_name = self.azure_adapter.get_deployment_name(
            cloud_service_name, deployment_slot)
        deployment = self.azure_adapter.get_deployment_by_name(
            cloud_service_name, deployment_name)
        virtual_machine_name = '%s-%d' % (
            context.template_unit.get_virtual_machine_name(),
            context.experiment_id)
        status = self.azure_adapter.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 = '%s [%s] started by %s before' % (
                    AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name,
                    AZURE_FORMATION)
                commit_azure_log(context.experiment_id,
                                 ALOperation.START_VIRTUAL_MACHINE,
                                 ALStatus.END, m, 1)
            else:
                m = '%s [%s] started but not by %s before' % (
                    AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name,
                    AZURE_FORMATION)
                self.__start_virtual_machine_helper(context.experiment_id,
                                                    context.template_unit)
                commit_azure_log(context.experiment_id,
                                 ALOperation.START_VIRTUAL_MACHINE,
                                 ALStatus.END, m, 2)
            self.log.debug(m)
        else:
            try:
                result = self.azure_adapter.start_virtual_machine(
                    cloud_service_name, deployment_name, virtual_machine_name)
            except Exception as e:
                m = '%s [%s] %s' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                    virtual_machine_name, e.message)
                commit_azure_log(context.experiment_id,
                                 ALOperation.START_VIRTUAL_MACHINE,
                                 ALStatus.FAIL, 0)
                self.log.error(e)
                self.log.error(m)
                return False

            # query async operation status
            query_context = Context(
                request_id=result.id,
                azure_key_id=context.azure_key_id,
                feature='azure_vm_service',
                true_method='start_virtual_machine_async_true',
                false_method='start_virtual_machine_async_false',
                method_args_context=context)
            self.scheduler.add_once(feature='azure_service',
                                    method='query_async_operation_status',
                                    context=query_context,
                                    seconds=3)
        return True
示例#44
0
 def start_virtual_machine_vm_true(self, context):
     virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
     self.__start_virtual_machine_helper(context.experiment_id, context.template_unit)
     m = '%s [%s] started' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name)
     commit_azure_log(context.experiment_id, ALOperation.START_VIRTUAL_MACHINE, ALStatus.END, m, 0)
     self.log.debug(m)
示例#45
0
 def stop_virtual_machine(self, context):
     """
     0. Prerequisites: a. virtual machine exist in both azure and database
                       b. input parameters are correct
     :param context :
                     azure_key_id,
                     experiment_id,
                     template_unit,
                     action: AVMStatus.STOPPED or AVMStatus.STOPPED_DEALLOCATED
     :return:
     """
     commit_azure_log(context.experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.START)
     # need_status: AVMStatus.STOPPED_VM or AVMStatus.STOPPED_DEALLOCATED
     need_status = AVMStatus.STOPPED_VM if context.action == AVMStatus.STOPPED else AVMStatus.STOPPED_DEALLOCATED
     cloud_service_name = context.template_unit.get_cloud_service_name()
     deployment_slot = context.template_unit.get_deployment_slot()
     deployment_name = self.azure_adapter.get_deployment_name(cloud_service_name, deployment_slot)
     deployment = self.azure_adapter.get_deployment_by_name(cloud_service_name, deployment_name)
     virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(),
                                       context.experiment_id)
     now_status = self.azure_adapter.get_virtual_machine_instance_status(deployment, virtual_machine_name)
     if need_status == AVMStatus.STOPPED_VM and now_status == AVMStatus.STOPPED_DEALLOCATED:
         m = '%s [%s] need status %s but now status %s' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                                           virtual_machine_name,
                                                           AVMStatus.STOPPED_VM,
                                                           AVMStatus.STOPPED_DEALLOCATED)
         commit_azure_log(context.experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.FAIL, m, 1)
         self.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 = '%s [%s] %s and by %s before' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                                  virtual_machine_name,
                                                  need_status,
                                                  AZURE_FORMATION)
             commit_azure_log(context.experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.END, m, 1)
         else:
             m = '%s [%s] %s but not by %s before' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                                      virtual_machine_name,
                                                      need_status,
                                                      AZURE_FORMATION)
             self.__stop_virtual_machine_helper(context.experiment_id, context.template_unit, need_status)
             commit_azure_log(context.experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.END, m, 2)
         self.log.debug(m)
     else:
         try:
             result = self.azure_adapter.stop_virtual_machine(cloud_service_name,
                                                              deployment_name,
                                                              virtual_machine_name,
                                                              context.action)
         except Exception as e:
             m = '%s [%s] %s' % (
                 AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE, virtual_machine_name, e.message)
             commit_azure_log(context.experiment_id, ALOperation.STOP_VIRTUAL_MACHINE, ALStatus.FAIL, 0)
             self.log.error(m)
             self.log.error(e)
             return False
         # query async operation status
         method_args_context = self.__generate_base_context(context.azure_key_id, context.experiment_id, context.template_unit)
         method_args_context.need_status = need_status
         query_context = Context(
             request_id=result.id,
             azure_key_id=context.azure_key_id,
             feature='azure_vm_service',
             true_method='stop_virtual_machine_async_true',
             false_method='stop_virtual_machine_async_false',
             method_args_context=method_args_context
         )
         self.scheduler.add_once(feature='azure_service',
                                 method='query_async_operation_status',
                                 context=query_context,
                                 seconds=3)
     return True
示例#46
0
 def create_virtual_machine_async_false_2(self, context):
     virtual_machine_name = '%s-%d' % (context.template_unit.get_virtual_machine_name(), context.experiment_id)
     m = '%s [%s] wait for async fail (update network config)' % (AZURE_RESOURCE_TYPE.VIRTUAL_MACHINE,
                                                                  virtual_machine_name)
     commit_azure_log(context.experiment_id, ALOperation.CREATE_VIRTUAL_MACHINE, ALStatus.FAIL, m, 3)
     self.log.error(m)