xenadmin/XenAdmin/Wizards/ImportWizard/ImportSelectHostPage.cs
Danilo Del Busso 6c584bb351
CA-375532, CA-336510, and CP-41825: Add warnings when user selects too many vCPUs (#3119)
* Tidy up `SelectMultipleVMDestinationPage.cs`
* CA-375532 & CA-336510: Add warning when importing VMs with too many vCPUs
* CA-375532 & CA-336510: Prevent users from starting appliance if a VM has too many vCPUs
* Tidy up `Page_CpuMem`: use expression-bodied properties
* Tidy up `Page_CpuMem`: use conventional naming scheme
* Tidy up `Page_CpuMem`: use explicit modifiers
* Tidy up `Page_CpuMem`: use `en-US` locale for names
* Tidy up `Page_CpuMem`: use `var` when possible
* Tidy up `Page_CpuMem`: rename `Vcpus` to `VCpus`
* Tidy up `Page_CpuMem`: reorder elements
* Tidy up `Page_CpuMem`: revert class renaming and fix `vCpus` typos
* CA-375532: Prevent users from starting VM if they select too many vCPUs
* Tidy up `Page_Finish`: use expression-bodied properties
* Tidy up `Page_Finish`: fix typo
* Tidy up `Page_CpuMem`: rename `CanStartVM` to `CanStartVm`
* Remove unnecessary using directives
* Anchor warning icon to the top-left corner of its parent
* CA-375532 & CA-336510: Warn users when selecting more vCPUs than pCPUs in the Properties page
* CP-41825: Add warning for imported VMs with > 32 vCPUs
* CP-41825: Add warning for new VMs with > 32 vCPUs
* CP-41825: Add warning for existing VMs when selecting > 32 vCPUs
* CP-41825: Update wording used to alert users running VMs with > 32vCPUs
* Remove unused local variable in `VappStartCommand.cs`
* Reword some vCPUs messages
* Simplify assignment of `CanStartImmediately` in `Page_Finish.cs`
* Simplify assignment of `CanStartVmsAutomatically` in `ImportFinishPage.cs`
* Remove useless `Count` check in `VappStartCommand`
* Rename `pictureBox1` to `warningPictureBox` and change its `SizeMode`
* Separate CPU and memory warnings in `Page_CpuMem`
* Shorten warning messages shown in `Page_CpuMem`
* Disable option to start VM when memory configuration isn't valid
* Report number of vCPUs and pCPUs when showing warning in New VM wizard
* Tidy up `CpuMemoryEditPage`: rename elements
* Tidy up `CpuMemoryEditPage`: use explicity modifiers
* Tidy up `CpuMemoryEditPage`: Rename `CPUMemoryEditPage` to `CpuMemoryEditPage`
* Tidy up `CpuMemoryEditPage`: Miscellaneous renames and improvements
* Tidy up `CpuMemoryEditPage`: Fix whitespace
* Tidy up `CpuMemoryEditPage`: Use ReSharper code formatter
* Tidy up `CpuMemoryEditPage`: Move events to own region
* Tidy up `CpuMemoryEditPage`: Move `IEditPage` members to own region
* Tidy up `CpuMemoryEditPage`: Order members outside of regions
* Replace topology and vCPU warning labels with structured warnings
* Ensure warnings ends with a dot
* Allow multiple CPU warnings to show at the same time
* Remove warning link for vCPUS > pCPUS Option is not achievable
* Enable option to force unit used in `MemorySpinner`
* Replace `NumericUpDown` with `MemorySpinner` and show warnings at bottom of control
* Check destination pCPUs count when destination is not a pool
* Do not disable option to start VMs after import if user selects a shared SR
* Add memory warning when importing appliance with too much memory
* Sort vCPUs and memory warning strings
* Remove target from `ImportWizard`'s summary
Target is not useful and can cause confusion as appliance VMs could start on non-target hosts
* The home server is needed in the summary when importing XVA. The target server is not needed in the summary when migrating a VM.
* Update warnings when the server selection changes. Corrected memory calculation.
* Show all warnings for vCPUs. Moved topology warning to the bottom. Separated methods for showing memory and vCPU warnings.
* Removed the memory setting from the VM Properties dialog. Split running VM info to a separate panel.
* Corrections to the logic for starting the VM automatically. Also, Fixed a bit the layout of the Finish page to make better use of the available space.
* Corrections as per code review.

Signed-off-by: Danilo Del Busso <danilo.delbusso@cloud.com>
Co-authored-by: Konstantina Chremmou <Konstantina.Chremmou@cloud.com>
2023-06-08 11:24:39 +01:00

415 lines
16 KiB
C#
Executable File

/* Copyright (c) Cloud Software Group, Inc.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using XenAdmin.Actions.OvfActions;
using XenAdmin.Core;
using XenAdmin.Network;
using XenAdmin.Wizards.GenericPages;
using XenAdmin.Wizards.ImportWizard.Filters;
using XenAPI;
using XenOvf;
using XenOvf.Definitions;
namespace XenAdmin.Wizards.ImportWizard
{
class ImportSelectHostPage : SelectMultipleVMDestinationPage
{
private EnvelopeType _selectedOvfEnvelope;
private List<Xen_ConfigurationSettingData_Type> vgpuSettings = new List<Xen_ConfigurationSettingData_Type>();
private List<Xen_ConfigurationSettingData_Type> hardwarePlatformSettings = new List<Xen_ConfigurationSettingData_Type>();
private List<Xen_ConfigurationSettingData_Type> vendorDeviceSettings = new List<Xen_ConfigurationSettingData_Type>();
private int _ovfMaxVCpusCount;
private long _ovfMemory;
private readonly List<long> _ovfVCpusCount;
public event Action<IXenConnection> ConnectionSelectionChanged;
public ImportSelectHostPage()
{
InitializeText();
ShowWarning(null);
_ovfVCpusCount = new List<long>();
}
#region XenTabPage overrides
/// <summary>
/// Gets the page's title (headline)
/// </summary>
public override string PageTitle => Messages.IMPORT_SELECT_HOST_PAGE_TITLE;
/// <summary>
/// Gets the page's label in the (left hand side) wizard progress panel
/// </summary>
public override string Text => Messages.NEWSR_LOCATION;
protected override bool ImplementsIsDirty()
{
return true;
}
#endregion
public EnvelopeType SelectedOvfEnvelope
{
private get
{
return _selectedOvfEnvelope;
}
set
{
_selectedOvfEnvelope = value;
vgpuSettings.Clear();
hardwarePlatformSettings.Clear();
vendorDeviceSettings.Clear();
if (_selectedOvfEnvelope == null)
return;
var vsColl = SelectedOvfEnvelope.Item as VirtualSystemCollection_Type;
if (vsColl == null && SelectedOvfEnvelope.Item is VirtualSystemCollection_Type)
vsColl = new VirtualSystemCollection_Type {Content = new[] {SelectedOvfEnvelope.Item}};
if (vsColl == null)
return;
foreach (var vsType in vsColl.Content)
{
var vhs = OVF.FindVirtualHardwareSectionByAffinity(SelectedOvfEnvelope, vsType.id, "xen");
var data = vhs.VirtualSystemOtherConfigurationData;
if (data == null)
continue;
_ovfVCpusCount.Clear();
foreach (var rasdType in vhs.Item)
{
// Processor
if (rasdType.ResourceType.Value == 3 &&
int.TryParse(rasdType.VirtualQuantity.Value.ToString(), out var vCpusCount))
{
_ovfVCpusCount.Add(vCpusCount);
if (_ovfMaxVCpusCount < vCpusCount)
{
_ovfMaxVCpusCount = vCpusCount;
}
}
// Memory
if (rasdType.ResourceType.Value == 4 && double.TryParse(rasdType.VirtualQuantity.Value.ToString(), out var memory))
{
//The default memory unit is MB (2^20), however, the RASD may contain a different
//one with format byte*memoryBase^memoryPower (byte being a literal string)
double memoryBase = 2.0;
double memoryPower = 20.0;
if (rasdType.AllocationUnits.Value.ToLower().StartsWith("byte"))
{
string[] a1 = rasdType.AllocationUnits.Value.Split('*', '^');
if (a1.Length == 3)
{
if (!double.TryParse(a1[1].Trim(), out memoryBase))
memoryBase = 2.0;
if (!double.TryParse(a1[2].Trim(), out memoryPower))
memoryPower = 20.0;
}
}
double memoryMultiplier = Math.Pow(memoryBase, memoryPower);
memory *= memoryMultiplier;
if (memory > long.MaxValue)
memory = long.MaxValue;
if (_ovfMemory < memory)
_ovfMemory = (long)memory;
}
}
foreach (var s in data)
{
if (s.Name == "vgpu")
vgpuSettings.Add(s);
else if (s.Name == "hardware_platform_version")
hardwarePlatformSettings.Add(s);
else if (s.Name == "VM_has_vendor_device")
vendorDeviceSettings.Add(s);
}
}
}
}
protected override string InstructionText => Messages.IMPORT_WIZARD_DESTINATION_INSTRUCTIONS;
protected override string TargetPoolText => Messages.IMPORT_WIZARD_DESTINATION_DESTINATION;
protected override string TargetServerSelectionIntroText => Messages.IMPORT_WIZARD_DESTINATION_TABLE_INTRO;
protected override void OnSelectedTargetPoolChanged()
{
var warnings = new List<string>();
if (SelectedTargetPool != null)
{
if (!CheckDestinationSupportsVendorDevice())
{
//it shouldn't come to this as the hardware incompatibility filter
//will have already caught it but just to be on the safe side
if (VmMappings.Count == 1)
warnings.Add(Messages.IMPORT_VM_WITH_VENDOR_DEVICE_WARNING_ONE);
else if (VmMappings.Count > 1)
warnings.Add(Messages.IMPORT_VM_WITH_VENDOR_DEVICE_WARNING_MANY);
}
if (!CheckRightGpuExists())
{
if (VmMappings.Count == 1)
warnings.Add(Messages.IMPORT_VM_WITH_VGPU_WARNING_ONE);
else if (VmMappings.Count > 1)
warnings.Add(Messages.IMPORT_VM_WITH_VGPU_WARNING_MANY);
}
var ovfCountsAboveLimit = _ovfVCpusCount.Count(vCpusCount => vCpusCount > VM.MAX_VCPUS_FOR_NON_TRUSTED_VMS);
if (ovfCountsAboveLimit > 0)
{
warnings.Add(string.Format(Messages.IMPORT_VM_CPUS_COUNT_UNTRUSTED_WARNING, ovfCountsAboveLimit, VM.MAX_VCPUS_FOR_NON_TRUSTED_VMS, BrandManager.BrandConsole));
}
}
ApplianceCanBeStarted = true;
if (!CheckDestinationHasEnoughPhysicalCpus(out var physicalCpusWarningMessage))
{
warnings.Add(physicalCpusWarningMessage);
ApplianceCanBeStarted = false;
}
if (!CheckDestinationHasEnoughMemory(out var memoryWarningMessage))
{
warnings.Add(memoryWarningMessage);
ApplianceCanBeStarted = false;
}
ShowWarning(string.Join("\n\n", warnings));
ConnectionSelectionChanged?.Invoke(SelectedTargetPool?.Connection);
}
protected override void OnSelectedTargetChanged()
{
OnSelectedTargetPoolChanged();
}
protected override DelayLoadingOptionComboBoxItem CreateDelayLoadingOptionComboBoxItem(IXenObject xenItem)
{
var filters = new List<ReasoningFilter>
{
new HardwareCompatibilityFilter(xenItem, hardwarePlatformSettings)
};
return new DelayLoadingOptionComboBoxItem(xenItem, filters);
}
protected override List<ReasoningFilter> CreateTargetServerFilterList(IXenObject xenObject, List<string> vmOpaqueRefs)
{
var filters = new List<ReasoningFilter>();
if (xenObject != null)
filters.Add(new HardwareCompatibilityFilter(xenObject, hardwarePlatformSettings));
return filters;
}
private bool CheckRightGpuExists()
{
foreach (var vgpuSetting in vgpuSettings)
{
Match m = ImportApplianceAction.VGPU_REGEX.Match(vgpuSetting.Value.Value);
if (!m.Success)
continue;
var types = m.Groups[1].Value.Split(';');
var gpuGroup = SelectedTargetPool.Connection.Cache.GPU_groups.FirstOrDefault(g =>
g.GPU_types.Length == types.Length &&
g.GPU_types.Intersect(types).Count() == types.Length);
if (gpuGroup == null)
return false;
string vendorName = m.Groups[2].Value;
string modelName = m.Groups[3].Value;
var vgpuType = SelectedTargetPool.Connection.Cache.VGPU_types.FirstOrDefault(v =>
v.vendor_name == vendorName && v.model_name == modelName);
if (vgpuType == null)
return false;
}
return true;
}
/// <summary>
/// Check if the appliance can be started on the selected host or pool. Note that if the user selects
/// a shared SR in other pages, the VM could still start. The check considers both vCPU count and memory requirements
/// of the appliance.
/// </summary>
public bool ApplianceCanBeStarted { get; private set; } = true;
private bool CheckDestinationHasEnoughPhysicalCpus(out string warningMessage)
{
warningMessage = string.Empty;
var selectedTarget = SelectedTarget ?? SelectedTargetPool;
var physicalCpusCount = GetPhysicalCpus(selectedTarget);
if (physicalCpusCount < 0 || physicalCpusCount >= _ovfMaxVCpusCount)
return true;
if (selectedTarget is Pool)
warningMessage = string.Format(Messages.IMPORT_WIZARD_CPUS_COUNT_MISMATCH_POOL, _ovfMaxVCpusCount, physicalCpusCount);
else if (selectedTarget is Host)
warningMessage = string.Format(Messages.IMPORT_WIZARD_CPUS_COUNT_MISMATCH_HOST, _ovfMaxVCpusCount, physicalCpusCount);
else
return true;
return false;
}
private bool CheckDestinationHasEnoughMemory(out string warningMessage)
{
warningMessage = string.Empty;
var selectedTarget = SelectedTarget ?? SelectedTargetPool;
var memory = GetFreeMemory(selectedTarget);
if (memory >= _ovfMemory)
return true;
if (selectedTarget is Pool)
warningMessage = string.Format(Messages.IMPORT_WIZARD_INSUFFICIENT_MEMORY_POOL, Util.MemorySizeStringSuitableUnits(_ovfMemory, true), Util.MemorySizeStringSuitableUnits(memory, true));
else if (selectedTarget is Host)
warningMessage = string.Format(Messages.IMPORT_WIZARD_INSUFFICIENT_MEMORY_HOST, Util.MemorySizeStringSuitableUnits(_ovfMemory, true), Util.MemorySizeStringSuitableUnits(memory, true));
else
return true;
return false;
}
/// <summary>
/// Returns the number of physical CPUs on the specified <paramref name="xenObject"/> (<see cref="Host"/> or <see cref="Pool"/>) or -1 if the value cannot be determined.
/// </summary>
/// <param name="xenObject">The XenObject for which to determine the number of physical CPUs.</param>
/// <returns>The number of physical CPUs on the specified <paramref name="xenObject"/> or -1 if the value cannot be determined.</returns>
private int GetPhysicalCpus(IXenObject xenObject)
{
var physicalCpusCount = -1;
switch (xenObject)
{
case Host host:
{
var hostCpuCount = host.CpuCount();
if(hostCpuCount > 0)
physicalCpusCount = hostCpuCount;
break;
}
case Pool pool:
{
var hosts = pool.Connection.Cache.Hosts;
var maxCpuCounts = hosts
.Select(h => h.CpuCount())
.ToList();
if (maxCpuCounts.Count > 0)
{
physicalCpusCount = maxCpuCounts.Max();
}
break;
}
}
return physicalCpusCount;
}
private long GetFreeMemory(IXenObject xenObject)
{
long memory = 0;
switch (xenObject)
{
case Host host:
{
var hostMemory = host.memory_available_calc();
if (hostMemory > 0)
memory = hostMemory;
break;
}
case Pool pool:
{
var hosts = pool.Connection.Cache.Hosts;
var maxMemories = hosts
.Select(h => h.memory_available_calc())
.ToList();
if (maxMemories.Count > 0)
{
memory = maxMemories.Max();
}
break;
}
}
return memory;
}
private bool CheckDestinationSupportsVendorDevice()
{
var dundeeOrNewerHosts = Helpers.DundeeOrGreater(SelectedTargetPool.Connection) ? SelectedTargetPool.Connection.Cache.Hosts : new Host[] {};
foreach (var setting in vendorDeviceSettings)
{
if (!bool.TryParse(setting.Value.Value, out var hasVendorDevice))
continue;
if (hasVendorDevice && dundeeOrNewerHosts.Length == 0)
return false;
}
return true;
}
}
}