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;
|
2016-06-23 16:26:32 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using XenAdmin.Actions;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Dialogs;
|
2017-11-13 16:07:32 +01:00
|
|
|
|
using System.Text;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Commands
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shuts down the selected hosts. Shows a confirmation dialog.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class ShutDownHostCommand : Command
|
|
|
|
|
{
|
|
|
|
|
/// <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 ShutDownHostCommand()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShutDownHostCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
|
|
|
|
|
: base(mainWindow, selection)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ShutDownHostCommand(IMainWindow mainWindow, Host host, Control parent)
|
|
|
|
|
: base(mainWindow, host)
|
|
|
|
|
{
|
2020-09-13 00:21:05 +02:00
|
|
|
|
Parent = parent;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override void RunCore(SelectedItemCollection selection)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
List<AsyncAction> actions = new List<AsyncAction>();
|
2021-08-31 12:31:16 +02:00
|
|
|
|
foreach (Host host in selection.AsXenObjects<Host>(CanRun))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
this.MainWindowCommandInterface.CloseActiveWizards(host.Connection);
|
|
|
|
|
ShutdownHostAction action = new ShutdownHostAction(host,AddHostToPoolCommand.NtolDialog);
|
2018-10-16 17:24:54 +02:00
|
|
|
|
action.Completed += Program.MainWindow.action_Completed;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
actions.Add(action);
|
|
|
|
|
}
|
|
|
|
|
RunMultipleActions(actions, null, Messages.ACTION_HOSTS_SHUTTING_DOWN, Messages.ACTION_HOSTS_SHUTDOWN, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override bool CanRunCore(SelectedItemCollection selection)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return selection.AllItemsAre<Host>() && selection.AtLeastOneXenObjectCan<Host>(CanRun);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
private static bool CanRun(Host host)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
return host != null && host.IsLive() && !HelpersGUI.HasActiveHostAction(host) ;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 00:21:05 +02:00
|
|
|
|
public override string MenuText => Messages.MAINWINDOW_SHUTDOWN;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 00:21:05 +02:00
|
|
|
|
public override Image MenuImage => Images.StaticImages._001_ShutDown_h32bit_16;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 00:21:05 +02:00
|
|
|
|
protected override bool ConfirmationRequired => true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
protected override string ConfirmationDialogTitle
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
SelectedItemCollection selection = GetSelection();
|
|
|
|
|
|
|
|
|
|
if (selection.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
return Messages.CONFIRM_SHUTDOWN_SERVER_TITLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Messages.CONFIRM_SHUTDOWN_SERVERS_TITLE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override string ConfirmationDialogText
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
List<Host> hosts = GetSelection().AsXenObjects<Host>();
|
2016-06-23 16:26:32 +02:00
|
|
|
|
bool hasRunningVMs = false;
|
|
|
|
|
var hciHosts = new List<Host>();
|
2021-08-31 12:31:16 +02:00
|
|
|
|
var poolCoordinators = new List<Host>();
|
2016-06-23 16:26:32 +02:00
|
|
|
|
|
|
|
|
|
foreach (Host h in hosts)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
if (h.HasRunningVMs())
|
2016-06-23 16:26:32 +02:00
|
|
|
|
hasRunningVMs = true;
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
if (h.Connection.ResolveAll(h.resident_VMs).Exists(v => v.HciWarnBeforeShutdown()))
|
2016-06-23 16:26:32 +02:00
|
|
|
|
hciHosts.Add(h);
|
2017-11-13 16:07:32 +01:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
if (Helpers.HostIsCoordinator(h) && h.Connection.Cache.HostCount > 1)
|
|
|
|
|
poolCoordinators.Add(h);
|
2016-06-23 16:26:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 16:07:32 +01:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2017-11-16 16:16:55 +01:00
|
|
|
|
string firstWarning;
|
|
|
|
|
|
2017-11-13 16:07:32 +01:00
|
|
|
|
|
2016-06-23 16:26:32 +02:00
|
|
|
|
if (hciHosts.Count > 0)
|
2017-11-16 16:16:55 +01:00
|
|
|
|
firstWarning = hciHosts.Count == 1
|
2017-11-17 11:07:14 +01:00
|
|
|
|
? string.Format(Messages.CONFIRM_SHUTDOWN_HCI_WARN_SERVER, hciHosts[0].Name())
|
|
|
|
|
: string.Format(Messages.CONFIRM_SHUTDOWN_HCI_WARN_SERVERS, string.Join("\n", hciHosts.Select(h => h.Name())));
|
2017-11-13 16:07:32 +01:00
|
|
|
|
|
|
|
|
|
else if (hasRunningVMs)
|
2017-11-16 16:16:55 +01:00
|
|
|
|
firstWarning = hosts.Count == 1
|
2017-11-17 11:07:14 +01:00
|
|
|
|
? string.Format(Messages.CONFIRM_SHUTDOWN_SERVER, hosts[0].Name())
|
|
|
|
|
: Messages.CONFIRM_SHUTDOWN_SERVERS;
|
2017-11-16 16:16:55 +01:00
|
|
|
|
|
|
|
|
|
else firstWarning = hosts.Count == 1
|
2017-11-17 11:07:14 +01:00
|
|
|
|
? string.Format(Messages.CONFIRM_SHUTDOWN_SERVER_NO_VMS, hosts[0].Name())
|
|
|
|
|
: Messages.CONFIRM_SHUTDOWN_SERVERS_NO_VMS;
|
2017-11-13 16:07:32 +01:00
|
|
|
|
|
2017-11-16 16:16:55 +01:00
|
|
|
|
sb.Append(firstWarning);
|
2017-11-13 16:07:32 +01:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
if (poolCoordinators.Count == 1)
|
2017-11-13 16:07:32 +01:00
|
|
|
|
{
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
sb.AppendLine();
|
2021-08-31 12:31:16 +02:00
|
|
|
|
sb.AppendFormat(Messages.SHUT_DOWN_POOL_COORDINATOR_SINGLE, poolCoordinators[0].Name());
|
2017-11-13 16:07:32 +01:00
|
|
|
|
}
|
2021-08-31 12:31:16 +02:00
|
|
|
|
else if (poolCoordinators.Count > 1)
|
2017-11-13 16:07:32 +01:00
|
|
|
|
{
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
sb.AppendLine();
|
2021-08-31 12:31:16 +02:00
|
|
|
|
sb.AppendFormat(Messages.SHUT_DOWN_POOL_COORDINATOR_MULTIPLE, string.Join(", ", poolCoordinators.Select(h => h.Name())));
|
2017-11-13 16:07:32 +01:00
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
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 (Host host in GetSelection().AsXenObjects<Host>())
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
if (!CanRun(host) && host.IsLive())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return new CommandErrorDialog(Messages.ERROR_DIALOG_SHUTDOWN_HOST_TITLE, Messages.ERROR_DIALOG_SHUTDOWN_HOST_TEXT, cantRunReasons);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
Host host = item as Host;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (host == null)
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return base.GetCantRunReasonCore(item);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2017-09-03 04:33:29 +02:00
|
|
|
|
if (!host.IsLive())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
return Messages.HOST_ALREADY_SHUT_DOWN;
|
|
|
|
|
}
|
|
|
|
|
else if (HelpersGUI.HasActiveHostAction(host))
|
|
|
|
|
{
|
|
|
|
|
return Messages.HOST_ACTION_IN_PROGRESS;
|
|
|
|
|
}
|
2021-08-31 12:31:16 +02:00
|
|
|
|
return base.GetCantRunReasonCore(item);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 00:21:05 +02:00
|
|
|
|
protected override string ConfirmationDialogYesButtonLabel => Messages.CONFIRM_SHUTDOWN_SERVER_YES_BUTTON_LABEL;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-09-13 00:21:05 +02:00
|
|
|
|
protected override bool ConfirmationDialogNoButtonSelected => true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|