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;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
public class VMSnapshotRevertAction : AsyncAction
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
private readonly VM m_Snapshot;
|
|
|
|
|
private readonly Host previousHost; // The host the VM was running on before the snapshot
|
|
|
|
|
private bool _finished;
|
|
|
|
|
private readonly bool _revertPowerState;
|
2016-09-21 18:39:03 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public VMSnapshotRevertAction(VM snapshot)
|
2022-12-22 00:02:07 +01:00
|
|
|
|
: base(snapshot.Connection, string.Format(Messages.ACTION_VM_REVERT_SNAPSHOT_TITLE, snapshot.Name()))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
VM = Connection.Resolve(snapshot.snapshot_of);
|
|
|
|
|
previousHost = Connection.Resolve(VM.resident_on);
|
|
|
|
|
m_Snapshot = snapshot;
|
|
|
|
|
Description = string.Format(Messages.VM_REVERTING, m_Snapshot.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-12-22 00:02:07 +01:00
|
|
|
|
if (snapshot.snapshot_info.TryGetValue("power-state-at-snapshot", out var state) &&
|
|
|
|
|
state == vm_power_state.Running.ToString())
|
|
|
|
|
_revertPowerState = true;
|
|
|
|
|
|
|
|
|
|
ApiMethodsToRoleCheck.Add("VM.async_revert");
|
|
|
|
|
|
|
|
|
|
if (_revertPowerState)
|
|
|
|
|
{
|
|
|
|
|
if (VM.power_state == vm_power_state.Halted)
|
|
|
|
|
{
|
|
|
|
|
if (previousHost == null)
|
|
|
|
|
ApiMethodsToRoleCheck.Add("VM.async_start");
|
|
|
|
|
else
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange("VM.assert_can_boot_here", "VM.async_start_on");
|
|
|
|
|
}
|
|
|
|
|
else if (VM.power_state == vm_power_state.Suspended)
|
|
|
|
|
{
|
|
|
|
|
if (previousHost == null)
|
|
|
|
|
ApiMethodsToRoleCheck.Add("VM.async_resume");
|
|
|
|
|
else
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange("VM.assert_can_boot_here", "VM.async_resume_on");
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2022-12-22 00:02:07 +01:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
Description = string.Format(Messages.VM_REVERTING, m_Snapshot.Name());
|
|
|
|
|
RelatedTask = VM.async_revert(Session, m_Snapshot.opaque_ref);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
PollToCompletion();
|
|
|
|
|
_finished = true;
|
2022-01-11 14:20:16 +01:00
|
|
|
|
Tick(90, string.Format(Messages.REVERTING_POWER_STATE, VM.Name()));
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
if (_revertPowerState)
|
|
|
|
|
RevertPowerState(VM);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
2021-09-10 15:19:08 +02:00
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 14:20:16 +01:00
|
|
|
|
Tick(100, string.Format(Messages.VM_REVERTED, m_Snapshot.Name()));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int PercentComplete
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return base.PercentComplete;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if ((value < 90 && !_finished) || (value >= 90 && _finished))
|
|
|
|
|
base.PercentComplete = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-22 00:02:07 +01:00
|
|
|
|
private void RevertPowerState(VM vm)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
if (vm.power_state == vm_power_state.Halted)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
if (previousHost != null && VMCanBootOnHost(vm, previousHost))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
RelatedTask = VM.async_start_on(Session, vm.opaque_ref, previousHost.opaque_ref, false, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RelatedTask = VM.async_start(Session, vm.opaque_ref, false, false);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-22 00:02:07 +01:00
|
|
|
|
else if (vm.power_state == vm_power_state.Suspended)
|
|
|
|
|
{
|
|
|
|
|
if (previousHost != null && VMCanBootOnHost(vm, previousHost))
|
|
|
|
|
{
|
|
|
|
|
RelatedTask = VM.async_resume_on(Session, vm.opaque_ref, previousHost.opaque_ref, false, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
RelatedTask = VM.async_resume(Session, vm.opaque_ref, false, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PollToCompletion();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2016-09-22 14:36:43 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|