xenadmin/XenModel/Actions/Pool_Patch/GetDiskSpaceRequirementsAction.cs
Mihaela Stoica 19f66414a7 CP-10797: Ensuring sufficient disk space for service pack installation: Changes following code review
- don't update the progress bar when checking disk space (only the description)
- the color used for "in progress" action description (above the progress bar) should be the system default font color, not black
- actions should throw ArgumentNullException, not NullReferenceException

Signed-off-by: Mihaela Stoica <mihaela.stoica@citrix.com>
2015-02-06 10:34:18 +00:00

174 lines
6.9 KiB
C#

/* Copyright (c) Citrix Systems Inc.
* 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;
using System.IO;
using System.Text;
using XenAPI;
namespace XenAdmin.Actions
{
public class GetDiskSpaceRequirementsAction : PureAsyncAction
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private readonly string updateName;
private readonly long updateSize;
private readonly Pool_patch currentPatch;
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)
: this(host, patch.Name, patch.size, suppressHistory)
{
currentPatch = patch;
}
/// <summary>
/// This constructor is used to calculate the disk space requirements for uploading a single update file
/// </summary>
public GetDiskSpaceRequirementsAction(Host host, string path, bool suppressHistory)
: this(host, FileName(path), FileSize(path), suppressHistory)
{ }
/// <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)
{
if (host == null)
throw new ArgumentNullException("host");
Host = host;
this.updateName = updateName;
updateSize = size;
}
private static long FileSize(string path)
{
FileInfo fileInfo = new FileInfo(path);
return fileInfo.Length;
}
private static string FileName(string path)
{
FileInfo fileInfo = new FileInfo(path);
return fileInfo.Name;
}
protected override void Run()
{
Description = String.Format(Messages.ACTION_GET_DISK_SPACE_REQUIREMENTS_DESCRIPTION, Host.Name);
long requiredDiskSpace = updateSize;
string result;
// 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)
{
log.WarnFormat("Plugin call disk-space.get_avail_host_disk_space on {0} failed with {1}", Host.Name, failure.Message);
}
// get reclaimable disk space (excluding current patch)
long reclaimableDiskSpace = 0;
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)
{
log.WarnFormat("Plugin call disk-space.get_reclaimable_disk_space on {0} failed with {1}", Host.Name, failure.Message);
}
DiskSpaceRequirements = new DiskSpaceRequirements(Host, updateName, requiredDiskSpace, availableDiskSpace, reclaimableDiskSpace);
log.WarnFormat("Cleanup message: \r\n{0}", DiskSpaceRequirements.GetSpaceRequirementsMessage());
}
}
public class DiskSpaceRequirements
{
public readonly Host Host;
public readonly string UpdateName;
public readonly long RequiredDiskSpace;
public readonly long AvailableDiskSpace;
public readonly long ReclaimableDiskSpace;
public DiskSpaceRequirements(Host host, string updateName, long requiredDiskSpace, long availableDiskSpace, long reclaimableDiskSpace)
{
Host = host;
UpdateName = updateName;
RequiredDiskSpace = requiredDiskSpace;
AvailableDiskSpace = availableDiskSpace;
ReclaimableDiskSpace = reclaimableDiskSpace;
}
public bool CanCleanup
{
get { return RequiredDiskSpace < ReclaimableDiskSpace; }
}
public string GetSpaceRequirementsMessage()
{
StringBuilder sbMessage = new StringBuilder();
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE, Host.Name, UpdateName);
sbMessage.AppendLine();
sbMessage.AppendLine();
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_REQUIRED_SPACE, Util.DiskSizeString(RequiredDiskSpace));
sbMessage.AppendLine();
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_AVAILABLE_SPACE, Util.DiskSizeString(AvailableDiskSpace));
sbMessage.AppendLine();
sbMessage.AppendLine();
if (CanCleanup)
sbMessage.AppendFormat(Messages.NOT_ENOUGH_SPACE_MESSAGE_CLEANUP, Util.DiskSizeString(ReclaimableDiskSpace));
else
sbMessage.AppendLine(Messages.NOT_ENOUGH_SPACE_MESSAGE_NOCLEANUP);
return sbMessage.ToString();
}
}
}