Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CopyAcrImagesCommand to utilize ACR library vs AZ CLI #180

Merged
merged 2 commits into from
May 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions Microsoft.DotNet.ImageBuilder/src/AzureHelper.cs

This file was deleted.

56 changes: 44 additions & 12 deletions Microsoft.DotNet.ImageBuilder/src/Commands/CopyAcrImagesCommand.cs
Original file line number Diff line number Diff line change
@@ -2,7 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ContainerRegistry.Fluent;
using Microsoft.Azure.Management.ContainerRegistry.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.DotNet.ImageBuilder.ViewModel;

namespace Microsoft.DotNet.ImageBuilder.Commands
@@ -13,25 +20,50 @@ public CopyAcrImagesCommand() : base()
{
}

public override Task ExecuteAsync()
public override async Task ExecuteAsync()
{
Logger.WriteHeading("COPING IMAGES");

using (AzureHelper helper = AzureHelper.Create(Options.Username, Options.Password, Options.Tenant, Options.IsDryRun))
string registryName = Manifest.Registry.TrimEnd(".azurecr.io");

await Task.WhenAll(Manifest.GetFilteredPlatformTags().Select(platformTag => ImportImage(platformTag, registryName)));
}

private async Task ImportImage(TagInfo platformTag, string registryName)
{
AzureCredentials credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(Options.Username, Options.Password, Options.Tenant, AzureEnvironment.AzureGlobalCloud);
IAzure azure = Microsoft.Azure.Management.Fluent.Azure
.Configure()
.Authenticate(credentials)
.WithSubscription(Options.Subscription);

string destTagName = platformTag.FullyQualifiedName.TrimStart($"{Manifest.Registry}/");
string sourceTagName = destTagName.Replace(Options.RepoPrefix, Options.SourceRepoPrefix);
ImportImageParametersInner importParams = new ImportImageParametersInner()
{
string registryName = $"{Manifest.Registry}/";
Mode = "Force",
Source = new ImportSource(
sourceTagName,
$"/subscriptions/{Options.Subscription}/resourceGroups/{Options.ResourceGroup}/providers" +
$"/Microsoft.ContainerRegistry/registries/{registryName}"),
TargetTags = new string[] { destTagName }
};

foreach (TagInfo platformTag in Manifest.GetFilteredPlatformTags())
Logger.WriteMessage($"Importing '{destTagName}' from '{sourceTagName}'");

if (!Options.IsDryRun)
{
try
{
string sourceImage = platformTag.FullyQualifiedName.Replace(Options.RepoPrefix, Options.SourceRepoPrefix);
string destImage = platformTag.FullyQualifiedName.TrimStart(registryName);
helper.ExecuteAzCommand(
$"acr import -n {Manifest.Registry.TrimEnd(".azurecr.io")} --source {sourceImage} -t {destImage} --force",
Options.IsDryRun);
await azure.ContainerRegistries.Inner.ImportImageAsync(Options.ResourceGroup, registryName, importParams);
}
};

return Task.CompletedTask;
catch (Exception e)
{
Logger.WriteMessage($"Importing Failure: {destTagName}{Environment.NewLine}{e}");
throw;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ public class CopyAcrImagesOptions : Options, IFilterableOptions

public ManifestFilterOptions FilterOptions { get; } = new ManifestFilterOptions();
public string Password { get; set; }
public string ResourceGroup { get; set; }
public string SourceRepoPrefix { get; set; }
public string Subscription { get; set; }
public string Tenant { get; set; }
@@ -46,6 +47,10 @@ public override void ParseCommandLine(ArgumentSyntax syntax)
string subscription = null;
syntax.DefineParameter("subscription", ref subscription, "Azure subscription to operate on");
Subscription = subscription;

string resourceGroup = null;
syntax.DefineParameter("resource-group", ref resourceGroup, "Azure resource group to operate on");
ResourceGroup = resourceGroup;
}
}
}
Original file line number Diff line number Diff line change
@@ -192,38 +192,24 @@ private IEnumerable<MatrixInfo> GenerateMatrixInfo()
.ThenBy(platformGroup => platformGroup.Key.Architecture)
.ThenByDescending(platformGroup => platformGroup.Key.Variant);

string baseMatrixName = $"{Options.MatrixType.ToString().ToCamelCase()}Matrix";

if (Options.MatrixType == MatrixType.DependencyGraph)
foreach (var platformGrouping in platformGroups)
{
MatrixInfo matrix = new MatrixInfo() { Name = baseMatrixName };
string[] matrixNameParts =
{
$"{Options.MatrixType.ToString().ToCamelCase()}Matrix",
platformGrouping.Key.OsVersion ?? platformGrouping.Key.OS.GetDockerName(),
platformGrouping.Key.Architecture.GetDisplayName(platformGrouping.Key.Variant)
};
MatrixInfo matrix = new MatrixInfo() { Name = FormatMatrixName(matrixNameParts) };
matrices.Add(matrix);
foreach (var platformGrouping in platformGroups)

if (Options.MatrixType == MatrixType.PlatformDependencyGraph)
{
AddDockerfilePathLegs(matrix, Enumerable.Empty<string>(), platformGrouping);
AddDockerfilePathLegs(matrix, matrixNameParts, platformGrouping);
}
}
else
{
foreach (var platformGrouping in platformGroups)
else if (Options.MatrixType == MatrixType.PlatformVersionedOs)
{
string[] matrixNameParts =
{
baseMatrixName,
platformGrouping.Key.OsVersion ?? platformGrouping.Key.OS.GetDockerName(),
platformGrouping.Key.Architecture.GetDisplayName(platformGrouping.Key.Variant)
};
MatrixInfo matrix = new MatrixInfo() { Name = FormatMatrixName(matrixNameParts) };
matrices.Add(matrix);

if (Options.MatrixType == MatrixType.PlatformDependencyGraph)
{
AddDockerfilePathLegs(matrix, matrixNameParts, platformGrouping);
}
else if (Options.MatrixType == MatrixType.PlatformVersionedOs)
{
AddVersionedOsLegs(matrix, platformGrouping);
}
AddVersionedOsLegs(matrix, platformGrouping);
}
}

3 changes: 1 addition & 2 deletions Microsoft.DotNet.ImageBuilder/src/Commands/MatrixType.cs
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ namespace Microsoft.DotNet.ImageBuilder.Commands
public enum MatrixType
{
PlatformDependencyGraph,
PlatformVersionedOs,
DependencyGraph
PlatformVersionedOs
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
@@ -8,8 +8,9 @@

<ItemGroup>
<PackageReference Include="ILLink.Tasks" Version="0.1.5-preview-1461378" />
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.22.0" />
<PackageReference Include="Microsoft.DotNet.VersionTools" Version="3.0.0-preview1-03623-01" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.1"/>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>
<PackageReference Include="System.CommandLine" Version="0.1.0-e170603-2"/>
</ItemGroup>
</Project>