2023-01-24 15:29:31 +01:00
|
|
|
|
/* Copyright (c) Cloud Software Group, Inc.
|
2013-06-24 13:41:48 +02: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.IO;
|
2020-09-03 01:36:40 +02:00
|
|
|
|
using System.Linq;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2016-07-20 00:51:48 +02:00
|
|
|
|
using XenAdmin.Core;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
2021-01-09 02:49:13 +01:00
|
|
|
|
using XenCenterLib;
|
2020-09-29 15:28:52 +02:00
|
|
|
|
using XenCenterLib.Compression;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using XenOvf;
|
|
|
|
|
using XenOvf.Definitions;
|
|
|
|
|
|
2020-09-01 04:21:41 +02:00
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Actions.OvfActions
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-09-01 04:21:41 +02:00
|
|
|
|
public partial class ExportApplianceAction : ApplianceAction
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
#region Private fields
|
|
|
|
|
|
|
|
|
|
private readonly string m_applianceDirectory;
|
|
|
|
|
private readonly string m_applianceFileName;
|
|
|
|
|
private readonly List<VM> m_vmsToExport;
|
|
|
|
|
private readonly IEnumerable<string> m_eulas;
|
|
|
|
|
private readonly bool m_signAppliance;
|
|
|
|
|
private readonly bool m_createManifest;
|
|
|
|
|
private readonly X509Certificate2 m_certificate;
|
|
|
|
|
private readonly bool m_encryptFiles;
|
|
|
|
|
private readonly string m_encryptPassword;
|
|
|
|
|
private readonly bool m_createOVA;
|
|
|
|
|
private readonly bool m_compressOVFfiles;
|
|
|
|
|
private readonly bool m_shouldVerify;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2020-08-28 14:07:57 +02:00
|
|
|
|
public ExportApplianceAction(IXenConnection connection, string applianceDirectory, string applianceFileName, List<VM> vmsToExport,
|
|
|
|
|
IEnumerable<string> eulas, bool signAppliance, bool createManifest, X509Certificate2 certificate,
|
|
|
|
|
bool encryptFiles, string encryptPassword, bool createOVA, bool compressOVFfiles, bool shouldVerify)
|
|
|
|
|
: base(connection, string.Format(createOVA ? Messages.EXPORT_OVA_PACKAGE : Messages.EXPORT_OVF_PACKAGE,
|
|
|
|
|
applianceFileName, Helpers.GetName(connection)))
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
m_applianceDirectory = applianceDirectory;
|
|
|
|
|
m_applianceFileName = applianceFileName;
|
|
|
|
|
m_vmsToExport = vmsToExport;
|
|
|
|
|
m_eulas = eulas;
|
|
|
|
|
m_signAppliance = signAppliance;
|
|
|
|
|
m_createManifest = createManifest;
|
2020-12-30 01:01:08 +01:00
|
|
|
|
m_certificate = certificate;
|
2020-12-06 00:52:24 +01:00
|
|
|
|
if (m_signAppliance && m_certificate == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(m_certificate));
|
2013-06-24 13:41:48 +02:00
|
|
|
|
m_encryptFiles = encryptFiles;
|
|
|
|
|
m_encryptPassword = encryptPassword;
|
|
|
|
|
m_createOVA = createOVA;
|
|
|
|
|
m_compressOVFfiles = compressOVFfiles;
|
|
|
|
|
m_shouldVerify = shouldVerify;
|
|
|
|
|
|
|
|
|
|
if (m_vmsToExport.Count == 1)
|
2020-06-20 15:35:25 +02:00
|
|
|
|
VM = m_vmsToExport[0];
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 19:26:09 +02:00
|
|
|
|
protected override void RunCore()
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2020-09-03 01:36:40 +02:00
|
|
|
|
log.Info($"Exporting VMs to package {m_applianceFileName}");
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
var appFolder = Path.Combine(m_applianceDirectory, m_applianceFileName);
|
|
|
|
|
var appFile = string.Format("{0}.ovf", m_applianceFileName);
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(appFolder))
|
|
|
|
|
Directory.CreateDirectory(appFolder);
|
|
|
|
|
|
2020-09-03 01:36:40 +02:00
|
|
|
|
var envList = new List<EnvelopeType>();
|
2020-08-29 16:29:34 +02:00
|
|
|
|
|
2020-09-03 01:36:40 +02:00
|
|
|
|
for (int i = 0; i < m_vmsToExport.Count; i++)
|
2020-08-29 16:29:34 +02:00
|
|
|
|
{
|
2020-09-03 01:36:40 +02:00
|
|
|
|
var vm = m_vmsToExport[i];
|
2020-08-29 16:29:34 +02:00
|
|
|
|
CheckForCancellation();
|
2020-09-03 01:36:40 +02:00
|
|
|
|
Description = string.Format(Messages.EXPORTING_VM_PREPARE, vm.Name());
|
|
|
|
|
|
|
|
|
|
int curVm = i;
|
|
|
|
|
void UpdatePercentage(float x)
|
|
|
|
|
{
|
|
|
|
|
PercentComplete = (int)((curVm + x) * 80 / m_vmsToExport.Count);
|
|
|
|
|
}
|
2020-08-29 16:29:34 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-03 01:36:40 +02:00
|
|
|
|
var envelope = Export(appFolder, vm, UpdatePercentage);
|
2020-08-29 16:29:34 +02:00
|
|
|
|
envList.Add(envelope);
|
2020-09-03 01:36:40 +02:00
|
|
|
|
PercentComplete = (i + 1) * 80 / m_vmsToExport.Count;
|
2020-08-29 16:29:34 +02:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw new CancelledException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EnvelopeType env = OVF.Merge(envList, m_applianceFileName);
|
|
|
|
|
PercentComplete = 80;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
foreach (var eula in m_eulas)
|
|
|
|
|
{
|
2020-08-29 16:29:34 +02:00
|
|
|
|
CheckForCancellation();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Description = Messages.ADDING_EULAS;
|
|
|
|
|
OVF.AddEula(env, eula);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 16:29:34 +02:00
|
|
|
|
CheckForCancellation();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
var ovfPath = Path.Combine(appFolder, appFile);
|
|
|
|
|
OVF.SaveAs(env, ovfPath);
|
2020-08-29 16:29:34 +02:00
|
|
|
|
PercentComplete = 85;
|
|
|
|
|
|
|
|
|
|
CheckForCancellation();
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
if (m_createOVA)
|
|
|
|
|
{
|
2020-12-06 00:52:24 +01:00
|
|
|
|
ManifestAndSign(env, appFolder, appFile);
|
2020-09-30 16:11:43 +02:00
|
|
|
|
PercentComplete = 90;
|
2020-12-06 00:52:24 +01:00
|
|
|
|
log.Info($"Archiving OVF package {m_applianceFileName} into OVA");
|
|
|
|
|
Description = string.Format(Messages.CREATING_OVA_FILE, string.Format("{0}.ova", m_applianceFileName));
|
2020-12-06 03:43:54 +01:00
|
|
|
|
OVF.ConvertOVFtoOVA(env, ovfPath, CheckForCancellation, m_compressOVFfiles);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
else if (m_compressOVFfiles)
|
|
|
|
|
{
|
2020-09-03 01:36:40 +02:00
|
|
|
|
log.Info($"Compressing package {m_applianceFileName}");
|
2013-06-24 13:41:48 +02:00
|
|
|
|
Description = Messages.COMPRESSING_FILES;
|
2020-12-06 03:43:54 +01:00
|
|
|
|
OVF.CompressFiles(env, ovfPath, CompressionFactory.Type.Gz, CheckForCancellation);
|
2020-09-30 16:11:43 +02:00
|
|
|
|
PercentComplete = 95;
|
2020-12-06 00:52:24 +01:00
|
|
|
|
ManifestAndSign(env, appFolder, appFile);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ManifestAndSign(env, appFolder, appFile);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 14:20:16 +01:00
|
|
|
|
Tick(100, Messages.COMPLETED);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 00:52:24 +01:00
|
|
|
|
private void ManifestAndSign(EnvelopeType ovfEnv, string appFolder, string appFile)
|
2020-09-30 16:11:43 +02:00
|
|
|
|
{
|
2020-12-06 00:52:24 +01:00
|
|
|
|
if (m_signAppliance || m_createManifest)
|
|
|
|
|
Manifest(ovfEnv, appFolder, appFile);
|
|
|
|
|
|
2020-09-30 16:11:43 +02:00
|
|
|
|
if (m_signAppliance)
|
2020-12-06 00:52:24 +01:00
|
|
|
|
Sign(m_certificate, appFolder, appFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Manifest(EnvelopeType ovfEnv, string pathToOvf, string ovfFileName)
|
|
|
|
|
{
|
|
|
|
|
var manifestFiles = new List<string> {ovfFileName};
|
|
|
|
|
|
|
|
|
|
var files = ovfEnv?.References?.File;
|
|
|
|
|
if (files != null)
|
|
|
|
|
manifestFiles.AddRange(files.Select(f => f.href).ToList());
|
|
|
|
|
|
|
|
|
|
var fileDigests = new List<FileDigest>();
|
|
|
|
|
foreach (var mf in manifestFiles)
|
|
|
|
|
{
|
|
|
|
|
CheckForCancellation();
|
|
|
|
|
|
|
|
|
|
var mfPath = Path.Combine(pathToOvf, mf);
|
|
|
|
|
if (!File.Exists(mfPath))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
Description = string.Format(Messages.CREATING_MANIFEST_CHECKSUM, mf);
|
|
|
|
|
log.Info($"Calculating checksum for file {mf}");
|
|
|
|
|
|
|
|
|
|
using (FileStream stream = new FileStream(mfPath, FileMode.Open, FileAccess.Read))
|
|
|
|
|
{
|
2021-01-09 02:49:13 +01:00
|
|
|
|
var hash = StreamUtilities.ComputeHash(stream, out var hashAlgorithm);
|
|
|
|
|
fileDigests.Add(new FileDigest(Path.GetFileName(mf), hash, hashAlgorithm));
|
2020-12-06 00:52:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckForCancellation();
|
|
|
|
|
Description = Messages.CREATING_MANIFEST;
|
|
|
|
|
string manifest = Path.Combine(pathToOvf, $"{Path.GetFileNameWithoutExtension(ovfFileName)}{Package.MANIFEST_EXT}");
|
|
|
|
|
|
|
|
|
|
using (var stream = new FileStream(manifest, FileMode.Create, FileAccess.Write))
|
|
|
|
|
using (var sw = new StreamWriter(stream))
|
|
|
|
|
{
|
|
|
|
|
foreach (var fileDigest in fileDigests)
|
|
|
|
|
sw.WriteLine(fileDigest.ToManifestLine());
|
|
|
|
|
sw.Flush();
|
2020-09-30 16:11:43 +02:00
|
|
|
|
}
|
2020-12-06 00:52:24 +01:00
|
|
|
|
|
|
|
|
|
log.Info($"Created manifest for package {ovfFileName}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Sign(X509Certificate2 certificate, string pathToOvf, string ovfFileName)
|
|
|
|
|
{
|
|
|
|
|
Description = Messages.SIGNING_APPLIANCE;
|
|
|
|
|
|
|
|
|
|
var packageName = Path.GetFileNameWithoutExtension(ovfFileName);
|
|
|
|
|
string manifestFileName = packageName + Package.MANIFEST_EXT;
|
|
|
|
|
string manifestPath = Path.Combine(pathToOvf, manifestFileName);
|
|
|
|
|
|
|
|
|
|
CheckForCancellation();
|
|
|
|
|
|
2021-01-09 02:49:13 +01:00
|
|
|
|
FileDigest fileDigest;
|
2020-12-06 00:52:24 +01:00
|
|
|
|
using (FileStream stream = new FileStream(manifestPath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
2020-09-30 16:11:43 +02:00
|
|
|
|
{
|
2021-01-09 02:49:13 +01:00
|
|
|
|
var signedHash = StreamUtilities.ComputeSignedHash(stream, certificate, out var hashAlgorithm);
|
|
|
|
|
fileDigest = new FileDigest(manifestFileName, signedHash, hashAlgorithm);
|
2020-09-30 16:11:43 +02:00
|
|
|
|
}
|
2020-12-06 00:52:24 +01:00
|
|
|
|
|
|
|
|
|
string signatureFileName = packageName + Package.CERTIFICATE_EXT;
|
|
|
|
|
string signaturePath = Path.Combine(pathToOvf, signatureFileName);
|
|
|
|
|
|
|
|
|
|
using (FileStream stream = new FileStream(signaturePath, FileMode.Create, FileAccess.Write, FileShare.None))
|
|
|
|
|
using (StreamWriter writer = new StreamWriter(stream))
|
|
|
|
|
{
|
|
|
|
|
writer.WriteLine(fileDigest.ToManifestLine());
|
|
|
|
|
|
|
|
|
|
// Export the certificate encoded in Base64 using DER
|
|
|
|
|
string b64Cert = Convert.ToBase64String(certificate.Export(X509ContentType.SerializedCert));
|
|
|
|
|
|
|
|
|
|
writer.WriteLine("-----BEGIN CERTIFICATE-----");
|
|
|
|
|
writer.WriteLine(b64Cert);
|
|
|
|
|
writer.WriteLine("-----END CERTIFICATE-----");
|
|
|
|
|
writer.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info($"Digitally signed package {ovfFileName}");
|
2020-09-30 16:11:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 01:36:40 +02:00
|
|
|
|
protected override void CleanOnError()
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(m_applianceDirectory))
|
|
|
|
|
return;
|
|
|
|
|
if (Directory.EnumerateFileSystemEntries(m_applianceDirectory).Any())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(m_applianceDirectory);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
//ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|