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.Core;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
using XenAdmin.Wizards.ExportWizard;
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Commands
|
|
|
|
|
{
|
|
|
|
|
class ExportCommand : Command
|
|
|
|
|
{
|
|
|
|
|
public ExportCommand()
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
public ExportCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
|
|
|
|
|
: base(mainWindow, selection)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
public override string MenuText { get { return Messages.MENU_EXPORT; } }
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override bool CanRunCore(SelectedItemCollection selection)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Host hostAncestor = selection.HostAncestorFromConnection;
|
|
|
|
|
Pool poolAncestor = selection.PooAncestorFromConnection;
|
|
|
|
|
|
|
|
|
|
if ((poolAncestor != null || hostAncestor != null) //CA-61207: this check ensures there's no cross-pool selection
|
2016-10-14 13:21:59 +02:00
|
|
|
|
&& (selection.FirstIs<Pool>() || selection.FirstIs<Host>() || selection.FirstIsRealVM || selection.FirstIs<VM_appliance>()))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (selection.AllItemsAre<VM>())
|
|
|
|
|
return selection.AtLeastOneXenObjectCan<VM>(CanExportVm);
|
|
|
|
|
|
|
|
|
|
if (selection.AllItemsAre<VM_appliance>())
|
|
|
|
|
{
|
|
|
|
|
if (selection.Count != 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var appliance = ((VM_appliance)selection.FirstAsXenObject);
|
|
|
|
|
return appliance.VMs.TrueForAll(vmRef =>
|
|
|
|
|
{
|
|
|
|
|
var vm = appliance.Connection.Resolve(vmRef);
|
|
|
|
|
return vm != null
|
|
|
|
|
&& CanExportVm(vm);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
if ((hostAncestor != null && hostAncestor.enabled && hostAncestor.IsLive() && selection[0].Connection.IsConnected)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|| (poolAncestor != null && Helpers.PoolHasEnabledHosts(poolAncestor)))
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
var vms = selection.FirstAsXenObject.Connection.Cache.VMs.Where(vm => vm.is_a_real_vm() && CanExportVm(vm) && vm.Show(Properties.Settings.Default.ShowHiddenVMs)).ToList();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (vms.Count > 0)
|
|
|
|
|
return vms.Any(CanExportVm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
if (vm != null && vm.power_state != vm_power_state.Halted)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return Messages.SHUTDOWN_BEFORE_EXPORT;
|
2019-03-01 00:31:48 +01:00
|
|
|
|
|
|
|
|
|
if (item != null && item.Connection != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2019-03-01 00:31:48 +01:00
|
|
|
|
var vms = item.Connection.Cache.VMs.Where(xvm =>
|
2017-09-03 04:33:29 +02:00
|
|
|
|
xvm.is_a_real_vm() && CanExportVm(xvm) &&
|
2013-06-24 13:41:48 +02:00
|
|
|
|
xvm.Show(Properties.Settings.Default.ShowHiddenVMs)).ToList();
|
2019-03-01 00:31:48 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (vms.Count == 0)
|
|
|
|
|
return Messages.NO_HALTED_VMS;
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
protected override void RunCore(SelectedItemCollection selection)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
var con = selection.GetConnectionOfFirstItem();
|
|
|
|
|
MainWindowCommandInterface.ShowPerConnectionWizard(con, new ExportApplianceWizard(con, selection));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CanExportVm(VM vm)
|
|
|
|
|
{
|
|
|
|
|
return !vm.is_a_template && !vm.Locked && vm.allowed_operations != null && vm.allowed_operations.Contains(vm_operations.export);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|