Ejemplo n.º 1
0
    def stop_virtual_machine(self, sctx):
        ctx = sctx.job_ctxs[sctx.current_job_index]
        adapter = VirtualMachineAdapter(ctx.subscription_id, ctx.pem_url, host=ctx.management_host)

        deployment_name = adapter.get_deployment_name(ctx.cloud_service_name, ctx.deployment_slot)
        now_status = adapter.get_virtual_machine_instance_status(
            ctx.cloud_service_name, ctx.deployment_slot, ctx.virtual_machine_name)

        if now_status is None:
            self.log.error(
                "azure virtual environment %d stop vm failed: cannot get status of vm %r" %
                (sctx.current_job_index, ctx.virtual_machine_name))
            self.__on_stop_virtual_machine_failed(sctx)
        elif now_status != AVMStatus.STOPPED_DEALLOCATED:
            try:
                req = adapter.stop_virtual_machine(
                    ctx.cloud_service_name, deployment_name, ctx.virtual_machine_name, AVMStatus.STOPPED_DEALLOCATED)
            except Exception as e:
                self.log.error(
                    "azure virtual environment %d stop vm failed: %r" %
                    (sctx.current_job_index, str(e.message)))
                self.__on_stop_virtual_machine_failed(sctx)
                return False

            ctx.request_id = req.request_id
            self.__wait_for_stop_virtual_machine(sctx)
        else:
            self.__stop_virtual_machine_done(sctx)
Ejemplo n.º 2
0
    def stop_virtual_machine(self, sctx):
        ctx = sctx.job_ctxs[sctx.current_job_index]
        adapter = VirtualMachineAdapter(ctx.subscription_id,
                                        ctx.pem_url,
                                        host=ctx.management_host)

        deployment_name = adapter.get_deployment_name(ctx.cloud_service_name,
                                                      ctx.deployment_slot)
        now_status = adapter.get_virtual_machine_instance_status(
            ctx.cloud_service_name, ctx.deployment_slot,
            ctx.virtual_machine_name)

        if now_status is None:
            self.log.error(
                "azure virtual environment %d stop vm failed: cannot get status of vm %r"
                % (sctx.current_job_index, ctx.virtual_machine_name))
            self.__on_stop_virtual_machine_failed(sctx)
        elif now_status != AVMStatus.STOPPED_DEALLOCATED:
            try:
                req = adapter.stop_virtual_machine(
                    ctx.cloud_service_name, deployment_name,
                    ctx.virtual_machine_name, AVMStatus.STOPPED_DEALLOCATED)
            except Exception as e:
                self.log.error(
                    "azure virtual environment %d stop vm failed: %r" %
                    (sctx.current_job_index, str(e.message)))
                self.__on_stop_virtual_machine_failed(sctx)
                return False

            ctx.request_id = req.request_id
            self.__wait_for_stop_virtual_machine(sctx)
        else:
            self.__stop_virtual_machine_done(sctx)
Ejemplo n.º 3
0
    def __setup_virtual_machine_with_deployment_existed(self, sctx):
        # get context from super context
        ctx = sctx.job_ctxs[sctx.current_job_index]

        adapter = VirtualMachineAdapter(ctx.subscription_id,
                                        ctx.pem_url,
                                        host=ctx.management_host)

        deployment_name = adapter.get_deployment_name(ctx.cloud_service_name,
                                                      ctx.deployment_slot)
        if not ctx.is_vm_image:
            network_config = get_network_config(
                ctx.raw_network_config,
                adapter.get_assigned_endpoints(ctx.cloud_service_name))
        else:
            network_config = None

        # add virtual machine to deployment
        try:
            # create if vm is not created
            if not adapter.virtual_machine_exists(ctx.cloud_service_name,
                                                  deployment_name,
                                                  ctx.virtual_machine_name):
                req = adapter.add_virtual_machine(
                    ctx.cloud_service_name,
                    deployment_name,
                    ctx.virtual_machine_name,
                    ctx.system_config,
                    ctx.os_virtual_hard_disk,
                    network_config=network_config,
                    role_size=ctx.virtual_machine_size,
                    vm_image_name=ctx.image_name if ctx.is_vm_image else None)
            # NOTE: if the vm is created, we think is well configured
            # else:  # if vm is created, then we need to config the vm
            #    ctx.vm_need_config = True
            #    self.__wait_for_virtual_machine_ready(sctx)
            #    return
            else:
                self.__setup_virtual_machine_done(sctx)
                return
        except Exception as e:
            self.log.error(
                "azure virtual environment %d create virtual machine %r failed: %r"
                % (sctx.current_job_index, ctx.virtual_machine_name, str(e)))
            self.__on_setup_failed(sctx)
            return

        # wait for add virtual machine to finish
        ctx.request_id = req.request_id
        ctx.vm_need_config = True if ctx.is_vm_image else False
        self.__wait_for_add_virtual_machine(sctx)
Ejemplo n.º 4
0
    def __setup_virtual_machine_with_deployment_existed(self, sctx):
        # get context from super context
        ctx = sctx.job_ctxs[sctx.current_job_index]

        adapter = VirtualMachineAdapter(ctx.subscription_id, ctx.pem_url, host=ctx.management_host)

        deployment_name = adapter.get_deployment_name(ctx.cloud_service_name, ctx.deployment_slot)
        if not ctx.is_vm_image:
            network_config = get_network_config(
                ctx.raw_network_config,
                adapter.get_assigned_endpoints(ctx.cloud_service_name))
        else:
            network_config = None

        # add virtual machine to deployment
        try:
            # create if vm is not created
            if not adapter.virtual_machine_exists(ctx.cloud_service_name, deployment_name, ctx.virtual_machine_name):
                req = adapter.add_virtual_machine(
                    ctx.cloud_service_name,
                    deployment_name,
                    ctx.virtual_machine_name,
                    ctx.system_config,
                    ctx.os_virtual_hard_disk,
                    network_config=network_config,
                    provision_guest_agent=True,
                    resource_extension_references=ctx.resource_extension_references,
                    role_size=ctx.virtual_machine_size,
                    vm_image_name=ctx.image_name if ctx.is_vm_image else None)
            # NOTE: if the vm is created, we think is well configured
            # else:  # if vm is created, then we need to config the vm
            #    ctx.vm_need_config = True
            #    self.__wait_for_virtual_machine_ready(sctx)
            #    return
            else:
                self.__setup_virtual_machine_done(sctx)
                return
        except Exception as e:
            self.log.error(
                "azure virtual environment %d create virtual machine %r failed: %r"
                % (sctx.current_job_index, ctx.virtual_machine_name, str(e)))
            self.__on_setup_failed(sctx)
            return

        # wait for add virtual machine to finish
        ctx.request_id = req.request_id
        ctx.vm_need_config = True if ctx.is_vm_image else False
        self.__wait_for_add_virtual_machine(sctx)