-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
DockerfileArchive.cs
194 lines (165 loc) · 7.72 KB
/
DockerfileArchive.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
namespace DotNet.Testcontainers.Images
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Configurations;
using ICSharpCode.SharpZipLib.Tar;
using Microsoft.Extensions.Logging;
/// <summary>
/// Generates a tar archive with Docker configuration files. The tar archive can be used to build a Docker image.
/// </summary>
internal sealed class DockerfileArchive : ITarArchive
{
private static readonly Regex FromLinePattern = new Regex("FROM (?<arg>--\\S+\\s)*(?<image>\\S+).*", RegexOptions.None, TimeSpan.FromSeconds(1));
private readonly DirectoryInfo _dockerfileDirectory;
private readonly FileInfo _dockerfile;
private readonly IImage _image;
private readonly ILogger _logger;
/// <summary>
/// Initializes a new instance of the <see cref="DockerfileArchive" /> class.
/// </summary>
/// <param name="dockerfileDirectory">Directory to Docker configuration files.</param>
/// <param name="dockerfile">Name of the Dockerfile, which is necessary to start the Docker build.</param>
/// <param name="image">Docker image information to create the tar archive for.</param>
/// <param name="logger">The logger.</param>
/// <exception cref="ArgumentException">Thrown when the Dockerfile directory does not exist or the directory does not contain a Dockerfile.</exception>
public DockerfileArchive(string dockerfileDirectory, string dockerfile, IImage image, ILogger logger)
: this(new DirectoryInfo(dockerfileDirectory), new FileInfo(dockerfile), image, logger)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DockerfileArchive" /> class.
/// </summary>
/// <param name="dockerfileDirectory">Directory to Docker configuration files.</param>
/// <param name="dockerfile">Name of the Dockerfile, which is necessary to start the Docker build.</param>
/// <param name="image">Docker image information to create the tar archive for.</param>
/// <param name="logger">The logger.</param>
/// <exception cref="ArgumentException">Thrown when the Dockerfile directory does not exist or the directory does not contain a Dockerfile.</exception>
public DockerfileArchive(DirectoryInfo dockerfileDirectory, FileInfo dockerfile, IImage image, ILogger logger)
{
if (!dockerfileDirectory.Exists)
{
throw new ArgumentException($"Directory '{dockerfileDirectory.FullName}' does not exist.");
}
if (dockerfileDirectory.GetFiles(dockerfile.ToString(), SearchOption.TopDirectoryOnly).Length == 0)
{
throw new ArgumentException($"{dockerfile} does not exist in '{dockerfileDirectory.FullName}'.");
}
_dockerfileDirectory = dockerfileDirectory;
_dockerfile = dockerfile;
_image = image;
_logger = logger;
}
/// <summary>
/// Gets a collection of base images.
/// </summary>
/// <remarks>
/// This method reads the Dockerfile and collects a list of base images. It
/// excludes stages that do not correspond to base images. For example, it will not include
/// the second line from the following Dockerfile configuration:
/// <code>
/// FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
/// FROM build
/// </code>
/// </remarks>
/// <returns>An <see cref="IEnumerable{T}" /> of <see cref="IImage" />.</returns>
public IEnumerable<IImage> GetBaseImages()
{
const string imageGroup = "image";
var lines = File.ReadAllLines(Path.Combine(_dockerfileDirectory.FullName, _dockerfile.ToString()))
.Select(line => line.Trim())
.Where(line => !string.IsNullOrEmpty(line))
.Where(line => !line.StartsWith("#", StringComparison.Ordinal))
.Select(line => FromLinePattern.Match(line))
.Where(match => match.Success)
.ToArray();
var stages = lines
.Select(line => line.Value)
.Select(line => line.Split(new [] { " AS ", " As ", " aS ", " as " }, StringSplitOptions.RemoveEmptyEntries))
.Where(substrings => substrings.Length > 1)
.Select(substrings => substrings[substrings.Length - 1])
.Distinct()
.ToArray();
var images = lines
.Select(match => match.Groups[imageGroup])
.Select(match => match.Value)
// Until now, we are unable to resolve variables within Dockerfiles. Ignore base
// images that utilize variables. Expect them to exist on the host.
.Where(line => !line.Contains('$'))
.Where(line => !line.Any(char.IsUpper))
.Where(value => !stages.Contains(value))
.Distinct()
.Select(value => new DockerImage(value))
.ToArray();
return images;
}
/// <inheritdoc />
public async Task<string> Tar(CancellationToken ct = default)
{
var dockerfileDirectoryPath = Unix.Instance.NormalizePath(_dockerfileDirectory.FullName);
var dockerfileFilePath = Unix.Instance.NormalizePath(_dockerfile.ToString());
var dockerfileArchiveFileName = Regex.Replace(_image.FullName, "[^a-zA-Z0-9]", "-", RegexOptions.None, TimeSpan.FromSeconds(1)).ToLowerInvariant();
var dockerfileArchiveFilePath = Path.Combine(Path.GetTempPath(), $"{dockerfileArchiveFileName}.tar");
var dockerIgnoreFile = new DockerIgnoreFile(dockerfileDirectoryPath, ".dockerignore", dockerfileFilePath, _logger);
using (var tarOutputFileStream = new FileStream(dockerfileArchiveFilePath, FileMode.Create, FileAccess.Write))
{
using (var tarOutputStream = new TarOutputStream(tarOutputFileStream, Encoding.Default))
{
tarOutputStream.IsStreamOwner = false;
foreach (var absoluteFilePath in GetFiles(dockerfileDirectoryPath))
{
// SharpZipLib drops the root path: https://github.com/icsharpcode/SharpZipLib/pull/582.
var relativeFilePath = absoluteFilePath.Substring(dockerfileDirectoryPath.TrimEnd(Path.AltDirectorySeparatorChar).Length + 1);
if (dockerIgnoreFile.Denies(relativeFilePath))
{
continue;
}
try
{
using (var inputStream = new FileStream(absoluteFilePath, FileMode.Open, FileAccess.Read))
{
var entry = TarEntry.CreateTarEntry(relativeFilePath);
entry.TarHeader.Size = inputStream.Length;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
entry.TarHeader.Mode = (int)Unix.FileMode755;
}
await tarOutputStream.PutNextEntryAsync(entry, ct)
.ConfigureAwait(false);
await inputStream.CopyToAsync(tarOutputStream, 81920, ct)
.ConfigureAwait(false);
await tarOutputStream.CloseEntryAsync(ct)
.ConfigureAwait(false);
}
}
catch (IOException e)
{
throw new IOException("Cannot create Docker image tar archive.", e);
}
}
}
}
return dockerfileArchiveFilePath;
}
/// <summary>
/// Gets all accepted Docker archive files.
/// </summary>
/// <param name="directory">Directory to Docker configuration files.</param>
/// <returns>Returns a list with all accepted Docker archive files.</returns>
private static IEnumerable<string> GetFiles(string directory)
{
return Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)
.AsParallel()
.Select(Path.GetFullPath)
.Select(Unix.Instance.NormalizePath)
.ToArray();
}
}
}