115 lines
3.6 KiB
C#
115 lines
3.6 KiB
C#
using Docker.DotNet;
|
|
using MoonlightServers.Daemon.Models;
|
|
using MoonlightServers.Daemon.ServerSystem.Abstractions;
|
|
|
|
namespace MoonlightServers.Daemon.ServerSystem.Implementations.Docker;
|
|
|
|
public class DockerRuntimeEnvService : IRuntimeEnvironmentService
|
|
{
|
|
private readonly DockerClient DockerClient;
|
|
private readonly ILoggerFactory LoggerFactory;
|
|
private readonly DockerEventService DockerEventService;
|
|
private readonly ContainerConfigMapper ConfigMapper;
|
|
|
|
private const string NameTemplate = "ml-runtime-{0}";
|
|
|
|
public DockerRuntimeEnvService(
|
|
DockerClient dockerClient,
|
|
ILoggerFactory loggerFactory,
|
|
DockerEventService dockerEventService,
|
|
ContainerConfigMapper configMapper
|
|
)
|
|
{
|
|
DockerClient = dockerClient;
|
|
LoggerFactory = loggerFactory;
|
|
DockerEventService = dockerEventService;
|
|
ConfigMapper = configMapper;
|
|
}
|
|
|
|
public async Task<IRuntimeEnvironment?> FindAsync(string id)
|
|
{
|
|
try
|
|
{
|
|
var dockerInspect = await DockerClient.Containers.InspectContainerAsync(
|
|
string.Format(NameTemplate, id)
|
|
);
|
|
|
|
var logger = LoggerFactory.CreateLogger($"MoonlightServers.Daemon.ServerSystem.Implementations.Docker({id})");
|
|
|
|
return new DockerRuntimeEnv(dockerInspect.ID, DockerClient, logger, DockerEventService);
|
|
}
|
|
catch (DockerContainerNotFoundException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public async Task<IRuntimeEnvironment> CreateAsync(
|
|
string id,
|
|
RuntimeConfiguration configuration,
|
|
IRuntimeStorage storage
|
|
)
|
|
{
|
|
try
|
|
{
|
|
var dockerInspect = await DockerClient.Containers.InspectContainerAsync(
|
|
string.Format(NameTemplate, id)
|
|
);
|
|
|
|
if (dockerInspect.State.Running)
|
|
await DockerClient.Containers.KillContainerAsync(dockerInspect.ID, new());
|
|
|
|
await DockerClient.Containers.RemoveContainerAsync(dockerInspect.ID, new());
|
|
}
|
|
catch (DockerContainerNotFoundException)
|
|
{
|
|
// Ignored
|
|
}
|
|
|
|
var storagePath = await storage.GetBindPathAsync();
|
|
|
|
var parameters = ConfigMapper.GetRuntimeConfig(
|
|
id,
|
|
string.Format(NameTemplate, id),
|
|
configuration,
|
|
storagePath
|
|
);
|
|
|
|
var container = await DockerClient.Containers.CreateContainerAsync(parameters);
|
|
|
|
var logger = LoggerFactory.CreateLogger($"MoonlightServers.Daemon.ServerSystem.Implementations.Docker({id})");
|
|
|
|
return new DockerRuntimeEnv(container.ID, DockerClient, logger, DockerEventService);
|
|
}
|
|
|
|
public Task UpdateAsync(IRuntimeEnvironment environment, RuntimeConfiguration configuration)
|
|
=> Task.CompletedTask;
|
|
|
|
public async Task DeleteAsync(IRuntimeEnvironment environment)
|
|
{
|
|
if (environment is not DockerRuntimeEnv dockerRuntimeEnv)
|
|
{
|
|
throw new ArgumentException(
|
|
$"You cannot delete runtime environments which haven't been created by {nameof(DockerRuntimeEnvService)}"
|
|
);
|
|
}
|
|
|
|
await dockerRuntimeEnv.DisposeAsync();
|
|
|
|
try
|
|
{
|
|
var dockerInspect = await DockerClient.Containers.InspectContainerAsync(
|
|
dockerRuntimeEnv.ContainerId
|
|
);
|
|
|
|
if (dockerInspect.State.Running)
|
|
await DockerClient.Containers.KillContainerAsync(dockerInspect.ID, new());
|
|
|
|
await DockerClient.Containers.RemoveContainerAsync(dockerInspect.ID, new());
|
|
}
|
|
catch (DockerContainerNotFoundException)
|
|
{
|
|
// Ignored
|
|
}
|
|
}
|
|
} |