-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds uv example * unshipped api definition for uv * working uv integration * adds doc * adds test * removes comment for pythonexecutable * installs uv on devcontainer * added uv setup in cicd * fixes project.toml --------- Co-authored-by: Alireza Baloochi <[email protected]>
- Loading branch information
1 parent
254101c
commit 66046c2
Showing
16 changed files
with
206 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
4 changes: 4 additions & 0 deletions
4
examples/python/CommunityToolkit.Aspire.Hosting.Python.Extensions.AppHost/Program.cs
This file contains 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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
#pragma warning disable CS0612 | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var uvicorn = builder.AddUvicornApp("uvicornapp", "../uvicornapp-api", "main:app") | ||
.WithHttpEndpoint(env: "UVICORN_PORT"); | ||
|
||
var uv = builder.AddUvApp("uvapp", "../uv-api", "uv-api") | ||
.WithHttpEndpoint(env: "PORT"); | ||
|
||
builder.Build().Run(); |
This file contains 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,3 @@ | ||
.venv | ||
uv.lock | ||
**/__pycache__/ |
This file contains 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 @@ | ||
3.12 |
This file contains 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,19 @@ | ||
[project] | ||
name = "uv-api" | ||
version = "0.1.0" | ||
description = "Test project for uv-api" | ||
authors = [ | ||
{ name = "Tommaso Stocchi", email = "[email protected]" } | ||
] | ||
requires-python = ">=3.12" | ||
dependencies = [ | ||
"fastapi>=0.115.6", | ||
"uvicorn>=0.32.1", | ||
] | ||
|
||
[project.scripts] | ||
uv-api = "uv_api:main" | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" |
This file contains 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,13 @@ | ||
from fastapi import FastAPI | ||
import uvicorn | ||
import os | ||
|
||
app = FastAPI() | ||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"message": "Hello, World!"} | ||
|
||
def main() -> None: | ||
port = int(os.environ.get("PORT", 8000)) | ||
uvicorn.run(app, host="127.0.0.1", port=port) |
5 changes: 4 additions & 1 deletion
5
src/CommunityToolkit.Aspire.Hosting.Python.Extensions/PublicAPI.Unshipped.txt
This file contains 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
#nullable enable | ||
|
||
Aspire.Hosting.ApplicationModel.UvAppResource | ||
Aspire.Hosting.ApplicationModel.UvAppResource.UvAppResource(string! name, string! workingDirectory) -> void | ||
Aspire.Hosting.UvAppHostingExtension | ||
static Aspire.Hosting.UvAppHostingExtension.AddUvApp(this Aspire.Hosting.IDistributedApplicationBuilder! builder, string! name, string! projectDirectory, string! scriptPath, params string![]! scriptArgs) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.ApplicationModel.UvAppResource!>! |
This file contains 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
99 changes: 99 additions & 0 deletions
99
src/CommunityToolkit.Aspire.Hosting.Python.Extensions/UvAppHostingExtension.cs
This file contains 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,99 @@ | ||
using Aspire.Hosting.ApplicationModel; | ||
using CommunityToolkit.Aspire.Utils; | ||
|
||
namespace Aspire.Hosting; | ||
|
||
/// <summary> | ||
/// Provides extension methods for adding Uv applications to an <see cref="IDistributedApplicationBuilder"/>. | ||
/// </summary> | ||
public static class UvAppHostingExtension | ||
{ | ||
/// <summary> | ||
/// Adds a Uv application to the distributed application builder. | ||
/// </summary> | ||
/// <param name="builder">The distributed application builder.</param> | ||
/// <param name="name">The name of the Uv application.</param> | ||
/// <param name="projectDirectory">The directory of the project containing the Uv application.</param> | ||
/// <param name="scriptPath">The name of the Uv app.</param> | ||
/// <param name="scriptArgs">Optional arguments to pass to the script.</param> | ||
/// <returns>An <see cref="IResourceBuilder{UvAppResource}"/> for the Uv application resource.</returns> | ||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="builder"/> is null.</exception> | ||
public static IResourceBuilder<UvAppResource> AddUvApp( | ||
this IDistributedApplicationBuilder builder, | ||
string name, | ||
string projectDirectory, | ||
string scriptPath, | ||
params string[] scriptArgs) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
|
||
return builder.AddUvApp(name, scriptPath, projectDirectory, ".venv", scriptArgs); | ||
} | ||
|
||
private static IResourceBuilder<UvAppResource> AddUvApp(this IDistributedApplicationBuilder builder, | ||
string name, | ||
string scriptPath, | ||
string projectDirectory, | ||
string virtualEnvironmentPath, | ||
params string[] args) | ||
{ | ||
ArgumentNullException.ThrowIfNull(builder); | ||
ArgumentNullException.ThrowIfNull(name); | ||
ArgumentNullException.ThrowIfNull(scriptPath); | ||
|
||
string wd = projectDirectory ?? Path.Combine("..", name); | ||
|
||
projectDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, wd)); | ||
|
||
var virtualEnvironment = new VirtualEnvironment(Path.IsPathRooted(virtualEnvironmentPath) | ||
? virtualEnvironmentPath | ||
: Path.Join(projectDirectory, virtualEnvironmentPath)); | ||
|
||
var instrumentationExecutable = virtualEnvironment.GetExecutable("opentelemetry-instrument"); | ||
// var pythonExecutable = virtualEnvironment.GetRequiredExecutable("python"); | ||
// var projectExecutable = instrumentationExecutable ?? pythonExecutable; | ||
|
||
string[] allArgs = args is { Length: > 0 } | ||
? ["run", scriptPath, .. args] | ||
: ["run", scriptPath]; | ||
|
||
var projectResource = new UvAppResource(name, projectDirectory); | ||
|
||
var resourceBuilder = builder.AddResource(projectResource) | ||
.WithArgs(allArgs) | ||
.WithArgs(context => | ||
{ | ||
// If the project is to be automatically instrumented, add the instrumentation executable arguments first. | ||
if (!string.IsNullOrEmpty(instrumentationExecutable)) | ||
{ | ||
AddOpenTelemetryArguments(context); | ||
|
||
// // Add the python executable as the next argument so we can run the project. | ||
// context.Args.Add(pythonExecutable!); | ||
} | ||
}); | ||
|
||
if (!string.IsNullOrEmpty(instrumentationExecutable)) | ||
{ | ||
resourceBuilder.WithOtlpExporter(); | ||
|
||
// Make sure to attach the logging instrumentation setting, so we can capture logs. | ||
// Without this you'll need to configure logging yourself. Which is kind of a pain. | ||
resourceBuilder.WithEnvironment("OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED", "true"); | ||
} | ||
|
||
return resourceBuilder; | ||
} | ||
|
||
private static void AddOpenTelemetryArguments(CommandLineArgsCallbackContext context) | ||
{ | ||
context.Args.Add("--traces_exporter"); | ||
context.Args.Add("otlp"); | ||
|
||
context.Args.Add("--logs_exporter"); | ||
context.Args.Add("console,otlp"); | ||
|
||
context.Args.Add("--metrics_exporter"); | ||
context.Args.Add("otlp"); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/CommunityToolkit.Aspire.Hosting.Python.Extensions/UvAppResource.cs
This file contains 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,14 @@ | ||
using Aspire.Hosting.Python; | ||
|
||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
/// <summary> | ||
/// Represents a Uv application. | ||
/// </summary> | ||
/// <param name="name">The name of the resource.</param> | ||
/// <param name="workingDirectory">The working directory for uv.</param> | ||
public class UvAppResource(string name, string workingDirectory) | ||
: PythonAppResource(name, "uv", workingDirectory), IResourceWithServiceDiscovery | ||
{ | ||
internal const string HttpEndpointName = "http"; | ||
} |
This file contains 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 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 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