Merge pull request #1180 from mcintyre94/CA-218260

CA-218260: Reverting to Snapshots causes VM to go back to parent host
This commit is contained in:
Mihaela Stoica 2016-09-22 14:12:05 +01:00 committed by GitHub
commit 502f436b03

View File

@ -38,10 +38,13 @@ namespace XenAdmin.Actions
public class VMSnapshotRevertAction : PureAsyncAction
{
private VM m_Snapshot;
private Host previousHost; // The host the VM was running on before the snapshot
public VMSnapshotRevertAction(VM snapshot)
: base(snapshot.Connection, String.Format(Messages.ACTION_VM_REVERT_SNAPSHOT_TITLE, snapshot.Name))
{
this.VM = Connection.Resolve<VM>(snapshot.snapshot_of);
previousHost = Connection.Resolve<Host>(VM.resident_on);
this.m_Snapshot = snapshot;
Description = String.Format(Messages.VM_REVERTING, m_Snapshot.Name);
@ -91,19 +94,48 @@ namespace XenAdmin.Actions
{
if (vm.power_state == vm_power_state.Halted)
{
RelatedTask = XenAPI.VM.async_start(Session,
vm.opaque_ref, false, false);
if (previousHost != null && VMCanBootOnHost(vm, previousHost))
{
RelatedTask = XenAPI.VM.async_start_on(Session,
vm.opaque_ref, previousHost.opaque_ref, false, false);
}
else
{
RelatedTask = XenAPI.VM.async_start(Session,
vm.opaque_ref, false, false);
}
}
else if (vm.power_state == vm_power_state.Suspended)
{
RelatedTask = XenAPI.VM.async_resume(Session, vm.opaque_ref, false, false);
if (previousHost != null && VMCanBootOnHost(vm, previousHost))
{
RelatedTask = XenAPI.VM.async_resume_on(Session, vm.opaque_ref, previousHost.opaque_ref,
false, false);
}
else
{
RelatedTask = XenAPI.VM.async_resume(Session, vm.opaque_ref, false, false);
}
}
PollToCompletion();
}
}
}
private bool VMCanBootOnHost(VM vm, Host host)
{
try
{
VM.assert_can_boot_here(Session, vm.opaque_ref, host.opaque_ref);
}
catch
{
return false;
}
return true;
}
}
}