예제 #1
0
        public static JobResult CreateResultFromPSResults(Collection<PSObject> objJob)
        {
            if (objJob == null || objJob.Count == 0)
                return null;

            JobResult result = new JobResult();

            result.Job = CreateFromPSObject(objJob);
            result.ReturnValue = ReturnCode.JobStarted;

            switch (result.Job.JobState)
            {
                case ConcreteJobState.Failed:
                    result.ReturnValue = ReturnCode.Failed;
                    break;
            }

            return result;
        }
예제 #2
0
        public static JobResult CreateResultFromPSResults(Collection <PSObject> objJob)
        {
            if (objJob == null || objJob.Count == 0)
            {
                return(null);
            }

            JobResult result = new JobResult();

            result.Job         = CreateFromPSObject(objJob);
            result.ReturnValue = ReturnCode.JobStarted;

            switch (result.Job.JobState)
            {
            case ConcreteJobState.Failed:
                result.ReturnValue = ReturnCode.Failed;
                break;
            }

            return(result);
        }
예제 #3
0
		public JobResult DeleteSnapshot(string vmId, string snapshotId)
		{
			JobResult ret = new JobResult();

			try
			{
				using (WSPVirtualMachineManagementServiceClient client = GetVMMSClient())
				{
					ret.Job = new ConcreteJob();
					ret.Job.Id = vmId;
					ret.Job.JobState = ConcreteJobState.Starting;
					ret.ReturnValue = ReturnCode.JobStarted;

					VirtualMachineInfo vm = client.GetVirtualMachineByName(vmId);

					client.DeleteVirtualMachineCheckpoint(snapshotId);
				}
			}
			catch (Exception ex)
			{
				ret.Job.ErrorDescription = ex.Message;
				ret.Job.JobState = ConcreteJobState.Exception;
				ret.ReturnValue = ReturnCode.Failed;
			}

			return ret;
		}
예제 #4
0
		public JobResult ApplySnapshot(string vmId, string snapshotId)
		{
			JobResult ret = new JobResult();
			bool error = false;
			try
			{
				using (WSPVirtualMachineManagementServiceClient client = GetVMMSClient())
				{
					ret.Job = new ConcreteJob();
					ret.Job.Id = vmId;
					ret.Job.JobState = ConcreteJobState.Starting;
					ret.ReturnValue = ReturnCode.JobStarted;

					client.RestoreVirtualMachineCheckpoint(snapshotId);
				}
			}
			catch (TimeoutException)
			{
				error = true;
				ret.ReturnValue = ReturnCode.JobStarted;
			}
			catch (Exception ex)
			{
				error = true;
				ret.Job.ErrorDescription = ex.Message;
				ret.Job.JobState = ConcreteJobState.Exception;
				ret.ReturnValue = ReturnCode.Failed;
			}

			if (!error)
			{
				ret.ReturnValue = ReturnCode.OK;
			}

			return ret;
		}
예제 #5
0
		public JobResult CreateSnapshot(string vmId)
		{
			JobResult ret = new JobResult();

			try
			{
				using (WSPVirtualMachineManagementServiceClient client = GetVMMSClient())
				{
					ret.Job = new ConcreteJob();
					ret.Job.Id = vmId;
					ret.Job.JobState = ConcreteJobState.Starting;
					ret.ReturnValue = ReturnCode.JobStarted;

					VirtualMachineInfo vm = client.GetVirtualMachineByName(vmId);

					client.NewVirtualMachineCheckpoint(vm.Id, String.Format("{0} - {1}", vm.Name, DateTime.Now), String.Empty);
				}
			}
			catch (TimeoutException)
			{
				ret.ReturnValue = ReturnCode.JobStarted;
			}
			catch (Exception ex)
			{
				ret.Job.ErrorDescription = ex.Message;
				ret.Job.JobState = ConcreteJobState.Exception;
				ret.ReturnValue = ReturnCode.Failed;
			}

			return ret;
		}
예제 #6
0
		public JobResult DeleteVirtualMachine(string vmId)
		{
			// check state

			VMInfo vm = GetVirtualMachine(vmId);

			JobResult ret = new JobResult();
			ret.Job = new ConcreteJob();
			ret.Job.Id = vmId;
			ret.Job.JobState = ConcreteJobState.Completed;
			ret.ReturnValue = ReturnCode.OK;

			if (vm.VmGuid == Guid.Empty)
			{
				return ret;
			}

			// The virtual computer system must be in the powered off or saved state prior to calling this method.
			if (vm.State == WebsitePanel.Providers.Virtualization.VMComputerSystemStateInfo.Saved
				|| vm.State == WebsitePanel.Providers.Virtualization.VMComputerSystemStateInfo.PowerOff
				|| vm.State == WebsitePanel.Providers.Virtualization.VMComputerSystemStateInfo.CreationFailed
				|| vm.State == WebsitePanel.Providers.Virtualization.VMComputerSystemStateInfo.Stored
				|| vm.State == WebsitePanel.Providers.Virtualization.VMComputerSystemStateInfo.IncompleteVMConfig)
			{
				// delete network adapters and ports
				try
				{
					if (vm.State == WebsitePanel.Providers.Virtualization.VMComputerSystemStateInfo.PowerOff)
					{
						DeleteNetworkAdapters(vm.VmGuid);
					}

					using (WSPVirtualMachineManagementServiceClient client = GetVMMSClient())
					{
						client.DeleteVirtualMachine(vm.VmGuid);

						ret.Job.Caption = "Delete VM Done";
						ret.Job.JobState = ConcreteJobState.Running;
						ret.Job.TargetState = Virtualization.VMComputerSystemStateInfo.Deleting;
						ret.ReturnValue = ReturnCode.JobStarted;
					}
				}
				catch (Exception ex)
				{
					ret.Job.Caption = ex.Message;
					ret.Job.Description = ex.StackTrace;
					ret.Job.JobState = ConcreteJobState.Exception;
					ret.ReturnValue = ReturnCode.Failed;
				}

				return ret;
			}
			else
			{
				throw new Exception("The virtual computer system must be in the powered off or saved state prior to calling Destroy method.");
			}
		}
예제 #7
0
		public JobResult ChangeVirtualMachineState(string vmId, VirtualMachineRequestedState newState)
		{
			// target computer
			JobResult ret = new JobResult();
			ret.Job = new ConcreteJob();
			ret.Job.Id = vmId;

			try
			{
				using (WSPVirtualMachineManagementServiceClient client = GetVMMSClient())
				{

					VirtualMachineInfo vm = client.GetVirtualMachineByName(vmId);

					switch (newState)
					{
						case VirtualMachineRequestedState.Start:
							{
								client.StartVirtualMachine(vm.Id);
								ret.Job.TargetState = Virtualization.VMComputerSystemStateInfo.Running;
								break;
							}
						case VirtualMachineRequestedState.Resume:
							{
								client.ResumeVirtualMachine(vm.Id);
								ret.Job.TargetState = Virtualization.VMComputerSystemStateInfo.Running;
								break;
							}
						case VirtualMachineRequestedState.Pause:
							{
								client.PauseVirtualMachine(vm.Id);
								ret.Job.TargetState = Virtualization.VMComputerSystemStateInfo.Paused;
								break;
							}
						case VirtualMachineRequestedState.ShutDown:
							{
								client.ShutdownVirtualMachine(vm.Id);
								ret.Job.TargetState = Virtualization.VMComputerSystemStateInfo.Stored;
								break;
							}
						case VirtualMachineRequestedState.TurnOff:
							{
								client.StopVirtualMachine(vm.Id);
								ret.Job.TargetState = Virtualization.VMComputerSystemStateInfo.PowerOff;
								break;
							}
						default:
							{
								break;
							}
					}

					ret.Job.JobState = ConcreteJobState.Running;
					ret.Job.Caption = newState.ToString();
					ret.ReturnValue = ReturnCode.JobStarted;
				}
			}
			catch (Exception ex)
			{
				Log.WriteError("Could not change virtual machine state", ex);
				//
				ret.Job.JobState = ConcreteJobState.Exception;
				ret.Job.Caption = newState.ToString();
				ret.ReturnValue = ReturnCode.Failed;
			}

			return ret;
		}
예제 #8
0
        public JobResult ChangeVirtualMachineState(string vmId, VirtualMachineRequestedState newState)
        {
            HostedSolutionLog.LogStart("ChangeVirtualMachineState");
            var jobResult = new JobResult();

            var vm = GetVirtualMachine(vmId);

            try
            {
                string cmdTxt;
                List<string> paramList = new List<string>();

                switch (newState)
                {
                    case VirtualMachineRequestedState.Start:
                        cmdTxt = "Start-VM";
                        break;
                    case VirtualMachineRequestedState.Pause:
                        cmdTxt = "Suspend-VM";
                        break;
                    case VirtualMachineRequestedState.Reset:
                        cmdTxt = "Restart-VM";
                        break;
                    case VirtualMachineRequestedState.Resume:
                        cmdTxt = "Resume-VM";
                        break;
                    case VirtualMachineRequestedState.ShutDown:
                        cmdTxt = "Stop-VM";
                        break;
                    case VirtualMachineRequestedState.TurnOff:
                        cmdTxt = "Stop-VM";
                        paramList.Add("TurnOff");
                        break;
                    case VirtualMachineRequestedState.Save:
                        cmdTxt = "Save-VM";
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("newState");
                }

                Command cmd = new Command(cmdTxt);

                cmd.Parameters.Add("Name", vm.Name);
                //cmd.Parameters.Add("AsJob");
                paramList.ForEach(p => cmd.Parameters.Add(p));

                PowerShell.Execute(cmd, true);
                jobResult = JobHelper.CreateSuccessResult(ReturnCode.JobStarted);
            }
            catch (Exception ex)
            {
                HostedSolutionLog.LogError("ChangeVirtualMachineState", ex);
                throw;
            }

            HostedSolutionLog.LogEnd("ChangeVirtualMachineState");

            return jobResult;
        }
예제 #9
0
        protected JobResult CreateJobResultFromWmiMethodResults(ManagementBaseObject outParams)
        {
            JobResult result = new JobResult();

            // return value
            result.ReturnValue = (ReturnCode)Convert.ToInt32(outParams["ReturnValue"]);

            // try getting job details job
            try
            {
                ManagementBaseObject objJob = wmi.GetWmiObjectByPath((string)outParams["Job"]);
                if (objJob != null && objJob.Properties.Count > 0)
                {
                    result.Job = CreateJobFromWmiObject(objJob);
                }
            }
            catch { /* dumb */ }

            return result;
        }
 private static void LogReturnValueResult(ResultObject res, JobResult job)
 {
     res.ErrorCodes.Add(VirtualizationErrorCodes.JOB_START_ERROR + ":" + job.ReturnValue);
 }