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;
|
2015-03-17 17:35:12 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions.VMActions
|
|
|
|
|
{
|
2015-03-17 17:35:12 +01:00
|
|
|
|
public class VMMoveAction : AsyncAction
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-05-06 17:43:08 +02:00
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
private Dictionary<string, SR> _storageMapping;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2015-03-17 17:35:12 +01:00
|
|
|
|
public VMMoveAction(VM vm, Dictionary<string, SR> storageMapping, Host host)
|
2021-12-01 16:07:11 +01:00
|
|
|
|
: base(vm.Connection, "")
|
2015-03-17 17:35:12 +01:00
|
|
|
|
{
|
2021-08-10 13:37:37 +02:00
|
|
|
|
VM = vm;
|
|
|
|
|
Host = host;
|
|
|
|
|
Pool = Helpers.GetPool(vm.Connection);
|
2015-03-17 17:35:12 +01:00
|
|
|
|
if (vm.is_a_template)
|
2021-08-10 13:37:37 +02:00
|
|
|
|
Template = vm;
|
2015-03-17 17:35:12 +01:00
|
|
|
|
|
2020-05-06 17:43:08 +02:00
|
|
|
|
_storageMapping = storageMapping;
|
|
|
|
|
SR = _storageMapping.Values.FirstOrDefault();
|
2015-03-17 17:35:12 +01:00
|
|
|
|
|
|
|
|
|
PopulateApiMethodsToRoleCheck();
|
2021-12-01 16:07:11 +01:00
|
|
|
|
|
|
|
|
|
var sourceHost = vm.Home();
|
|
|
|
|
if (sourceHost != null && !sourceHost.Equals(host))
|
|
|
|
|
Title = string.Format(Messages.ACTION_VM_MOVING_HOST, vm.Name(), sourceHost, host.Name());
|
|
|
|
|
else if (storageMapping.Count == 1)
|
|
|
|
|
Title = string.Format(Messages.ACTION_VM_MOVING_SR, vm.Name(), storageMapping.Values.ElementAt(0).Name());
|
|
|
|
|
else
|
|
|
|
|
Title = string.Format(Messages.ACTION_VM_MOVING, vm.Name());
|
2015-03-17 17:35:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 13:37:37 +02:00
|
|
|
|
public VMMoveAction(VM vm, SR sr, Host host)
|
|
|
|
|
: this(vm, GetStorageMapping(vm, sr), host)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-10 13:37:37 +02:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-10 13:37:37 +02:00
|
|
|
|
private static Dictionary<string, SR> GetStorageMapping(VM vm, SR sr)
|
|
|
|
|
{
|
|
|
|
|
var storageMapping = new Dictionary<string, SR>();
|
2020-05-06 17:43:08 +02:00
|
|
|
|
foreach (var vbdRef in vm.VBDs)
|
2015-03-17 17:35:12 +01:00
|
|
|
|
{
|
2020-05-06 17:43:08 +02:00
|
|
|
|
var vbd = vm.Connection.Resolve(vbdRef);
|
|
|
|
|
if (vbd != null)
|
2021-08-10 13:37:37 +02:00
|
|
|
|
storageMapping.Add(vbd.VDI.opaque_ref, sr);
|
2015-03-17 17:35:12 +01:00
|
|
|
|
}
|
2021-08-10 13:37:37 +02:00
|
|
|
|
return storageMapping;
|
2015-03-17 17:35:12 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region RBAC Dependencies
|
|
|
|
|
private void PopulateApiMethodsToRoleCheck()
|
|
|
|
|
{
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ApiMethodsToRoleCheck.AddRange(Role.CommonSessionApiList);
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange(Role.CommonTaskApiList);
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.copy");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.set_name_description");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.set_suspend_SR");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vm.destroy");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vdi.destroy");
|
|
|
|
|
}
|
2015-03-17 17:35:12 +01:00
|
|
|
|
#endregion
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2020-05-06 17:43:08 +02:00
|
|
|
|
// move the progress bar above 0, it's more reassuring to see than a blank bar as we copy the first disk
|
|
|
|
|
PercentComplete += 10;
|
|
|
|
|
int halfstep = 90 / (VM.VBDs.Count * 2);
|
|
|
|
|
|
|
|
|
|
var exceptions = new List<Exception>();
|
|
|
|
|
|
|
|
|
|
foreach (var vbdRef in VM.VBDs)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-05-06 17:43:08 +02:00
|
|
|
|
if (Cancelling)
|
|
|
|
|
throw new CancelledException();
|
|
|
|
|
|
|
|
|
|
var oldVBD = Connection.Resolve(vbdRef);
|
|
|
|
|
|
|
|
|
|
if (oldVBD == null || !oldVBD.GetIsOwner())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (_storageMapping == null ||
|
|
|
|
|
!_storageMapping.TryGetValue(oldVBD.VDI.opaque_ref, out SR sr) || sr == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var curVdi = Connection.Resolve(oldVBD.VDI);
|
|
|
|
|
if (curVdi == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (curVdi.SR.opaque_ref == sr.opaque_ref)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Description = string.Format(Messages.ACTION_MOVING_VDI_TO_SR,
|
2021-08-09 16:24:15 +02:00
|
|
|
|
Helpers.GetName(curVdi), Helpers.GetName(Connection.Resolve(curVdi.SR)), Helpers.GetName(sr));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-05-06 17:43:08 +02:00
|
|
|
|
RelatedTask = VDI.async_copy(Session, oldVBD.VDI.opaque_ref, sr.opaque_ref);
|
|
|
|
|
PollToCompletion(PercentComplete, PercentComplete + halfstep);
|
|
|
|
|
var newVDI = Connection.WaitForCache(new XenRef<VDI>(Result));
|
|
|
|
|
|
|
|
|
|
var newVBD = new VBD
|
|
|
|
|
{
|
|
|
|
|
userdevice = oldVBD.userdevice,
|
|
|
|
|
bootable = oldVBD.bootable,
|
|
|
|
|
mode = oldVBD.mode,
|
|
|
|
|
type = oldVBD.type,
|
|
|
|
|
unpluggable = oldVBD.unpluggable,
|
|
|
|
|
other_config = oldVBD.other_config,
|
|
|
|
|
VDI = new XenRef<VDI>(newVDI.opaque_ref),
|
|
|
|
|
VM = new XenRef<VM>(VM.opaque_ref)
|
|
|
|
|
};
|
|
|
|
|
newVBD.SetIsOwner(oldVBD.GetIsOwner());
|
|
|
|
|
|
|
|
|
|
try
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-05-06 17:43:08 +02:00
|
|
|
|
VDI.destroy(Session, oldVBD.VDI.opaque_ref);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.ErrorFormat("Failed to destroy old VDI {0}", oldVBD.VDI.opaque_ref);
|
|
|
|
|
exceptions.Add(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 17:43:08 +02:00
|
|
|
|
Connection.WaitForCache(VBD.create(Session, newVBD));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-05-06 17:43:08 +02:00
|
|
|
|
PercentComplete += halfstep;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2020-05-06 17:43:08 +02:00
|
|
|
|
|
2021-12-01 16:07:11 +01:00
|
|
|
|
Description = string.Empty;
|
2021-08-10 13:37:37 +02:00
|
|
|
|
|
2020-05-06 17:43:08 +02:00
|
|
|
|
if (SR != null)
|
|
|
|
|
VM.set_suspend_SR(Session, VM.opaque_ref, SR.opaque_ref);
|
|
|
|
|
|
|
|
|
|
if (exceptions.Count > 0)
|
2021-08-10 13:37:37 +02:00
|
|
|
|
throw new Exception(string.Format(Messages.ACTION_VM_MOVING_VDI_DESTROY_FAILURE, VM.NameWithLocation()));
|
2020-05-06 17:43:08 +02:00
|
|
|
|
|
|
|
|
|
Description = Messages.MOVED;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|