|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using Valleysoft.DockerfileModel; |
| 6 | + |
| 7 | +namespace SdkVersionAnalyzer; |
| 8 | + |
| 9 | +internal static partial class DockerfileAnalyzer |
| 10 | +{ |
| 11 | + public static bool VerifyVersions(string root, DotnetSdkVersion expectedDotnetSdkVersion) |
| 12 | + { |
| 13 | + return FileAnalyzer.VerifyMultiple(GetDockerfiles(root), VerifySdkVersions, expectedDotnetSdkVersion); |
| 14 | + } |
| 15 | + |
| 16 | + public static void ModifyVersions(string root, DotnetSdkVersion requestedDotnetSdkVersion) |
| 17 | + { |
| 18 | + FileAnalyzer.ModifyMultiple(GetDockerfiles(root), ModifySdkVersions, requestedDotnetSdkVersion); |
| 19 | + } |
| 20 | + |
| 21 | + [GeneratedRegex(@"-v (\d\.\d\.\d{3})\s", RegexOptions.IgnoreCase, "en-US")] |
| 22 | + private static partial Regex VersionRegex(); |
| 23 | + |
| 24 | + private static string ModifySdkVersions(string content, DotnetSdkVersion requestedDotnetSdkVersion) |
| 25 | + { |
| 26 | + var dockerfile = Dockerfile.Parse(content); |
| 27 | + var runInstruction = GetDotnetInstallingInstruction(dockerfile); |
| 28 | + |
| 29 | + if (runInstruction is not null) |
| 30 | + { |
| 31 | + runInstruction.Command = GetModifiedInstallCommand(runInstruction.Command, requestedDotnetSdkVersion); |
| 32 | + } |
| 33 | + |
| 34 | + var fromInstruction = GetFromInstruction(dockerfile); |
| 35 | + var imageName = ImageName.Parse(fromInstruction.ImageName); |
| 36 | + |
| 37 | + if (IsDotnetSdkImage(imageName)) |
| 38 | + { |
| 39 | + fromInstruction.ImageName = GetModifiedImageName(requestedDotnetSdkVersion, imageName); |
| 40 | + } |
| 41 | + |
| 42 | + return dockerfile.ToString(); |
| 43 | + } |
| 44 | + |
| 45 | + private static bool VerifySdkVersions(string content, DotnetSdkVersion expectedDotnetSdkVersion) |
| 46 | + { |
| 47 | + string? net6SdkVersion = null; |
| 48 | + string? net7SdkVersion = null; |
| 49 | + string? net8SdkVersion = null; |
| 50 | + |
| 51 | + var dockerfile = Dockerfile.Parse(content); |
| 52 | + var instruction = GetDotnetInstallingInstruction(dockerfile); |
| 53 | + |
| 54 | + // Extract all the versions from an instruction like: |
| 55 | + // RUN curl -sSL https://dot.net/v1/dotnet-install.sh --output dotnet-install.sh \ |
| 56 | + // && echo "SHA256: $(sha256sum dotnet-install.sh)" \ |
| 57 | + // && echo "de4957e41252191427a8ba0866f640b9f19c98fad62305919de41bd332e9c820 dotnet-install.sh" | sha256sum -c \ |
| 58 | + // && chmod +x ./dotnet-install.sh \ |
| 59 | + // && ./dotnet-install.sh -v 6.0.427 --install-dir /usr/share/dotnet --no-path \ |
| 60 | + // && ./dotnet-install.sh -v 7.0.410 --install-dir /usr/share/dotnet --no-path \ |
| 61 | + // && rm dotnet-install.sh |
| 62 | + |
| 63 | + if (instruction is not null) |
| 64 | + { |
| 65 | + var matchCollection = VersionRegex().Matches(instruction.ToString()); |
| 66 | + foreach (Match match in matchCollection) |
| 67 | + { |
| 68 | + var extractedSdkVersion = match.Groups[1].Value; |
| 69 | + if (VersionComparer.IsNet6Version(extractedSdkVersion)) |
| 70 | + { |
| 71 | + net6SdkVersion = extractedSdkVersion; |
| 72 | + } |
| 73 | + else if (VersionComparer.IsNet7Version(extractedSdkVersion)) |
| 74 | + { |
| 75 | + net7SdkVersion = extractedSdkVersion; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Extract NET8 SDK version from the base image tag |
| 81 | + // e.g. FROM mcr.microsoft.com/dotnet/sdk:8.0.403-alpine3.20 |
| 82 | + |
| 83 | + var fromInstruction = GetFromInstruction(dockerfile); |
| 84 | + |
| 85 | + var imageName = ImageName.Parse(fromInstruction.ImageName); |
| 86 | + |
| 87 | + if (IsDotnetSdkImage(imageName)) |
| 88 | + { |
| 89 | + var (sdkVersion, _) = GetSdkVersionAndSuffix(imageName); |
| 90 | + net8SdkVersion = sdkVersion; |
| 91 | + } |
| 92 | + |
| 93 | + return VersionComparer.CompareVersions(expectedDotnetSdkVersion, net6SdkVersion, net7SdkVersion, net8SdkVersion); |
| 94 | + } |
| 95 | + |
| 96 | + private static string GetModifiedImageName(DotnetSdkVersion requestedDotnetSdkVersion, ImageName imageName) |
| 97 | + { |
| 98 | + var (_, suffix) = GetSdkVersionAndSuffix(imageName); |
| 99 | + var modifiedTag = $"{requestedDotnetSdkVersion.Net8SdkVersion}-{suffix}"; |
| 100 | + |
| 101 | + return ImageName.FormatImageName(imageName.Repository, imageName.Registry, modifiedTag, null); |
| 102 | + } |
| 103 | + |
| 104 | + private static (string SdkVersion, string Suffix) GetSdkVersionAndSuffix(ImageName imageName) |
| 105 | + { |
| 106 | + // Extract sdk version and suffix from a tag like '8.0.403-alpine3.20' |
| 107 | + var parts = imageName.Tag!.Split('-', 2); |
| 108 | + return (parts[0], parts[1]); |
| 109 | + } |
| 110 | + |
| 111 | + private static bool IsDotnetSdkImage(ImageName imageName) |
| 112 | + { |
| 113 | + return imageName is { Registry: "mcr.microsoft.com", Repository: "dotnet/sdk" }; |
| 114 | + } |
| 115 | + |
| 116 | + private static FromInstruction GetFromInstruction(Dockerfile dockerfile) |
| 117 | + { |
| 118 | + return dockerfile |
| 119 | + .Items |
| 120 | + .OfType<FromInstruction>() |
| 121 | + .First(); |
| 122 | + } |
| 123 | + |
| 124 | + private static Command GetModifiedInstallCommand(Command command, DotnetSdkVersion requestedDotnetSdkVersion) |
| 125 | + { |
| 126 | + var newCommandText = VersionRegex().Replace(command.ToString(), match => $"-v {GetNewVersion(match.Groups[1].Value, requestedDotnetSdkVersion)} "); |
| 127 | + return command.CommandType == CommandType.ShellForm ? ShellFormCommand.Parse(newCommandText) : ExecFormCommand.Parse(newCommandText); |
| 128 | + } |
| 129 | + |
| 130 | + private static string GetNewVersion(string oldVersion, DotnetSdkVersion requestedDotnetSdkVersion) |
| 131 | + { |
| 132 | + if (VersionComparer.IsNet6Version(oldVersion)) |
| 133 | + { |
| 134 | + return requestedDotnetSdkVersion.Net6SdkVersion!; |
| 135 | + } |
| 136 | + |
| 137 | + if (VersionComparer.IsNet7Version(oldVersion)) |
| 138 | + { |
| 139 | + return requestedDotnetSdkVersion.Net7SdkVersion!; |
| 140 | + } |
| 141 | + |
| 142 | + return oldVersion; |
| 143 | + } |
| 144 | + |
| 145 | + private static string[] GetDockerfiles(string root) |
| 146 | + { |
| 147 | + var dockerfilesDir = Path.Combine(root, "docker"); |
| 148 | + return Directory.GetFiles(dockerfilesDir, "*.dockerfile"); |
| 149 | + } |
| 150 | + |
| 151 | + private static RunInstruction? GetDotnetInstallingInstruction(Dockerfile dockerfile) |
| 152 | + { |
| 153 | + return dockerfile.Items.OfType<RunInstruction>().SingleOrDefault(i => i.ToString().Contains("./dotnet-install.sh")); |
| 154 | + } |
| 155 | +} |
0 commit comments