|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.ComponentModel.Composition; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Cottle; |
| 13 | +using Cottle.Exceptions; |
| 14 | +using Microsoft.DotNet.ImageBuilder.Models.Manifest; |
| 15 | +using Microsoft.DotNet.ImageBuilder.ViewModel; |
| 16 | + |
| 17 | +namespace Microsoft.DotNet.ImageBuilder.Commands |
| 18 | +{ |
| 19 | + [Export(typeof(ICommand))] |
| 20 | + public class GenerateDockerfilesCommand : ManifestCommand<GenerateDockerfilesOptions> |
| 21 | + { |
| 22 | + private readonly DocumentConfiguration _config = new DocumentConfiguration |
| 23 | + { |
| 24 | + BlockBegin = "{{", |
| 25 | + BlockContinue = "^", |
| 26 | + BlockEnd = "}}", |
| 27 | + Escape = '@', |
| 28 | + Trimmer = DocumentConfiguration.TrimNothing |
| 29 | + }; |
| 30 | + |
| 31 | + private readonly IEnvironmentService _environmentService; |
| 32 | + |
| 33 | + [ImportingConstructor] |
| 34 | + public GenerateDockerfilesCommand(IEnvironmentService environmentService) : base() |
| 35 | + { |
| 36 | + _environmentService = environmentService ?? throw new ArgumentNullException(nameof(environmentService)); |
| 37 | + } |
| 38 | + |
| 39 | + public override async Task ExecuteAsync() |
| 40 | + { |
| 41 | + Logger.WriteHeading("GENERATING DOCKERFILES"); |
| 42 | + |
| 43 | + List<string> outOfSyncDockerfiles = new List<string>(); |
| 44 | + List<string> invalidTemplates = new List<string>(); |
| 45 | + |
| 46 | + IEnumerable<PlatformInfo> platforms = Manifest.GetFilteredPlatforms() |
| 47 | + .Where(platform => platform.DockerfileTemplate != null); |
| 48 | + foreach (PlatformInfo platform in platforms) |
| 49 | + { |
| 50 | + Logger.WriteSubheading($"Generating '{platform.DockerfilePath}' from '{platform.DockerfileTemplate}'"); |
| 51 | + |
| 52 | + string template = await File.ReadAllTextAsync(platform.DockerfileTemplate); |
| 53 | + if (Options.IsVerbose) |
| 54 | + { |
| 55 | + Logger.WriteMessage($"Template:{Environment.NewLine}{template}"); |
| 56 | + } |
| 57 | + |
| 58 | + await GenerateDockerfileAsync(template, platform, outOfSyncDockerfiles, invalidTemplates); |
| 59 | + } |
| 60 | + |
| 61 | + if (outOfSyncDockerfiles.Any() || invalidTemplates.Any()) |
| 62 | + { |
| 63 | + if (outOfSyncDockerfiles.Any()) |
| 64 | + { |
| 65 | + string dockerfileList = string.Join(Environment.NewLine, outOfSyncDockerfiles); |
| 66 | + Logger.WriteError($"Dockerfiles out of sync with templates:{Environment.NewLine}{dockerfileList}"); |
| 67 | + } |
| 68 | + |
| 69 | + if (invalidTemplates.Any()) |
| 70 | + { |
| 71 | + string templateList = string.Join(Environment.NewLine, invalidTemplates); |
| 72 | + Logger.WriteError($"Invalid Templates:{Environment.NewLine}{templateList}"); |
| 73 | + } |
| 74 | + |
| 75 | + _environmentService.Exit(1); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private async Task GenerateDockerfileAsync(string template, PlatformInfo platform, List<string> outOfSyncDockerfiles, List<string> invalidTemplates) |
| 80 | + { |
| 81 | + try |
| 82 | + { |
| 83 | + IDocument document = Document.CreateDefault(template, _config).DocumentOrThrow; |
| 84 | + string generatedDockerfile = document.Render(Context.CreateBuiltin(GetSymbols(platform))); |
| 85 | + |
| 86 | + string currentDockerfile = File.Exists(platform.DockerfilePath) ? |
| 87 | + await File.ReadAllTextAsync(platform.DockerfilePath) : string.Empty; |
| 88 | + if (currentDockerfile == generatedDockerfile) |
| 89 | + { |
| 90 | + Logger.WriteMessage("Dockerfile in sync with template"); |
| 91 | + } |
| 92 | + else if (Options.Validate) |
| 93 | + { |
| 94 | + Logger.WriteError("Dockerfile out of sync with template"); |
| 95 | + outOfSyncDockerfiles.Add(platform.DockerfilePath); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + if (Options.IsVerbose) |
| 100 | + { |
| 101 | + Logger.WriteMessage($"Generated Dockerfile:{Environment.NewLine}{generatedDockerfile}"); |
| 102 | + } |
| 103 | + |
| 104 | + if (!Options.IsDryRun) |
| 105 | + { |
| 106 | + await File.WriteAllTextAsync(platform.DockerfilePath, generatedDockerfile); |
| 107 | + Logger.WriteMessage($"Updated '{platform.DockerfilePath}'"); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + catch (ParseException e) |
| 112 | + { |
| 113 | + Logger.WriteError($"Error: {e}{Environment.NewLine}Invalid Syntax:{Environment.NewLine}{template.Substring(e.LocationStart)}"); |
| 114 | + invalidTemplates.Add(platform.DockerfileTemplate); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static string GetOsArchHyphenatedName(PlatformInfo platform) |
| 119 | + { |
| 120 | + string osName; |
| 121 | + if (platform.BaseOsVersion.Contains("nanoserver")) |
| 122 | + { |
| 123 | + string version = platform.BaseOsVersion.Split('-')[1]; |
| 124 | + osName = $"NanoServer-{version}"; |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + osName = platform.GetOSDisplayName().Replace(' ', '-'); |
| 129 | + } |
| 130 | + |
| 131 | + string archName = platform.Model.Architecture != Architecture.AMD64 ? $"-{platform.Model.Architecture.GetDisplayName()}" : string.Empty; |
| 132 | + |
| 133 | + return osName + archName; |
| 134 | + } |
| 135 | + |
| 136 | + public IReadOnlyDictionary<Value, Value> GetSymbols(PlatformInfo platform) |
| 137 | + { |
| 138 | + string versionedArch = platform.Model.Architecture.GetDisplayName(platform.Model.Variant); |
| 139 | + |
| 140 | + return new Dictionary<Value, Value> |
| 141 | + { |
| 142 | + ["ARCH_SHORT"] = platform.Model.Architecture.GetShortName(), |
| 143 | + ["ARCH_NUPKG"] = platform.Model.Architecture.GetNupkgName(), |
| 144 | + ["ARCH_VERSIONED"] = versionedArch, |
| 145 | + ["ARCH_TAG_SUFFIX"] = platform.Model.Architecture != Architecture.AMD64 ? $"-{versionedArch}" : string.Empty, |
| 146 | + ["OS_VERSION"] = platform.Model.OsVersion, |
| 147 | + ["OS_VERSION_BASE"] = platform.BaseOsVersion, |
| 148 | + ["OS_VERSION_NUMBER"] = Regex.Match(platform.Model.OsVersion, @"\d+.\d+").Value, |
| 149 | + ["OS_ARCH_HYPHENATED"] = GetOsArchHyphenatedName(platform), |
| 150 | + ["VARIABLES"] = Manifest.Model?.Variables?.ToDictionary(kvp => (Value)kvp.Key, kvp => (Value)kvp.Value) |
| 151 | + }; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments