xenadmin/XenAdmin/Commands/DeactivateVBDCommand.cs
Danilo Del Busso afb419af04
CP-44767, CP-44766 & CP-44765: Refactor usage of VM restrictions and add reference_label to OVFs (#3211)
* Tidy up `VM` extension: use `var`
* Tidy up `VM` extension: remove redundant initialisers
* Tidy up `VM` extension: use type keywords when possible
* Tidy up `VM` extension: merge conditional expressions
* Tidy up `VM` extension: remove redundant qualifiers
* Tidy up `VM` extension: remove redundant type arguments
* Tidy up `VM` extension: remove redundant `else`s and parentheses
* Tidy up `VM` extension: fix naming
* Tidy up `VM` extension: misc changes
* Tidy up `VM` extension: use `null` propagation
* Tidy up `VM` extension: fix whitespace
* Tidy up `VM` extension: apply ReSharper Code Cleanup utility
* Tidy up `VM` extension: Fix naming of private string array
* CP-44767: Ignore VM restriction when fetching `MaxVCPUsAllowed`
Instead, fetch the highest available value in all templates for the host.
This means that VMs imported from vhd won't automatically default to `DEFAULT_NUM_VCPUS_ALLOWED`, and that VMs that have been kept across XenServer upgrades won't be limited to the number of vCPUs in their own (possibly outdated) restrictions
* CP-44766: Use value in template with a matching `reference_label` when checking VM restrictions
* Move restriction getters to own region
* CP-44766: Use matching templates to fetch VM restrictions
- Rewrite `GetRestrictions...` methods to perform simpler operations
- Add `GetIntRestrictionValue` and `GetBoolRestrictionValue` wrappers to `GetRestrictionValueFromMatchingTemplate`
- Now all calls to a restrictions first check the template value, with a fall-back to defaults
* CP-44765: Export `reference-label` when generating OVFs
---------

Signed-off-by: Danilo Del Busso <danilo.delbusso@cloud.com>
2023-09-29 03:45:17 +01:00

193 lines
7.5 KiB
C#

/* 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 XenAPI;
using XenAdmin.Core;
using XenAdmin.Dialogs;
using XenAdmin.Actions;
namespace XenAdmin.Commands
{
class DeactivateVBDCommand : Command
{
/// <summary>
/// Deactivates a selection of VBDs
/// </summary>
/// <param name="mainWindow"></param>
/// <param name="selection"></param>
public DeactivateVBDCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
: base(mainWindow, selection)
{
}
/// <summary>
/// Deactivates a single VBD
/// </summary>
/// <param name="mainWindow"></param>
/// <param name="vbd"></param>
public DeactivateVBDCommand(IMainWindow mainWindow, VBD vbd)
: base(mainWindow, vbd)
{
}
public override string ContextMenuText => Messages.MESSAGEBOX_DEACTIVATE_VD_TITLE;
public override string ButtonText => Messages.DEACTIVATE;
protected override bool CanRunCore(SelectedItemCollection selection)
{
return selection.AllItemsAre<VBD>() && selection.AtLeastOneXenObjectCan<VBD>(CanRun);
}
// We only need to check for IO Drivers for hosts before Ely
private bool AreIODriversNeededAndMissing(VM vm)
{
if (Helpers.ElyOrGreater(vm.Connection))
{
return false;
}
return !vm.GetVirtualizationStatus(out _).HasFlag(VM.VirtualizationStatus.IoDriversInstalled);
}
private bool CanRun(VBD vbd)
{
VDI vdi = vbd.Connection.Resolve<VDI>(vbd.VDI);
VM vm = vbd.Connection.Resolve<VM>(vbd.VM);
if (vm == null || !vm.IsRealVm() || vdi == null || vdi.Locked || vbd.Locked)
return false;
if (vm.power_state != vm_power_state.Running)
return false;
if (vdi.type == vdi_type.system && vbd.GetIsOwner())
return false;
if (AreIODriversNeededAndMissing(vm))
return false;
if (!vbd.currently_attached)
return false;
return vbd.allowed_operations.Contains(vbd_operations.unplug);
}
protected override string GetCantRunReasonCore(IXenObject item)
{
VBD vbd = item as VBD;
if (vbd == null)
return base.GetCantRunReasonCore(item);
VDI vdi = vbd.Connection.Resolve<VDI>(vbd.VDI);
VM vm = vbd.Connection.Resolve<VM>(vbd.VM);
if (vm == null || vdi == null)
return base.GetCantRunReasonCore(item);
if (vm.is_a_template)
return Messages.CANNOT_ACTIVATE_TEMPLATE_DISK;
if (!vm.IsRealVm())
return base.GetCantRunReasonCore(item);
SR sr = vdi.Connection.Resolve<SR>(vdi.SR);
if (sr == null)
return Messages.SR_COULD_NOT_BE_CONTACTED;
if (vdi.Locked)
{
var vdiType = vdi.VDIType();
return vdiType == VDI.FriendlyType.SNAPSHOT
? Messages.CANNOT_DEACTIVATE_SNAPSHOT_IN_USE
: vdiType == VDI.FriendlyType.ISO
? Messages.CANNOT_DEACTIVATE_ISO_IN_USE
: Messages.CANNOT_DEACTIVATE_VDI_IN_USE;
}
if (vm.power_state != vm_power_state.Running)
return string.Format(
Messages.CANNOT_DEACTIVATE_VDI_VM_NOT_RUNNING,
Helpers.GetName(vm).Ellipsise(50));
if (vdi.type == vdi_type.system && vbd.GetIsOwner())
return Messages.TOOLTIP_DEACTIVATE_SYSVDI;
if (AreIODriversNeededAndMissing(vm))
return vm.HasNewVirtualizationStates()
? string.Format(Messages.CANNOT_DEACTIVATE_VDI_NEEDS_IO_DRIVERS, Helpers.GetName(vm).Ellipsise(50))
: string.Format(Messages.CANNOT_DEACTIVATE_VDI_NEEDS_TOOLS, BrandManager.VmTools, Helpers.GetName(vm).Ellipsise(50));
if (!vbd.currently_attached)
return string.Format(Messages.CANNOT_DEACTIVATE_NOT_ACTIVE, Helpers.GetName(vm).Ellipsise(50));
return base.GetCantRunReasonCore(item);
}
protected override CommandErrorDialog GetErrorDialogCore(IDictionary<IXenObject, string> cantRunReasons)
{
return new CommandErrorDialog(Messages.ERROR_DEACTIVATING_MULTIPLE_VDIS_TITLE, Messages.ERROR_DEACTIVATING_MULTIPLE_VDIS_MESSAGE, cantRunReasons);
}
protected override void RunCore(SelectedItemCollection selection)
{
List<AsyncAction> actionsToComplete = new List<AsyncAction>();
foreach (VBD vbd in selection.AsXenObjects<VBD>())
{
if (vbd.Locked)
continue;
actionsToComplete.Add(getDeactivateVBDAction(vbd));
}
if (actionsToComplete.Count == 0)
return;
if (actionsToComplete.Count > 1)
RunMultipleActions(actionsToComplete, Messages.ACTION_DEACTIVATING_MULTIPLE_VDIS_TITLE, Messages.ACTION_DEACTIVATING_MULTIPLE_VDIS_STATUS, Messages.COMPLETED, true);
else
actionsToComplete[0].RunAsync();
}
private AsyncAction getDeactivateVBDAction(VBD vbd)
{
VDI vdi = vbd.Connection.Resolve<VDI>(vbd.VDI);
VM vm = vbd.Connection.Resolve<VM>(vbd.VM);
String title = String.Format(Messages.ACTION_DISK_DEACTIVATING_TITLE, vdi.Name(), vm.Name());
String startDesc = Messages.ACTION_DISK_DEACTIVATING;
String endDesc = Messages.ACTION_DISK_DEACTIVATED;
AsyncAction action = new DelegatedAsyncAction(vbd.Connection,
title, startDesc, endDesc,session => VBD.unplug(session, vbd.opaque_ref), "vbd.unplug");
action.VM = vm;
return action;
}
}
}