-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
659 lines (569 loc) · 23.9 KB
/
Program.cs
File metadata and controls
659 lines (569 loc) · 23.9 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
using System.Collections.Immutable;
using System.Reflection;
using Captin.ConsoleIntercept;
using CommandLine;
using ContainerUpdater;
using ContainerUpdater.Helpers;
using ContainerUpdater.Models;
using Docker.DotNet;
using Docker.DotNet.BasicAuth;
using Docker.DotNet.Models;
const string DockerRegistry = "index.docker.io";
const string DockerComposeProjectOnKey = "com.docker.compose.project";
const string DockerComposeDependsOnKey = "com.docker.compose.depends_on";
const string WatchtowerMonitorOnlyKey = "com.centurylinklabs.watchtower.monitor-only";
const string WatchtowerEnableKey = "com.centurylinklabs.watchtower.enable";
const string WatchtowerDependsOnKey = "com.centurylinklabs.watchtower.depends-on";
const string WatchtowerNoPullOnKey = "com.centurylinklabs.watchtower.no-pull";
// Steps to auto update base images and containers that use them:
// 1. Get all the current image digests and tags to perform a manifest lookup.
// 2. Lookup latest manifest and check if it matches the current image digest.
// 3. If not the latest, get the containers that are using the old/existing image and stop them.
// 4. Inspect and retain the information to re-install containers.
// 5. Remove the containers using the old image.
// 6. Remove the old image.
// 7. Pull the new image.
// 8. Re-create the containers from previous inspect data.
// 9. Start the containers if they were previously running.
var headerPadding = "".PadRight(10);
var headerApplication = $"Container Updater {Assembly.GetExecutingAssembly().GetName().Version}";
var dashLength = headerApplication.Length + (headerPadding.Length * 2) + 2;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
Console.WriteLine("".PadRight(dashLength, '-'));
Console.WriteLine($"|{headerPadding}{headerApplication}{headerPadding}|");
Console.WriteLine("".PadRight(dashLength, '-'));
Console.WriteLine();
Console.ResetColor();
await Parser.Default.ParseArguments<Options>(args).WithParsedAsync(ExecuteAsync);
static async Task ExecuteAsync(Options options)
{
Credentials dockerCredentials = (!string.IsNullOrEmpty(options.Username) || !string.IsNullOrEmpty(options.Password)) ?
new BasicAuthCredentials(options.Username, options.Password) :
new AnonymousCredentials();
using var dockerConfiguration = (options.Host is not null) ?
new DockerClientConfiguration(options.Host, dockerCredentials) :
new DockerClientConfiguration(dockerCredentials);
using var dockerClient = dockerConfiguration.CreateClient();
using var consoleOut = ConsoleOut.Observe();
var connected = false;
var exitCode = 0;
try
{
// Check if Docker is running.
await dockerClient.System.PingAsync();
connected = true;
List<CheckImage> imagesToCheck = [];
List<UpdateImage> imagesToUpdate = [];
var version = await dockerClient.System.GetVersionAsync();
Console.WriteLine("Docker Client Information");
Console.WriteLine("-------------------------");
Console.WriteLine($"Host : {dockerConfiguration.EndpointBaseUri}");
Console.WriteLine($"OS : {version.Os} ({version.Arch})");
Console.WriteLine($"Version : {version.Version}");
Console.WriteLine($"API : {version.APIVersion}");
Console.WriteLine($"Kernel : {version.KernelVersion}");
Console.WriteLine();
if (options.Exclude.Any())
{
Console.WriteLine($"EXCLUDE: {string.Join(" ", options.Exclude)}");
}
if (options.Include.Any())
{
Console.WriteLine($"INCLUDE: {string.Join(" ", options.Include)}");
}
if (options.DryRun)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("DRY RUN: No changes will be made to your images or containers");
Console.WriteLine();
Console.ResetColor();
}
// Get all images, parse them and create registry clients.
foreach (var image in await dockerClient.Images.ListImagesAsync(new() { All = true }))
{
var repoDigest = image.RepoDigests.FirstOrDefault();
var repoTag = image.RepoTags.FirstOrDefault();
if (repoDigest is null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Image {image.ID} does not have a repository digest and will not be updated");
Console.ResetColor();
continue;
}
if (repoTag is null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Image {image.ID} does not have a repository tag and will not be updated");
Console.ResetColor();
continue;
}
// Parse the various properties to get the registry, repository, tag and digest.
var digestParts = repoDigest.Split('@');
var tagParts = repoTag.Split(':');
var imageName = digestParts[0];
var digest = digestParts.Length > 1 ? digestParts[1] : digestParts[0];
var tag = tagParts[1];
// Some images do not have the name prefixed with the digest, we can fallback to the repository name.
if (imageName == digest)
{
imageName = tagParts[0];
}
var repositoryParts = imageName.Split('/');
if (repositoryParts.Length < 2)
{
repositoryParts = [DockerRegistry, "library", .. repositoryParts];
}
if (repositoryParts.Length < 3)
{
repositoryParts = [DockerRegistry, .. repositoryParts];
}
var registry = repositoryParts[0];
var repository = string.Join('/', repositoryParts.Skip(1));
// Special handling for Docker.io
if (registry.Equals("docker.io", StringComparison.OrdinalIgnoreCase))
{
registry = DockerRegistry;
}
imagesToCheck.Add(new(image.ID, imageName, repoTag, registry, repository, tag, digest));
}
Console.WriteLine($"Checking {imagesToCheck.Count} images across {imagesToCheck.DistinctBy(img => img.Registry).Count()} registries...");
Console.WriteLine();
// Get all containers to check their labels for Watchtower label-enable mode
var allContainers = await dockerClient.Containers.ListContainersAsync(new() { All = true });
var containerLabelMap = new Dictionary<string, IDictionary<string, string>>();
foreach (var container in allContainers)
{
containerLabelMap[container.ImageID] = container.Labels ?? ImmutableDictionary<string, string>.Empty;
}
// Check if we're in label-enable mode (any container has the enable label)
var isWatchtowerLabelEnable = containerLabelMap.Values
.Any(labels => labels.ContainsKey(WatchtowerEnableKey) &&
labels[WatchtowerEnableKey]?.Equals("true", StringComparison.OrdinalIgnoreCase) == true);
// Check each image for updates.
foreach (var image in imagesToCheck)
{
Console.Write($"Checking image {image.OriginalTag}...");
// Check if the image has been excluded by filter options
if (options.Exclude.Contains(image.Repository, StringComparer.OrdinalIgnoreCase) ||
image.Repository.Split('/').Any(x => options.Exclude.Contains(x, StringComparer.OrdinalIgnoreCase)))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("EXCLUDED (FILTER OPTION)");
Console.ResetColor();
continue;
}
// Check if the image has been included by filter options
if (options.Include.Any() &&
!options.Include.Contains(image.Repository, StringComparer.OrdinalIgnoreCase) &&
!image.Repository.Split('/').Any(x => options.Include.Contains(x, StringComparer.OrdinalIgnoreCase)))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("EXCLUDED (FILTER OPTION)");
Console.ResetColor();
continue;
}
// Get container labels for this image (if any containers use this image)
var containerLabels = containerLabelMap.TryGetValue(image.Id, out var containerLabelsValue) ? containerLabelsValue : ImmutableDictionary<string, string>.Empty;
// Watchtower label-based exclusion logic
var enableValue = containerLabels.TryGetValue(WatchtowerEnableKey, out var watchtowerEnableKeyValue) ? watchtowerEnableKeyValue : null;
// If in label-enable mode, only process containers with enable=true
if (isWatchtowerLabelEnable)
{
if (string.IsNullOrEmpty(enableValue) || !enableValue.Equals("true", StringComparison.OrdinalIgnoreCase))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("EXCLUDED (WATCHTOWER LABEL)");
Console.ResetColor();
continue;
}
}
// If not in label-enable mode, exclude containers with enable=false
else if (!string.IsNullOrEmpty(enableValue) && enableValue.Equals("false", StringComparison.OrdinalIgnoreCase))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("EXCLUDED (WATCHTOWER LABEL)");
Console.ResetColor();
continue;
}
try
{
// Check for monitor-only label on containers
var isMonitorOnly = containerLabels.TryGetValue(WatchtowerMonitorOnlyKey, out var watchtowerMonitorOnlyKeyValue) &&
watchtowerMonitorOnlyKeyValue.Equals("true", StringComparison.OrdinalIgnoreCase);
// Check for no-pull label on containers
var isNoPull = containerLabels.TryGetValue(WatchtowerNoPullOnKey, out var watchtowerNoPullOnKeyValue) &&
watchtowerNoPullOnKeyValue.Equals("true", StringComparison.OrdinalIgnoreCase);
var remoteDigests = await RegistryHelper.GetDigestsAsync(image.Registry, image.Repository, image.Tag);
if (!isMonitorOnly && !isNoPull && !remoteDigests.Any(digest => digest == image.LocalDigest))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("UPDATE AVAILABLE (DIGEST)");
Console.ResetColor();
imagesToUpdate.Add(new(image.Id, image.OriginalName, image.OriginalTag, image.Tag, image.LocalDigest, remoteDigests.First()));
}
else if (isNoPull && !remoteDigests.Any(digest => digest == image.LocalDigest))
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("EXCLUDED (NO-PULL LABEL)");
Console.ResetColor();
}
else
{
var latestVersion = image.Tag;
if (latestVersion.Contains('.'))
{
var tags = await RegistryHelper.GetTagsAsync(image.Registry, image.Repository);
latestVersion = VersionHelper.FindLatestMatchingVersion(image.Tag, tags);
}
if (latestVersion != image.Tag)
{
if (options.DigestOnly || isMonitorOnly || isNoPull)
{
Console.ForegroundColor = ConsoleColor.Yellow;
if (isNoPull)
{
Console.WriteLine($"EXCLUDED (NO-PULL LABEL, VERSION {latestVersion})");
}
else
{
Console.WriteLine($"EXCLUDED (VERSION {latestVersion})");
}
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"UPDATE AVAILABLE (VERSION {latestVersion})");
Console.ResetColor();
imagesToUpdate.Add(new(image.Id, image.OriginalName, image.OriginalTag, latestVersion, image.LocalDigest, string.Empty));
}
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("NO UPDATE");
Console.ResetColor();
}
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("UPDATE CHECK FAILED");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine(ex.ToString());
Console.WriteLine();
}
}
// Update images.
if (imagesToUpdate.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine($"Found {imagesToUpdate.Count} image updates.");
Console.WriteLine();
Console.ResetColor();
var pullProgress = new Progress<JSONMessage>(_ => Console.Write("."));
var dockerContainers = await dockerClient.Containers.ListContainersAsync(new() { All = true });
// Group containers by image and build dependency information
var containerUpdateGroups = new List<ContainerUpdateGroup>();
foreach (var image in imagesToUpdate)
{
if (options.Interactive)
{
Console.WriteLine();
Console.Write($"Update {image.OriginalTag}? [Y/N]: ");
var choice = Console.ReadKey(true);
while (choice.Key != ConsoleKey.Y && choice.Key != ConsoleKey.N)
{
choice = Console.ReadKey(true);
}
if (choice.Key == ConsoleKey.Y)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Yes");
Console.ResetColor();
}
else if (choice.Key == ConsoleKey.N)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("No");
Console.ResetColor();
continue;
}
}
// Find all containers using the image and build dependency information
var containersForImage = new List<ContainerInfo>();
foreach (var container in dockerContainers.Where(c => c.ImageID == image.Id))
{
var inspect = await dockerClient.Containers.InspectContainerAsync(container.ID);
var labels = container.Labels ?? ImmutableDictionary<string, string>.Empty;
var project = labels.TryGetValue(DockerComposeProjectOnKey, out var dockerComposeProjectOnKeyValue) ? dockerComposeProjectOnKeyValue : string.Empty;
var dependencies = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// Parse Docker Compose dependencies
if (labels.TryGetValue(DockerComposeDependsOnKey, out var dockerComposeDependsOnKeyValue))
{
foreach (var dependencyName in dockerComposeDependsOnKeyValue
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(d => d.Trim().Split(':')[0]) // Remove condition if present (service:condition)
.Where(d => !string.IsNullOrEmpty(d)))
{
// For Docker Compose, dependencies might be prefixed with project name
dependencies.Add(string.IsNullOrEmpty(project) ? dependencyName : $"{project}_{dependencyName}");
dependencies.Add(dependencyName); // Also add without prefix for flexibility
}
}
// Parse Watchtower dependencies
if (labels.TryGetValue(WatchtowerDependsOnKey, out var watchtowerDependsOnKeyValue))
{
foreach (var dependencyName in watchtowerDependsOnKeyValue
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(d => d.Trim())
.Where(d => !string.IsNullOrEmpty(d)))
{
dependencies.Add(dependencyName);
}
}
var containerConfig = new CreateContainerParameters
{
Image = $"{image.OriginalName}:{image.Tag}",
HostConfig = inspect.HostConfig,
Name = inspect.Name.TrimStart('/'),
Env = inspect.Config.Env,
Cmd = inspect.Config.Cmd,
Entrypoint = inspect.Config.Entrypoint,
WorkingDir = inspect.Config.WorkingDir,
ExposedPorts = inspect.Config.ExposedPorts,
Labels = inspect.Config.Labels,
Volumes = inspect.Config.Volumes,
Healthcheck = inspect.Config.Healthcheck,
OnBuild = inspect.Config.OnBuild,
StopSignal = inspect.Config.StopSignal,
StopTimeout = inspect.Config.StopTimeout,
Tty = inspect.Config.Tty,
Shell = inspect.Config.Shell,
User = inspect.Config.User,
Hostname = inspect.Config.Hostname,
Domainname = inspect.Config.Domainname,
AttachStdin = inspect.Config.AttachStdin,
AttachStdout = inspect.Config.AttachStdout,
AttachStderr = inspect.Config.AttachStderr,
StdinOnce = inspect.Config.StdinOnce,
OpenStdin = inspect.Config.OpenStdin,
ArgsEscaped = inspect.Config.ArgsEscaped,
NetworkDisabled = inspect.Config.NetworkDisabled,
MacAddress = inspect.NetworkSettings.MacAddress,
NetworkingConfig = new NetworkingConfig
{
EndpointsConfig = inspect.NetworkSettings.Networks,
},
};
containersForImage.Add(new ContainerInfo(
container.ID,
inspect.Name.TrimStart('/'),
inspect.State.Running,
containerConfig,
dependencies,
project));
}
// Always add the image to the update groups, even if it has no containers
containerUpdateGroups.Add(new ContainerUpdateGroup(
image.Id,
image.OriginalName,
image.Tag,
containersForImage));
}
// Process each update group
foreach (var updateGroup in containerUpdateGroups)
{
if (updateGroup.Containers.Count > 0)
{
Console.WriteLine($"Processing containers for image {updateGroup.ImageName}");
// Sort containers by dependencies for proper stop order (dependencies first)
foreach (var container in SortContainersByDependencies(updateGroup.Containers))
{
// Stop and remove containers in dependency order
try
{
if (container.IsRunning)
{
Console.WriteLine($"Stopping container {container.Name} ({container.Id})...");
if (!options.DryRun && !await dockerClient.Containers.StopContainerAsync(container.Id, new() { WaitBeforeKillSeconds = 15 }))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed to stop container, killing it instead...");
Console.ResetColor();
await dockerClient.Containers.KillContainerAsync(container.Id, new());
}
}
if (!options.DryRun)
{
await dockerClient.Containers.RemoveContainerAsync(container.Id, new() { Force = true, RemoveLinks = false, RemoveVolumes = false });
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("CONTAINER REMOVAL FAILED");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine(ex.ToString());
Console.WriteLine();
}
}
}
else
{
Console.WriteLine($"Processing standalone image {updateGroup.ImageName} (no containers)");
}
// Remove old image and pull new one
try
{
var imageToUpdate = imagesToUpdate.First(img => img.Id == updateGroup.ImageId);
Console.WriteLine($"Removing old image for {imageToUpdate.OriginalTag}");
Console.WriteLine(imageToUpdate.LocalDigest);
if (!options.DryRun)
{
await dockerClient.Images.DeleteImageAsync(updateGroup.ImageId, new() { Force = true, NoPrune = true });
}
Console.WriteLine($"Pulling new image for {updateGroup.ImageName}:{updateGroup.NewTag}");
if (!string.IsNullOrEmpty(imageToUpdate.NewDigest))
{
Console.WriteLine(imageToUpdate.NewDigest);
}
if (!options.DryRun)
{
await dockerClient.Images.CreateImageAsync(new() { FromImage = updateGroup.ImageName, Tag = updateGroup.NewTag }, null, pullProgress);
Console.WriteLine();
Console.WriteLine();
}
else
{
Console.WriteLine("".PadRight(50, '.'));
Console.WriteLine();
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("REMOVING/PULLING NEW FAILED");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine(ex.ToString());
Console.WriteLine();
continue; // Skip container recreation if image update failed
}
// Recreate and start containers in reverse dependency order (dependents first)
if (updateGroup.Containers.Count > 0)
{
foreach (var container in SortContainersByDependencies(updateGroup.Containers).AsEnumerable().Reverse())
{
try
{
Console.WriteLine($"Restoring container {container.Name}...");
if (!options.DryRun)
{
// Update the image reference in the container config
container.CreateParameters.Image = $"{updateGroup.ImageName}:{updateGroup.NewTag}";
var newContainer = await dockerClient.Containers.CreateContainerAsync(container.CreateParameters);
if (container.IsRunning)
{
Console.WriteLine($"Starting container {container.Name} ({newContainer.ID})...");
await dockerClient.Containers.StartContainerAsync(newContainer.ID, new());
// Small delay to allow container to start before starting dependents
await Task.Delay(1000);
}
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("CONTAINER CREATION FAILED");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine(ex.ToString());
Console.WriteLine();
}
}
}
Console.WriteLine($"Completed update for {updateGroup.ImageName}");
Console.WriteLine();
}
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine("Nothing to update :)");
Console.WriteLine();
Console.ResetColor();
}
}
catch (Exception ex)
{
if (connected)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine(ex.ToString());
Console.ResetColor();
exitCode = 1;
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Docker engine is not running :(");
Console.ResetColor();
exitCode = 2;
}
}
finally
{
await File.AppendAllTextAsync("ContainerUpdater.log", $"--- {DateTime.Now:yyyy/MMdd HH:mm:ss} ---{Environment.NewLine}{consoleOut}{Environment.NewLine}");
}
if (options.Interactive)
{
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
Environment.Exit(exitCode);
}
static List<ContainerInfo> SortContainersByDependencies(List<ContainerInfo> containers)
{
var sorted = new List<ContainerInfo>();
var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var visiting = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var containerMap = containers.ToDictionary(c => c.Name, StringComparer.OrdinalIgnoreCase);
void Visit(ContainerInfo container)
{
if (visiting.Contains(container.Name))
{
Console.WriteLine($"Warning: Circular dependency detected involving container '{container.Name}'");
return;
}
if (visited.Contains(container.Name))
{
return;
}
visiting.Add(container.Name);
// Visit dependencies first (for stopping order)
foreach (var dependency in container.Dependencies)
{
if (containerMap.TryGetValue(dependency, out var depContainer))
{
Visit(depContainer);
}
}
visiting.Remove(container.Name);
visited.Add(container.Name);
sorted.Add(container);
}
foreach (var container in containers)
{
Visit(container);
}
return sorted;
}