Skip to content

Commit

Permalink
Merge pull request #15 from Team-MPM/deployment
Browse files Browse the repository at this point in the history
Deployment
  • Loading branch information
g-martin772 authored Apr 30, 2024
2 parents 806c2d4 + 7ee2810 commit 8b0d20e
Show file tree
Hide file tree
Showing 35 changed files with 827 additions and 290 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
bin/
int/
obj/
obj/
.azure/
15 changes: 7 additions & 8 deletions MPM-Betting.Api/MPM-Betting.Api.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>MPM_Betting.Api</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0-preview.3.24210.17" />
<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="9.0.0-preview.3.24210.17" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="9.0.0-preview.3.24172.13" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0-preview.3.24172.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0-preview.3.24172.4" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0-preview.3.24172.9" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.0-preview.3" />
<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="8.0.0-preview.6.24214.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions MPM-Betting.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5241",
"environmentVariables": {
Expand All @@ -22,7 +22,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7258;http://localhost:5241",
"environmentVariables": {
Expand Down
47 changes: 47 additions & 0 deletions MPM-Betting.Api/manifests/containerApp.tmpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
location: {{ .Env.AZURE_LOCATION }}
identity:
type: UserAssigned
userAssignedIdentities:
? "{{ .Env.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID }}"
: {}
properties:
environmentId: {{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_ID }}
configuration:
activeRevisionsMode: single
ingress:
external: true
targetPort: {{ targetPortOrDefault 8080 }}
transport: http
allowInsecure: false
registries:
- server: {{ .Env.AZURE_CONTAINER_REGISTRY_ENDPOINT }}
identity: {{ .Env.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID }}
secrets:
- name: connectionstrings--mpm-betting
value: Server=tcp:{{ .Env.SQL_SQLSERVERFQDN }},1433;Encrypt=True;Authentication="Active Directory Default";Database=MPM-Betting
- name: connectionstrings--redis
value: redis:6379
template:
containers:
- image: {{ .Image }}
name: api
env:
- name: AZURE_CLIENT_ID
value: {{ .Env.MANAGED_IDENTITY_CLIENT_ID }}
- name: ASPNETCORE_FORWARDEDHEADERS_ENABLED
value: "true"
- name: OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EVENT_LOG_ATTRIBUTES
value: "true"
- name: OTEL_DOTNET_EXPERIMENTAL_OTLP_EMIT_EXCEPTION_LOG_ATTRIBUTES
value: "true"
- name: OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY
value: in_memory
- name: ConnectionStrings__MPM-Betting
secretRef: connectionstrings--mpm-betting
- name: ConnectionStrings__redis
secretRef: connectionstrings--redis
scale:
minReplicas: 1
tags:
azd-service-name: api
aspire-resource-name: api
18 changes: 12 additions & 6 deletions MPM-Betting.Aspire/MPM-Betting.Aspire.AppHost/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static string GetParameterValue(IConfiguration configuration, string nam
var configurationKey = $"Parameters:{name}";
return configuration[configurationKey]
?? parameterDefault?.GetDefaultValue()
?? throw new DistributedApplicationException($"Parameter resource could not be used because configuration key '{configurationKey}' is missing and the Parameter has no default value."); ;
?? throw new DistributedApplicationException($"Parameter resource could not be used because configuration key '{configurationKey}' is missing and the Parameter has no default value.");
}

private class UserSecretsParameterDefault(string applicationName, string parameterName, ParameterDefault parameterDefault) : ParameterDefault
Expand All @@ -117,10 +117,13 @@ public override string GetDefaultValue()

private static bool TrySetUserSecret(string applicationName, string name, string value)
{
if (string.IsNullOrEmpty(applicationName)) return false;
if (string.IsNullOrEmpty(applicationName))
return false;

var appAssembly = Assembly.Load(new AssemblyName(applicationName));
if (appAssembly is null || appAssembly.GetCustomAttribute<UserSecretsIdAttribute>()?.UserSecretsId is not
{ } userSecretsId) return false;
if (appAssembly?.GetCustomAttribute<UserSecretsIdAttribute>()?.UserSecretsId is not { } userSecretsId)
return false;

// Save the value to the secret store
try
{
Expand All @@ -135,13 +138,16 @@ private static bool TrySetUserSecret(string applicationName, string name, string
setUserSecrets?.WaitForExit(TimeSpan.FromSeconds(10));
return setUserSecrets?.ExitCode == 0;
}
catch (Exception) { }
catch (Exception)
{
// ignored
}

return false;
}
}

private class ResourceBuilder
private static class ResourceBuilder
{
public static IResourceBuilder<T> Create<T>(T resource, IDistributedApplicationBuilder distributedApplicationBuilder) where T : IResource
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
</PropertyGroup>
<UserSecretsId>09ac8703-e432-460f-8352-32adaac9cef0</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0-preview.3.24210.17" />
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.0.0-preview.3.24210.17" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.0.0-preview.3.24210.17" />
<PackageReference Include="Aspire.Hosting.SqlServer" Version="9.0.0-preview.3.24210.17" />
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.0.0-preview.6.24214.1" />
<PackageReference Include="Aspire.Hosting.Azure.Sql" Version="8.0.0-preview.6.24214.1" />
<PackageReference Include="Aspire.Hosting.Redis" Version="8.0.0-preview.6.24214.1" />
<PackageReference Include="Aspire.Hosting.SqlServer" Version="8.0.0-preview.6.24214.1" />
<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0-preview.6.24214.1" />
<PackageReference Include="Grpc.AspNetCore" Version="2.62.0" />
<PackageReference Include="KubernetesClient" Version="13.0.26" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0-preview.3.24172.9" />
<PackageReference Include="KubernetesClient" Version="13.0.37" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Polly" Version="8.3.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static IResourceBuilder<MailDevResource> AddMailDev(this IDistributedAppl
.WithImage(MailDevContainerImageTags.Image)
.WithImageRegistry(MailDevContainerImageTags.Registry)
.WithImageTag(MailDevContainerImageTags.Tag)
.WithHttpEndpoint(1080, httpPort, name: "http")
.WithEndpoint(1025, smtpPort, name: "smtp");
.WithHttpEndpoint(httpPort, 1080, name: "http")
.WithEndpoint(smtpPort, 1025, name: "smtp");
}
}
19 changes: 11 additions & 8 deletions MPM-Betting.Aspire/MPM-Betting.Aspire.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
if (!builder.ExecutionContext.IsPublishMode)
{
var azurite = builder.AddContainer("azurite", "mcr.microsoft.com/azure-storage/azurite")
.WithEndpoint(containerPort: 10000, name: "blob", hostPort: 11000)
.WithEndpoint(containerPort: 10001, name: "queue", hostPort: 11001)
.WithEndpoint(containerPort: 10002, name: "table", hostPort: 11002);
.WithEndpoint(port: 10000, name: "blob", targetPort: 11000)
.WithEndpoint(port: 10001, name: "queue", targetPort: 11001)
.WithEndpoint(port: 10002, name: "table", targetPort: 11002);

var queueConnStrCallback = () => $"DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;QueueEndpoint={azurite.GetEndpoint("queue").Url.Replace("tcp", "http")}/devstoreaccount1;";

Expand All @@ -24,34 +24,37 @@
var grafana = builder.AddContainer("grafana", "grafana/grafana")
.WithBindMount(GetFullPath("../grafana/config"), "/etc/grafana")
.WithBindMount(GetFullPath("../grafana/dashboards"), "/var/lib/grafana/dashboards")
.WithEndpoint(containerPort: 3000, hostPort: 3000, name: "grafana-http", scheme: "http");
.WithEndpoint(port: 3000, targetPort: 3000, name: "grafana-http", scheme: "http");

var prometheus = builder.AddContainer("prometheus", "prom/prometheus")
.WithBindMount(GetFullPath("../prometheus"), "/etc/prometheus")
.WithEndpoint(containerPort: 9090, hostPort: 9090);
.WithEndpoint(port: 9090, targetPort: 9090);

var redis = builder.AddRedis("redis")
.WithPersistence()
.WithDataVolume();

var sql = builder.AddPostgres("sql", password: builder.CreateStablePassword("MPM-Betting-Password"))
//.WithDataVolume() // note gabriel: me too scared to touch for now....
var sql = builder.AddSqlServer("sql", password: builder.CreateStablePassword("MPM-Betting-Password"))
.WithDataVolume()
.PublishAsAzureSqlDatabase()
.AddDatabase("MPM-Betting");

if (builder.ExecutionContext.IsPublishMode)
{
var api = builder.AddProject<MPM_Betting_Api>("api")
.WithExternalHttpEndpoints()
.WithReference(sql)
.WithReference(redis);

var blazor = builder.AddProject<MPM_Betting_Blazor>("blazor")
.WithExternalHttpEndpoints()
.WithReference(api)
.WithReference(redis)
.WithReference(sql);
}
else
{
var mailDev = builder.AddMailDev("maildev");
var mailDev = builder.AddMailDev("maildev", 9324, 9325);

var api = builder.AddProjectWithDotnetWatch<MPM_Betting_Api>("api")
.WithReference(sql)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:16175",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:17037",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:22001",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:2200",
"DOTNET_ASPIRE_SHOW_DASHBOARD_RESOURCES": "true"
}
},
Expand All @@ -29,7 +29,7 @@
},
"generate-manifest": {
"commandName": "Project",
"launchBrowser": true,
"launchBrowser": false,
"dotnetRunMessages": true,
"commandLineArgs": "--publisher manifest --output-path aspire-manifest/manifest.json",
"applicationUrl": "http://localhost:15888",
Expand Down
Loading

0 comments on commit 8b0d20e

Please sign in to comment.