public static void Update(PowerShellManager powerShell, VirtualMachine vm, bool bootFromCD, bool numLockEnabled) { // for Win2012R2+ and Win8.1+ if (vm.Generation == 2) { Command cmd = new Command("Set-VMFirmware"); cmd.Parameters.Add("VMName", vm.Name); if (bootFromCD) cmd.Parameters.Add("FirstBootDevice", DvdDriveHelper.GetPS(powerShell, vm.Name)); else cmd.Parameters.Add("FirstBootDevice", HardDriveHelper.GetPS(powerShell, vm.Name).FirstOrDefault()); powerShell.Execute(cmd, true); } // for others win and linux else { Command cmd = new Command("Set-VMBios"); cmd.Parameters.Add("VMName", vm.Name); var bootOrder = bootFromCD ? new[] { "CD", "IDE", "LegacyNetworkAdapter", "Floppy" } : new[] { "IDE", "CD", "LegacyNetworkAdapter", "Floppy" }; cmd.Parameters.Add("StartupOrder", bootOrder); powerShell.Execute(cmd, true); } }
public static void Update(PowerShellManager powerShell, VirtualMachine vm, bool dvdDriveShouldBeInstalled) { if (!vm.DvdDriveInstalled && dvdDriveShouldBeInstalled) Add(powerShell, vm.Name); else if (vm.DvdDriveInstalled && !dvdDriveShouldBeInstalled) Remove(powerShell, vm.Name); }
public static void Update(PowerShellManager powerShell, VirtualMachine vm) { // External NIC if (!vm.ExternalNetworkEnabled && !String.IsNullOrEmpty(vm.ExternalNicMacAddress)) { Delete(powerShell, vm.Name, vm.ExternalNicMacAddress); vm.ExternalNicMacAddress = null; // reset MAC } else if (vm.ExternalNetworkEnabled && !String.IsNullOrEmpty(vm.ExternalNicMacAddress) && Get(powerShell,vm.Name,vm.ExternalNicMacAddress) == null) { Add(powerShell, vm.Name, vm.ExternalSwitchId, vm.ExternalNicMacAddress, Constants.EXTERNAL_NETWORK_ADAPTER_NAME, vm.LegacyNetworkAdapter); } // Private NIC if (!vm.PrivateNetworkEnabled && !String.IsNullOrEmpty(vm.PrivateNicMacAddress)) { Delete(powerShell, vm.Name, vm.PrivateNicMacAddress); vm.PrivateNicMacAddress = null; // reset MAC } else if (vm.PrivateNetworkEnabled && !String.IsNullOrEmpty(vm.PrivateNicMacAddress) && Get(powerShell, vm.Name, vm.PrivateNicMacAddress) == null) { Add(powerShell, vm.Name, vm.PrivateSwitchId, vm.PrivateNicMacAddress, Constants.PRIVATE_NETWORK_ADAPTER_NAME, vm.LegacyNetworkAdapter); } }
public void BindItem(VirtualMachine item) { var generation = item.Generation > 1 ? item.Generation.ToString () : "1"; ddlGeneration.SelectedValue = generation; lblGeneration.Text = litGeneration.Text = GetLocalizedString("ddlGenerationItem." + generation); }
public static void UpdateIOPS(PowerShellManager powerShell, VirtualMachine vm, int IOPSmin, int IOPSmax) { if (vm.Disks != null && (IOPSmax >= IOPSmin)) { foreach (VirtualHardDiskInfo d in vm.Disks) { Command cmd = new Command("Set-VMHardDiskDrive"); cmd.Parameters.Add("VMName", vm.Name); cmd.Parameters.Add("ControllerType", d.VHDControllerType.ToString()); cmd.Parameters.Add("ControllerNumber", d.ControllerNumber); cmd.Parameters.Add("ControllerLocation", d.ControllerLocation); if (IOPSmin == IOPSmax) //disable QoS control { cmd.Parameters.Add("MinimumIOPS", false); cmd.Parameters.Add("MaximumIOPS", false); } else { cmd.Parameters.Add("MinimumIOPS", IOPSmin); cmd.Parameters.Add("MaximumIOPS", IOPSmax); } powerShell.Execute(cmd, true, true); } } }
protected void btnUpdate_Click(object sender, EventArgs e) { if (!Page.IsValid) return; try { // check rights bool manageAllowed = VirtualMachines2012Helper.IsVirtualMachineManagementAllowed(PanelSecurity.PackageId); if (!manageAllowed) { return; } VirtualMachine virtualMachine = new VirtualMachine(); // the custom provider control this.SaveSettingsControls(ref virtualMachine); ResultObject res = ES.Services.VPS2012.UpdateVirtualMachineConfiguration(PanelRequest.ItemID, Utils.ParseInt(ddlCpu.SelectedValue), Utils.ParseInt(txtRam.Text.Trim()), Utils.ParseInt(txtHdd.Text.Trim()), Utils.ParseInt(txtHddIOPSmin.Text.Trim()), Utils.ParseInt(txtHddIOPSmax.Text.Trim()), Utils.ParseInt(txtSnapshots.Text.Trim()), chkDvdInstalled.Checked, chkBootFromCd.Checked, chkNumLock.Checked, chkStartShutdown.Checked, chkPauseResume.Checked, chkReboot.Checked, chkReset.Checked, chkReinstall.Checked, chkExternalNetworkEnabled.Checked, chkPrivateNetworkEnabled.Checked, virtualMachine); if (res.IsSuccess) { // redirect back RedirectBack("changed"); } else { // show error messageBox.ShowMessage(res, "VPS_CHANGE_VM_CONFIGURATION", "VPS"); } } catch (Exception ex) { messageBox.ShowErrorMessage("VPS_CHANGE_VM_CONFIGURATION", ex); } }
protected void wizard_FinishButtonClick(object sender, WizardNavigationEventArgs e) { if (!Page.IsValid) return; try { VirtualMachine virtualMachine = new VirtualMachine(); // the user controls this.SaveSettingsControls(ref virtualMachine); // collect and prepare data string hostname = String.Format("{0}.{1}", txtHostname.Text.Trim(), txtDomain.Text.Trim()); string adminPassword = (string)ViewState["Password"]; // external IPs List<int> extIps = new List<int>(); foreach (ListItem li in listExternalAddresses.Items) if (li.Selected) extIps.Add(Utils.ParseInt(li.Value)); // private IPs string[] privIps = Utils.ParseDelimitedString(txtPrivateAddressesList.Text, '\n', '\r', ' ', '\t'); string summaryEmail = chkSendSummary.Checked ? txtSummaryEmail.Text.Trim() : null; // create virtual machine IntResult res = ES.Services.VPS2012.CreateVirtualMachine(PanelSecurity.PackageId, hostname, listOperatingSystems.SelectedValue, adminPassword, summaryEmail, Utils.ParseInt(ddlCpu.SelectedValue), Utils.ParseInt(txtRam.Text.Trim()), Utils.ParseInt(txtHdd.Text.Trim()), Utils.ParseInt(txtHddIOPSmin.Text.Trim()), Utils.ParseInt(txtHddIOPSmax.Text.Trim()), Utils.ParseInt(txtSnapshots.Text.Trim()), chkDvdInstalled.Checked, chkBootFromCd.Checked, chkNumLock.Checked, chkStartShutdown.Checked, chkPauseResume.Checked, chkReboot.Checked, chkReset.Checked, chkReinstall.Checked, chkExternalNetworkEnabled.Checked, Utils.ParseInt(txtExternalAddressesNumber.Text.Trim()), radioExternalRandom.Checked, extIps.ToArray(), chkPrivateNetworkEnabled.Checked, Utils.ParseInt(txtPrivateAddressesNumber.Text.Trim()), radioPrivateRandom.Checked, privIps, virtualMachine); if (res.IsSuccess) { Response.Redirect(EditUrl("ItemID", res.Value.ToString(), "vps_general", "SpaceID=" + PanelSecurity.PackageId.ToString())); } else { messageBox.ShowMessage(res, "VPS_ERROR_CREATE", "VPS"); } } catch (Exception ex) { messageBox.ShowErrorMessage("VPS_ERROR_CREATE", ex); } }
public static void UpdateProcessors(PowerShellManager powerShell, VirtualMachine vm, int cpuCores, int cpuLimitSettings, int cpuReserveSettings, int cpuWeightSettings) { Command cmd = new Command("Set-VMProcessor"); cmd.Parameters.Add("VMName", vm.Name); cmd.Parameters.Add("Count", cpuCores); cmd.Parameters.Add("Maximum", cpuLimitSettings); cmd.Parameters.Add("Reserve", cpuReserveSettings); cmd.Parameters.Add("RelativeWeight", cpuWeightSettings); powerShell.Execute(cmd, true); }
public void SaveItem(ref VirtualMachine item) { if (Mode != VirtualMachineSettingsMode.Edit) return; item.DynamicMemory = new DynamicMemory { Enabled = chkDynamicMemoryEnabled.Checked, Minimum = ParseInt(txtMinimum.Text), Maximum = ParseInt(txtMaximum.Text), Buffer = ParseInt(txtBuffer.Text), Priority = ParseInt(txtPriority.Text), }; }
public void BindItem(VirtualMachine item) { if (item.DynamicMemory == null) item.DynamicMemory = new DynamicMemory(); // set values chkDynamicMemoryEnabled.Checked = optionDymanicMemoryDisplay.Value = optionDymanicMemorySummary.Value = item.DynamicMemory.Enabled; txtMinimum.Text = litMinimumDisplay.Text = litMinimumSummary.Text = item.DynamicMemory.Minimum.ToString(); txtMaximum.Text = litMaximumDisplay.Text = litMaximumSummary.Text = item.DynamicMemory.Maximum.ToString(); txtBuffer.Text = litBufferDisplay.Text = litBufferSummary.Text = item.DynamicMemory.Buffer.ToString(); txtPriority.Text = litPriorityDisplay.Text = litPrioritySummary.Text = item.DynamicMemory.Priority.ToString(); // set visibilities trMinimumDisplay.Visible = trMaximumDisplay.Visible = trBufferDisplay.Visible = trPriorityDisplay.Visible = item.DynamicMemory.Enabled; trMinimumSummary.Visible = trMaximumSummary.Visible = trBufferSummary.Visible = trPrioritySummary.Visible = item.DynamicMemory.Enabled; }
private void BindVirtualMachine() { vm = ES.Services.VPS.GetVirtualMachineItem(PanelRequest.ItemID); // external network if (!vm.ExternalNetworkEnabled) { secExternalNetwork.Visible = false; ExternalNetworkPanel.Visible = false; } // private network if (!vm.PrivateNetworkEnabled) { secPrivateNetwork.Visible = false; PrivateNetworkPanel.Visible = false; } }
public static void CleanUpReplicaServer(VirtualMachine originalVm) { try { ResultObject result = new ResultObject(); // Get replica server var replicaServer = GetReplicaForService(originalVm.ServiceId, ref result); // Clean up replica server var replicaVm = replicaServer.GetVirtualMachines().FirstOrDefault(m => m.Name == originalVm.Name); if (replicaVm != null) { replicaServer.DisableVmReplication(replicaVm.VirtualMachineId); replicaServer.ShutDownVirtualMachine(replicaVm.VirtualMachineId, true, "ReplicaDelete"); replicaServer.DeleteVirtualMachine(replicaVm.VirtualMachineId); } } catch { /* skip */ } }
public static void Update(PowerShellManager powerShell, VirtualMachine vm, int ramMb, DynamicMemory dynamicMemory) { Command cmd = new Command("Set-VMMemory"); cmd.Parameters.Add("VMName", vm.Name); cmd.Parameters.Add("StartupBytes", ramMb * Constants.Size1M); if (dynamicMemory != null && dynamicMemory.Enabled) { cmd.Parameters.Add("DynamicMemoryEnabled", true); cmd.Parameters.Add("MinimumBytes", dynamicMemory.Minimum * Constants.Size1M); cmd.Parameters.Add("MaximumBytes", dynamicMemory.Maximum * Constants.Size1M); cmd.Parameters.Add("Buffer", dynamicMemory.Buffer); cmd.Parameters.Add("Priority", dynamicMemory.Priority); } else { cmd.Parameters.Add("DynamicMemoryEnabled", false); } powerShell.Execute(cmd, true, true); }
/// <remarks/> public void CreateVirtualMachineAsync( int packageId, string hostname, string osTemplateFile, string password, string summaryLetterEmail, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, int externalAddressesNumber, bool randomExternalAddresses, int[] externalAddresses, bool privateNetworkEnabled, int privateAddressesNumber, bool randomPrivateAddresses, string[] privateAddresses, VirtualMachine otherSettings) { this.CreateVirtualMachineAsync(packageId, hostname, osTemplateFile, password, summaryLetterEmail, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, externalAddressesNumber, randomExternalAddresses, externalAddresses, privateNetworkEnabled, privateAddressesNumber, randomPrivateAddresses, privateAddresses, otherSettings, null); }
/// <remarks/> public System.IAsyncResult BeginCreateVirtualMachine( int packageId, string hostname, string osTemplateFile, string password, string summaryLetterEmail, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, int externalAddressesNumber, bool randomExternalAddresses, int[] externalAddresses, bool privateNetworkEnabled, int privateAddressesNumber, bool randomPrivateAddresses, string[] privateAddresses, VirtualMachine otherSettings, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("CreateVirtualMachine", new object[] { packageId, hostname, osTemplateFile, password, summaryLetterEmail, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, externalAddressesNumber, randomExternalAddresses, externalAddresses, privateNetworkEnabled, privateAddressesNumber, randomPrivateAddresses, privateAddresses, otherSettings}, callback, asyncState); }
public IntResult CreateVirtualMachine( int packageId, string hostname, string osTemplateFile, string password, string summaryLetterEmail, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, int externalAddressesNumber, bool randomExternalAddresses, int[] externalAddresses, bool privateNetworkEnabled, int privateAddressesNumber, bool randomPrivateAddresses, string[] privateAddresses, VirtualMachine otherSettings) { object[] results = this.Invoke("CreateVirtualMachine", new object[] { packageId, hostname, osTemplateFile, password, summaryLetterEmail, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, externalAddressesNumber, randomExternalAddresses, externalAddresses, privateNetworkEnabled, privateAddressesNumber, randomPrivateAddresses, privateAddresses, otherSettings}); return ((IntResult)(results[0])); }
public VirtualMachine CreateVirtualMachine(VirtualMachine vm) { // evaluate paths vm.RootFolderPath = FileUtils.EvaluateSystemVariables(vm.RootFolderPath); vm.OperatingSystemTemplatePath = FileUtils.EvaluateSystemVariables(vm.OperatingSystemTemplatePath); vm.VirtualHardDrivePath = FileUtils.EvaluateSystemVariables(vm.VirtualHardDrivePath); try { // Add new VM Command cmdNew = new Command("New-VM"); cmdNew.Parameters.Add("Name", vm.Name); cmdNew.Parameters.Add("Generation", vm.Generation > 1 ? vm.Generation : 1); cmdNew.Parameters.Add("VHDPath", vm.VirtualHardDrivePath); cmdNew.Parameters.Add("Path", vm.RootFolderPath); PowerShell.Execute(cmdNew, true, true); // Delete default adapter (MacAddress in not running and newly created VM is 00-00-00-00-00-00) NetworkAdapterHelper.Delete(PowerShell, vm.Name, "000000000000"); // Set VM Command cmdSet = new Command("Set-VM"); cmdSet.Parameters.Add("Name", vm.Name); cmdSet.Parameters.Add("SmartPagingFilePath", vm.RootFolderPath); cmdSet.Parameters.Add("SnapshotFileLocation", vm.RootFolderPath); // startup/shutdown actions var autoStartAction = (AutomaticStartAction) AutomaticStartActionSettings; var autoStopAction = (AutomaticStopAction) AutomaticStartActionSettings; if (autoStartAction != AutomaticStartAction.Undefined) { cmdSet.Parameters.Add("AutomaticStartAction", autoStartAction.ToString()); cmdSet.Parameters.Add("AutomaticStartDelay", AutomaticStartupDelaySettings); } if (autoStopAction != AutomaticStopAction.Undefined) cmdSet.Parameters.Add("AutomaticStopAction", autoStopAction.ToString()); PowerShell.Execute(cmdSet, true); // Get created machine Id var createdMachine = GetVirtualMachines().FirstOrDefault(m => m.Name == vm.Name); if (createdMachine == null) throw new Exception("Can't find created machine"); vm.VirtualMachineId = createdMachine.VirtualMachineId; // Update common settings UpdateVirtualMachine(vm); } catch (Exception ex) { HostedSolutionLog.LogError("CreateVirtualMachine", ex); throw; } return vm; }
private void SetUsagesFromKVP(ref VirtualMachine vm) { // Use the WebsitePanel VMConfig Windows service to get the RAM usage as well as the HDD usage / sizes List<KvpExchangeDataItem> vmKvps = GetKVPItems(vm.VirtualMachineId); foreach (KvpExchangeDataItem vmKvp in vmKvps) { // RAM if (vmKvp.Name == Constants.KVP_RAM_SUMMARY_KEY) { string[] ram = vmKvp.Data.Split(':'); int freeRam = Int32.Parse(ram[0]); int availRam = Int32.Parse(ram[1]); vm.RamUsage = availRam - freeRam; } // HDD if (vmKvp.Name == Constants.KVP_HDD_SUMMARY_KEY) { string[] disksArray = vmKvp.Data.Split(';'); vm.HddLogicalDisks = new LogicalDisk[disksArray.Length]; for (int i = 0; i < disksArray.Length; i++) { string[] disk = disksArray[i].Split(':'); vm.HddLogicalDisks[i] = new LogicalDisk { DriveLetter = disk[0], FreeSpace = Int32.Parse(disk[1]), Size = Int32.Parse(disk[2]) }; } } } }
/// <remarks/> public void CreateVirtualMachineAsync( int packageId, string hostname, string osTemplateFile, string password, string summaryLetterEmail, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, int externalAddressesNumber, bool randomExternalAddresses, int[] externalAddresses, bool privateNetworkEnabled, int privateAddressesNumber, bool randomPrivateAddresses, string[] privateAddresses, VirtualMachine otherSettings, object userState) { if ((this.CreateVirtualMachineOperationCompleted == null)) { this.CreateVirtualMachineOperationCompleted = new System.Threading.SendOrPostCallback(this.OnCreateVirtualMachineOperationCompleted); } this.InvokeAsync("CreateVirtualMachine", new object[] { packageId, hostname, osTemplateFile, password, summaryLetterEmail, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, externalAddressesNumber, randomExternalAddresses, externalAddresses, privateNetworkEnabled, privateAddressesNumber, randomPrivateAddresses, privateAddresses, otherSettings}, this.CreateVirtualMachineOperationCompleted, userState); }
public void SaveItem(VirtualMachine item) { }
/// <remarks/> public void UpdateVirtualMachineConfigurationAsync( int itemId, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, bool privateNetworkEnabled, VirtualMachine otherSettings, object userState) { if ((this.UpdateVirtualMachineConfigurationOperationCompleted == null)) { this.UpdateVirtualMachineConfigurationOperationCompleted = new System.Threading.SendOrPostCallback(this.OnUpdateVirtualMachineConfigurationOperationCompleted); } this.InvokeAsync("UpdateVirtualMachineConfiguration", new object[] { itemId, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, privateNetworkEnabled, otherSettings}, this.UpdateVirtualMachineConfigurationOperationCompleted, userState); }
/// <remarks/> public System.IAsyncResult BeginUpdateVirtualMachineConfiguration( int itemId, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, bool privateNetworkEnabled, VirtualMachine otherSettings, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("UpdateVirtualMachineConfiguration", new object[] { itemId, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, privateNetworkEnabled, otherSettings}, callback, asyncState); }
public ResultObject UpdateVirtualMachineConfiguration( int itemId, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, bool privateNetworkEnabled, VirtualMachine otherSettings) { object[] results = this.Invoke("UpdateVirtualMachineConfiguration", new object[] { itemId, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, privateNetworkEnabled, otherSettings}); return ((ResultObject)(results[0])); }
public VirtualMachine UpdateVirtualMachine(VirtualMachine vm) { HostedSolutionLog.LogStart("UpdateVirtualMachine"); HostedSolutionLog.DebugInfo("Virtual Machine: {0}", vm.VirtualMachineId); try { var realVm = GetVirtualMachineEx(vm.VirtualMachineId); DvdDriveHelper.Update(PowerShell, realVm, vm.DvdDriveInstalled); // Dvd should be before bios because bios sets boot order BiosHelper.Update(PowerShell, realVm, vm.BootFromCD, vm.NumLockEnabled); VirtualMachineHelper.UpdateProcessors(PowerShell, realVm, vm.CpuCores, CpuLimitSettings, CpuReserveSettings, CpuWeightSettings); MemoryHelper.Update(PowerShell, realVm, vm.RamSize, vm.DynamicMemory); NetworkAdapterHelper.Update(PowerShell, vm); HardDriveHelper.UpdateIOPS(PowerShell, realVm, vm.IOPSmin, vm.IOPSmax); //IOPS (TODO: for update IOPS dont need reboot) } catch (Exception ex) { HostedSolutionLog.LogError("UpdateVirtualMachine", ex); throw; } HostedSolutionLog.LogEnd("UpdateVirtualMachine"); return vm; }
/// <remarks/> public void UpdateVirtualMachineConfigurationAsync( int itemId, int cpuCores, int ramMB, int hddGB, int snapshots, bool dvdInstalled, bool bootFromCD, bool numLock, bool startShutdownAllowed, bool pauseResumeAllowed, bool rebootAllowed, bool resetAllowed, bool reinstallAllowed, bool externalNetworkEnabled, bool privateNetworkEnabled, VirtualMachine otherSettings) { this.UpdateVirtualMachineConfigurationAsync(itemId, cpuCores, ramMB, hddGB, snapshots, dvdInstalled, bootFromCD, numLock, startShutdownAllowed, pauseResumeAllowed, rebootAllowed, resetAllowed, reinstallAllowed, externalNetworkEnabled, privateNetworkEnabled, otherSettings, null); }
protected VirtualMachine GetVirtualMachineInternal(string vmId, bool extendedInfo) { HostedSolutionLog.LogStart("GetVirtualMachine"); HostedSolutionLog.DebugInfo("Virtual Machine: {0}", vmId); VirtualMachine vm = new VirtualMachine(); vm.VirtualMachineId = vmId; try { Command cmd = new Command("Get-VM"); cmd.Parameters.Add("Id", vmId); Collection<PSObject> result = PowerShell.Execute(cmd, true); if (result != null && result.Count > 0) { vm.Name = result[0].GetString("Name"); vm.State = result[0].GetEnum<VirtualMachineState>("State"); vm.CpuUsage = ConvertNullableToInt32(result[0].GetProperty("CpuUsage")); // This does not truly give the RAM usage, only the memory assigned to the VPS // Lets handle detection of total memory and usage else where. SetUsagesFromKVP method have been made for it. vm.RamUsage = Convert.ToInt32(ConvertNullableToInt64(result[0].GetProperty("MemoryAssigned")) / Constants.Size1M); vm.RamSize = Convert.ToInt32(ConvertNullableToInt64(result[0].GetProperty("MemoryStartup")) / Constants.Size1M); vm.Uptime = Convert.ToInt64(result[0].GetProperty<TimeSpan>("UpTime").TotalMilliseconds); vm.Status = result[0].GetProperty("Status").ToString(); vm.Generation = result[0].GetInt("Generation"); vm.ProcessorCount = result[0].GetInt("ProcessorCount"); vm.ParentSnapshotId = result[0].GetString("ParentSnapshotId"); vm.Heartbeat = VirtualMachineHelper.GetVMHeartBeatStatus(PowerShell, vm.Name); vm.CreatedDate = result[0].GetProperty<DateTime>("CreationTime"); vm.ReplicationState = result[0].GetEnum<ReplicationState>("ReplicationState"); if (extendedInfo) { vm.CpuCores = VirtualMachineHelper.GetVMProcessors(PowerShell, vm.Name); // BIOS BiosInfo biosInfo = BiosHelper.Get(PowerShell, vm.Name, vm.Generation); vm.NumLockEnabled = biosInfo.NumLockEnabled; vm.BootFromCD = biosInfo.BootFromCD; // DVD drive var dvdInfo = DvdDriveHelper.Get(PowerShell, vm.Name); vm.DvdDriveInstalled = dvdInfo != null; // HDD vm.Disks = HardDriveHelper.Get(PowerShell, vm.Name); if (vm.Disks != null && vm.Disks.GetLength(0) > 0) { vm.VirtualHardDrivePath = vm.Disks[0].Path; vm.HddSize = Convert.ToInt32(vm.Disks[0].MaxInternalSize / Constants.Size1G); vm.IOPSmin = vm.Disks[0].MinimumIOPS; vm.IOPSmax = vm.Disks[0].MaximumIOPS; } // network adapters vm.Adapters = NetworkAdapterHelper.Get(PowerShell, vm.Name); } vm.DynamicMemory = MemoryHelper.GetDynamicMemory(PowerShell, vm.Name); // If it is possible get usage ram and usage hdd data from KVP SetUsagesFromKVP(ref vm); } } catch (Exception ex) { HostedSolutionLog.LogError("GetVirtualMachine", ex); throw; } HostedSolutionLog.LogEnd("GetVirtualMachine"); return vm; }
public void BindItem(VirtualMachine item) { }
private void ChangeVirtualMachineServiceItemState(VirtualMachine vm, bool started) { try { VirtualMachine vps = GetVirtualMachine(vm.VirtualMachineId); JobResult result = null; if (vps == null) { HostedSolutionLog.LogWarning(String.Format("Virtual machine '{0}' object with ID '{1}' was not found. Change state operation aborted.", vm.Name, vm.VirtualMachineId)); return; } #region Start if (started && (vps.State == VirtualMachineState.Off || vps.State == VirtualMachineState.Paused || vps.State == VirtualMachineState.Saved)) { VirtualMachineRequestedState state = VirtualMachineRequestedState.Start; if (vps.State == VirtualMachineState.Paused) state = VirtualMachineRequestedState.Resume; result = ChangeVirtualMachineState(vm.VirtualMachineId, state); // check result if (result.ReturnValue != ReturnCode.JobStarted) { HostedSolutionLog.LogWarning(String.Format("Cannot {0} '{1}' virtual machine: {2}", state, vm.Name, result.ReturnValue)); return; } // wait for completion if (!JobCompleted(result.Job)) { HostedSolutionLog.LogWarning(String.Format("Cannot complete {0} '{1}' of virtual machine: {1}", state, vm.Name, result.Job.ErrorDescription)); return; } } #endregion #region Stop else if (!started && (vps.State == VirtualMachineState.Running || vps.State == VirtualMachineState.Paused)) { if (vps.State == VirtualMachineState.Running) { // try to shutdown the system ReturnCode code = ShutDownVirtualMachine(vm.VirtualMachineId, true, "Virtual Machine has been suspended from WebsitePanel"); if (code == ReturnCode.OK) return; } // turn off VirtualMachineRequestedState state = VirtualMachineRequestedState.TurnOff; result = ChangeVirtualMachineState(vm.VirtualMachineId, state); // check result if (result.ReturnValue != ReturnCode.JobStarted) { HostedSolutionLog.LogWarning(String.Format("Cannot {0} '{1}' virtual machine: {2}", state, vm.Name, result.ReturnValue)); return; } // wait for completion if (!JobCompleted(result.Job)) { HostedSolutionLog.LogWarning(String.Format("Cannot complete {0} '{1}' of virtual machine: {1}", state, vm.Name, result.Job.ErrorDescription)); return; } } #endregion } catch (Exception ex) { HostedSolutionLog.LogError(String.Format("Error {0} Virtual Machine '{1}'", started ? "starting" : "turning off", vm.Name), ex); } }
public VirtualMachine UpdateVirtualMachine(VirtualMachine vm) { try { Log.WriteStart("'{0}' UpdateVirtualMachine", ProviderSettings.ProviderName); VirtualMachine result = VirtualizationProvider.UpdateVirtualMachine(vm); Log.WriteEnd("'{0}' UpdateVirtualMachine", ProviderSettings.ProviderName); return result; } catch (Exception ex) { Log.WriteError(String.Format("'{0}' UpdateVirtualMachine", ProviderSettings.ProviderName), ex); throw; } }
private void DeleteVirtualMachineServiceItem(VirtualMachine vm) { try { JobResult result = null; VirtualMachine vps = GetVirtualMachine(vm.VirtualMachineId); if (vps == null) { HostedSolutionLog.LogWarning(String.Format("Virtual machine '{0}' object with ID '{1}' was not found. Delete operation aborted.", vm.Name, vm.VirtualMachineId)); return; } #region Turn off (if required) if (vps.State != VirtualMachineState.Off) { result = ChangeVirtualMachineState(vm.VirtualMachineId, VirtualMachineRequestedState.TurnOff); // check result if (result.ReturnValue != ReturnCode.JobStarted) { HostedSolutionLog.LogWarning(String.Format("Cannot Turn off '{0}' virtual machine before deletion: {1}", vm.Name, result.ReturnValue)); return; } // wait for completion if (!JobCompleted(result.Job)) { HostedSolutionLog.LogWarning(String.Format("Cannot complete Turn off '{0}' of virtual machine before deletion: {1}", vm.Name, result.Job.ErrorDescription)); return; } } #endregion #region Delete virtual machine result = DeleteVirtualMachine(vm.VirtualMachineId); // check result if (result.ReturnValue != ReturnCode.JobStarted) { HostedSolutionLog.LogWarning(String.Format("Cannot delete '{0}' virtual machine: {1}", vm.Name, result.ReturnValue)); return; } // wait for completion if (!JobCompleted(result.Job)) { HostedSolutionLog.LogWarning(String.Format("Cannot complete deletion of '{0}' virtual machine: {1}", vm.Name, result.Job.ErrorDescription)); return; } #endregion #region Delete virtual machine try { DeleteFile(vm.RootFolderPath); } catch (Exception ex) { HostedSolutionLog.LogError(String.Format("Cannot delete virtual machine folder '{0}'", vm.RootFolderPath), ex); } #endregion } catch (Exception ex) { HostedSolutionLog.LogError(String.Format("Error deleting Virtual Machine '{0}'", vm.Name), ex); } }