Skip to content

Commit

Permalink
VCI-724: Fail installation of modules on errors (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
krankenbro authored Dec 7, 2023
1 parent 6d0b59f commit ca51b1a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Serilog;
using VirtoCommerce.Build.PlatformTools;
using VirtoCommerce.Platform.Core.Modularity;
using AzureBlobs = Azure.Storage.Blobs;

namespace PlatformTools.Azure
Expand All @@ -20,7 +20,7 @@ public AzureBlobModuleInstaller(string token, string destination)
_destination = destination;
}

protected override Task InnerInstall(ModuleSource source)
protected override Task InnerInstall(ModuleSource source, IProgress<ProgressMessage> progress)
{
var azureBlobSource = (AzureBlob)source;
var blobClientOptions = new AzureBlobs.BlobClientOptions();
Expand All @@ -29,7 +29,7 @@ protected override Task InnerInstall(ModuleSource source)
Directory.CreateDirectory(_destination);
foreach (var moduleBlobName in azureBlobSource.Modules.Select(m => m.BlobName))
{
Log.Information($"Installing {moduleBlobName}");
progress.ReportInfo($"Installing {moduleBlobName}");
var zipName = moduleBlobName;
if(!zipName.EndsWith(".zip"))
{
Expand All @@ -42,12 +42,12 @@ protected override Task InnerInstall(ModuleSource source)
{
moduleDestination = moduleDestination.Replace(".zip", "");
}
Log.Information($"Downloading Blob {moduleBlobName}");
progress.ReportInfo($"Downloading Blob {moduleBlobName}");
var blobClient = containerClient.GetBlobClient(moduleBlobName);
blobClient.DownloadTo(zipPath);
Log.Information($"Extracting Blob {moduleBlobName}");
progress.ReportInfo($"Extracting Blob {moduleBlobName}");
ZipFile.ExtractToDirectory(zipPath, moduleDestination, true);
Log.Information($"Successfully installed {moduleBlobName}");
progress.ReportInfo($"Successfully installed {moduleBlobName}");
}
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Serilog;
using VirtoCommerce.Build.PlatformTools;
using VirtoCommerce.Platform.Core.Modularity;

namespace PlatformTools.Azure
{
Expand All @@ -19,31 +19,31 @@ public AzurePipelineArtifactsModuleInstaller(string token, string discoveryPath)
this._discoveryPath = discoveryPath;
}

protected override async Task InnerInstall(ModuleSource source)
protected override async Task InnerInstall(ModuleSource source, IProgress<ProgressMessage> progress)
{
var artifacts = (AzurePipelineArtifacts)source;
var azureClient = new AzureDevClient(artifacts.Organization, _token);
var clientOptions = ExtModuleCatalog.GetOptions(_token, new List<string>() { "https://virtocommerce.com" });
var downloadClient = new AzurePipelineArtifactsClient(clientOptions);
foreach (var module in artifacts.Modules)
{
Log.Information($"Installing {module.Id}");
progress.ReportInfo($"Installing {module.Id}");
var moduleDestination = Path.Join(_discoveryPath, module.Id);
Directory.CreateDirectory(moduleDestination);
var zipName = $"{module.Id}.zip";
var zipDestination = Path.Join(moduleDestination, zipName);
var artifactUrl = await azureClient.GetArtifactUrl(Guid.Parse(artifacts.Project), module.Branch, module.Definition);
Log.Information($"Downloading {artifactUrl}");
progress.ReportInfo($"Downloading {artifactUrl}");
using (var stream = downloadClient.OpenRead(artifactUrl))
{
using (var output = File.OpenWrite(zipDestination))
{
await stream.CopyToAsync(output);
}
}
Log.Information($"Extracting {zipName}");
progress.ReportInfo($"Extracting {zipName}");
ZipFile.ExtractToDirectory(zipDestination, moduleDestination);
Log.Information($"Successfully installed {module.Id}");
progress.ReportInfo($"Successfully installed {module.Id}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Nuke.Common;
using Nuke.Common.IO;
using Nuke.Common.Tooling;
using Serilog;
using VirtoCommerce.Build.PlatformTools;
using VirtoCommerce.Platform.Core.Modularity;

namespace PlatformTools.Azure
{
Expand All @@ -21,12 +21,12 @@ public AzureUniversalPackagesModuleInstaller(string token, string discoveryPath)
this.discoveryPath = discoveryPath;
}

protected override Task InnerInstall(ModuleSource source)
protected override Task InnerInstall(ModuleSource source, IProgress<ProgressMessage> progress)
{
var artifacts = (AzureUniversalPackages)source;
foreach (var module in artifacts.Modules)
{
Log.Information($"Installing {module.Id}");
progress.ReportInfo($"Installing {module.Id}");
var moduleDestination = Path.Join(discoveryPath, module.Id);
Directory.CreateDirectory(moduleDestination);
FileSystemTasks.EnsureCleanDirectory(moduleDestination);
Expand All @@ -49,21 +49,16 @@ protected override Task InnerInstall(ModuleSource source)
var zipPath = Directory.GetFiles(moduleDestination).FirstOrDefault(p => p.EndsWith(".zip"));
if (zipPath == null)
{
Assert.Fail($"Can't download {module.Id} - {module.Version}");
progress.ReportError($"Can't download {module.Id} - {module.Version}");
}

Log.Information($"Extracting {zipPath}");
progress.ReportInfo($"Extracting {zipPath}");
ZipFile.ExtractToDirectory(zipPath, moduleDestination);
Log.Information($"Successfully installed {module.Id}");
progress.ReportInfo($"Successfully installed {module.Id}");
}

return Task.CompletedTask;
}

public override Task Install(ModuleSource source)
{
return InnerInstall((AzureUniversalPackages)source);
}
}
}

6 changes: 3 additions & 3 deletions src/VirtoCommerce.Build/PlatformTools/Build.PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private static bool IsPlatformInstallationNeeded(string version)
{
var versionInfo = FileVersionInfo.GetVersionInfo(platformWebDllPath);

if (versionInfo.ProductVersion != null && newVersion <= Version.Parse(versionInfo.ProductVersion))
if (versionInfo.FileVersion != null && newVersion <= Version.Parse(versionInfo.FileVersion))
{
result = false;
}
Expand Down Expand Up @@ -369,7 +369,7 @@ private static bool IsPlatformInstallationNeeded(string version)
{
if (m.Level == ProgressMessageLevel.Error)
{
Log.Error(m.Message);
Assert.Fail(m.Message);
}
else
{
Expand Down Expand Up @@ -404,7 +404,7 @@ private static bool IsPlatformInstallationNeeded(string version)
{
var installer = GetModuleInstaller(moduleSource);

await installer.Install(moduleSource);
await installer.Install(moduleSource, progress);
}
var absoluteDiscoveryPath = (AbsolutePath)Path.GetFullPath(discoveryPath);
var zipFiles = absoluteDiscoveryPath.GlobFiles("*/*.zip");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Nuke.Common.IO;
using Octokit;
using Serilog;
using VirtoCommerce.Build.PlatformTools;
using VirtoCommerce.Platform.Core.Modularity;

namespace PlatformTools.Github
{
Expand All @@ -24,40 +25,40 @@ public GithubPrivateModulesInstaller(string token, string discoveryPath)
_client.Credentials = new Credentials(token);
}

protected override async Task InnerInstall(ModuleSource source)
protected override async Task InnerInstall(ModuleSource source, IProgress<ProgressMessage> progress)
{
var githubPrivateRepos = (GithubPrivateRepos) source;
foreach (var module in githubPrivateRepos.Modules)
{
var moduleDestination = Path.Join(_discoveryPath, module.Id);
Log.Information($"Installing {module.Id}");
progress.ReportInfo($"Installing {module.Id}");
Directory.CreateDirectory(moduleDestination);
FileSystemTasks.EnsureCleanDirectory(moduleDestination);
var zipName = $"{module.Id}.zip";
var zipDestination = Path.Join(moduleDestination, zipName);
var release = await _client.Repository.Release.Get(githubPrivateRepos.Owner, module.Id, module.Version);
if (release == null)
{
Log.Error($"{module.Id}:{module.Version} is not found");
progress.ReportError($"{module.Id}:{module.Version} is not found");
continue;
}
var asset = release.Assets.FirstOrDefault();
if (asset == null)
{
Log.Error($"{module.Id}:{module.Version} has no assets");
progress.ReportError($"{module.Id}:{module.Version} has no assets");
continue;
}
Log.Information($"Downloading {module.Id}");
progress.ReportInfo($"Downloading {module.Id}");
await HttpTasks.HttpDownloadFileAsync(asset.Url, zipDestination, clientConfigurator: c =>
{
c.DefaultRequestHeaders.Add("User-Agent", "VirtoCommerce.Build");
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token);
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
return c;
});
Log.Information($"Extracting {module.Id}");
progress.ReportInfo($"Extracting {module.Id}");
ZipFile.ExtractToDirectory(zipDestination, moduleDestination);
Log.Information($"Successfully installed {module.Id}");
progress.ReportInfo($"Successfully installed {module.Id}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Nuke.Common.IO;
using Serilog;
using VirtoCommerce.Build.PlatformTools;
using VirtoCommerce.Platform.Core.Modularity;

namespace PlatformTools.Gitlab;

Expand All @@ -18,19 +19,19 @@ public GitlabJobArtifactsModuleInstaller(string server, string token, string dis
_client = new GitLabClient(token, server);
}

protected override async Task InnerInstall(ModuleSource source)
protected override async Task InnerInstall(ModuleSource source, IProgress<ProgressMessage> progress)
{
var gitlabJobArtifacts = (GitlabJobArtifacts) source;
foreach (var module in gitlabJobArtifacts.Modules)
{
var moduleDestination = Path.Join(_discoveryPath, module.Id);
Directory.CreateDirectory(moduleDestination);
FileSystemTasks.EnsureCleanDirectory(moduleDestination);
Log.Information($"Downloading {module.Id}");
progress.ReportInfo($"Downloading {module.Id}");
var artifactZipPath = await _client.DownloadArtifact(module.Id, module.JobId, module.ArtifactName, moduleDestination);
Log.Information($"Extracting {module.Id}");
progress.ReportInfo($"Extracting {module.Id}");
ZipFile.ExtractToDirectory(artifactZipPath, moduleDestination);
Log.Information($"Successfully installed {module.Id}");
progress.ReportInfo($"Successfully installed {module.Id}");
}
}
}
20 changes: 17 additions & 3 deletions src/VirtoCommerce.Build/PlatformTools/ModulesInstallerBase.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
using System;
using System.Threading.Tasks;
using VirtoCommerce.Platform.Core.Modularity;

namespace VirtoCommerce.Build.PlatformTools
{
public abstract class ModulesInstallerBase
{
public virtual Task Install(ModuleSource source)
public virtual Task Install(ModuleSource source, IProgress<ProgressMessage> progress)
{
try
{
return InnerInstall(source);
return InnerInstall(source, progress);
}
catch (Exception exception)
{
progress.ReportError(exception.Message);
throw new ModuleInstallationException("Error while module installation", exception);
}
}

protected abstract Task InnerInstall(ModuleSource source);
protected abstract Task InnerInstall(ModuleSource source, IProgress<ProgressMessage> progress);
}

public static class ProgressExtension
{
public static void ReportInfo(this IProgress<ProgressMessage> progress, string message)
{
progress.Report(new ProgressMessage { Level = ProgressMessageLevel.Info, Message = message });
}
public static void ReportError(this IProgress<ProgressMessage> progress, string message)
{
progress.Report(new ProgressMessage { Level = ProgressMessageLevel.Error, Message = message });
}
}
}

0 comments on commit ca51b1a

Please sign in to comment.