Skip to content

Commit d98609a

Browse files
authored
[tool] Helper tool to modify .NET SDK version used in dockerfiles and action workflows (#3740)
1 parent 5745437 commit d98609a

14 files changed

+354
-127
lines changed

OpenTelemetry.AutoInstrumentation.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.RabbitMq",
243243
EndProject
244244
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApplication.Owin.IIS.NetFramework", "test\test-applications\integrations\TestApplication.Owin.IIS.NetFramework\TestApplication.Owin.IIS.NetFramework.csproj", "{AA3E0C5C-A4E2-46AB-BD18-2D30D3ABF692}"
245245
EndProject
246-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SdkVersionVerifier", "tools\SdkVersionVerifier\SdkVersionVerifier.csproj", "{C75FA076-D460-414B-97F7-6F8D0E85AE74}"
246+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SdkVersionAnalyzer", "tools\SdkVersionAnalyzer\SdkVersionAnalyzer.csproj", "{C75FA076-D460-414B-97F7-6F8D0E85AE74}"
247247
EndProject
248248
Global
249249
GlobalSection(SolutionConfigurationPlatforms) = preSolution

build/Build.Steps.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,11 @@ void RemoveFilesInNetFolderAvailableInAdditionalStore()
365365
Target VerifySdkVersions => _ => _
366366
.Executes(() =>
367367
{
368-
var verifier = Solution.GetProjectByName(Projects.Tools.SdkVersionVerifierTool);
368+
var verifier = Solution.GetProjectByName(Projects.Tools.SdkVersionAnalyzerTool);
369369

370370
DotNetRun(s => s
371371
.SetProjectFile(verifier)
372-
.SetApplicationArguments(RootDirectory));
372+
.SetApplicationArguments($"--verify {RootDirectory}"));
373373
});
374374

375375
Target GenerateLibraryVersionFiles => _ => _

build/Projects.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public static class Tools
3535
{
3636
public const string LibraryVersionsGenerator = "LibraryVersionsGenerator";
3737
public const string GacInstallTool = "GacInstallTool";
38-
public const string SdkVersionVerifierTool = "SdkVersionVerifier";
38+
public const string SdkVersionAnalyzerTool = "SdkVersionAnalyzer";
3939
}
4040
}

tools/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project>
22
<Import Project="..\Directory.Packages.props" />
33
<ItemGroup>
4-
<PackageVersion Include="Dockerfile" Version="1.0.0" />
54
<PackageVersion Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
65
<PackageVersion Include="Microsoft.Build" Version="17.7.2" />
76
<!-- System.Text.Json - Indirect vulnerable dependency https://github.com/advisories/GHSA-hh2w-p6rv-4g7w from Microsoft.Build -->
@@ -10,6 +9,7 @@
109
<!-- NuGet.ProjectModel - Indirect vulnerable dependency https://github.com/advisories/GHSA-447r-wph3-92pm from NuGet.ProjectModel -->
1110
<PackageVersion Include="System.Formats.Asn1" Version="8.0.1" />
1211
<PackageVersion Include="System.IO.Abstractions" Version="21.0.29" />
12+
<PackageVersion Include="Valleysoft.DockerfileModel" Version="1.2.0" />
1313
<PackageVersion Include="YamlDotNet" Version="16.1.3" />
1414
</ItemGroup>
1515
</Project>

tools/SdkVersionVerifier/ActionWorkflowVerifier.cs renamed to tools/SdkVersionAnalyzer/ActionWorkflowAnalyzer.cs

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,100 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using System.Text;
5+
using YamlDotNet.Core;
6+
using YamlDotNet.Core.Events;
47
using YamlDotNet.RepresentationModel;
58

6-
namespace SdkVersionVerifier;
9+
namespace SdkVersionAnalyzer;
710

8-
internal static class ActionWorkflowVerifier
11+
internal static class ActionWorkflowAnalyzer
912
{
1013
public static DotnetSdkVersion? GetExpectedSdkVersionFromSampleWorkflow(string root)
1114
{
1215
var defaultWorkflow = Path.Combine(GetWorkflowsDirectory(root), "build.yml");
13-
return ExtractDotnetSdkVersions(File.ReadAllText(defaultWorkflow)).FirstOrDefault();
16+
var content = File.ReadAllText(defaultWorkflow);
17+
return ExtractDotnetSdkVersions(content).FirstOrDefault();
1418
}
1519

1620
public static bool VerifyVersions(string root, DotnetSdkVersion expectedDotnetSdkVersion)
1721
{
18-
var workflowsDir = GetWorkflowsDirectory(root);
19-
var workflows = Directory.GetFiles(workflowsDir, "*.yml");
22+
return FileAnalyzer.VerifyMultiple(GetWorkflows(root), VerifySdkVersions, expectedDotnetSdkVersion);
23+
}
24+
25+
public static void ModifyVersions(string root, DotnetSdkVersion newDotnetSdkVersion)
26+
{
27+
FileAnalyzer.ModifyMultiple(GetWorkflows(root), ModifySdkVersions, newDotnetSdkVersion);
28+
}
29+
30+
private static string ModifySdkVersions(string content, DotnetSdkVersion newDotnetSdkVersion)
31+
{
32+
using var stringReader = new StringReader(content);
33+
var scanner = new Scanner(stringReader, skipComments: false);
34+
var parser = new Parser(scanner);
35+
36+
var stringBuilder = new StringBuilder();
37+
using var writer = new StringWriter(stringBuilder);
38+
var emitter = new Emitter(writer);
2039

21-
return FileVerifier.VerifyMultiple(workflows, VerifySdkVersions, expectedDotnetSdkVersion);
40+
// Use the parser/emitter approach to ensure comments in workflows are preserved
41+
while (parser.MoveNext())
42+
{
43+
var current = parser.Current;
44+
if (current is Scalar { IsKey: true, Value: "dotnet-version" } scalar)
45+
{
46+
emitter.Emit(scalar);
47+
parser.MoveNext();
48+
49+
var newScalar = GetNewDotnetVersionScalar(newDotnetSdkVersion);
50+
emitter.Emit(newScalar);
51+
continue;
52+
}
53+
54+
if (current is Scalar { Value: "" })
55+
{
56+
var newScalar = GetScalarWithExpectedFormatting();
57+
emitter.Emit(newScalar);
58+
continue;
59+
}
60+
61+
if (current != null)
62+
{
63+
emitter.Emit(current);
64+
}
65+
}
66+
67+
return stringBuilder.ToString();
68+
}
69+
70+
private static Scalar GetScalarWithExpectedFormatting()
71+
{
72+
// Ensure empty values end up as expected in action workflow, e.g.:
73+
// workflow_call:
74+
// and not:
75+
// workflow_call: ''
76+
return new Scalar(new TagName("tag:yaml.org,2002:null"), string.Empty);
77+
}
78+
79+
private static Scalar GetNewDotnetVersionScalar(DotnetSdkVersion newDotnetSdkVersion)
80+
{
81+
const char separator = '\n';
82+
var val = $"{newDotnetSdkVersion.Net6SdkVersion!}{separator}{newDotnetSdkVersion.Net7SdkVersion!}{separator}{newDotnetSdkVersion.Net8SdkVersion!}{separator}";
83+
84+
// Use ctor with default values, apart from ScalarStyle.
85+
// Use ScalarStyle.Literal to get dotnet-version with value similar to below:
86+
// dotnet-version: |
87+
// 6.0.437
88+
// 7.0.420
89+
// 8.0.413
90+
91+
return new Scalar(AnchorName.Empty, TagName.Empty, val, ScalarStyle.Literal, true, true, Mark.Empty, Mark.Empty);
92+
}
93+
94+
private static string[] GetWorkflows(string root)
95+
{
96+
var workflowsDir = GetWorkflowsDirectory(root);
97+
return Directory.GetFiles(workflowsDir, "*.yml");
2298
}
2399

24100
private static string GetWorkflowsDirectory(string root)
@@ -85,17 +161,17 @@ private static IEnumerable<DotnetSdkVersion> ExtractDotnetSdkVersions(string con
85161

86162
foreach (var version in dotnetVersionNode.ToString().Split())
87163
{
88-
if (version.StartsWith('6'))
164+
if (VersionComparer.IsNet6Version(version))
89165
{
90166
sdk6Version = version;
91167
}
92168

93-
if (version.StartsWith('7'))
169+
if (VersionComparer.IsNet7Version(version))
94170
{
95171
sdk7Version = version;
96172
}
97173

98-
if (version.StartsWith('8'))
174+
if (VersionComparer.IsNet8Version(version))
99175
{
100176
sdk8Version = version;
101177
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
namespace SdkVersionVerifier;
4+
namespace SdkVersionAnalyzer;
55

66
internal record DotnetSdkVersion(string? Net6SdkVersion, string? Net7SdkVersion, string? Net8SdkVersion);

tools/SdkVersionVerifier/FileVerifier.cs renamed to tools/SdkVersionAnalyzer/FileAnalyzer.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
namespace SdkVersionVerifier;
4+
namespace SdkVersionAnalyzer;
55

6-
internal static class FileVerifier
6+
internal static class FileAnalyzer
77
{
88
public static bool VerifyMultiple(
99
IEnumerable<string> filePaths,
@@ -26,4 +26,18 @@ public static bool VerifyMultiple(
2626

2727
return true;
2828
}
29+
30+
public static void ModifyMultiple(
31+
IEnumerable<string> filePaths,
32+
Func<string, DotnetSdkVersion, string> modifier,
33+
DotnetSdkVersion dotnetSdkVersion)
34+
{
35+
foreach (var filePath in filePaths)
36+
{
37+
Console.WriteLine($"Modifying SDK versions in {filePath}");
38+
var content = File.ReadAllText(filePath);
39+
var modifiedContent = modifier(content, dotnetSdkVersion);
40+
File.WriteAllText(filePath, modifiedContent);
41+
}
42+
}
2943
}

0 commit comments

Comments
 (0)