wsp-10323 Convert the VSP provider into one utilizing PowerShell. Step 4 Snapshots

This commit is contained in:
AlexanderTr 2015-03-13 08:02:31 +03:00
parent 6a1efab8d5
commit 8f6b035e89
8 changed files with 377 additions and 181 deletions

View file

@ -12,7 +12,7 @@ namespace WebsitePanel.Providers.Virtualization
{
public static class BiosHelper
{
public static BiosInfo GetVMBios(PowerShellManager powerShell, string name, int generation)
public static BiosInfo Get(PowerShellManager powerShell, string name, int generation)
{
BiosInfo info = new BiosInfo();
@ -76,7 +76,7 @@ namespace WebsitePanel.Providers.Virtualization
return info;
}
public static void UpdateBios(PowerShellManager powerShell, VirtualMachine vm, bool bootFromCD, bool numLockEnabled)
public static void Update(PowerShellManager powerShell, VirtualMachine vm, bool bootFromCD, bool numLockEnabled)
{
// for Win2012R2+ and Win8.1+
if (vm.Generation == 2)

View file

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading.Tasks;
namespace WebsitePanel.Providers.Virtualization
{
public static class JobHelper
{
public static JobResult CreateSuccessResult(ReturnCode returnCode = ReturnCode.OK)
{
return new JobResult
{
Job = new ConcreteJob {JobState = ConcreteJobState.Completed},
ReturnValue = returnCode
};
}
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;
}
public static ConcreteJob CreateFromPSObject(Collection<PSObject> objJob)
{
if (objJob == null || objJob.Count == 0)
return null;
ConcreteJob job = new ConcreteJob();
job.Id = objJob[0].GetProperty<int>("Id").ToString();
job.JobState = objJob[0].GetEnum<ConcreteJobState>("JobStateInfo");
job.Caption = objJob[0].GetProperty<string>("Name");
job.Description = objJob[0].GetProperty<string>("Command");
job.StartTime = objJob[0].GetProperty<DateTime>("PSBeginTime");
job.ElapsedTime = objJob[0].GetProperty<DateTime?>("PSEndTime") ?? DateTime.Now;
// PercentComplete
job.PercentComplete = 0;
var progress = (PSDataCollection<ProgressRecord>)objJob[0].GetProperty("Progress");
if (progress != null && progress.Count > 0)
job.PercentComplete = progress[0].PercentComplete;
// Errors
var errors = (PSDataCollection<ErrorRecord>)objJob[0].GetProperty("Error");
if (errors != null && errors.Count > 0)
{
job.ErrorDescription = errors[0].ErrorDetails.Message + ". " + errors[0].ErrorDetails.RecommendedAction;
job.ErrorCode = errors[0].Exception != null ? -1 : 0;
}
return job;
}
}
}

View file

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading.Tasks;
namespace WebsitePanel.Providers.Virtualization
{
public static class SnapshotHelper
{
public static VirtualMachineSnapshot GetFromPS(PSObject psObject, string runningSnapshotId = null)
{
var snapshot = new VirtualMachineSnapshot
{
Id = psObject.GetString("Id"),
Name = psObject.GetString("Name"),
VMName = psObject.GetString("VMName"),
ParentId = psObject.GetString("ParentSnapshotId"),
Created = psObject.GetProperty<DateTime>("CreationTime")
};
if (string.IsNullOrEmpty(snapshot.ParentId))
snapshot.ParentId = null; // for capability
if (!String.IsNullOrEmpty(runningSnapshotId))
snapshot.IsCurrent = snapshot.Id == runningSnapshotId;
return snapshot;
}
public static VirtualMachineSnapshot GetFromWmi(ManagementBaseObject objSnapshot)
{
if (objSnapshot == null || objSnapshot.Properties.Count == 0)
return null;
VirtualMachineSnapshot snapshot = new VirtualMachineSnapshot();
snapshot.Id = (string)objSnapshot["InstanceID"];
snapshot.Name = (string)objSnapshot["ElementName"];
string parentId = (string)objSnapshot["Parent"];
if (!String.IsNullOrEmpty(parentId))
{
int idx = parentId.IndexOf("Microsoft:");
snapshot.ParentId = parentId.Substring(idx, parentId.Length - idx - 1);
snapshot.ParentId = snapshot.ParentId.ToLower().Replace("microsoft:", "");
}
if (!String.IsNullOrEmpty(snapshot.Id))
{
snapshot.Id = snapshot.Id.ToLower().Replace("microsoft:", "");
}
snapshot.Created = Wmi.ToDateTime((string)objSnapshot["CreationTime"]);
if (string.IsNullOrEmpty(snapshot.ParentId))
snapshot.ParentId = null; // for capability
return snapshot;
}
public static void Delete(PowerShellManager powerShell, VirtualMachineSnapshot snapshot, bool includeChilds)
{
Command cmd = new Command("Remove-VMSnapshot");
cmd.Parameters.Add("VMName", snapshot.VMName);
cmd.Parameters.Add("Name", snapshot.Name);
if (includeChilds) cmd.Parameters.Add("IncludeAllChildSnapshots", true);
powerShell.Execute(cmd, false);
}
}
}