-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generateDockerfiles command (#581)
- Loading branch information
1 parent
bfc7bb8
commit 68a7510
Showing
10 changed files
with
483 additions
and
6 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
src/Microsoft.DotNet.ImageBuilder/src/Commands/GenerateDockerfilesCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// 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.Collections.Generic; | ||
using System.ComponentModel.Composition; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using Cottle; | ||
using Cottle.Exceptions; | ||
using Microsoft.DotNet.ImageBuilder.Models.Manifest; | ||
using Microsoft.DotNet.ImageBuilder.ViewModel; | ||
|
||
namespace Microsoft.DotNet.ImageBuilder.Commands | ||
{ | ||
[Export(typeof(ICommand))] | ||
public class GenerateDockerfilesCommand : ManifestCommand<GenerateDockerfilesOptions> | ||
{ | ||
private readonly DocumentConfiguration _config = new DocumentConfiguration | ||
{ | ||
BlockBegin = "{{", | ||
BlockContinue = "^", | ||
BlockEnd = "}}", | ||
Escape = '@', | ||
Trimmer = DocumentConfiguration.TrimNothing | ||
}; | ||
|
||
private readonly IEnvironmentService _environmentService; | ||
|
||
[ImportingConstructor] | ||
public GenerateDockerfilesCommand(IEnvironmentService environmentService) : base() | ||
{ | ||
_environmentService = environmentService ?? throw new ArgumentNullException(nameof(environmentService)); | ||
} | ||
|
||
public override async Task ExecuteAsync() | ||
{ | ||
Logger.WriteHeading("GENERATING DOCKERFILES"); | ||
|
||
List<string> outOfSyncDockerfiles = new List<string>(); | ||
List<string> invalidTemplates = new List<string>(); | ||
|
||
IEnumerable<PlatformInfo> platforms = Manifest.GetFilteredPlatforms() | ||
.Where(platform => platform.DockerfileTemplate != null); | ||
foreach (PlatformInfo platform in platforms) | ||
{ | ||
Logger.WriteSubheading($"Generating '{platform.DockerfilePath}' from '{platform.DockerfileTemplate}'"); | ||
|
||
string template = await File.ReadAllTextAsync(platform.DockerfileTemplate); | ||
if (Options.IsVerbose) | ||
{ | ||
Logger.WriteMessage($"Template:{Environment.NewLine}{template}"); | ||
} | ||
|
||
await GenerateDockerfileAsync(template, platform, outOfSyncDockerfiles, invalidTemplates); | ||
} | ||
|
||
if (outOfSyncDockerfiles.Any() || invalidTemplates.Any()) | ||
{ | ||
if (outOfSyncDockerfiles.Any()) | ||
{ | ||
string dockerfileList = string.Join(Environment.NewLine, outOfSyncDockerfiles); | ||
Logger.WriteError($"Dockerfiles out of sync with templates:{Environment.NewLine}{dockerfileList}"); | ||
} | ||
|
||
if (invalidTemplates.Any()) | ||
{ | ||
string templateList = string.Join(Environment.NewLine, invalidTemplates); | ||
Logger.WriteError($"Invalid Templates:{Environment.NewLine}{templateList}"); | ||
} | ||
|
||
_environmentService.Exit(1); | ||
} | ||
} | ||
|
||
private async Task GenerateDockerfileAsync(string template, PlatformInfo platform, List<string> outOfSyncDockerfiles, List<string> invalidTemplates) | ||
{ | ||
try | ||
{ | ||
IDocument document = Document.CreateDefault(template, _config).DocumentOrThrow; | ||
string generatedDockerfile = document.Render(Context.CreateBuiltin(GetSymbols(platform))); | ||
|
||
string currentDockerfile = File.Exists(platform.DockerfilePath) ? | ||
await File.ReadAllTextAsync(platform.DockerfilePath) : string.Empty; | ||
if (currentDockerfile == generatedDockerfile) | ||
{ | ||
Logger.WriteMessage("Dockerfile in sync with template"); | ||
} | ||
else if (Options.Validate) | ||
{ | ||
Logger.WriteError("Dockerfile out of sync with template"); | ||
outOfSyncDockerfiles.Add(platform.DockerfilePath); | ||
} | ||
else | ||
{ | ||
if (Options.IsVerbose) | ||
{ | ||
Logger.WriteMessage($"Generated Dockerfile:{Environment.NewLine}{generatedDockerfile}"); | ||
} | ||
|
||
if (!Options.IsDryRun) | ||
{ | ||
await File.WriteAllTextAsync(platform.DockerfilePath, generatedDockerfile); | ||
Logger.WriteMessage($"Updated '{platform.DockerfilePath}'"); | ||
} | ||
} | ||
} | ||
catch (ParseException e) | ||
{ | ||
Logger.WriteError($"Error: {e}{Environment.NewLine}Invalid Syntax:{Environment.NewLine}{template.Substring(e.LocationStart)}"); | ||
invalidTemplates.Add(platform.DockerfileTemplate); | ||
} | ||
} | ||
|
||
private static string GetOsArchHyphenatedName(PlatformInfo platform) | ||
{ | ||
string osName; | ||
if (platform.BaseOsVersion.Contains("nanoserver")) | ||
{ | ||
string version = platform.BaseOsVersion.Split('-')[1]; | ||
osName = $"NanoServer-{version}"; | ||
} | ||
else | ||
{ | ||
osName = platform.GetOSDisplayName().Replace(' ', '-'); | ||
} | ||
|
||
string archName = platform.Model.Architecture != Architecture.AMD64 ? $"-{platform.Model.Architecture.GetDisplayName()}" : string.Empty; | ||
|
||
return osName + archName; | ||
} | ||
|
||
public IReadOnlyDictionary<Value, Value> GetSymbols(PlatformInfo platform) | ||
{ | ||
string versionedArch = platform.Model.Architecture.GetDisplayName(platform.Model.Variant); | ||
|
||
return new Dictionary<Value, Value> | ||
{ | ||
["ARCH_SHORT"] = platform.Model.Architecture.GetShortName(), | ||
["ARCH_NUPKG"] = platform.Model.Architecture.GetNupkgName(), | ||
["ARCH_VERSIONED"] = versionedArch, | ||
["ARCH_TAG_SUFFIX"] = platform.Model.Architecture != Architecture.AMD64 ? $"-{versionedArch}" : string.Empty, | ||
["OS_VERSION"] = platform.Model.OsVersion, | ||
["OS_VERSION_BASE"] = platform.BaseOsVersion, | ||
["OS_VERSION_NUMBER"] = Regex.Match(platform.Model.OsVersion, @"\d+.\d+").Value, | ||
["OS_ARCH_HYPHENATED"] = GetOsArchHyphenatedName(platform), | ||
["VARIABLES"] = Manifest.Model?.Variables?.ToDictionary(kvp => (Value)kvp.Key, kvp => (Value)kvp.Value) | ||
}; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Microsoft.DotNet.ImageBuilder/src/Commands/GenerateDockerfilesOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// 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.CommandLine; | ||
|
||
namespace Microsoft.DotNet.ImageBuilder.Commands | ||
{ | ||
public class GenerateDockerfilesOptions : ManifestOptions, IFilterableOptions | ||
{ | ||
protected override string CommandHelp => "Generates the Dockerfiles from Cottle based templates (http://r3c.github.io/cottle/)"; | ||
|
||
public ManifestFilterOptions FilterOptions { get; } = new ManifestFilterOptions(); | ||
|
||
public bool Validate { get; set; } | ||
|
||
public GenerateDockerfilesOptions() : base() | ||
{ | ||
} | ||
|
||
public override void DefineOptions(ArgumentSyntax syntax) | ||
{ | ||
base.DefineOptions(syntax); | ||
|
||
FilterOptions.DefineOptions(syntax); | ||
|
||
bool validate = false; | ||
syntax.DefineOption("validate", ref validate, "Validates the Dockerfiles and templates are in sync"); | ||
Validate = validate; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.