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;
|
|
|
|
|
using System.IO;
|
2017-03-16 12:41:14 +01:00
|
|
|
|
using System.Reflection;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions
|
|
|
|
|
{
|
2018-06-27 19:05:00 +02:00
|
|
|
|
public class UploadPatchAction : AsyncAction, IByteProgressAction
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
private Pool_patch _patch;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private readonly string retailPatchPath;
|
2018-06-27 19:05:00 +02:00
|
|
|
|
private readonly string _patchName;
|
2015-09-07 13:18:01 +02:00
|
|
|
|
private readonly bool deleteFileOnCancel;
|
2018-06-27 19:05:00 +02:00
|
|
|
|
private readonly long _totalPatchSize;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2015-01-09 13:54:12 +01:00
|
|
|
|
|
2015-09-07 13:18:01 +02:00
|
|
|
|
public UploadPatchAction(IXenConnection connection, string path, bool suppressHistory, bool deleteFileOnCancel)
|
2015-01-09 13:54:12 +01:00
|
|
|
|
: base(connection, null, Messages.UPLOADING_PATCH, suppressHistory)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Host master = Helpers.GetMaster(connection);
|
2015-09-07 13:18:01 +02:00
|
|
|
|
this.deleteFileOnCancel = deleteFileOnCancel;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
if (master == null)
|
|
|
|
|
throw new NullReferenceException();
|
|
|
|
|
|
|
|
|
|
ApiMethodsToRoleCheck.Add("pool.sync_database");
|
|
|
|
|
ApiMethodsToRoleCheck.Add("http/put_pool_patch_upload");
|
|
|
|
|
|
2015-11-08 17:31:48 +01:00
|
|
|
|
retailPatchPath = path;
|
2018-06-27 19:05:00 +02:00
|
|
|
|
_patchName = Path.GetFileNameWithoutExtension(retailPatchPath);
|
|
|
|
|
_totalPatchSize = (new FileInfo(path)).Length;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Host = master;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
public string ByteProgressDescription { get; set; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
public Pool_patch Patch
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 19:05:00 +02:00
|
|
|
|
get { return _patch; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Run()
|
|
|
|
|
{
|
|
|
|
|
SafeToExit = false;
|
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
try
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 19:05:00 +02:00
|
|
|
|
_patch = UploadRetailPatch();
|
|
|
|
|
log.InfoFormat("File '{0}' uploaded to server '{1}'", _patchName, Host.Name());
|
|
|
|
|
Tick(100, string.Format(Messages.UPLOAD_PATCH_UPLOADED_DESCRIPTION, _patchName));
|
|
|
|
|
}
|
|
|
|
|
catch (Failure f)
|
|
|
|
|
{
|
|
|
|
|
// Need to check if the patch already exists.
|
|
|
|
|
// If it does then we use it and ignore the PATCH_ALREADY_EXISTS error (CA-110209).
|
|
|
|
|
if (f.ErrorDescription != null
|
|
|
|
|
&& f.ErrorDescription.Count > 1
|
|
|
|
|
&& f.ErrorDescription[0] == Failure.PATCH_ALREADY_EXISTS)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 19:05:00 +02:00
|
|
|
|
string uuid = f.ErrorDescription[1];
|
|
|
|
|
Session session = Host.Connection.DuplicateSession();
|
|
|
|
|
_patch = Connection.Resolve(Pool_patch.get_by_uuid(session, uuid));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-06-27 19:05:00 +02:00
|
|
|
|
else
|
|
|
|
|
throw;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void RecomputeCanCancel()
|
|
|
|
|
{
|
|
|
|
|
CanCancel = !Cancelling;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
private Pool_patch UploadRetailPatch()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
Session session = NewSession();
|
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
Host master = Helpers.GetMaster(Connection);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
log.InfoFormat("Uploading file '{0}' to server '{1}'", _patchName, master.Name());
|
|
|
|
|
this.Description = string.Format(Messages.UPLOAD_PATCH_UPLOADING_DESCRIPTION, _patchName);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
RelatedTask = Task.create(session, "uploadTask", retailPatchPath);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = HTTPHelper.Put(UpdateProgress, GetCancelling, true, Connection, RelatedTask, ref session, retailPatchPath,
|
|
|
|
|
master.address, (HTTP_actions.put_ss)HTTP_actions.put_pool_patch_upload, session.opaque_ref);
|
2015-09-04 18:18:26 +02:00
|
|
|
|
|
2018-06-27 19:05:00 +02:00
|
|
|
|
return Connection.WaitForCache(new XenRef<Pool_patch>(result));
|
|
|
|
|
}
|
|
|
|
|
catch (CancelledException)
|
|
|
|
|
{
|
|
|
|
|
if (deleteFileOnCancel && File.Exists(retailPatchPath))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2018-06-27 19:05:00 +02:00
|
|
|
|
File.Delete(retailPatchPath);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2018-06-27 19:05:00 +02:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (TargetInvocationException ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex.InnerException != null)
|
|
|
|
|
throw ex.InnerException;
|
|
|
|
|
else
|
|
|
|
|
throw;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2018-06-27 19:05:00 +02:00
|
|
|
|
Task.destroy(session, RelatedTask);
|
|
|
|
|
RelatedTask = null;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-27 19:05:00 +02:00
|
|
|
|
|
|
|
|
|
private void UpdateProgress(int percent)
|
|
|
|
|
{
|
|
|
|
|
var descr = string.Format(Messages.UPLOAD_PATCH_UPLOADING_PROGRESS_DESCRIPTION, _patchName,
|
2018-06-28 17:28:57 +02:00
|
|
|
|
Util.DiskSizeString(percent * _totalPatchSize / 100, "F1"), Util.DiskSizeString(_totalPatchSize));
|
2018-06-27 19:05:00 +02:00
|
|
|
|
|
|
|
|
|
ByteProgressDescription = descr;
|
|
|
|
|
Tick(percent, descr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public interface IByteProgressAction
|
|
|
|
|
{
|
|
|
|
|
string ByteProgressDescription { get; set; }
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|