From f3e047eccc40e8e3aa81758c44c606a1f87b1051 Mon Sep 17 00:00:00 2001 From: Kara Alexandra Date: Fri, 28 Feb 2025 12:51:11 -0600 Subject: [PATCH] Set up docker image and k8s deployment --- .dockerignore | 10 ++++++ ALttPRandomizer/ALttPRandomizer.csproj | 3 ++ ALttPRandomizer/Options/ServiceOptions.cs | 2 ++ ALttPRandomizer/Program.cs | 11 +++++-- ALttPRandomizer/Randomizer.cs | 3 +- ALttPRandomizer/appsettings.Docker.json | 16 ++++++++++ Dockerfile | 37 +++++++++++++++++++++++ deployment.yaml | 21 +++++++++++++ 8 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 ALttPRandomizer/appsettings.Docker.json create mode 100644 Dockerfile create mode 100644 deployment.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2ee22a9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +ALttPRandomizer/appsettings.json +ALttPRandomizer/appsettings.Development.json +ALttPRandomizer/[Oo]bj +ALttPRandomizer/[Bb]in + +*/__pycache__ +*/.github +*/_vendor +ALttPDoorRandomizer/DR_* +ALttPDoorRandomizer/data/base2current.json diff --git a/ALttPRandomizer/ALttPRandomizer.csproj b/ALttPRandomizer/ALttPRandomizer.csproj index d063360..08537fd 100644 --- a/ALttPRandomizer/ALttPRandomizer.csproj +++ b/ALttPRandomizer/ALttPRandomizer.csproj @@ -17,6 +17,9 @@ + + Always + Always diff --git a/ALttPRandomizer/Options/ServiceOptions.cs b/ALttPRandomizer/Options/ServiceOptions.cs index 4e8582a..94b7760 100644 --- a/ALttPRandomizer/Options/ServiceOptions.cs +++ b/ALttPRandomizer/Options/ServiceOptions.cs @@ -2,12 +2,14 @@ using System; public class ServiceOptions { + public string Baserom { get; set; } = null!; public string PythonPath { get; set; } = null!; public string RandomizerPath { get; set; } = null!; public AzureSettings AzureSettings { get; set; } = new AzureSettings(); } public class AzureSettings { + public string? ClientId { get; set; } public Uri BlobstoreEndpoint { get; set; } = null!; } } diff --git a/ALttPRandomizer/Program.cs b/ALttPRandomizer/Program.cs index 4f78c7a..7e51767 100644 --- a/ALttPRandomizer/Program.cs +++ b/ALttPRandomizer/Program.cs @@ -4,6 +4,7 @@ using ALttPRandomizer.Azure; using ALttPRandomizer.Options; using ALttPRandomizer.Service; + using global::Azure.Core; using global::Azure.Identity; using global::Azure.Storage.Blobs; using Microsoft.AspNetCore.Builder; @@ -20,7 +21,7 @@ builder.Configuration .AddJsonFile("appsettings.json") - .AddJsonFile("appsettings.Development.json") + .AddJsonFile("appsettings.Development.json", true) .AddEnvironmentVariables(); builder.Services.Configure(builder.Configuration.GetSection("ALttPRandomizer")); @@ -34,7 +35,13 @@ var provider = builder.Services.BuildServiceProvider(); var settings = provider.GetRequiredService>().CurrentValue!; - var token = new DefaultAzureCredential(); + var options = new DefaultAzureCredentialOptions(); + + if (settings.AzureSettings.ClientId != null) { + options.ManagedIdentityClientId = new(settings.AzureSettings.ClientId); + } + + var token = new DefaultAzureCredential(options); var seedClient = new BlobContainerClient(settings.AzureSettings.BlobstoreEndpoint, token); builder.Services.AddSingleton(seedClient); diff --git a/ALttPRandomizer/Randomizer.cs b/ALttPRandomizer/Randomizer.cs index c897971..b9867d9 100644 --- a/ALttPRandomizer/Randomizer.cs +++ b/ALttPRandomizer/Randomizer.cs @@ -37,7 +37,8 @@ var args = start.ArgumentList; args.Add("DungeonRandomizer.py"); - args.Add("--rom=../alttp.sfc"); + args.Add("--rom"); + args.Add(this.Configuration.Baserom); args.Add("--bps"); args.Add("--outputpath"); diff --git a/ALttPRandomizer/appsettings.Docker.json b/ALttPRandomizer/appsettings.Docker.json new file mode 100644 index 0000000..54b6a2c --- /dev/null +++ b/ALttPRandomizer/appsettings.Docker.json @@ -0,0 +1,16 @@ +{ + "ALttPRandomizer": { + "baserom": "/randomizer/alttp.sfc", + "pythonPath": "/usr/bin/python3", + "randomizerPath": "/randomizer", + "azureSettings": { + "clientId": "2f242de5-7595-432c-b41d-30d21b2064f2", + "blobstoreEndpoint": "https://alttprstorage.blob.core.windows.net/seeds" + } + }, + "Logging": { + "LogLevel": { + "Default": "Information" + } + } +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b8a53dd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +FROM mcr.microsoft.com/dotnet/sdk:8.0-azurelinux3.0 AS build + +ARG BUILD_CONFIGURATION=Release +WORKDIR /src + +COPY ALttPRandomizer/ ALttPRandomizer/ +WORKDIR "/src/ALttPRandomizer" +RUN dotnet build "./ALttPRandomizer.csproj" -c $BUILD_CONFIGURATION -o /app/build +RUN dotnet publish "./ALttPRandomizer.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM mcr.microsoft.com/dotnet/aspnet:8.0-azurelinux3.0 AS final +EXPOSE 8080 +EXPOSE 8081 + +RUN tdnf install -y python3 + +RUN mkdir -p /randomizer/data +RUN touch /randomizer/data/base2current.json +RUN chown $APP_UID:$APP_UID /randomizer/data/base2current.json + +USER $APP_UID + +RUN python3 -m ensurepip --upgrade + +WORKDIR /randomizer +COPY alttp.sfc . + +COPY ALttPDoorRandomizer/resources/app/meta/manifests/pip_requirements.txt requirements.txt +RUN python3 -m pip install -r requirements.txt + +COPY ALttPDoorRandomizer/ . + +WORKDIR /app +COPY --from=build /app/publish . +COPY ALttPRandomizer/appsettings.Docker.json appsettings.json + +ENTRYPOINT ["dotnet", "ALttPRandomizer.dll"] diff --git a/deployment.yaml b/deployment.yaml new file mode 100644 index 0000000..bfd6efb --- /dev/null +++ b/deployment.yaml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: alttpr-backend +spec: + selector: + matchLabels: + app: alttpr-backend + template: + metadata: + labels: + app: alttpr-backend + spec: + nodeSelector: + kubernetes.io/os: linux + containers: + - name: alttpr-backend + image: alttpracr.azurecr.io/alttpr-backend + ports: + - containerPort: 8080 + name: http