Skip to content
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

.NET 8 #47

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 11 additions & 64 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,73 +1,20 @@
name: Publish Docker Images
name: Build and Publish Image

on:
pull_request:
branches:
- development
push:
branches: [ development, staging ]
branches: [development, staging]
release:
types: [ "published" ]
workflow_dispatch:
inputs:
tagName:
description: 'Tag of the image you want to build and push'
required: true
types: ["published"]

jobs:
build:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v2

- name: Prepare
id: prep
run: |
DOCKER_IMAGE=cmusei/vm-api
VERSION=development
if [[ ! -z "${{ github.event.inputs.tagName }}" ]]; then
VERSION=${{ github.event.inputs.tagName }}
TAGS="${DOCKER_IMAGE}:${VERSION}"
elif [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
MAJORMINORVERSION=$(echo $VERSION | grep -oP '(\d+)\.(\d+)')
TAGS="${DOCKER_IMAGE}:${VERSION},${DOCKER_IMAGE}:${MAJORMINORVERSION}"
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
TAGS="${DOCKER_IMAGE}:${VERSION}"
fi
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
echo ::set-output name=push::false
echo "event is pull_request, not pushing image"
else
echo ::set-output name=push::true
echo "event is not pull_request, pushing image"
fi
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: ${{ steps.prep.outputs.push }}
pull: true
tags: ${{ steps.prep.outputs.tags }}
labels: |
org.opencontainers.image.source=${{ github.event.repository.clone_url }}
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.revision=${{ github.sha }}
build-and-publish:
name: Build and Publish
uses: cmu-sei/Crucible-Github-Actions/.github/workflows/[email protected]
with:
imageName: cmusei/vm-api
secrets:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
14 changes: 6 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#
#multi-stage target: dev
#
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS dev
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS dev

ENV ASPNETCORE_URLS=http://0.0.0.0:4302 \
ASPNETCORE_ENVIRONMENT=DEVELOPMENT
ENV ASPNETCORE_HTTP_PORTS=4302
ENV ASPNETCORE_ENVIRONMENT=DEVELOPMENT

COPY . /app
WORKDIR /app
Expand All @@ -16,17 +16,15 @@ CMD ["dotnet", "run"]
#
#multi-stage target: prod
#
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS prod
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS prod
ARG commit
ENV COMMIT=$commit
ENV DOTNET_HOSTBUILDER__RELOADCONFIGCHANGE=false

COPY --from=dev /app/dist /app

WORKDIR /app
ENV ASPNETCORE_URLS=http://*:80
ENV ASPNETCORE_HTTP_PORTS=80
EXPOSE 80

CMD ["dotnet","Player.Vm.Api.dll"]

RUN apt-get update && \
apt-get install -y jq
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.100",
"version": "8.0.304",
"allowPrerelease": false,
"rollForward": "latestMinor"
}
Expand Down
8 changes: 3 additions & 5 deletions src/Player.Vm.Api/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
"isRoot": true,
"tools": {
"swashbuckle.aspnetcore.cli": {
"version": "6.2.3",
"commands": [
"swagger"
]
"version": "6.8.0",
"commands": ["swagger"]
}
}
}
}
60 changes: 60 additions & 0 deletions src/Player.Vm.Api/Data/Entry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Player.Vm.Api.Data;

public class Entry
{
public object Entity { get; set; }
public EntityState State { get; set; }
public IEnumerable<PropertyEntry> Properties { get; set; }
private Dictionary<string, bool> IsPropertyModified { get; set; } = new();

public Entry(EntityEntry entry, Entry oldEntry = null)
{
Entity = entry.Entity;
State = entry.State;
Properties = entry.Properties;

ProcessOldEntry(oldEntry);

foreach (var prop in Properties)
{
IsPropertyModified[prop.Metadata.Name] = prop.IsModified;
}
}

private void ProcessOldEntry(Entry oldEntry)
{
if (oldEntry == null) return;

if (oldEntry.State != EntityState.Unchanged && oldEntry.State != EntityState.Detached)
{
State = oldEntry.State;
}

var modifiedProperties = oldEntry.GetModifiedProperties();

foreach (var property in Properties)
{
if (modifiedProperties.Contains(property.Metadata.Name))
{
property.IsModified = true;
}
}
}

public string[] GetModifiedProperties()
{
return IsPropertyModified
.Where(x => x.Value)
.Select(x => x.Key)
.ToArray();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Copyright 2022 Carnegie Mellon University. All Rights Reserved.

namespace Player.Vm.Api.Data.Migrations.Postgres
{
public partial class urlarray : Migration
public partial class UrlArray : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Copyright 2022 Carnegie Mellon University. All Rights Reserved.

namespace Player.Vm.Api.Data.Migrations.Postgres
{
public partial class proxmox : Migration
public partial class Proxmox : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
Expand Down
11 changes: 4 additions & 7 deletions src/Player.Vm.Api/Data/VmContext.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// Copyright 2022 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal;
using Player.Vm.Api.Domain.Models;
using Player.Vm.Api.Infrastructure.Extensions;

namespace Player.Vm.Api.Data
{
public class VmContext : DbContext
{
private DbContextOptions _options;
// Needed for EventInterceptor
public IServiceProvider ServiceProvider;

public VmContext(DbContextOptions options)
: base(options)
{
_options = options;
}
: base(options) { }

public DbSet<Domain.Models.Vm> Vms { get; set; }
public DbSet<VmTeam> VmTeams { get; set; }
Expand Down
30 changes: 30 additions & 0 deletions src/Player.Vm.Api/Data/VmContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore;

namespace Player.Vm.Api.Data;

public class VmContextFactory : IDbContextFactory<VmContext>
{
private readonly IDbContextFactory<VmContext> _pooledFactory;
private readonly IServiceProvider _serviceProvider;

public VmContextFactory(
IDbContextFactory<VmContext> pooledFactory,
IServiceProvider serviceProvider)
{
_pooledFactory = pooledFactory;
_serviceProvider = serviceProvider;
}

public VmContext CreateDbContext()
{
var context = _pooledFactory.CreateDbContext();

// Inject the current scope's ServiceProvider
context.ServiceProvider = _serviceProvider;
return context;
}
}
6 changes: 3 additions & 3 deletions src/Player.Vm.Api/Domain/Proxmox/Services/ProxmoxService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<ProxmoxConsole> GetConsole(ProxmoxVmInfo info)
if (!success)
{
// Check if vm exists on a different node and try again
var vm = await _pveClient.GetVm(info.Id);
var vm = await _pveClient.GetVmAsync(info.Id);

if (vm != null)
{
Expand Down Expand Up @@ -97,12 +97,12 @@ public async Task<ProxmoxConsole> GetConsole(ProxmoxVmInfo info)

public async Task<IEnumerable<IClusterResourceVm>> GetVms()
{
return await _pveClient.GetResources(ClusterResourceType.Vm);
return await _pveClient.GetResourcesAsync(ClusterResourceType.Vm);
}

private async Task<IClusterResourceVm> RefreshVm(int id)
{
var vm = await _pveClient.GetVm(id);
var vm = await _pveClient.GetVmAsync(id);

return vm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private TokenResponse RenewToken(CancellationToken ct)
}
catch (Exception ex)
{
_logger.LogError("Exception renewing auth token.", ex);
_logger.LogError(ex, "Exception renewing auth token.");
}

return null;
Expand Down
6 changes: 3 additions & 3 deletions src/Player.Vm.Api/Domain/Services/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public async Task<bool> CanManageTeamsAsync(IEnumerable<Guid> teamIds, bool all,
return true;
}
}
catch (Exception ex)
catch (Exception)
{

}
Expand Down Expand Up @@ -141,7 +141,7 @@ public async Task<bool> CanAccessTeamsAsync(IEnumerable<Guid> teamIds, Cancellat
if (team.CanManage || team.IsPrimary)
return true;
}
catch (Exception ex)
catch (Exception)
{
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public async Task<Team> GetTeamById(Guid id)
{
return await _playerApiClient.GetTeamAsync(id);
}
catch (Exception ex)
catch (Exception)
{
return null;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Player.Vm.Api/Domain/Services/VmUsageLoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public class DisabledVmUsageLoggingService : IVmUsageLoggingService
{
public async Task CreateVmLogEntry(Guid userId, Guid vmId, IEnumerable<Guid> teamIds, CancellationToken ct)
{
return;
await Task.CompletedTask;
}

public async Task CloseVmLogEntry(Guid userId, Guid vmId, CancellationToken ct)
{
return;
await Task.CompletedTask;
}
}

Expand All @@ -43,7 +43,6 @@ public class VmUsageLoggingService : IVmUsageLoggingService
private readonly VmLoggingContext _dbContext;

public VmUsageLoggingService(
IServiceScopeFactory scopeFactory,
VmUsageLoggingOptions loggingOptions,
IPlayerService playerService,
IVmService vmService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public CheckTasksBehavior(ITaskService taskService)
_taskService = taskService;
}

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var response = await next();

Expand Down
Loading
Loading