Skip to content

Commit ec26765

Browse files
Surayya Huseyn ZadaSurayya Huseyn Zada
authored andcommitted
extract publishing image logic into a seperate class
1 parent 0399310 commit ec26765

File tree

3 files changed

+263
-320
lines changed

3 files changed

+263
-320
lines changed
Lines changed: 253 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,262 @@
1-
// // Licensed to the .NET Foundation under one or more agreements.
2-
// // The .NET Foundation licenses this file to you under the MIT license.
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
// using Microsoft.NET.Build.Containers.LocalDaemons;
5-
// using Microsoft.NET.Build.Containers.Resources;
4+
using Microsoft.Build.Framework;
5+
using Microsoft.NET.Build.Containers.Resources;
66

7-
// namespace Microsoft.NET.Build.Containers;
7+
namespace Microsoft.NET.Build.Containers;
88

9+
internal static class ImagePublisher
10+
{
11+
public static async Task PublishImage(
12+
BuiltImage image,
13+
SourceImageReference sourceImageReference,
14+
DestinationImageReference destinationImageReference,
15+
Microsoft.Build.Utilities.TaskLoggingHelper Log,
16+
IBuildEngine? BuildEngine,
17+
Telemetry telemetry,
18+
CancellationToken cancellationToken)
19+
{
20+
cancellationToken.ThrowIfCancellationRequested();
921

10-
// internal static class ImagePublisher
11-
// {
12-
// public static async Task PublishImage(BuiltImage builtImage, SourceImageReference sourceImageReference,
13-
// DestinationImageReference destinationImageReference,
14-
// Telemetry telemetry,
15-
// CancellationToken cancellationToken)
16-
// {
17-
// cancellationToken.ThrowIfCancellationRequested();
22+
switch (destinationImageReference.Kind)
23+
{
24+
case DestinationImageReferenceKind.LocalRegistry:
25+
await PushToLocalRegistryAsync(
26+
image,
27+
sourceImageReference,
28+
destinationImageReference,
29+
Log,
30+
BuildEngine,
31+
telemetry,
32+
cancellationToken).ConfigureAwait(false);
33+
break;
34+
case DestinationImageReferenceKind.RemoteRegistry:
35+
await PushToRemoteRegistryAsync(
36+
image,
37+
sourceImageReference,
38+
destinationImageReference,
39+
Log,
40+
BuildEngine,
41+
cancellationToken).ConfigureAwait(false);
42+
break;
43+
default:
44+
throw new ArgumentOutOfRangeException();
45+
}
1846

19-
// switch (destinationImageReference.Kind)
20-
// {
21-
// case DestinationImageReferenceKind.LocalRegistry:
22-
// await PushToLocalRegistryAsync(builtImage,
23-
// sourceImageReference,
24-
// destinationImageReference,
25-
// telemetry,
26-
// cancellationToken).ConfigureAwait(false);
27-
// break;
28-
// case DestinationImageReferenceKind.RemoteRegistry:
29-
// await PushToRemoteRegistryAsync(builtImage,
30-
// sourceImageReference,
31-
// destinationImageReference,
32-
// cancellationToken).ConfigureAwait(false);
33-
// break;
34-
// default:
35-
// throw new ArgumentOutOfRangeException();
36-
// }
47+
telemetry.LogPublishSuccess();
48+
}
3749

38-
// telemetry.LogPublishSuccess();
39-
// }
50+
public static async Task PublishImage(
51+
BuiltImage[] images,
52+
SourceImageReference sourceImageReference,
53+
DestinationImageReference destinationImageReference,
54+
Microsoft.Build.Utilities.TaskLoggingHelper Log,
55+
IBuildEngine? BuildEngine,
56+
Telemetry telemetry,
57+
CancellationToken cancellationToken)
58+
{
59+
cancellationToken.ThrowIfCancellationRequested();
4060

41-
// private static async Task PushToLocalRegistryAsync(
42-
// BuiltImage builtImage,
43-
// SourceImageReference sourceImageReference,
44-
// DestinationImageReference destinationImageReference,
45-
// Telemetry telemetry,
46-
// CancellationToken cancellationToken)
47-
// {
48-
// ILocalRegistry localRegistry = destinationImageReference.LocalRegistry!;
49-
// if (!(await localRegistry.IsAvailableAsync(cancellationToken).ConfigureAwait(false)))
50-
// {
51-
// telemetry.LogMissingLocalBinary();
52-
// Log.LogErrorWithCodeFromResources(nameof(Strings.LocalRegistryNotAvailable));
53-
// return;
54-
// }
55-
// try
56-
// {
57-
// await localRegistry.LoadAsync(builtImage, sourceImageReference, destinationImageReference, cancellationToken).ConfigureAwait(false);
58-
// SafeLog(Strings.ContainerBuilder_ImageUploadedToLocalDaemon, destinationImageReference, localRegistry);
61+
switch (destinationImageReference.Kind)
62+
{
63+
case DestinationImageReferenceKind.LocalRegistry:
64+
await PushToLocalRegistryAsync(
65+
images,
66+
sourceImageReference,
67+
destinationImageReference,
68+
Log,
69+
BuildEngine,
70+
telemetry,
71+
cancellationToken).ConfigureAwait(false);
72+
break;
73+
case DestinationImageReferenceKind.RemoteRegistry:
74+
await PushToRemoteRegistryAsync(
75+
images,
76+
sourceImageReference,
77+
destinationImageReference,
78+
Log,
79+
BuildEngine,
80+
cancellationToken).ConfigureAwait(false);
81+
break;
82+
default:
83+
throw new ArgumentOutOfRangeException();
84+
}
5985

60-
// if (localRegistry is ArchiveFileRegistry archive)
61-
// {
62-
// GeneratedArchiveOutputPath = archive.ArchiveOutputPath;
63-
// }
64-
// }
65-
// catch (ContainerHttpException e)
66-
// {
67-
// if (BuildEngine != null)
68-
// {
69-
// Log.LogErrorFromException(e, true);
70-
// }
71-
// }
72-
// catch (AggregateException ex) when (ex.InnerException is DockerLoadException dle)
73-
// {
74-
// telemetry.LogLocalLoadError();
75-
// Log.LogErrorFromException(dle, showStackTrace: false);
76-
// }
77-
// catch (ArgumentException argEx)
78-
// {
79-
// Log.LogErrorFromException(argEx, showStackTrace: false);
80-
// }
81-
// }
86+
telemetry.LogPublishSuccess();
87+
}
8288

83-
// private static async Task PushToRemoteRegistryAsync(
84-
// BuiltImage builtImage,
85-
// SourceImageReference sourceImageReference,
86-
// DestinationImageReference destinationImageReference,
87-
// CancellationToken cancellationToken)
88-
// {
89-
// try
90-
// {
91-
// await destinationImageReference.RemoteRegistry!.PushAsync(
92-
// builtImage,
93-
// sourceImageReference,
94-
// destinationImageReference,
95-
// cancellationToken).ConfigureAwait(false);
96-
// SafeLog(Strings.ContainerBuilder_ImageUploadedToRegistry, destinationImageReference, OutputRegistry);
97-
// }
98-
// catch (UnableToAccessRepositoryException)
99-
// {
100-
// if (BuildEngine != null)
101-
// {
102-
// Log.LogErrorWithCodeFromResources(nameof(Strings.UnableToAccessRepository), destinationImageReference.Repository, destinationImageReference.RemoteRegistry!.RegistryName);
103-
// }
104-
// }
105-
// catch (ContainerHttpException e)
106-
// {
107-
// if (BuildEngine != null)
108-
// {
109-
// Log.LogErrorFromException(e, true);
110-
// }
111-
// }
112-
// catch (Exception e)
113-
// {
114-
// if (BuildEngine != null)
115-
// {
116-
// Log.LogErrorWithCodeFromResources(nameof(Strings.RegistryOutputPushFailed), e.Message);
117-
// Log.LogMessage(MessageImportance.Low, "Details: {0}", e);
118-
// }
119-
// }
120-
// }
121-
// }
89+
private static async Task PushToLocalRegistryAsync(
90+
BuiltImage image,
91+
SourceImageReference sourceImageReference,
92+
DestinationImageReference destinationImageReference,
93+
Microsoft.Build.Utilities.TaskLoggingHelper Log,
94+
IBuildEngine? BuildEngine,
95+
Telemetry telemetry,
96+
CancellationToken cancellationToken)
97+
{
98+
ILocalRegistry localRegistry = destinationImageReference.LocalRegistry!;
99+
if (!(await localRegistry.IsAvailableAsync(cancellationToken).ConfigureAwait(false)))
100+
{
101+
telemetry.LogMissingLocalBinary();
102+
Log.LogErrorWithCodeFromResources(nameof(Strings.LocalRegistryNotAvailable));
103+
return;
104+
}
105+
try
106+
{
107+
await localRegistry.LoadAsync(image, sourceImageReference, destinationImageReference, cancellationToken).ConfigureAwait(false);
108+
if (BuildEngine != null)
109+
{
110+
Log.LogMessage(MessageImportance.High, Strings.ContainerBuilder_ImageUploadedToLocalDaemon, destinationImageReference, localRegistry);
111+
}
112+
}
113+
catch (ContainerHttpException e)
114+
{
115+
if (BuildEngine != null)
116+
{
117+
Log.LogErrorFromException(e, true);
118+
}
119+
}
120+
catch (AggregateException ex) when (ex.InnerException is DockerLoadException dle)
121+
{
122+
telemetry.LogLocalLoadError();
123+
Log.LogErrorFromException(dle, showStackTrace: false);
124+
}
125+
catch (ArgumentException argEx)
126+
{
127+
Log.LogErrorFromException(argEx, showStackTrace: false);
128+
}
129+
}
130+
131+
private static async Task PushToLocalRegistryAsync(
132+
BuiltImage[] images,
133+
SourceImageReference sourceImageReference,
134+
DestinationImageReference destinationImageReference,
135+
Microsoft.Build.Utilities.TaskLoggingHelper Log,
136+
IBuildEngine? BuildEngine,
137+
Telemetry telemetry,
138+
CancellationToken cancellationToken)
139+
{
140+
ILocalRegistry localRegistry = destinationImageReference.LocalRegistry!;
141+
if (!(await localRegistry.IsAvailableAsync(cancellationToken).ConfigureAwait(false)))
142+
{
143+
telemetry.LogMissingLocalBinary();
144+
Log.LogErrorWithCodeFromResources(nameof(Strings.LocalRegistryNotAvailable));
145+
return;
146+
}
147+
try
148+
{
149+
await localRegistry.LoadAsync(images, sourceImageReference, destinationImageReference, cancellationToken).ConfigureAwait(false);
150+
if (BuildEngine != null)
151+
{
152+
Log.LogMessage(MessageImportance.High, Strings.ContainerBuilder_ImageUploadedToLocalDaemon, destinationImageReference, localRegistry);
153+
}
154+
}
155+
catch (ContainerHttpException e)
156+
{
157+
if (BuildEngine != null)
158+
{
159+
Log.LogErrorFromException(e, true);
160+
}
161+
}
162+
catch (AggregateException ex) when (ex.InnerException is DockerLoadException dle)
163+
{
164+
telemetry.LogLocalLoadError();
165+
Log.LogErrorFromException(dle, showStackTrace: false);
166+
}
167+
catch (ArgumentException argEx)
168+
{
169+
Log.LogErrorFromException(argEx, showStackTrace: false);
170+
}
171+
}
172+
173+
private static async Task PushToRemoteRegistryAsync(
174+
BuiltImage image,
175+
SourceImageReference sourceImageReference,
176+
DestinationImageReference destinationImageReference,
177+
Microsoft.Build.Utilities.TaskLoggingHelper Log,
178+
IBuildEngine? BuildEngine,
179+
CancellationToken cancellationToken)
180+
{
181+
try
182+
{
183+
await destinationImageReference.RemoteRegistry!.PushAsync(
184+
image,
185+
sourceImageReference,
186+
destinationImageReference,
187+
cancellationToken).ConfigureAwait(false);
188+
if (BuildEngine != null)
189+
{
190+
Log.LogMessage(MessageImportance.High, Strings.ContainerBuilder_ImageUploadedToRegistry, destinationImageReference, destinationImageReference.RemoteRegistry!.RegistryName);
191+
}
192+
}
193+
catch (UnableToAccessRepositoryException)
194+
{
195+
if (BuildEngine != null)
196+
{
197+
Log.LogErrorWithCodeFromResources(nameof(Strings.UnableToAccessRepository), destinationImageReference.Repository, destinationImageReference.RemoteRegistry!.RegistryName);
198+
}
199+
}
200+
catch (ContainerHttpException e)
201+
{
202+
if (BuildEngine != null)
203+
{
204+
Log.LogErrorFromException(e, true);
205+
}
206+
}
207+
catch (Exception e)
208+
{
209+
if (BuildEngine != null)
210+
{
211+
Log.LogErrorWithCodeFromResources(nameof(Strings.RegistryOutputPushFailed), e.Message);
212+
Log.LogMessage(MessageImportance.Low, "Details: {0}", e);
213+
}
214+
}
215+
}
216+
217+
private static async Task PushToRemoteRegistryAsync(
218+
BuiltImage[] images,
219+
SourceImageReference sourceImageReference,
220+
DestinationImageReference destinationImageReference,
221+
Microsoft.Build.Utilities.TaskLoggingHelper Log,
222+
IBuildEngine? BuildEngine,
223+
CancellationToken cancellationToken)
224+
{
225+
try
226+
{
227+
(string imageIndex, string mediaType) = ImageIndexGenerator.GenerateImageIndex(images);
228+
await destinationImageReference.RemoteRegistry!.PushManifestListAsync(
229+
destinationImageReference.Repository,
230+
destinationImageReference.Tags,
231+
imageIndex,
232+
mediaType,
233+
cancellationToken).ConfigureAwait(false);
234+
if (BuildEngine != null)
235+
{
236+
Log.LogMessage(MessageImportance.High, Strings.ImageIndexUploadedToRegistry, destinationImageReference, destinationImageReference.RemoteRegistry!.RegistryName);
237+
}
238+
}
239+
catch (UnableToAccessRepositoryException)
240+
{
241+
if (BuildEngine != null)
242+
{
243+
Log.LogErrorWithCodeFromResources(nameof(Strings.UnableToAccessRepository), destinationImageReference.Repository, destinationImageReference.RemoteRegistry!.RegistryName);
244+
}
245+
}
246+
catch (ContainerHttpException e)
247+
{
248+
if (BuildEngine != null)
249+
{
250+
Log.LogErrorFromException(e, true);
251+
}
252+
}
253+
catch (Exception e)
254+
{
255+
if (BuildEngine != null)
256+
{
257+
Log.LogErrorWithCodeFromResources(nameof(Strings.RegistryOutputPushFailed), e.Message);
258+
Log.LogMessage(MessageImportance.Low, "Details: {0}", e);
259+
}
260+
}
261+
}
262+
}

0 commit comments

Comments
 (0)