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;
|
2018-04-20 14:26:42 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
|
|
|
|
public class SrRepairAction : AsyncAction
|
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
private readonly List<Host> _hostList = new List<Host>();
|
|
|
|
|
private Failure failure;
|
|
|
|
|
private string failureDescription;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="connection"></param>
|
|
|
|
|
/// <param name="sr">Must not be null.</param>
|
|
|
|
|
public SrRepairAction(IXenConnection connection, SR sr,bool isSharedAction)
|
2017-09-03 04:33:29 +02:00
|
|
|
|
: base(connection, isSharedAction ? string.Format(Messages.ACTION_SR_SHARING, sr.NameWithoutHost()) : string.Format(Messages.ACTION_SR_REPAIRING, sr.NameWithoutHost()))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
this.isSharedAction = isSharedAction;
|
2021-09-15 16:47:20 +02:00
|
|
|
|
SR = sr;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
#region RBAC Dependencies
|
|
|
|
|
ApiMethodsToRoleCheck.Add("pbd.plug");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("pbd.create");
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange(Role.CommonSessionApiList);
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly bool isSharedAction = false;
|
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2019-11-08 13:09:48 +01:00
|
|
|
|
log.DebugFormat("Repairing SR='{0}'", SR.Name());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
//CA-176935, CA-173497 - we need to run Plug for the coordinator first - creating a new list of hosts where the coordinator is always first
|
2015-07-24 15:48:09 +02:00
|
|
|
|
var allHosts = new List<Host>();
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
var coordinator = Helpers.GetCoordinator(Connection);
|
|
|
|
|
if (coordinator != null)
|
|
|
|
|
allHosts.Add(coordinator);
|
2015-07-24 15:48:09 +02:00
|
|
|
|
|
|
|
|
|
foreach (var host in Connection.Cache.Hosts)
|
|
|
|
|
if (!allHosts.Contains(host))
|
|
|
|
|
allHosts.Add(host);
|
|
|
|
|
|
|
|
|
|
foreach (Host host in allHosts)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Host stoHost = SR.GetStorageHost();
|
|
|
|
|
if (SR.shared
|
|
|
|
|
|| (stoHost != null && host.opaque_ref == stoHost.opaque_ref))
|
|
|
|
|
{
|
|
|
|
|
_hostList.Add(host);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_hostList.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 13:09:48 +01:00
|
|
|
|
log.DebugFormat("_hostList: {0}", string.Join(",", _hostList.Select(s => s.Name())));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
int max = _hostList.Count * 2;
|
|
|
|
|
int delta = 100 / max;
|
|
|
|
|
|
2018-04-20 14:26:42 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (SR.GetSRType(true) == SR.SRTypes.gfs2)
|
|
|
|
|
{
|
|
|
|
|
var cluster = Connection.Cache.Clusters.FirstOrDefault();
|
|
|
|
|
if (cluster != null)
|
|
|
|
|
{
|
|
|
|
|
Cluster.pool_resync(Session, cluster.opaque_ref);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2019-11-08 13:53:24 +01:00
|
|
|
|
log.Debug("Cluster pool resync failed.", e);
|
2018-04-20 14:26:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
foreach (Host host in _hostList)
|
|
|
|
|
{
|
|
|
|
|
if (!host.HasPBDTo(SR) && SR.shared && SR.PBDs.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
PBD template = SR.Connection.Resolve(SR.PBDs[0]);
|
|
|
|
|
if (template != null)
|
|
|
|
|
{
|
|
|
|
|
this.Description = string.Format(Messages.ACTION_SR_REPAIR_CREATE_PBD, Helpers.GetName(host));
|
2019-11-08 12:42:18 +01:00
|
|
|
|
log.Debug($"Creating PBD for {Helpers.GetName(host)}.");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-09-14 12:52:15 +02:00
|
|
|
|
var newPbd = new PBD
|
|
|
|
|
{
|
|
|
|
|
currently_attached = false,
|
|
|
|
|
device_config = new Dictionary<string, string>(template.device_config),
|
|
|
|
|
SR = template.SR,
|
|
|
|
|
host = new XenRef<Host>(host.opaque_ref)
|
|
|
|
|
};
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-09-14 12:52:15 +02:00
|
|
|
|
RelatedTask = XenAPI.PBD.async_create(this.Session, newPbd);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (PercentComplete + delta <= 100)
|
|
|
|
|
{
|
|
|
|
|
PollToCompletion(PercentComplete, PercentComplete + delta);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PollToCompletion(PercentComplete, 100);
|
|
|
|
|
PercentComplete = 100;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (XenAPI.Failure f)
|
|
|
|
|
{
|
|
|
|
|
failure = f;
|
|
|
|
|
failureDescription = Description;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PercentComplete += delta;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PBD thePBD = host.GetPBDTo(SR);
|
|
|
|
|
if (thePBD != null && !thePBD.currently_attached)
|
|
|
|
|
{
|
|
|
|
|
this.Description = string.Format(Messages.ACTION_SR_REPAIR_PLUGGING_PBD, Helpers.GetName(host));
|
2019-11-08 12:42:18 +01:00
|
|
|
|
log.Debug($"Plugging PBD for {Helpers.GetName(host)}.");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
RelatedTask = XenAPI.PBD.async_plug(this.Session, thePBD.opaque_ref);
|
|
|
|
|
|
|
|
|
|
if (PercentComplete + delta <= 100)
|
|
|
|
|
{
|
|
|
|
|
PollToCompletion(PercentComplete, PercentComplete + delta);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PollToCompletion(PercentComplete, 100);
|
|
|
|
|
PercentComplete = 100;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (XenAPI.Failure f)
|
|
|
|
|
{
|
|
|
|
|
failure = f;
|
|
|
|
|
failureDescription = Description;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PercentComplete += delta;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (failure != null && failureDescription != null)
|
|
|
|
|
{
|
|
|
|
|
Description = failureDescription;
|
|
|
|
|
throw failure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CA-14928: Sometimes we have too many PBDs if there has just been a host
|
|
|
|
|
// eject and the GC hasn't collected the PBD yet.
|
|
|
|
|
//
|
|
|
|
|
//if (SR.PBDs.Count != _hostList.Count && SR.shared && !SR.IsToolsSR)
|
|
|
|
|
//{
|
|
|
|
|
// throw new Exception(Messages.ACTION_SR_REPAIR_FAILED);
|
|
|
|
|
//}
|
|
|
|
|
if (isSharedAction)
|
2017-09-03 04:33:29 +02:00
|
|
|
|
Description = string.Format(Messages.ACTION_SR_SHARE_SUCCESSFUL, SR.NameWithoutHost());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
else
|
2017-09-03 04:33:29 +02:00
|
|
|
|
Description = string.Format(Messages.ACTION_SR_REPAIR_SUCCESSFUL, SR.NameWithoutHost());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void CancelRelatedTask()
|
|
|
|
|
{
|
|
|
|
|
this.Description = Messages.ACTION_SR_REPAIR_CANCELLED;
|
|
|
|
|
|
|
|
|
|
base.CancelRelatedTask();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|