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;
|
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
|
|
|
|
{
|
2015-03-17 17:35:12 +01:00
|
|
|
|
public 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)
|
|
|
|
|
: base(vm.Connection, Messages.ACTION_VM_MOVING)
|
|
|
|
|
{
|
|
|
|
|
this.Description = Messages.ACTION_PREPARING;
|
|
|
|
|
this.VM = vm;
|
|
|
|
|
this.Host = host;
|
|
|
|
|
this.Pool = Helpers.GetPool(vm.Connection);
|
|
|
|
|
if (vm.is_a_template)
|
|
|
|
|
this.Template = vm;
|
|
|
|
|
|
|
|
|
|
StorageMapping = storageMapping;
|
|
|
|
|
SR = StorageMapping.Values.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
PopulateApiMethodsToRoleCheck();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public VMMoveAction(VM vm, SR sr, Host host, string namelabel)
|
2017-09-03 04:33:29 +02:00
|
|
|
|
: base(vm.Connection, string.Format(Messages.ACTION_VM_MOVING_TITLE, vm.Name(), namelabel, sr.NameWithoutHost()))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
this.Description = Messages.ACTION_PREPARING;
|
|
|
|
|
this.VM = vm;
|
|
|
|
|
this.Host = host;
|
2015-03-17 17:35:12 +01:00
|
|
|
|
this.Pool = Helpers.GetPool(vm.Connection);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
this.SR = sr;
|
|
|
|
|
if (vm.is_a_template)
|
|
|
|
|
this.Template = vm;
|
|
|
|
|
|
2015-03-17 17:35:12 +01:00
|
|
|
|
// create a storage map where all VDIs are mapped to the same SR
|
|
|
|
|
StorageMapping = new Dictionary<string, SR>();
|
|
|
|
|
var vbds = vm.Connection.ResolveAll(vm.VBDs);
|
|
|
|
|
foreach (var vbd in vbds)
|
|
|
|
|
{
|
|
|
|
|
StorageMapping.Add(vbd.VDI.opaque_ref, sr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PopulateApiMethodsToRoleCheck();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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()
|
|
|
|
|
{
|
|
|
|
|
this.Description = Messages.ACTION_VM_MOVING;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var vbds = Connection.ResolveAll(VM.VBDs);
|
2013-07-16 11:25:02 +02:00
|
|
|
|
int halfstep = (int)(90/(vbds.Count * 2));
|
2013-06-24 13:41:48 +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;
|
|
|
|
|
Exception exn = null;
|
|
|
|
|
|
|
|
|
|
foreach (VBD oldVBD in vbds)
|
|
|
|
|
{
|
2017-09-05 03:15:38 +02:00
|
|
|
|
if (!oldVBD.GetIsOwner())
|
2013-06-24 13:41:48 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var curVdi = Connection.Resolve(oldVBD.VDI);
|
2016-10-28 11:57:10 +02:00
|
|
|
|
if (curVdi == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (StorageMapping == null || !StorageMapping.ContainsKey(oldVBD.VDI.opaque_ref))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
SR sr = StorageMapping[oldVBD.VDI.opaque_ref];
|
|
|
|
|
if (sr == null || curVdi.SR.opaque_ref == sr.opaque_ref)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
2015-03-17 17:35:12 +01:00
|
|
|
|
RelatedTask = XenAPI.VDI.async_copy(Session, oldVBD.VDI.opaque_ref, sr.opaque_ref);
|
2013-07-16 11:25:02 +02:00
|
|
|
|
PollToCompletion(PercentComplete, PercentComplete + halfstep);
|
|
|
|
|
var newVDI = Connection.WaitForCache(new XenRef<VDI>(Result));
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
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)
|
|
|
|
|
};
|
2017-09-05 03:15:38 +02:00
|
|
|
|
newVBD.SetIsOwner(oldVBD.GetIsOwner());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
VBD vbd = oldVBD;
|
|
|
|
|
BestEffort(ref exn, () => VDI.destroy(Session, vbd.VDI.opaque_ref));
|
|
|
|
|
Connection.WaitForCache<VBD>(VBD.create(Session, newVBD));
|
|
|
|
|
|
|
|
|
|
PercentComplete += halfstep;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 17:01:55 +01:00
|
|
|
|
if (SR != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
VM.set_suspend_SR(Session, VM.opaque_ref, SR.opaque_ref);
|
|
|
|
|
|
|
|
|
|
if (exn != null)
|
|
|
|
|
throw exn;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (CancelledException)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
this.Description = string.Format(Messages.MOVE_CANCELLED, VM.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
this.Description = Messages.MOVED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|