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 System.Collections.Generic;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
2016-08-05 16:46:07 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
2022-03-28 14:43:24 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Run several actions. The outer action is asynchronous, but the sub-actions are run synchronously within it.
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public class MultipleAction : AsyncAction, IDisposable
|
|
|
|
|
{
|
2022-03-28 14:43:24 +02:00
|
|
|
|
private static readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2023-07-22 16:52:32 +02:00
|
|
|
|
public List<AsyncAction> SubActions { get; }
|
2022-03-28 14:43:24 +02:00
|
|
|
|
private readonly string _endDescription;
|
|
|
|
|
private readonly bool _stopOnFirstException;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2022-02-21 22:53:43 +01:00
|
|
|
|
public bool ShowSubActionsDetails { get; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public string SubActionTitle { get; private set; }
|
|
|
|
|
public string SubActionDescription { get; private set; }
|
|
|
|
|
|
2022-02-21 22:53:43 +01:00
|
|
|
|
public MultipleAction(IXenConnection connection, string title, string startDescription,
|
|
|
|
|
string endDescription, List<AsyncAction> subActions, bool suppressHistory = false,
|
|
|
|
|
bool showSubActionsDetails = false, bool stopOnFirstException = false)
|
2014-07-21 12:19:04 +02:00
|
|
|
|
: base(connection, title, startDescription, suppressHistory)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-28 14:43:24 +02:00
|
|
|
|
_endDescription = endDescription;
|
2023-07-22 16:52:32 +02:00
|
|
|
|
SubActions = subActions;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
ShowSubActionsDetails = showSubActionsDetails;
|
2022-02-21 22:53:43 +01:00
|
|
|
|
_stopOnFirstException = stopOnFirstException;
|
|
|
|
|
Completed += MultipleAction_Completed;
|
|
|
|
|
RegisterEvents();
|
2015-01-09 13:54:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 14:43:24 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The multiple action gets its ApiMethodsToRoleCheck by accumulating the lists from each of the sub-actions
|
|
|
|
|
/// </summary>
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public override RbacMethodList GetApiMethodsToRoleCheck
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2022-03-28 14:43:24 +02:00
|
|
|
|
var list = new RbacMethodList();
|
2023-07-22 16:52:32 +02:00
|
|
|
|
foreach (var subAction in SubActions)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
list.AddRange(subAction.GetApiMethodsToRoleCheck);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-21 22:53:43 +01:00
|
|
|
|
protected sealed override void Run()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
PercentComplete = 0;
|
|
|
|
|
var exceptions = new List<Exception>();
|
2016-08-05 16:46:07 +02:00
|
|
|
|
RecomputeCanCancel();
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
RunSubActions(exceptions);
|
|
|
|
|
|
2022-03-28 14:43:24 +02:00
|
|
|
|
Tick(100, _endDescription);
|
2022-01-11 14:20:16 +01:00
|
|
|
|
|
2014-07-21 12:24:33 +02:00
|
|
|
|
if (exceptions.Count > 1)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-03-28 14:43:24 +02:00
|
|
|
|
foreach (var e in exceptions)
|
|
|
|
|
_log.Error(e);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
Exception = new Exception(Messages.SOME_ERRORS_ENCOUNTERED);
|
|
|
|
|
}
|
2016-08-05 16:46:07 +02:00
|
|
|
|
if (Cancelling)
|
|
|
|
|
Exception = new CancelledException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void RecomputeCanCancel()
|
|
|
|
|
{
|
2023-07-22 16:52:32 +02:00
|
|
|
|
CanCancel = !IsCompleted && SubActions.Any(a => !a.IsCompleted);
|
2016-08-05 16:46:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void CancelRelatedTask()
|
|
|
|
|
{
|
2023-07-22 16:52:32 +02:00
|
|
|
|
foreach (var subAction in SubActions.Where(a => !a.IsCompleted))
|
2016-08-05 16:46:07 +02:00
|
|
|
|
{
|
|
|
|
|
subAction.Cancel();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterEvents()
|
|
|
|
|
{
|
2023-07-22 16:52:32 +02:00
|
|
|
|
foreach (var subAction in SubActions)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
subAction.Changed += SubActionChanged;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 14:43:24 +02:00
|
|
|
|
private void DeRegisterEvents()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2023-07-22 16:52:32 +02:00
|
|
|
|
foreach (var subAction in SubActions)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
subAction.Changed -= SubActionChanged;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 15:28:21 +02:00
|
|
|
|
private void SubActionChanged(ActionBase sender)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2022-02-21 22:53:43 +01:00
|
|
|
|
if (sender is AsyncAction subAction)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
SubActionTitle = subAction.Title;
|
|
|
|
|
SubActionDescription = subAction.Description;
|
2016-08-05 16:46:07 +02:00
|
|
|
|
RecalculatePercentComplete();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
OnChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-09 11:03:11 +02:00
|
|
|
|
protected virtual void RecalculatePercentComplete()
|
2016-08-05 16:46:07 +02:00
|
|
|
|
{
|
|
|
|
|
int total = 0;
|
2023-07-22 16:52:32 +02:00
|
|
|
|
int n = SubActions.Count;
|
|
|
|
|
foreach (var action in SubActions)
|
2016-08-05 16:46:07 +02:00
|
|
|
|
total += action.PercentComplete;
|
2022-02-21 22:53:43 +01:00
|
|
|
|
PercentComplete = total / n;
|
2016-08-05 16:46:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
protected virtual void RunSubActions(List<Exception> exceptions)
|
|
|
|
|
{
|
2023-07-22 16:52:32 +02:00
|
|
|
|
foreach (var subAction in SubActions)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2016-08-05 16:46:07 +02:00
|
|
|
|
if (Cancelling) // don't start any more actions
|
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-02-03 18:41:23 +01:00
|
|
|
|
SubActionTitle = subAction.Title;
|
2022-04-05 12:52:32 +02:00
|
|
|
|
subAction.RunSync(Session);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-02-21 22:53:43 +01:00
|
|
|
|
if (e is Failure f && Connection != null && f.ErrorDescription[0] == Failure.RBAC_PERMISSION_DENIED)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Failure.ParseRBACFailure(f, Connection, Session ?? Connection.Session);
|
2022-03-28 14:43:24 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
exceptions.Add(e);
|
|
|
|
|
// Record the first exception we come to. Though later if there are more than one we will replace this with non specific one.
|
|
|
|
|
if (Exception == null)
|
|
|
|
|
Exception = e;
|
2022-02-21 22:53:43 +01:00
|
|
|
|
if (_stopOnFirstException)
|
2015-01-09 13:54:12 +01:00
|
|
|
|
break;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 15:28:21 +02:00
|
|
|
|
protected virtual void MultipleAction_Completed(ActionBase sender)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
// The MultipleAction can sometimes complete prematurely, for example
|
|
|
|
|
// if the sudo dialog is cancelled, of (less likely) if one of the
|
2022-03-28 14:43:24 +02:00
|
|
|
|
// sub-actions throws an exception in its GetApiMethodsToRoleCheck.
|
|
|
|
|
// In this case, we need to cancel this action's sub-actions.
|
2023-07-22 16:52:32 +02:00
|
|
|
|
foreach (var subAction in SubActions)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
if (!subAction.IsCompleted)
|
|
|
|
|
subAction.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2022-03-28 14:43:24 +02:00
|
|
|
|
DeRegisterEvents();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|