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;
|
|
|
|
using System.Collections.Generic;
|
2013-11-19 18:22:38 +01:00
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
using System.Windows.Forms;
|
|
|
|
using XenAdmin.Controls;
|
|
|
|
using XenAPI;
|
|
|
|
using XenAdmin.Core;
|
2018-04-27 13:13:10 +02:00
|
|
|
using XenAdmin.Actions;
|
2013-06-24 13:41:48 +02:00
|
|
|
using XenAdmin.Network;
|
|
|
|
|
|
|
|
|
2018-04-27 13:13:10 +02:00
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
namespace XenAdmin.Commands
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// This is the base ToolStripMenuItem for StartVMOnHostToolStripMenuItem, ResumeVMOnHostToolStripMenuItem and MigrateVMToolStripMenuItem.
|
|
|
|
/// </summary>
|
|
|
|
internal abstract class VMOperationToolStripMenuItem : CommandToolStripMenuItem
|
|
|
|
{
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
private readonly vm_operations _operation;
|
2016-11-03 16:35:25 +01:00
|
|
|
private readonly bool _resumeAfter;
|
|
|
|
|
|
|
|
protected VMOperationToolStripMenuItem(Command command, bool inContextMenu, vm_operations operation)
|
2013-06-24 13:41:48 +02:00
|
|
|
: base(command, inContextMenu)
|
|
|
|
{
|
|
|
|
if (operation != vm_operations.start_on && operation != vm_operations.resume_on && operation != vm_operations.pool_migrate)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid operation", "operation");
|
|
|
|
}
|
|
|
|
|
2016-11-03 16:35:25 +01:00
|
|
|
if (operation.Equals(vm_operations.resume_on))
|
|
|
|
_resumeAfter = true;
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
_operation = operation;
|
|
|
|
base.DropDownItems.Add(new ToolStripMenuItem());
|
|
|
|
}
|
|
|
|
|
2017-11-06 14:50:05 +01:00
|
|
|
protected override void OnDropDownClosed(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnDropDownClosed(e);
|
2018-05-04 16:40:42 +02:00
|
|
|
Stop();
|
2017-11-06 14:50:05 +01:00
|
|
|
}
|
|
|
|
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
protected override void OnDropDownOpening(EventArgs e)
|
|
|
|
{
|
|
|
|
base.DropDownItems.Clear();
|
|
|
|
|
|
|
|
// Work around bug in tool kit where disabled menu items show their dropdown menus
|
|
|
|
if (!Enabled)
|
|
|
|
{
|
|
|
|
ToolStripMenuItem emptyMenuItem = new ToolStripMenuItem(Messages.HOST_MENU_EMPTY);
|
|
|
|
emptyMenuItem.Font = Program.DefaultFont;
|
|
|
|
emptyMenuItem.Enabled = false;
|
|
|
|
base.DropDownItems.Add(emptyMenuItem);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisualMenuItemAlignData.ParentStrip = this;
|
|
|
|
IXenConnection connection = Command.GetSelection()[0].Connection;
|
|
|
|
bool wlb = Helpers.WlbEnabled(connection);
|
|
|
|
|
|
|
|
if (wlb)
|
|
|
|
{
|
2017-01-11 14:54:56 +01:00
|
|
|
base.DropDownItems.Add(new VMOperationToolStripMenuSubItem(Messages.WLB_OPT_MENU_OPTIMAL_SERVER, Images.StaticImages._000_ServerWlb_h32bit_16));
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-11 14:54:56 +01:00
|
|
|
base.DropDownItems.Add(new VMOperationToolStripMenuSubItem(Messages.HOME_SERVER_MENU_ITEM, Images.StaticImages._000_ServerHome_h32bit_16));
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
List<Host> hosts = new List<Host>(connection.Cache.Hosts);
|
|
|
|
hosts.Sort();
|
|
|
|
foreach (Host host in hosts)
|
|
|
|
{
|
2017-01-11 14:54:56 +01:00
|
|
|
VMOperationToolStripMenuSubItem item = new VMOperationToolStripMenuSubItem(String.Format(Messages.MAINWINDOW_CONTEXT_UPDATING, host.name_label.EscapeAmpersands()), Images.StaticImages._000_ServerDisconnected_h32bit_16);
|
2013-06-24 13:41:48 +02:00
|
|
|
item.Tag = host;
|
|
|
|
base.DropDownItems.Add(item);
|
|
|
|
}
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
UpdateHostList();
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 10:59:18 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Hook to add additional members to the menu item
|
|
|
|
/// Note: Called on main window thread by executing code
|
|
|
|
/// </summary>
|
|
|
|
protected virtual void AddAdditionalMenuItems(SelectedItemCollection selection) { return; }
|
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
#region UpdateHostList
|
2018-03-20 08:07:19 +01:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
private ProduceConsumerQueue workerQueueWithoutWlb;
|
|
|
|
readonly object locker = new object();
|
|
|
|
private bool stopped;
|
|
|
|
|
|
|
|
private bool Stopped
|
|
|
|
{
|
|
|
|
set
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
lock (locker)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
stopped = value;
|
2018-04-23 10:59:18 +02:00
|
|
|
}
|
2018-05-04 16:40:42 +02:00
|
|
|
}
|
|
|
|
get
|
|
|
|
{
|
|
|
|
lock (locker)
|
2018-04-23 10:59:18 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
return stopped;
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
2018-03-20 08:07:19 +01:00
|
|
|
}
|
2018-05-04 16:40:42 +02:00
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
private void Stop()
|
|
|
|
{
|
|
|
|
Stopped = true;
|
|
|
|
if (workerQueueWithoutWlb != null)
|
|
|
|
workerQueueWithoutWlb.CancelWorkers(false);
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
private void UpdateHostList()
|
|
|
|
{
|
|
|
|
Stopped = false;
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
var selection = Command.GetSelection();
|
|
|
|
var connection = selection[0].Connection;
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
if (Helpers.WlbEnabled(connection))
|
|
|
|
{
|
|
|
|
var vms = selection.AsXenObjects<VM>();
|
|
|
|
var retrieveVmRecommendationsAction = new WlbRetrieveVmRecommendationsAction(connection, vms);
|
|
|
|
retrieveVmRecommendationsAction.Completed += delegate
|
2018-04-27 13:13:10 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
if (Stopped || retrieveVmRecommendationsAction.Cancelled ||
|
|
|
|
!retrieveVmRecommendationsAction.Succeeded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var recommendations = new WlbRecommendations(vms, retrieveVmRecommendationsAction.Recommendations);
|
|
|
|
|
|
|
|
Program.Invoke(Program.MainWindow, delegate
|
2018-04-23 10:59:18 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
if (recommendations.IsError)
|
|
|
|
EnableAppropriateHostsNoWlb();
|
|
|
|
else
|
|
|
|
EnableAppropriateHostsWlb(recommendations);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
retrieveVmRecommendationsAction.RunAsync();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EnableAppropriateHostsNoWlb();
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
private void EnableAppropriateHostsWlb(WlbRecommendations recommendations)
|
|
|
|
{
|
|
|
|
if (Stopped || DropDownItems.Count == 0)
|
|
|
|
return;
|
2018-04-27 13:13:10 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// set the first menu item to be the WLB optimal server menu item
|
|
|
|
var firstItem = DropDownItems[0] as VMOperationToolStripMenuSubItem;
|
|
|
|
if (firstItem == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var selection = Command.GetSelection();
|
|
|
|
|
|
|
|
var firstItemCmd = new VMOperationWlbOptimalServerCommand(Command.MainWindowCommandInterface,
|
|
|
|
selection, _operation, recommendations);
|
|
|
|
|
|
|
|
firstItem.Command = firstItemCmd;
|
|
|
|
firstItem.Enabled = firstItemCmd.CanExecute();
|
|
|
|
|
|
|
|
var hostMenuItems = new List<VMOperationToolStripMenuSubItem>();
|
|
|
|
foreach (var item in DropDownItems)
|
|
|
|
{
|
|
|
|
var hostMenuItem = item as VMOperationToolStripMenuSubItem;
|
|
|
|
if (hostMenuItem == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
var host = hostMenuItem.Tag as Host;
|
|
|
|
if (host != null)
|
2018-04-27 13:13:10 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
var cmd = new VMOperationWlbHostCommand(Command.MainWindowCommandInterface, selection, host,
|
|
|
|
_operation, recommendations.GetStarRating(host));
|
|
|
|
|
|
|
|
hostMenuItem.Command = cmd;
|
|
|
|
hostMenuItem.Enabled = cmd.CanExecute();
|
|
|
|
|
|
|
|
hostMenuItems.Add(hostMenuItem);
|
2018-04-27 13:13:10 +02:00
|
|
|
}
|
2018-03-20 08:07:19 +01:00
|
|
|
}
|
2015-12-16 13:58:24 +01:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// sort the hostMenuItems by star rating
|
|
|
|
hostMenuItems.Sort(new WlbHostStarCompare());
|
2018-04-27 13:13:10 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// refresh the drop-down-items from the menuItems.
|
|
|
|
foreach (VMOperationToolStripMenuSubItem menuItem in hostMenuItems)
|
2018-04-23 10:59:18 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
DropDownItems.Insert(hostMenuItems.IndexOf(menuItem) + 1, menuItem);
|
|
|
|
}
|
2018-04-23 13:54:09 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
AddAdditionalMenuItems(selection);
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
private void EnableAppropriateHostsNoWlb()
|
|
|
|
{
|
|
|
|
if (Stopped || DropDownItems.Count == 0)
|
|
|
|
return;
|
2014-07-09 10:56:30 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
var firstItem = DropDownItems[0] as VMOperationToolStripMenuSubItem;
|
|
|
|
if (firstItem == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// API calls could happen in CanExecute(), which take time to wait. So a Producer-Consumer-Queue with size 25 is used here to :
|
|
|
|
// 1. Make API calls for different menu items happen in parallel;
|
|
|
|
// 2. Limit the count of concurrent threads (now it's 25).
|
|
|
|
workerQueueWithoutWlb = new ProduceConsumerQueue(25);
|
2014-07-09 10:56:30 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
var selection = Command.GetSelection();
|
|
|
|
var connection = selection[0].Connection;
|
|
|
|
var session = connection.DuplicateSession();
|
2018-04-23 13:54:09 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
var affinityHost = connection.Resolve(((VM) selection[0].XenObject).affinity);
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
EnqueueHostMenuItem(this, session, affinityHost, firstItem, true);
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
var hostMenuItems = DropDownItems.OfType<VMOperationToolStripMenuSubItem>().ToList();
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
if (Stopped)
|
|
|
|
return;
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// Adds the migrate wizard button, do this before the enable checks on the other items
|
|
|
|
AddAdditionalMenuItems(selection);
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
foreach (VMOperationToolStripMenuSubItem item in hostMenuItems)
|
|
|
|
{
|
|
|
|
var host = item.Tag as Host;
|
|
|
|
if (host != null)
|
2018-03-20 08:07:19 +01:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
var tempItem = item;
|
|
|
|
EnqueueHostMenuItem(this, session, host, tempItem, false);
|
2018-03-20 08:07:19 +01:00
|
|
|
}
|
2018-04-23 10:59:18 +02:00
|
|
|
}
|
2018-05-04 16:40:42 +02:00
|
|
|
}
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
private void EnqueueHostMenuItem(VMOperationToolStripMenuItem menu, Session session, Host host, VMOperationToolStripMenuSubItem hostMenuItem, bool isHomeServer)
|
|
|
|
{
|
|
|
|
workerQueueWithoutWlb.EnqueueItem(() =>
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
var selection = menu.Command.GetSelection();
|
|
|
|
var cmd = isHomeServer
|
|
|
|
? new VMOperationHomeServerCommand(menu.Command.MainWindowCommandInterface, selection, menu._operation, session)
|
|
|
|
: new VMOperationHostCommand(menu.Command.MainWindowCommandInterface, selection, delegate { return host; }, host.Name().EscapeAmpersands(), menu._operation, session);
|
|
|
|
|
|
|
|
var oldMigrateCmdCanRun = cmd.CanExecute();
|
2018-04-27 13:13:10 +02:00
|
|
|
if (Stopped)
|
|
|
|
return;
|
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
if (host == null || menu._operation == vm_operations.start_on || oldMigrateCmdCanRun)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
Program.Invoke(Program.MainWindow, delegate
|
|
|
|
{
|
|
|
|
hostMenuItem.Command = cmd;
|
|
|
|
hostMenuItem.Enabled = oldMigrateCmdCanRun;
|
|
|
|
});
|
2018-04-27 13:13:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
var cpmCmd = isHomeServer
|
|
|
|
? new CrossPoolMigrateToHomeCommand(menu.Command.MainWindowCommandInterface, selection, host)
|
|
|
|
: new CrossPoolMigrateCommand(menu.Command.MainWindowCommandInterface, selection, host, menu._resumeAfter);
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
var crossPoolMigrateCmdCanRun = cpmCmd.CanExecute();
|
|
|
|
if (Stopped)
|
|
|
|
return;
|
2018-04-23 10:59:18 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
Program.Invoke(Program.MainWindow, delegate
|
2018-04-23 10:59:18 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
if (crossPoolMigrateCmdCanRun || !string.IsNullOrEmpty(cpmCmd.CantExecuteReason))
|
2018-04-23 10:59:18 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
hostMenuItem.Command = cpmCmd;
|
|
|
|
hostMenuItem.Enabled = crossPoolMigrateCmdCanRun;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hostMenuItem.Command = cmd;
|
|
|
|
hostMenuItem.Enabled = false;
|
|
|
|
}
|
|
|
|
});
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
2018-05-04 16:40:42 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
/// <summary>
|
|
|
|
/// This class is an implementation of the 'IComparer' interface
|
|
|
|
/// for sorting vm placement menuItem List when wlb is enabled
|
|
|
|
/// </summary>
|
|
|
|
private class WlbHostStarCompare : IComparer<VMOperationToolStripMenuSubItem>
|
|
|
|
{
|
|
|
|
public int Compare(VMOperationToolStripMenuSubItem x, VMOperationToolStripMenuSubItem y)
|
2013-06-24 13:41:48 +02:00
|
|
|
{
|
2018-05-04 16:40:42 +02:00
|
|
|
int result = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// if x and y are enabled, compare their start rating
|
|
|
|
if (x.Enabled && y.Enabled)
|
|
|
|
result = y.StarRating.CompareTo(x.StarRating);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// if x and y are disabled, they are equal
|
|
|
|
else if (!x.Enabled && !y.Enabled)
|
|
|
|
result = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// if x is disabled, y is greater
|
|
|
|
else if (!x.Enabled)
|
|
|
|
result = 1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
// if y is disabled, x is greater
|
|
|
|
else if (!y.Enabled)
|
|
|
|
result = -1;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
2018-05-04 16:40:42 +02:00
|
|
|
return result;
|
2013-06-24 13:41:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|