Skip to content

Commit

Permalink
Add System.Containers.Tasks & CreateNewImage Task (dotnet#41)
Browse files Browse the repository at this point in the history
* Add System.Containers.Tasks & CreateNewImage Task
  • Loading branch information
benvillalobos authored Jul 22, 2022
1 parent aef3c7e commit e4d3c25
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="17.2.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageVersion Include="MSTest.TestFramework" Version="2.2.10" />
Expand Down
115 changes: 115 additions & 0 deletions System.Containers.Tasks/CreateNewImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Resources;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

#nullable disable

namespace System.Containers.Tasks
{
public class CreateNewImage : Microsoft.Build.Utilities.Task
{
/// <summary>
/// Base image name.
/// </summary>
[Required]
public string BaseImageName { get; set; }

[Required]
public string BaseImageTag { get; set; }

[Required]
public string InputRegistryURL { get; set; }

[Required]
public string OutputRegistryURL { get; set; }

[Required]
public ITaskItem[] Files { get; set; }

/// <summary>
/// $(ContainerWorkingDirectory)
/// </summary>
[Required]
public string WorkingDirectory { get; set; }

[Required]
public string NewImageName { get; set; }

[Required]
public string Entrypoint { get; set; }

/// <summary>
/// Arguments to pass alongside Entrypoint.
/// </summary>
public string EntrypointArgs { get; set; }

public string PublishDirectory { get; set; }

/// <summary>
/// CreateNewImage needs to:
/// 1. Pull a base image (needs parameters: URL, BaseImage, BaseImageTag)
/// 2. Add output of build as a new layer
/// 3. Push image back to some registry (needs parameters: OutputURL, NewName, EntryPoint)
/// </summary>
/// <returns></returns>
public override bool Execute()
{
if (Files.Length == 0)
{
Log.LogError("No files to publish, aborting");
return false;
}

Registry reg = new Registry(new Uri(InputRegistryURL));

Image image;
try
{
image = reg.GetImageManifest(BaseImageName, BaseImageTag).Result;
}
catch (Exception ex)
{
Log.LogError("GetImageManifest Failed: {0}.\n{1}", ex.Message, ex.InnerException);
return false;
}

// Turn the build output from items into an array of filepaths
string[] filePaths = Files.Select((f) => f.ItemSpec).ToArray();

// preserve the folder structure of items published
string[] relativeFilePaths = filePaths.Select((x) => Path.GetRelativePath(PublishDirectory, x)).ToArray<string>();

List<(string file, string relativePath)> filesWithPaths = new List<(string file, string relativePath)>();

for (int i = 0; i < filePaths.Length; i++)
{
Log.LogMessage("File {0} has relative path of {1}", filePaths[i], relativeFilePaths[i]);
filesWithPaths.Add((filePaths[i], relativeFilePaths[i]));
}

Layer newLayer = Layer.FromFiles(filesWithPaths.AsEnumerable());

image.AddLayer(newLayer);
image.SetEntrypoint(Entrypoint, EntrypointArgs?.Split(' ').ToArray());

Registry outputReg = new Registry(new Uri(OutputRegistryURL));

try
{
outputReg.Push(image, NewImageName, BaseImageName).Wait();
}
catch (Exception e)
{
Log.LogError("Failed to push to the output registry: {0}\n{1}", e.Message, e.InnerException);
return false;
}

return true;
}
}
}
17 changes: 17 additions & 0 deletions System.Containers.Tasks/System.Containers.Tasks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\System.Containers\System.Containers.csproj" />
</ItemGroup>

</Project>
11 changes: 9 additions & 2 deletions System.Containers.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.32710.52
MinimumVisualStudioVersion = 10.0.40219.1
Expand All @@ -8,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.System.Containers", "T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.System.Containers.Filesystem", "Test.System.Containers.Filesystem\Test.System.Containers.Filesystem.csproj", "{CC249FAD-C24D-4B37-AF8F-FA05D0FD5620}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "containerize", "containerize\containerize.csproj", "{151F7937-D2AF-4242-9B6D-81FD5228D132}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "containerize", "containerize\containerize.csproj", "{151F7937-D2AF-4242-9B6D-81FD5228D132}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Containers.Tasks", "System.Containers.Tasks\System.Containers.Tasks.csproj", "{1CA8810B-8582-478E-A1E6-C9C7D53D8004}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -32,6 +35,10 @@ Global
{151F7937-D2AF-4242-9B6D-81FD5228D132}.Debug|Any CPU.Build.0 = Debug|Any CPU
{151F7937-D2AF-4242-9B6D-81FD5228D132}.Release|Any CPU.ActiveCfg = Release|Any CPU
{151F7937-D2AF-4242-9B6D-81FD5228D132}.Release|Any CPU.Build.0 = Release|Any CPU
{1CA8810B-8582-478E-A1E6-C9C7D53D8004}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1CA8810B-8582-478E-A1E6-C9C7D53D8004}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1CA8810B-8582-478E-A1E6-C9C7D53D8004}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1CA8810B-8582-478E-A1E6-C9C7D53D8004}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit e4d3c25

Please sign in to comment.