182 lines
6.5 KiB
C#
182 lines
6.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using MoonlightServers.Api.Infrastructure.Database;
|
|
using MoonlightServers.Api.Infrastructure.Database.Entities;
|
|
using MoonlightServers.Api.Infrastructure.Database.Json;
|
|
|
|
namespace MoonlightServers.Api.Admin.Templates;
|
|
|
|
public class TemplateTransferService
|
|
{
|
|
private readonly DatabaseRepository<Template> TemplateRepository;
|
|
private readonly DatabaseRepository<TemplateDockerImage> DockerImageRepository;
|
|
|
|
public TemplateTransferService(DatabaseRepository<Template> templateRepository,
|
|
DatabaseRepository<TemplateDockerImage> dockerImageRepository)
|
|
{
|
|
TemplateRepository = templateRepository;
|
|
DockerImageRepository = dockerImageRepository;
|
|
}
|
|
|
|
public async Task<TemplateTransferModel?> ExportAsync(int id)
|
|
{
|
|
var template = await TemplateRepository
|
|
.Query()
|
|
.Include(x => x.Variables)
|
|
.Include(x => x.DockerImages)
|
|
.Include(x => x.DefaultDockerImage)
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (template == null)
|
|
return null;
|
|
|
|
return new()
|
|
{
|
|
Name = template.Name,
|
|
Description = template.Description,
|
|
Author = template.Author,
|
|
Version = template.Version,
|
|
UpdateUrl = template.UpdateUrl,
|
|
DonateUrl = template.DonateUrl,
|
|
|
|
Files = new FilesConfigTransferModel
|
|
{
|
|
ConfigurationFiles = template.FilesConfig.ConfigurationFiles
|
|
.Select(cf => new ConfigurationFileTransferModel
|
|
{
|
|
Path = cf.Path,
|
|
Parser = cf.Parser,
|
|
Mappings = cf.Mappings
|
|
.Select(m => new ConfigurationFileMappingTransferModel { Key = m.Key, Value = m.Value })
|
|
.ToList()
|
|
})
|
|
.ToList()
|
|
},
|
|
|
|
Lifecycle = new LifecycleConfigTransferModel
|
|
{
|
|
StartupCommands = template.LifecycleConfig.StartupCommands
|
|
.Select(sc => new StartupCommandTransferModel
|
|
{ DisplayName = sc.DisplayName, Command = sc.Command })
|
|
.ToList(),
|
|
StopCommand = template.LifecycleConfig.StopCommand,
|
|
OnlineLogPatterns = template.LifecycleConfig.OnlineLogPatterns.ToList()
|
|
},
|
|
|
|
Installation = new InstallationConfigTransferModel
|
|
{
|
|
DockerImage = template.InstallationConfig.DockerImage,
|
|
Shell = template.InstallationConfig.Shell,
|
|
Script = template.InstallationConfig.Script
|
|
},
|
|
|
|
Miscellaneous = new MiscellaneousConfigTransferModel
|
|
{
|
|
UseLegacyStartup = template.MiscellaneousConfig.UseLegacyStartup
|
|
},
|
|
|
|
AllowUserDockerImageChange = template.AllowUserDockerImageChange,
|
|
DockerImages = template.DockerImages
|
|
.Select(img => new TemplateDockerImageTransferModel
|
|
{
|
|
DisplayName = img.DisplayName,
|
|
ImageName = img.ImageName,
|
|
SkipPulling = img.SkipPulling,
|
|
IsDefault = template.DefaultDockerImage != null && img.Id == template.DefaultDockerImage.Id
|
|
})
|
|
.ToList(),
|
|
|
|
Variables = template.Variables
|
|
.Select(v => new TemplateVariableTransferModel
|
|
{
|
|
DisplayName = v.DisplayName,
|
|
Description = v.Description,
|
|
EnvName = v.EnvName,
|
|
DefaultValue = v.DefaultValue
|
|
})
|
|
.ToList()
|
|
};
|
|
}
|
|
|
|
public async Task<Template> ImportAsync(TemplateTransferModel m)
|
|
{
|
|
var template = new Template
|
|
{
|
|
Name = m.Name,
|
|
Description = m.Description,
|
|
Author = m.Author,
|
|
Version = m.Version,
|
|
UpdateUrl = m.UpdateUrl,
|
|
DonateUrl = m.DonateUrl,
|
|
|
|
FilesConfig = new FilesConfig
|
|
{
|
|
ConfigurationFiles = m.Files.ConfigurationFiles
|
|
.Select(cf => new ConfigurationFile
|
|
{
|
|
Path = cf.Path,
|
|
Parser = cf.Parser,
|
|
Mappings = cf.Mappings
|
|
.Select(mp => new ConfigurationFileMapping { Key = mp.Key, Value = mp.Value })
|
|
.ToList()
|
|
})
|
|
.ToList()
|
|
},
|
|
|
|
LifecycleConfig = new LifecycleConfig
|
|
{
|
|
StartupCommands = m.Lifecycle.StartupCommands
|
|
.Select(sc => new StartupCommand { DisplayName = sc.DisplayName, Command = sc.Command })
|
|
.ToList(),
|
|
StopCommand = m.Lifecycle.StopCommand,
|
|
OnlineLogPatterns = m.Lifecycle.OnlineLogPatterns.ToList()
|
|
},
|
|
|
|
InstallationConfig = new InstallationConfig
|
|
{
|
|
DockerImage = m.Installation.DockerImage,
|
|
Shell = m.Installation.Shell,
|
|
Script = m.Installation.Script
|
|
},
|
|
|
|
MiscellaneousConfig = new MiscellaneousConfig { UseLegacyStartup = m.Miscellaneous.UseLegacyStartup },
|
|
|
|
AllowUserDockerImageChange = m.AllowUserDockerImageChange,
|
|
|
|
Variables = m.Variables
|
|
.Select(v => new TemplateVariable
|
|
{
|
|
DisplayName = v.DisplayName,
|
|
Description = v.Description,
|
|
EnvName = v.EnvName,
|
|
DefaultValue = v.DefaultValue
|
|
})
|
|
.ToList()
|
|
};
|
|
|
|
var finalTemplate = await TemplateRepository.AddAsync(template);
|
|
|
|
TemplateDockerImage? defaultDockerImage = null;
|
|
|
|
foreach (var img in m.DockerImages)
|
|
{
|
|
var entity = new TemplateDockerImage
|
|
{
|
|
DisplayName = img.DisplayName,
|
|
ImageName = img.ImageName,
|
|
SkipPulling = img.SkipPulling,
|
|
Template = template
|
|
};
|
|
|
|
var finalEntity = await DockerImageRepository.AddAsync(entity);
|
|
|
|
if (img.IsDefault)
|
|
defaultDockerImage = finalEntity;
|
|
}
|
|
|
|
finalTemplate.DefaultDockerImage = defaultDockerImage;
|
|
|
|
await TemplateRepository.UpdateAsync(finalTemplate);
|
|
|
|
return finalTemplate;
|
|
}
|
|
} |