/* Copyright (c) Citrix Systems Inc. * 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; using System.Text; using XenAPI; using XenAdmin.Actions; using XenAdmin.Core; using System.Windows.Forms; using XenAdmin.Dialogs; using System.Collections.ObjectModel; using XenAdmin.Network; using System.Drawing; namespace XenAdmin.Commands { /// /// Installs XenServer Tools on the selected VM. /// internal class InstallToolsCommand : Command { /// /// 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. /// public InstallToolsCommand() { } public InstallToolsCommand(IMainWindow mainWindow, IEnumerable selection) : base(mainWindow, selection) { } public InstallToolsCommand(IMainWindow mainWindow, IEnumerable vms) : this(mainWindow, ConvertToSelection(vms)) { } /// /// Initializes a new instance of the class. /// /// The main window. /// The vm. /// The parent window for any UI. public InstallToolsCommand(IMainWindow mainWindow, VM vm, Control parent) : base(mainWindow, vm) { SetParent(parent); } public InstallToolsCommand(IMainWindow mainWindow, VM vm) : this(mainWindow, vm, null) { } /// /// Installs tools on the single VM selected. /// /// The for the Command. public AsyncAction ExecuteGetAction() { SelectedItemCollection selection = GetSelection(); if (selection.Count > 1) { throw new InvalidOperationException("This method can only be used with a single VM selected."); } if (selection.ContainsOneItemOfType()) { VM vm = (VM)selection[0].XenObject; if (CanExecute(vm)) { return SingleVMExecute(vm); } } return null; } /// /// Attempts to install tools on the vm /// /// /// null if user cancels or an AsyncAction. This is either the InstallPVToolsAction or the CreateCdDriveAction if the VM needed a DVD drive. private AsyncAction SingleVMExecute(VM vm) { if (vm.FindVMCDROM() == null) { if (new ThreeButtonDialog( new ThreeButtonDialog.Details( null, Messages.NEW_DVD_DRIVE_REQUIRED, Messages.XENCENTER), ThreeButtonDialog.ButtonYes, ThreeButtonDialog.ButtonNo).ShowDialog(Parent) == DialogResult.Yes) { CreateCdDriveAction createDriveAction = new CreateCdDriveAction(vm, true,NewDiskDialog.ShowMustRebootBoxCD,NewDiskDialog.ShowVBDWarningBox); new ActionProgressDialog(createDriveAction, ProgressBarStyle.Marquee).ShowDialog(Parent); if (createDriveAction.Succeeded) { ShowMustRebootBox(); } return createDriveAction; } } else { DialogResult dr = new InstallToolsWarningDialog(vm.Connection).ShowDialog(Parent); if (dr == DialogResult.Yes) { InstallPVToolsAction installToolsAction = new InstallPVToolsAction( vm, XSToolsSRNotFound, Properties.Settings.Default.ShowHiddenVMs); installToolsAction.Completed += InstallToolsActionCompleted; installToolsAction.RunAsync(); return installToolsAction; } } return null; } private static void XSToolsSRNotFound() { Program.Invoke(Program.MainWindow, delegate { new ThreeButtonDialog( new ThreeButtonDialog.Details(SystemIcons.Error, Messages.XS_TOOLS_SR_NOT_FOUND, Messages.XENCENTER)).ShowDialog(Program.MainWindow); }); } /// /// Attempts to install tools on several VMs /// /// /// Whether the action was launched (i.e., the user didn't Cancel) private bool MultipleVMExecute(List vms) { bool newDvdDrivesRequired = false; foreach (VM vm in vms) { if (CanExecute(vm) && vm.FindVMCDROM() == null) { newDvdDrivesRequired = true; break; } } if (newDvdDrivesRequired) { if (new ThreeButtonDialog(new ThreeButtonDialog.Details(SystemIcons.Warning, Messages.NEW_DVD_DRIVES_REQUIRED, Messages.XENCENTER), ThreeButtonDialog.ButtonYes, ThreeButtonDialog.ButtonNo).ShowDialog(Parent) == DialogResult.Yes) { foreach (VM vm in vms) { if (CanExecute(vm) && vm.FindVMCDROM() == null) { CreateCdDriveAction createDriveAction = new CreateCdDriveAction(vm, true,NewDiskDialog.ShowMustRebootBoxCD,NewDiskDialog.ShowVBDWarningBox); new ActionProgressDialog(createDriveAction, ProgressBarStyle.Marquee).ShowDialog(Parent); } } ShowMustRebootBox(); return true; } } else { List vmConnections = new List(); foreach (VM vm in vms) vmConnections.Add(vm.Connection); if (new InstallToolsWarningDialog(null, true, vmConnections).ShowDialog(Parent) == DialogResult.Yes) { foreach (VM vm in vms) { InstallPVToolsAction installToolsAction = new InstallPVToolsAction(vm, XSToolsSRNotFound, Properties.Settings.Default.ShowHiddenVMs); if (vms.IndexOf(vm) == 0) { installToolsAction.Completed += FirstInstallToolsActionCompleted; } else { installToolsAction.Completed += InstallToolsActionCompleted; } installToolsAction.RunAsync(); } return true; } } return false; } private void FirstInstallToolsActionCompleted(ActionBase sender) { InstallPVToolsAction action = (InstallPVToolsAction)sender; MainWindowCommandInterface.Invoke(delegate { // Try to select the VM on which the PV tools are to be installed if (MainWindowCommandInterface.SelectObjectInTree(action.VM)) { // Switch to the VM's console tab MainWindowCommandInterface.SwitchToTab(MainWindow.Tab.Console); } }); } private void InstallToolsActionCompleted(ActionBase sender) { InstallPVToolsAction action = (InstallPVToolsAction)sender; MainWindowCommandInterface.Invoke(delegate { Program.MainWindow.SelectObject(action.VM); Program.MainWindow.TheTabControl.SelectedTab = Program.MainWindow.TabPageConsole; }); } private void ShowMustRebootBox() { if (!MainWindowCommandInterface.RunInAutomatedTestMode) { new ThreeButtonDialog( new ThreeButtonDialog.Details( SystemIcons.Information, Messages.NEW_DVD_DRIVE_REBOOT_TOOLS, Messages.NEW_DVD_DRIVE_CREATED)).ShowDialog(Parent); } } protected override void ExecuteCore(SelectedItemCollection selection) { List vms = selection.AsXenObjects(CanExecute); if (vms.Count == 1) SingleVMExecute(vms[0]); else MultipleVMExecute(vms); } public bool ConfirmAndExecute() { List vms = GetSelection().AsXenObjects(CanExecute); if (vms.Count == 0) return true; if (vms.Count == 1) return (SingleVMExecute(vms[0]) != null); return MultipleVMExecute(vms); } public static bool CanExecute(VM vm) { return vm != null && !vm.is_a_template && !vm.Locked && vm.GetVirtualisationStatus != VM.VirtualisationStatus.UNKNOWN && vm.GetVirtualisationStatus != VM.VirtualisationStatus.OPTIMIZED && vm.power_state == vm_power_state.Running && CanViewVMConsole(vm.Connection); } public static bool CanExecuteAll(List vms) { foreach (VM vm in vms) { if (!CanExecute(vm)) return false; } return true; } private static bool CanViewVMConsole(XenAdmin.Network.IXenConnection xenConnection) { if (xenConnection.Session == null) return false; if (!Helpers.MidnightRideOrGreater(xenConnection)) return true; RbacMethodList r = new RbacMethodList("http/connect_console"); if (Role.CanPerform(r, xenConnection, false)) return true; return false; } protected override bool CanExecuteCore(SelectedItemCollection selection) { return selection.AllItemsAre() && selection.AtLeastOneXenObjectCan(CanExecute); } public override string MenuText { get { return Messages.MAINWINDOW_INSTALL_TOOLS; } } } }