2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using XenAdmin.Actions;
|
|
|
|
|
using XenAdmin.Actions.VMActions;
|
2020-09-13 06:16:32 +02:00
|
|
|
|
using XenAdmin.Dialogs;
|
|
|
|
|
using XenAPI;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Commands
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Suspends the selected VMs. Shows a confirmation dialog.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class SuspendVMCommand : VMLifeCycleCommand
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of this Command. The parameter-less constructor is required if
|
|
|
|
|
/// this Command is to be attached to a ToolStrip menu item or button. It should not be used in any other scenario.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public SuspendVMCommand()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SuspendVMCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
|
|
|
|
|
: base(mainWindow, selection)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SuspendVMCommand(IMainWindow mainWindow, VM vm)
|
|
|
|
|
: base(mainWindow, vm)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SuspendVMCommand(IMainWindow mainWindow, VM vm, Control parent)
|
|
|
|
|
: base(mainWindow, vm, parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override bool CanRun(VM vm)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (!vm.is_a_template && vm.power_state == vm_power_state.Running && vm.allowed_operations != null && vm.allowed_operations.Contains(vm_operations.suspend))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override void Run(List<VM> vms)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-08-10 15:34:40 +02:00
|
|
|
|
RunAction(vms, Messages.ACTION_VMS_SUSPENDING_TITLE, Messages.ACTION_VMS_SUSPENDING_TITLE, Messages.ACTION_VM_SUSPENDED, null);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
public override string MenuText => Messages.MAINWINDOW_SUSPEND;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
public override Image MenuImage => Images.StaticImages._000_paused_h32bit_16;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
public override Image ToolBarImage => Images.StaticImages._000_Paused_h32bit_24;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
public override string ToolBarText => Messages.MAINWINDOW_TOOLBAR_SUSPEND;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
public override string EnabledToolTipText => Messages.MAINWINDOW_TOOLBAR_SUSPENDVM;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
protected override bool ConfirmationRequired => true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
protected override string ConfirmationDialogTitle
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (GetSelection().Count == 1)
|
|
|
|
|
{
|
|
|
|
|
return Messages.CONFIRM_SUSPEND_VM_TITLE;
|
|
|
|
|
}
|
|
|
|
|
return Messages.CONFIRM_SUSPEND_VMS_TITLE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string ConfirmationDialogText
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SelectedItemCollection selection = GetSelection();
|
|
|
|
|
if (selection.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
VM vm = (VM)selection[0].XenObject;
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return vm.HAIsProtected() ? Messages.HA_CONFIRM_SUSPEND_VM : Messages.CONFIRM_SUSPEND_VM;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
return Messages.CONFIRM_SUSPEND_VMS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
protected override string ConfirmationDialogHelpId => "WarningVmLifeCycleSuspend";
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override CommandErrorDialog GetErrorDialogCore(IDictionary<IXenObject, string> cantRunReasons)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
foreach (VM vm in GetSelection().AsXenObjects<VM>())
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
if (!CanRun(vm) && vm.power_state == vm_power_state.Running)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return new CommandErrorDialog(Messages.ERROR_DIALOG_SUSPEND_VM_TITLE, Messages.ERROR_DIALOG_SUSPEND_VM_TEXT, cantRunReasons);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
public override Keys ShortcutKeys => Keys.Control | Keys.Y;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 06:16:32 +02:00
|
|
|
|
public override string ShortcutKeyDisplayString => Messages.MAINWINDOW_CTRL_Y;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override string GetCantRunReasonCore(IXenObject item)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-03-01 00:31:48 +01:00
|
|
|
|
VM vm = item as VM;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (vm == null)
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return base.GetCantRunReasonCore(item);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
if (vm.power_state == vm_power_state.Halted)
|
|
|
|
|
{
|
|
|
|
|
return Messages.VM_SHUT_DOWN;
|
|
|
|
|
}
|
2018-11-08 23:27:59 +01:00
|
|
|
|
if (vm.power_state == vm_power_state.Suspended)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
return Messages.VM_ALREADY_SUSPENDED;
|
|
|
|
|
}
|
2015-09-09 14:56:11 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
var noToolsOrDriversReason = GetCantRunNoToolsOrDriversReasonCore(item);
|
2018-11-08 23:27:59 +01:00
|
|
|
|
if (noToolsOrDriversReason != null)
|
|
|
|
|
{
|
|
|
|
|
return noToolsOrDriversReason;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vm.HasGPUPassthrough())
|
|
|
|
|
{
|
|
|
|
|
return Messages.VM_HAS_GPU_PASSTHROUGH;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return base.GetCantRunReasonCore(item);
|
2018-11-08 23:27:59 +01:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
protected override AsyncAction BuildAction(VM vm)
|
|
|
|
|
{
|
|
|
|
|
return new VMSuspendAction(vm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|