2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2015-02-05 18:01:14 +01: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 System.Text;
|
2017-06-27 14:48:54 +02:00
|
|
|
|
using XenAdmin.Core;
|
2015-02-05 18:01:14 +01:00
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
public class GetDiskSpaceRequirementsAction : AsyncAction
|
2015-02-05 18:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
private readonly string updateName;
|
|
|
|
|
private readonly long updateSize;
|
2017-01-16 20:59:50 +01:00
|
|
|
|
private readonly Pool_patch currentPatch;
|
2022-12-22 00:02:07 +01:00
|
|
|
|
private readonly DiskSpaceRequirements.OperationTypes operation = DiskSpaceRequirements.OperationTypes.upload;
|
2015-02-05 18:01:14 +01:00
|
|
|
|
|
|
|
|
|
public DiskSpaceRequirements DiskSpaceRequirements { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This constructor is used to calculate the disk space requirements for installing or uploading a single patch
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GetDiskSpaceRequirementsAction(Host host, Pool_patch patch, bool suppressHistory)
|
2017-09-03 04:33:29 +02:00
|
|
|
|
: this(host, patch.Name(), patch.size, suppressHistory)
|
2015-02-05 18:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
currentPatch = patch;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 20:59:50 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This constructor is used to calculate the disk space when the required space is known only
|
|
|
|
|
/// </summary>
|
2022-12-22 00:02:07 +01:00
|
|
|
|
public GetDiskSpaceRequirementsAction(Host host, long size, bool suppressHistory, DiskSpaceRequirements.OperationTypes operation)
|
2017-01-16 20:59:50 +01:00
|
|
|
|
: this(host, null, size, suppressHistory)
|
|
|
|
|
{
|
2016-08-08 19:05:42 +02:00
|
|
|
|
this.operation = operation;
|
2016-08-05 16:56:55 +02:00
|
|
|
|
}
|
2015-02-05 18:01:14 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This constructor is used to check disk space for installing or uploading an update of given size
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GetDiskSpaceRequirementsAction(Host host, string updateName, long size, bool suppressHistory)
|
|
|
|
|
: base(host.Connection, Messages.ACTION_GET_DISK_SPACE_REQUIREMENTS_TITLE, suppressHistory)
|
|
|
|
|
{
|
|
|
|
|
Host = host;
|
|
|
|
|
this.updateName = updateName;
|
|
|
|
|
updateSize = size;
|
2022-12-22 00:02:07 +01:00
|
|
|
|
ApiMethodsToRoleCheck.Add("host.call_plugin");
|
2015-02-05 18:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
Description = string.Format(Messages.ACTION_GET_DISK_SPACE_REQUIREMENTS_DESCRIPTION, Host.Name());
|
2015-02-05 18:01:14 +01:00
|
|
|
|
|
|
|
|
|
string result;
|
|
|
|
|
|
2015-02-16 12:11:30 +01:00
|
|
|
|
// get required disk space
|
|
|
|
|
long requiredDiskSpace = updateSize;
|
2017-06-27 14:48:54 +02:00
|
|
|
|
if (!Helpers.ElyOrGreater(Host)) // for ElyOrGreater we don't need to call get_required_space, because it will always return updateSize
|
2015-02-16 12:11:30 +01:00
|
|
|
|
{
|
2017-06-27 14:48:54 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-22 00:02:07 +01:00
|
|
|
|
var args = new Dictionary<string, string> { { "size", updateSize.ToString() } };
|
2015-03-11 15:49:07 +01:00
|
|
|
|
|
2017-06-27 14:48:54 +02:00
|
|
|
|
result = Host.call_plugin(Session, Host.opaque_ref, "disk-space", "get_required_space", args);
|
|
|
|
|
requiredDiskSpace = Convert.ToInt64(result);
|
|
|
|
|
}
|
|
|
|
|
catch (Failure failure)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
log.WarnFormat("Plugin call disk-space.get_required_space on {0} failed with {1}", Host.Name(), failure.Message);
|
2017-06-27 14:48:54 +02:00
|
|
|
|
requiredDiskSpace = 0;
|
|
|
|
|
}
|
2015-02-16 12:11:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 18:01:14 +01:00
|
|
|
|
// get available disk space
|
|
|
|
|
long availableDiskSpace = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = Host.call_plugin(Session, Host.opaque_ref, "disk-space", "get_avail_host_disk_space", new Dictionary<string, string>());
|
|
|
|
|
availableDiskSpace = Convert.ToInt64(result);
|
|
|
|
|
}
|
|
|
|
|
catch (Failure failure)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
log.WarnFormat("Plugin call disk-space.get_avail_host_disk_space on {0} failed with {1}", Host.Name(), failure.Message);
|
2015-02-05 18:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get reclaimable disk space (excluding current patch)
|
2017-01-16 20:59:50 +01:00
|
|
|
|
long reclaimableDiskSpace = 0;
|
|
|
|
|
|
2017-06-27 14:48:54 +02:00
|
|
|
|
if (availableDiskSpace < requiredDiskSpace && !Helpers.ElyOrGreater(Host)) // for ElyOrGreater we shouldn't call get_reclaimable_disk_space
|
2017-01-16 20:59:50 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var args = new Dictionary<string, string>();
|
|
|
|
|
if (currentPatch != null)
|
|
|
|
|
args.Add("exclude", currentPatch.uuid);
|
|
|
|
|
result = Host.call_plugin(Session, Host.opaque_ref, "disk-space", "get_reclaimable_disk_space", args);
|
|
|
|
|
reclaimableDiskSpace = Convert.ToInt64(result);
|
|
|
|
|
}
|
|
|
|
|
catch (Failure failure)
|
|
|
|
|
{
|
2017-09-03 04:33:29 +02:00
|
|
|
|
log.WarnFormat("Plugin call disk-space.get_reclaimable_disk_space on {0} failed with {1}", Host.Name(), failure.Message);
|
2017-01-16 20:59:50 +01:00
|
|
|
|
}
|
2015-02-05 18:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 14:11:16 +01:00
|
|
|
|
DiskSpaceRequirements = new DiskSpaceRequirements(operation, Host, updateName, requiredDiskSpace, availableDiskSpace, reclaimableDiskSpace);
|
2015-02-05 18:01:14 +01:00
|
|
|
|
|
2017-03-09 16:57:21 +01:00
|
|
|
|
if (availableDiskSpace < requiredDiskSpace)
|
|
|
|
|
log.WarnFormat("Cleanup message: \r\n{0}", DiskSpaceRequirements.GetSpaceRequirementsMessage());
|
2015-02-05 18:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class DiskSpaceRequirements
|
|
|
|
|
{
|
2015-02-12 14:11:16 +01:00
|
|
|
|
public readonly OperationTypes Operation;
|
2015-02-05 18:01:14 +01:00
|
|
|
|
public readonly Host Host;
|
|
|
|
|
public readonly string UpdateName;
|
|
|
|
|
public readonly long RequiredDiskSpace;
|
|
|
|
|
public readonly long AvailableDiskSpace;
|
|
|
|
|
public readonly long ReclaimableDiskSpace;
|
|
|
|
|
|
2018-06-13 18:24:33 +02:00
|
|
|
|
public enum OperationTypes { install, upload, automatedUpdates, automatedUpdatesUploadOne, automatedUpdatesUploadAll }
|
2015-02-12 14:11:16 +01:00
|
|
|
|
|
|
|
|
|
public DiskSpaceRequirements(OperationTypes operation, Host host, string updateName, long requiredDiskSpace, long availableDiskSpace, long reclaimableDiskSpace)
|
2015-02-05 18:01:14 +01:00
|
|
|
|
{
|
2015-02-12 14:11:16 +01:00
|
|
|
|
Operation = operation;
|
2015-02-05 18:01:14 +01:00
|
|
|
|
Host = host;
|
|
|
|
|
UpdateName = updateName;
|
|
|
|
|
RequiredDiskSpace = requiredDiskSpace;
|
|
|
|
|
AvailableDiskSpace = availableDiskSpace;
|
|
|
|
|
ReclaimableDiskSpace = reclaimableDiskSpace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanCleanup
|
|
|
|
|
{
|
2015-02-16 12:11:30 +01:00
|
|
|
|
get { return ReclaimableDiskSpace + AvailableDiskSpace > RequiredDiskSpace && RequiredDiskSpace > 0 && ReclaimableDiskSpace > 0; }
|
2015-02-05 18:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetSpaceRequirementsMessage()
|
|
|
|
|
{
|
2015-02-12 14:59:49 +01:00
|
|
|
|
StringBuilder sbMessage = new StringBuilder();
|
2018-06-15 17:05:37 +02:00
|
|
|
|
var pool = Helpers.GetPool(Host.Connection);
|
2015-02-12 14:11:16 +01:00
|
|
|
|
|
|
|
|
|
switch (Operation)
|
|
|
|
|
{
|
|
|
|
|
case OperationTypes.install :
|
2017-09-03 04:33:29 +02:00
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_INSTALL, Host.Name(), UpdateName);
|
2015-02-12 14:11:16 +01:00
|
|
|
|
break;
|
|
|
|
|
case OperationTypes.upload :
|
2017-09-03 04:33:29 +02:00
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_UPLOAD, Host.Name(), UpdateName);
|
2015-02-12 14:11:16 +01:00
|
|
|
|
break;
|
2017-01-16 20:59:50 +01:00
|
|
|
|
case OperationTypes.automatedUpdates :
|
2017-09-03 04:33:29 +02:00
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_AUTO_UPDATE, Host.Name());
|
2016-08-05 16:56:55 +02:00
|
|
|
|
break;
|
2018-06-13 18:24:33 +02:00
|
|
|
|
case OperationTypes.automatedUpdatesUploadOne:
|
2018-06-15 17:05:37 +02:00
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_AUTO_UPDATE_UPLOAD_ONE, pool != null ? pool.Name() : Host.Name());
|
2018-06-13 18:24:33 +02:00
|
|
|
|
break;
|
|
|
|
|
case OperationTypes.automatedUpdatesUploadAll:
|
2018-06-15 17:05:37 +02:00
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_AUTO_UPDATE_UPLOAD_ALL, pool != null ? pool.Name() : Host.Name());
|
2018-06-13 18:24:33 +02:00
|
|
|
|
break;
|
2015-02-12 14:11:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 18:01:14 +01:00
|
|
|
|
sbMessage.AppendLine();
|
|
|
|
|
sbMessage.AppendLine();
|
2015-03-11 15:49:07 +01:00
|
|
|
|
if (RequiredDiskSpace > 0)
|
|
|
|
|
{
|
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_REQUIRED_SPACE, Util.DiskSizeString(RequiredDiskSpace));
|
|
|
|
|
sbMessage.AppendLine();
|
|
|
|
|
}
|
2015-02-05 18:01:14 +01:00
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_AVAILABLE_SPACE, Util.DiskSizeString(AvailableDiskSpace));
|
|
|
|
|
sbMessage.AppendLine();
|
|
|
|
|
sbMessage.AppendLine();
|
|
|
|
|
if (CanCleanup)
|
2021-03-16 02:50:45 +01:00
|
|
|
|
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_CLEANUP, BrandManager.BrandConsole,
|
|
|
|
|
Util.DiskSizeString(ReclaimableDiskSpace));
|
2015-02-05 18:01:14 +01:00
|
|
|
|
else
|
|
|
|
|
sbMessage.AppendLine(Messages.NOT_ENOUGH_SPACE_MESSAGE_NOCLEANUP);
|
|
|
|
|
return sbMessage.ToString();
|
|
|
|
|
}
|
2015-02-12 14:11:16 +01:00
|
|
|
|
|
|
|
|
|
public string GetMessageForActionLink()
|
|
|
|
|
{
|
2019-08-22 11:47:43 +02:00
|
|
|
|
return CanCleanup ? Messages.PATCHINGWIZARD_CLEANUP : Messages.MORE_INFO;
|
2015-02-12 14:11:16 +01:00
|
|
|
|
}
|
2015-02-05 18:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|