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;
|
|
|
|
|
using System.Collections.Generic;
|
2020-05-20 04:13:17 +02:00
|
|
|
|
using System.Net;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
|
2020-05-20 04:13:17 +02:00
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
|
|
|
|
public class DestroyBondAction : AsyncAction
|
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// All bonds equivalent to the one given in the constructor (i.e. one per host).
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly List<Bond> Bonds = new List<Bond>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-31 12:31:16 +02:00
|
|
|
|
/// The interfaces of all the bonds in Bonds.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// </summary>
|
2021-08-31 12:31:16 +02:00
|
|
|
|
private readonly List<PIF> Interfaces = new List<PIF>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-31 12:31:16 +02:00
|
|
|
|
/// All members under each bond in Bonds. These will be plugged at the end, in order to
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// get metrics like the carrier flag, if they haven't already been brought up as a
|
|
|
|
|
/// management interface.
|
|
|
|
|
/// </summary>
|
2021-08-31 12:31:16 +02:00
|
|
|
|
private readonly List<PIF> Members = new List<PIF>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-08-31 12:31:16 +02:00
|
|
|
|
/// The first member (ordered by name) under each interface.
|
2013-06-24 13:41:48 +02:00
|
|
|
|
/// </summary>
|
2021-08-31 12:31:16 +02:00
|
|
|
|
private readonly Dictionary<PIF, PIF> FirstMembers = new Dictionary<PIF, PIF>();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The network that we're either going to destroy or rename.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly XenAPI.Network Network;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The bond's name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly string Name;
|
|
|
|
|
|
2015-11-08 17:31:48 +01:00
|
|
|
|
public DestroyBondAction(Bond bond)
|
2017-09-03 04:33:29 +02:00
|
|
|
|
: base(bond.Connection, string.Format(Messages.ACTION_DESTROY_BOND_TITLE, bond.Name()),
|
|
|
|
|
string.Format(Messages.ACTION_DESTROY_BOND_DESCRIPTION, bond.Name()))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
#region RBAC Dependencies
|
|
|
|
|
ApiMethodsToRoleCheck.Add("host.management_reconfigure");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("network.destroy");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vif.plug");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("vif.unplug");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("pif.reconfigure_ip");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("pif.plug");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("bond.destroy");
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange(XenAPI.Role.CommonSessionApiList);
|
|
|
|
|
ApiMethodsToRoleCheck.AddRange(XenAPI.Role.CommonTaskApiList);
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-09-03 04:33:29 +02:00
|
|
|
|
Name = bond.Name();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
Pool = Helpers.GetPoolOfOne(Connection);
|
|
|
|
|
|
|
|
|
|
foreach (Host host in Connection.Cache.Hosts)
|
|
|
|
|
{
|
|
|
|
|
Bond b = NetworkingHelper.FindBond(host, bond);
|
|
|
|
|
if (b != null)
|
|
|
|
|
{
|
|
|
|
|
Bonds.Add(b);
|
|
|
|
|
|
|
|
|
|
b.Locked = true;
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
PIF bondInterface = Connection.Resolve(b.master);
|
|
|
|
|
if (bondInterface != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
Interfaces.Add(bondInterface);
|
|
|
|
|
bondInterface.Locked = true;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
List<PIF> members = Connection.ResolveAll(b.slaves);
|
|
|
|
|
NetworkingHelper.Sort(members);
|
|
|
|
|
foreach (PIF pif in members)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
Members.Add(pif);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
pif.Locked = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
FirstMembers[bondInterface] = Connection.Resolve(b.primary_slave);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
if (!FirstMembers.ContainsKey(bondInterface) && members.Count != 0)
|
|
|
|
|
FirstMembers[bondInterface] = members[0];
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppliesTo.Add(host.opaque_ref);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
PIF coordinator_bond_interface = Connection.Resolve(bond.master);
|
|
|
|
|
if (coordinator_bond_interface != null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
Network = Connection.Resolve(coordinator_bond_interface.network);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Network.Locked = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2020-05-20 04:13:17 +02:00
|
|
|
|
PercentComplete = 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Connection.ExpectDisruption = true;
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
int incr = Interfaces.Count > 0 ? 50 / Interfaces.Count : 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2020-05-20 04:13:17 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
foreach (PIF bondInterface in Interfaces)
|
2020-05-20 04:13:17 +02:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
NetworkingActionHelpers.MoveManagementInterfaceName(this, bondInterface, FirstMembers[bondInterface]);
|
2020-05-20 04:13:17 +02:00
|
|
|
|
PercentComplete += incr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (WebException we)
|
|
|
|
|
{
|
|
|
|
|
//ignore keep-alive failure since disruption is expected
|
|
|
|
|
if (we.Status != WebExceptionStatus.KeepAliveFailure)
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
PercentComplete = 50;
|
|
|
|
|
|
2020-11-30 14:40:16 +01:00
|
|
|
|
var caughtExceptions = new List<Exception>();
|
2020-05-20 04:13:17 +02:00
|
|
|
|
|
|
|
|
|
int inc = Bonds.Count > 0 ? 40 / Bonds.Count : 0;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
foreach (Bond bond in Bonds)
|
|
|
|
|
{
|
|
|
|
|
Bond bond1 = bond;
|
2020-11-30 14:40:16 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
RelatedTask = Bond.async_destroy(Session, bond1.opaque_ref);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
if (Connection != null && Connection.ExpectDisruption &&
|
|
|
|
|
exn is WebException webEx && webEx.Status == WebExceptionStatus.KeepAliveFailure)
|
|
|
|
|
{
|
|
|
|
|
//ignore
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Error($"Failed to destroy bond {bond1.opaque_ref}", exn);
|
|
|
|
|
caughtExceptions.Add(exn);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-20 04:13:17 +02:00
|
|
|
|
PollToCompletion(PercentComplete, PercentComplete + inc);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 04:13:17 +02:00
|
|
|
|
PercentComplete = 90;
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (Network != null)
|
|
|
|
|
{
|
2020-05-20 04:13:17 +02:00
|
|
|
|
string oldNetworkName = Network.Name();
|
|
|
|
|
|
|
|
|
|
log.DebugFormat("Destroying network {0} ({1})...", oldNetworkName, Network.uuid);
|
2020-11-30 14:40:16 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
XenAPI.Network.destroy(Session, Network.opaque_ref);
|
|
|
|
|
log.DebugFormat("Network {0} ({1}) destroyed.", oldNetworkName, Network.uuid);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
if (Connection != null && Connection.ExpectDisruption &&
|
|
|
|
|
exn is WebException webEx && webEx.Status == WebExceptionStatus.KeepAliveFailure)
|
2015-11-08 17:31:48 +01:00
|
|
|
|
{
|
2020-11-30 14:40:16 +01:00
|
|
|
|
//ignore
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log.Error($"Failed to destroy bond {Network.opaque_ref}", exn);
|
|
|
|
|
caughtExceptions.Add(exn);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 14:40:16 +01:00
|
|
|
|
if (caughtExceptions.Count > 0)
|
|
|
|
|
throw caughtExceptions[0];
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
Description = string.Format(Messages.ACTION_DESTROY_BOND_DONE, Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UnlockAll()
|
|
|
|
|
{
|
|
|
|
|
foreach (Bond bond in Bonds)
|
|
|
|
|
bond.Locked = false;
|
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
foreach (PIF bondInterface in Interfaces)
|
|
|
|
|
bondInterface.Locked = false;
|
2020-10-07 14:34:03 +02:00
|
|
|
|
|
2021-08-31 12:31:16 +02:00
|
|
|
|
foreach (PIF pif in Members)
|
2020-10-07 14:34:03 +02:00
|
|
|
|
pif.Locked = false;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (Network != null)
|
|
|
|
|
Network.Locked = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Clean()
|
|
|
|
|
{
|
|
|
|
|
Connection.ExpectDisruption = false;
|
|
|
|
|
UnlockAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|