2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* 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.Linq;
|
|
|
|
|
using XenAdmin.Controls;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using XenAdmin.Dialogs;
|
|
|
|
|
using XenAdmin.Actions.VMActions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Commands
|
|
|
|
|
{
|
|
|
|
|
internal class DragDropMigrateVMCommand : DragDropCommand
|
|
|
|
|
{
|
|
|
|
|
public DragDropMigrateVMCommand(IMainWindow mainWindow, VirtualTreeNode targetNode, IDataObject dragData)
|
|
|
|
|
: base(mainWindow, targetNode, dragData)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string StatusBarText
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-04-26 14:36:21 +02:00
|
|
|
|
var targetHost = GetTargetNodeAncestorAsXenObjectOrGroupingTag<Host>();
|
|
|
|
|
if (targetHost == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var draggedVMs = GetDraggedItemsAsXenObjects<VM>();
|
|
|
|
|
|
|
|
|
|
foreach (VM vm in draggedVMs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-26 14:36:21 +02:00
|
|
|
|
if (vm == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!vm.allowed_operations.Contains(vm_operations.migrate_send))
|
|
|
|
|
{
|
|
|
|
|
if (vm.power_state == vm_power_state.Running)
|
|
|
|
|
return string.Format(Messages.MIGRATION_NOT_ALLOWED, vm.Name().Ellipsise(50), BrandManager.VmTools);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!LiveMigrateAllowedInVersion(targetHost, vm))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-26 14:36:21 +02:00
|
|
|
|
if (IsACrossPoolMigrate(targetHost, vm))
|
|
|
|
|
return string.Format(Messages.MIGRATION_NOT_ALLOWED_OUTSIDE_POOL, vm.Name().Ellipsise(50));
|
|
|
|
|
|
|
|
|
|
if (vm.GetStorageHost(true) != null) //Non-agile
|
|
|
|
|
return string.Format(Messages.MIGRATION_NOT_ALLOWED_NO_SHARED_STORAGE, vm.Name().Ellipsise(50));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-26 14:36:21 +02:00
|
|
|
|
|
|
|
|
|
var homeHost = vm.Home();
|
|
|
|
|
|
2022-05-20 19:55:23 +02:00
|
|
|
|
if (Helpers.ProductVersionCompare(Helpers.HostProductVersion(targetHost),
|
2022-04-26 14:36:21 +02:00
|
|
|
|
Helpers.HostProductVersion(homeHost ?? Helpers.GetCoordinator(vm.Connection))) < 0)
|
|
|
|
|
return Messages.OLDER_THAN_CURRENT_SERVER;
|
|
|
|
|
|
|
|
|
|
if (targetHost != homeHost && VMOperationHostCommand.VmCpuIncompatibleWithHost(targetHost, vm))
|
|
|
|
|
return string.Format(Messages.MIGRATION_NOT_ALLOWED_CPU_FEATURES, vm.Name().Ellipsise(50));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-26 14:36:21 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool LiveMigrateAllowedInVersion(Host targetHost, VM draggedVM)
|
|
|
|
|
{
|
|
|
|
|
return !Helpers.FeatureForbidden(targetHost.Connection, Host.RestrictCrossPoolMigrate) &&
|
|
|
|
|
!Helpers.FeatureForbidden(draggedVM.Connection, Host.RestrictCrossPoolMigrate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsAnIntraPoolMigrate(Host targetHost, VM draggedVM)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
List<SR> draggedSRs = new List<SR>(draggedVM.SRs());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
bool allStorageShared = draggedSRs.TrueForAll(sr => sr.shared);
|
|
|
|
|
return !IsACrossPoolMigrate(targetHost, draggedVM) && !allStorageShared;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsACrossPoolMigrate(Host targetHost, VM draggedVM )
|
|
|
|
|
{
|
|
|
|
|
Pool draggedVMPool = Helpers.GetPool(draggedVM.Connection);
|
|
|
|
|
Pool targetPool = targetHost == null ? null : Helpers.GetPool(targetHost.Connection);
|
|
|
|
|
return targetHost != null && (targetPool == null || draggedVMPool == null || targetPool.opaque_ref != draggedVMPool.opaque_ref);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override bool CanRunCore()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Host targetHost = GetTargetNodeAncestorAsXenObjectOrGroupingTag<Host>();
|
2022-05-18 11:54:44 +02:00
|
|
|
|
if (targetHost == null || !targetHost.IsLive()|| !targetHost.Connection.IsConnected)
|
2022-04-27 14:15:19 +02:00
|
|
|
|
return false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-27 14:15:19 +02:00
|
|
|
|
List<VM> draggedVMs = GetDraggedItemsAsXenObjects<VM>();
|
|
|
|
|
if (draggedVMs.Count <= 0)
|
|
|
|
|
return false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-27 14:15:19 +02:00
|
|
|
|
foreach (VM vm in draggedVMs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-27 14:15:19 +02:00
|
|
|
|
if (vm.Connection == null || !vm.Connection.IsConnected)
|
|
|
|
|
return false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-27 14:15:19 +02:00
|
|
|
|
if (!LiveMigrateAllowedInVersion(targetHost, vm))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-27 14:15:19 +02:00
|
|
|
|
Pool targetPool = Helpers.GetPool(targetHost.Connection);
|
|
|
|
|
if (targetPool == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Pool sourcePool = Helpers.GetPool(vm.Connection);
|
|
|
|
|
|
|
|
|
|
if (sourcePool == null || sourcePool.opaque_ref != targetPool.opaque_ref)
|
|
|
|
|
{
|
|
|
|
|
// dragged VM must be in same pool as target
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vm.GetStorageHost(true) != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-04-27 14:15:19 +02:00
|
|
|
|
// dragged VM must be agile
|
|
|
|
|
return false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-27 14:15:19 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (Helpers.FeatureForbidden(vm.Connection, Host.RestrictIntraPoolMigrate))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-04-27 14:15:19 +02:00
|
|
|
|
if (vm.allowed_operations == null || !vm.allowed_operations.Contains(vm_operations.pool_migrate))
|
|
|
|
|
{
|
|
|
|
|
// migrate not allowed
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Host homeHost = vm.Home();
|
|
|
|
|
|
|
|
|
|
if (homeHost == null || homeHost == targetHost)
|
|
|
|
|
{
|
|
|
|
|
// dragged VM must currently be shown below a host
|
|
|
|
|
return false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-27 14:15:19 +02:00
|
|
|
|
|
2022-05-20 19:55:23 +02:00
|
|
|
|
if (Helpers.ProductVersionCompare(Helpers.HostProductVersion(targetHost), Helpers.HostProductVersion(homeHost)) < 0)
|
2022-04-27 14:15:19 +02:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (VMOperationHostCommand.VmCpuIncompatibleWithHost(targetHost, vm))
|
|
|
|
|
return false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-04-27 14:15:19 +02:00
|
|
|
|
|
|
|
|
|
return true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override void RunCore()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Host targetHost = GetTargetNodeAncestorAsXenObjectOrGroupingTag<Host>();
|
|
|
|
|
List<VM> draggedVMs = GetDraggedItemsAsXenObjects<VM>();
|
|
|
|
|
|
|
|
|
|
foreach (VM draggedVM in draggedVMs)
|
|
|
|
|
{
|
|
|
|
|
//Check if agile vm.
|
|
|
|
|
VBD cd = draggedVM.FindVMCDROM();
|
|
|
|
|
if (cd != null)
|
|
|
|
|
{
|
|
|
|
|
VDI vdi = cd.Connection.Resolve<VDI>(cd.VDI);
|
|
|
|
|
if (vdi != null)
|
|
|
|
|
{
|
|
|
|
|
SR sr = cd.Connection.Resolve<SR>(vdi.SR);
|
|
|
|
|
if (sr != null && !sr.shared)
|
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var dlg = new WarningDialog(Messages.DRAG_DROP_LOCAL_CD_LOADED)
|
|
|
|
|
{WindowTitle = Messages.DRAG_DROP_LOCAL_CD_LOADED_TITLE})
|
2016-06-20 11:49:12 +02:00
|
|
|
|
{
|
|
|
|
|
dlg.ShowDialog(MainWindowCommandInterface.Form);
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( draggedVMs.TrueForAll(vm => LiveMigrateAllowedInVersion(targetHost, vm)))
|
|
|
|
|
{
|
|
|
|
|
if (draggedVMs.Any(vm => IsACrossPoolMigrate(targetHost, vm)) ||
|
|
|
|
|
draggedVMs.Any(vm => IsAnIntraPoolMigrate(targetHost, vm)))
|
|
|
|
|
{
|
|
|
|
|
List<SelectedItem> selectedItems = new List<SelectedItem>();
|
|
|
|
|
draggedVMs.ForEach(vm => selectedItems.Add(new SelectedItem(vm)));
|
2021-08-31 12:31:16 +02:00
|
|
|
|
new CrossPoolMigrateCommand(MainWindowCommandInterface, selectedItems, targetHost).Run();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Confirm())
|
|
|
|
|
{
|
|
|
|
|
foreach (VM draggedVM in draggedVMs)
|
|
|
|
|
{
|
|
|
|
|
if (draggedVM.Connection != null && draggedVM.Connection.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
new VMMigrateAction(draggedVM, targetHost).RunAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool Confirm()
|
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
|
if (Program.RunInAutomatedTestMode)
|
|
|
|
|
return true;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
List<VM> draggedVMs = GetDraggedItemsAsXenObjects<VM>();
|
|
|
|
|
Host targetHost = GetTargetNodeAncestorAsXenObjectOrGroupingTag<Host>();
|
|
|
|
|
|
2020-04-22 15:47:03 +02:00
|
|
|
|
var msg = draggedVMs.Count == 1
|
|
|
|
|
? string.Format(Messages.MAINWINDOW_CONFIRM_MIGRATE, draggedVMs[0].Name().Ellipsise(50), targetHost.Name().Ellipsise(50))
|
|
|
|
|
: string.Format(Messages.MAINWINDOW_CONFIRM_MIGRATE_MULTIPLE, targetHost.Name().Ellipsise(50));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-04-22 15:47:03 +02:00
|
|
|
|
using (var dialog = new WarningDialog(msg, ThreeButtonDialog.ButtonYes, ThreeButtonDialog.ButtonNo)
|
|
|
|
|
{WindowTitle = Messages.MESSAGEBOX_CONFIRM})
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-04-22 15:47:03 +02:00
|
|
|
|
return dialog.ShowDialog(Program.MainWindow) == DialogResult.Yes;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-22 15:47:03 +02:00
|
|
|
|
|
2022-04-27 20:49:32 +02:00
|
|
|
|
public override VirtualTreeNode HighlightNode => GetTargetNodeAncestor<Host>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|