Skip to content

Commit

Permalink
Allow naming of containers (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmus authored May 14, 2022
1 parent 455886c commit ab8f4e9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 0.14-beta

* *Nothing yet...*
* New: Allow naming of containers by creating a `bake.yaml` file besides
the `Dockerfile`

# 0.13-beta

Expand Down
17 changes: 17 additions & 0 deletions Source/Bake.Tests/Helpers/DockerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,22 @@ await Client.Containers.StartContainerAsync(
CancellationToken.None).Wait(CancellationToken.None);
});
}

public static async Task<IReadOnlyCollection<string>> ListImagesAsync(
CancellationToken cancellationToken = default)
{
var imageResponse = await Client.Images.ListImagesAsync(
new ImagesListParameters
{
All = true,
},
cancellationToken);

return imageResponse
.Where(i => i.RepoTags != null)
.SelectMany(i => i.RepoTags)
.OrderBy(t => t)
.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
// SOFTWARE.

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Bake.Core;
using Bake.Tests.Helpers;
using FluentAssertions;
using NUnit.Framework;
using File = System.IO.File;

// ReSharper disable StringLiteralTypo

Expand All @@ -40,12 +42,16 @@ public DockerFileSimpleTests() : base("Dockerfile.Simple")
[Test]
public async Task Run()
{
// Arrange
var version = SemVer.Random.ToString();
var expectedContainerNameAndTag = $"awesome-container:{version}";

// Act
var returnCode = await ExecuteAsync(TestState.New(
"run",
"--convention=Release",
"--destination=container>localhost:5000",
"--build-version", SemVer.Random.ToString())
"--build-version", version)
.WithEnvironmentVariables(new Dictionary<string, string>
{
["bake_credentials_docker_localhost_username"] = "registryuser",
Expand All @@ -54,6 +60,48 @@ public async Task Run()

// Assert
returnCode.Should().Be(0);
var images = await DockerHelper.ListImagesAsync();
images.Should().Contain(new[]
{
$"bake.local/{expectedContainerNameAndTag}",
$"localhost:5000/{expectedContainerNameAndTag}"
});
}

[Test]
public async Task NamedDockerfile()
{
// Arrange
var version = SemVer.Random.ToString();
var projectFilePath = Path.Combine(
WorkingDirectory,
"Awesome.Container",
"bake.yaml");
await File.WriteAllTextAsync(
projectFilePath,
"name: \"magic-container\"");
var expectedContainerNameAndTag = $"magic-container:{version}";

// Act
var returnCode = await ExecuteAsync(TestState.New(
"run",
"--convention=Release",
"--destination=container>localhost:5000",
"--build-version", version)
.WithEnvironmentVariables(new Dictionary<string, string>
{
["bake_credentials_docker_localhost_username"] = "registryuser",
["bake_credentials_docker_localhost_password"] = "registrypassword",
}));

// Assert
returnCode.Should().Be(0);
var images = await DockerHelper.ListImagesAsync();
images.Should().Contain(new []
{
$"bake.local/{expectedContainerNameAndTag}",
$"localhost:5000/{expectedContainerNameAndTag}"
});
}
}
}
35 changes: 32 additions & 3 deletions Source/Bake/Cooking/Composers/DockerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Bake.ValueObjects.Recipes;
using Bake.ValueObjects.Recipes.Docker;
using Microsoft.Extensions.Logging;
using File = System.IO.File;

namespace Bake.Cooking.Composers
{
Expand All @@ -52,19 +53,22 @@ public class DockerComposer : Composer
private readonly IFileSystem _fileSystem;
private readonly IContainerTagParser _containerTagParser;
private readonly IConventionInterpreter _conventionInterpreter;
private readonly IBakeProjectParser _bakeProjectParser;

public DockerComposer(
ILogger<DockerComposer> logger,
IDefaults defaults,
IFileSystem fileSystem,
IContainerTagParser containerTagParser,
IConventionInterpreter conventionInterpreter)
IConventionInterpreter conventionInterpreter,
IBakeProjectParser bakeProjectParser)
{
_logger = logger;
_defaults = defaults;
_fileSystem = fileSystem;
_containerTagParser = containerTagParser;
_conventionInterpreter = conventionInterpreter;
_bakeProjectParser = bakeProjectParser;
}

public override async Task<IReadOnlyCollection<Recipe>> ComposeAsync(
Expand All @@ -89,8 +93,12 @@ public override async Task<IReadOnlyCollection<Recipe>> ComposeAsync(
_logger.LogInformation(
"Located Dockerfile at these locations, scheduling build: {DockerFiles}",
dockerFilePaths);
var directoryName = Path.GetFileName(Path.GetDirectoryName(dockerFilePath));
recipes.AddRange(CreateRecipes(dockerFilePath, directoryName, ingredients.Version, urls));

var containerName = await GetContainerNameAsync(
Path.GetDirectoryName(dockerFilePath),
cancellationToken);

recipes.AddRange(CreateRecipes(dockerFilePath, containerName, ingredients.Version, urls));
}

var dockerfileArtifacts = context
Expand Down Expand Up @@ -124,6 +132,27 @@ public override async Task<IReadOnlyCollection<Recipe>> ComposeAsync(
return recipes;
}

private async Task<string> GetContainerNameAsync(
string directory,
CancellationToken cancellationToken)
{
var projectFilePath = Path.Combine(
directory,
"bake.yaml"); // TODO: Rework to align file name

if (File.Exists(projectFilePath))
{
var fileContent = await File.ReadAllTextAsync(projectFilePath, cancellationToken);
var bakeProject = await _bakeProjectParser.ParseAsync(fileContent, cancellationToken);
if (!string.IsNullOrEmpty(bakeProject.Name))
{
return bakeProject.Name;
}
}

return Path.GetFileName(directory);
}

private IEnumerable<Recipe> CreateRecipes(
string path,
string name,
Expand Down
2 changes: 1 addition & 1 deletion Source/Bake/Cooking/Composers/GoComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private async Task<IReadOnlyCollection<Recipe>> CreateRecipesAsync(
var projectType = BakeProjectType.Tool;
var servicePort = -1;

var bakeProjectPath = Path.Combine(directoryPath, "bake.yaml");
var bakeProjectPath = Path.Combine(directoryPath, "bake.yaml"); // TODO: Rework to align file name
if (System.IO.File.Exists(bakeProjectPath))
{
var bakeProjectContent = await _fileSystem.ReadAllTextAsync(
Expand Down
3 changes: 3 additions & 0 deletions Source/Bake/ValueObjects/BakeProjects/BakeProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace Bake.ValueObjects.BakeProjects
{
public class BakeProject
{
[YamlMember(Alias = "name")]
public string? Name { get; set; }

[YamlMember]
public BakeProjectType Type { get; set; }

Expand Down

0 comments on commit ab8f4e9

Please sign in to comment.