-
Notifications
You must be signed in to change notification settings - Fork 722
feat(cli): aspire exec against container #10380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
01c967d
playground
DeagleGross 4d43095
some setup
DeagleGross baeda26
Merge branch 'main' into dmkorolev/exec-container
DeagleGross 23def1f
some setup
DeagleGross 4ac9347
setup?
DeagleGross a367239
meh so many changes
DeagleGross 1cf6ff9
accomodate all more changes
DeagleGross 4dead93
Merge branch 'main' into dmkorolev/exec-container
DeagleGross b1fb598
drop dependencies
DeagleGross 84e0088
merge test refactor
DeagleGross fbe50cd
container test
DeagleGross 644a01a
remove unneeded containers proj
DeagleGross ac40f55
re-review
DeagleGross 17a9acf
merge main
DeagleGross 8d8a37f
address comments 1
DeagleGross 0070ac4
rework eventing & naming
DeagleGross c846fd8
eventing & naming
DeagleGross 21b6695
rename & provide dcp name
DeagleGross c14f9e0
Merge branch 'main' into dmkorolev/exec-container
DeagleGross 7ead6bb
Merge branch 'main' into dmkorolev/exec-container
DeagleGross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Aspire.Hosting/ApplicationModel/ContainerExecutableResource.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace Aspire.Hosting.ApplicationModel; | ||
|
|
||
| internal class ContainerExecutableResource(string name, ContainerResource containerResource, string command, string? workingDirectory) | ||
| : Resource(name), IResourceWithEnvironment, IResourceWithArgs, IResourceWithEndpoints, IResourceWithWaitSupport | ||
| { | ||
| /// <summary> | ||
| /// Gets the command associated with this executable resource. | ||
| /// </summary> | ||
| public string Command { get; } = ThrowIfNullOrEmpty(command); | ||
|
|
||
| /// <summary> | ||
| /// Gets the working directory for the executable resource. | ||
| /// </summary> | ||
| public string? WorkingDirectory { get; } = workingDirectory; | ||
|
|
||
| public ICollection<string>? Args { get; init; } | ||
|
|
||
| public ContainerResource? TargetContainerResource { get; } = containerResource ?? throw new ArgumentNullException(nameof(containerResource)); | ||
|
|
||
| private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) | ||
| { | ||
| ArgumentException.ThrowIfNullOrEmpty(argument, paramName); | ||
| return argument; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Aspire.Hosting/ContainerExecutableResourceExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.ApplicationModel; | ||
|
|
||
| namespace Aspire.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Provides extension methods for working with <see cref="ExecutableResource"/> objects. | ||
| /// </summary> | ||
| internal static class ContainerExecutableResourceExtensions | ||
| { | ||
| /// <summary> | ||
| /// Returns an enumerable collection of executable resources from the specified distributed application model. | ||
| /// </summary> | ||
| /// <param name="model">The distributed application model to retrieve executable resources from.</param> | ||
| /// <returns>An enumerable collection of executable resources.</returns> | ||
| public static IEnumerable<ContainerExecutableResource> GetContainerExecutableResources(this DistributedApplicationModel model) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(model); | ||
|
|
||
| return model.Resources.OfType<ContainerExecutableResource>(); | ||
| } | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting; | ||
| using Aspire.Hosting.Backchannel; | ||
| using Aspire.Hosting.Dcp; | ||
| using Aspire.Hosting.Testing; | ||
| using Aspire.Hosting.Utils; | ||
| using Aspire.TestUtilities; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Projects; | ||
| using Xunit; | ||
|
|
||
| namespace Aspire.Cli.Tests.E2E; | ||
|
|
||
| public class ExecContainerResourceTests(ITestOutputHelper output) | ||
| { | ||
| private static string ContainersAppHostProjectPath => | ||
| Path.Combine(Containers_AppHost.ProjectPath, "Containers.AppHost.csproj"); | ||
|
|
||
| [Fact] | ||
| [RequiresDocker] | ||
| public async Task Exec_ListFilesInDirectory_ShouldProduceLogs() | ||
| { | ||
| Environment.SetEnvironmentVariable("DCP_DIAGNOSTICS_LOG_LEVEL", "debug"); | ||
| Environment.SetEnvironmentVariable("DCP_DIAGNOSTICS_LOG_FOLDER", @"D:\.other\dcp-logs"); | ||
|
|
||
| string[] args = [ | ||
| "--operation", "run", | ||
| "--project", ContainersAppHostProjectPath, | ||
| "--resource", "nginx", | ||
| "--command", "\"ls\"" | ||
| ]; | ||
|
|
||
| var app = await BuildAppAsync(args); | ||
| var logs = await ExecAndCollectLogsAsync(app, timeoutSec: /* TODO remove after debugging */ 6000); | ||
|
|
||
| Assert.True(logs.Count > 0, "No logs were produced during the exec operation."); | ||
| } | ||
|
|
||
| private async Task<List<CommandOutput>> ExecAndCollectLogsAsync(DistributedApplication app, int timeoutSec = 30) | ||
| { | ||
| var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSec)); | ||
|
|
||
| var appHostRpcTarget = app.Services.GetRequiredService<AppHostRpcTarget>(); | ||
| var outputStream = appHostRpcTarget.ExecAsync(cts.Token); | ||
|
|
||
| var logs = new List<CommandOutput>(); | ||
| var startTask = app.StartAsync(cts.Token); | ||
| await foreach (var message in outputStream) | ||
| { | ||
| var logLevel = message.IsErrorMessage ? LogLevel.Error : LogLevel.Information; | ||
| var log = $"Received output: #{message.LineNumber} [level={logLevel}] [type={message.Type}] {message.Text}"; | ||
|
|
||
| logs.Add(message); | ||
| output.WriteLine(log); | ||
| } | ||
|
|
||
| await startTask; | ||
| return logs; | ||
| } | ||
|
|
||
| private async Task<DistributedApplication> BuildAppAsync(string[] args, Action<DistributedApplicationOptions, HostApplicationBuilderSettings>? configureBuilder = null) | ||
| { | ||
| configureBuilder ??= (appOptions, _) => { }; | ||
| var builder = DistributedApplicationTestingBuilder.Create(args, configureBuilder, typeof(DatabaseMigration_AppHost).Assembly) | ||
| .WithTestAndResourceLogging(output); | ||
|
|
||
| builder.Services.Configure<DcpOptions>(options => | ||
| { | ||
| options.ContainerRuntime = "docker"; // This should use PATH lookup instead of full path | ||
| }); | ||
|
|
||
| var apiService = builder.AddProject<Containers_ApiService>("apiservice"); | ||
| var nginx = builder.AddContainer("nginx", "nginx", "1.25"); | ||
| var executable = builder.AddExecutable("cmd", "dotnet build", "C:\\code"); | ||
|
|
||
| return await builder.BuildAsync(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DeagleGross this merged in before I got a chance to fully review it - but one thing we should do here is ditch the
TargetContainerResourceproperty and make this resource implement theIResourceWithParentinterface.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also get rid of the
Argsproperty and instead make it work similar to theExecutableResource. Because you are implementingIResourceWithArgsit means that this resource should be able to work with theWithArgsmethod - and it should honor all of the semantics around deferred evaluation etc.