Ejemplo n.º 1
0
        protected bool ValidateNoImageInOtherType(out Model.VirtualMachineImageType otherType)
        {
            var allTypes = new VirtualMachineImageHelper(this.ComputeClient).GetImageType(this.ImageName);

            otherType = string.IsNullOrEmpty(this.OSState) ? Model.VirtualMachineImageType.VMImage
                                                           : Model.VirtualMachineImageType.OSImage;

            return(allTypes == Model.VirtualMachineImageType.None || !allTypes.HasFlag(otherType));
        }
Ejemplo n.º 2
0
        protected override void ExecuteCommand()
        {
            ServiceManagementProfile.Initialize();

            base.ExecuteCommand();

            if (CurrentDeploymentNewSM == null)
            {
                WriteWarning(string.Format(Resources.NoDeploymentFoundByServiceAndVMName, this.ServiceName, this.Name));
                return;
            }

            Model.VirtualMachineImageType otherImagetype = Model.VirtualMachineImageType.None;
            if (!ValidateNoImageInOtherType(out otherImagetype))
            {
                // If there is another type of image with the same name,
                // WAPS will stop here to avoid duplicates and potential conflicts
                WriteErrorWithTimestamp(
                    string.Format(
                        Resources.ErrorAnotherImageTypeFoundWithTheSameName,
                        otherImagetype,
                        this.ImageName));

                return;
            }

            Func <OperationStatusResponse> action = null;

            if (string.IsNullOrEmpty(this.OSState))
            {
                action = () => this.ComputeClient.VirtualMachines.CaptureOSImage(
                    this.ServiceName,
                    CurrentDeploymentNewSM.Name,
                    this.Name,
                    new VirtualMachineCaptureOSImageParameters
                {
                    PostCaptureAction = PostCaptureAction.Delete,
                    TargetImageLabel  = string.IsNullOrEmpty(this.ImageLabel) ? this.ImageName : this.ImageLabel,
                    TargetImageName   = this.ImageName
                });
            }
            else
            {
                if (string.Equals(GetRoleInstanceStatus(), RoleInstanceStatus.ReadyRole))
                {
                    WriteWarning(Resources.CaptureVMImageOperationWhileVMIsStillRunning);
                }

                action = () => this.ComputeClient.VirtualMachines.CaptureVMImage(
                    this.ServiceName,
                    CurrentDeploymentNewSM.Name,
                    this.Name, new VirtualMachineCaptureVMImageParameters
                {
                    VMImageName  = this.ImageName,
                    VMImageLabel = string.IsNullOrEmpty(this.ImageLabel) ? this.ImageName : this.ImageLabel,
                    OSState      = this.OSState
                });
            }

            if (action != null)
            {
                ExecuteClientActionNewSM(null, CommandRuntime.ToString(), action);
            }
        }