xenadmin/XenModel/Actions/Pool_Patch/ApplyPatchAction.cs
Konstantina Chremmou ae22560ce8 Converted all extension get properties of the API classes to methods in order to
prevent them from being serialised alongside the API properties. This will also
be useful for moving the API bindings out of XenModel.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
2017-09-03 03:35:30 +01:00

166 lines
6.0 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 XenAPI;
using XenAdmin.Network;
namespace XenAdmin.Actions
{
public abstract class PatchAction : AsyncAction
{
public PatchAction(IXenConnection conn, string title) : base(conn, title) { }
public PatchAction(IXenConnection conn, string title, bool suppress) : base(conn, title, suppress) { }
protected XenRef<Pool_patch> BringPatchToPoolForHost(Host host, Pool_patch patch)
{
// Check the patch exists on the pool this host is connected to
XenRef<Pool_patch> patch_ref = host.Connection.Cache.FindRef(patch);
if (patch_ref != null)
return patch_ref;
Description = String.Format(Messages.DOWNLOADING_PATCH_FROM, patch.Connection.Name);
// 1st download patch from the pool that has it (the connection on the xenobject)
string filename = Path.GetTempFileName();
try
{
Connection = patch.Connection;
Session = patch.Connection.DuplicateSession();
try
{
HTTPHelper.Get(this, true, filename, patch.Connection.Hostname,
(HTTP_actions.get_sss)HTTP_actions.get_pool_patch_download,
Session.uuid, patch.uuid);
}
catch (Exception e)
{
throw new PatchDownloadFailedException(string.Format(Messages.PATCH_DOWNLOAD_FAILED, patch.name_label, patch.Connection.Name), e);
}
finally
{
Connection = null;
Session = null;
}
// Then, put it on the pool that doesn't have it
Description = String.Format(Messages.UPLOADING_PATCH_TO, host.Name());
Connection = host.Connection;
Session = host.Connection.DuplicateSession();
try
{
string result = HTTPHelper.Put(this, true, filename, host.Connection.Hostname,
(HTTP_actions.put_ss)HTTP_actions.put_pool_patch_upload, Session.uuid);
return new XenRef<Pool_patch>(result);
}
finally
{
Connection = null;
Session = null;
Description = String.Format(Messages.PATCH_UPLOADED, host.Name());
}
}
finally
{
File.Delete(filename);
}
}
}
public class ApplyPatchAction : PatchAction
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private readonly Pool_patch patch;
private readonly Host host;
private string output = "";
public ApplyPatchAction(Pool_patch patch, Host host)
: base(host.Connection, string.Format(Messages.UPDATES_WIZARD_APPLYING_UPDATE, patch.Name(), host.Name()))
{
this.patch = patch;
this.host = host;
}
protected override void Run()
{
SafeToExit = false;
if (patch.AppliedOn(host) == DateTime.MaxValue)
ApplyPatch();
}
private void ApplyPatch()
{
XenRef<Pool_patch> patchRef = BringPatchToPoolForHost(host, patch);
try
{
this.Description = String.Format(Messages.APPLYING_PATCH, patch.Name(), host.Name());
output += String.Format(Messages.APPLY_PATCH_LOG_MESSAGE, patch.Name(), host.Name());
output += Pool_patch.apply(Session, patchRef, host.opaque_ref);
this.Description = String.Format(Messages.PATCH_APPLIED, patch.Name(), host.Name());
}
catch (Failure f)
{
if (f.ErrorDescription.Count > 1 && f.ErrorDescription[0] == XenAPI.Failure.PATCH_APPLY_FAILED)
{
output += Messages.APPLY_PATCH_FAILED_LOG_MESSAGE;
output += f.ErrorDescription[1];
}
throw;
}
}
}
public class PatchDownloadFailedException : ApplicationException
{
public PatchDownloadFailedException(string message, Exception innerException) :
base(message, innerException) { }
}
}