Cleaned up using in project. Improved prohect structure and refactored page names. Upgraded dependencies
This commit is contained in:
220
Moonlight.Frontend/Admin/Sys/ContainerHelper/Index.razor
Normal file
220
Moonlight.Frontend/Admin/Sys/ContainerHelper/Index.razor
Normal file
@@ -0,0 +1,220 @@
|
||||
@using LucideBlazor
|
||||
@using Moonlight.Shared.Admin.Sys.ContainerHelper
|
||||
@using Moonlight.Shared.Admin.Sys.Versions
|
||||
@using ShadcnBlazor.Buttons
|
||||
@using ShadcnBlazor.Cards
|
||||
@using ShadcnBlazor.Emptys
|
||||
@using ShadcnBlazor.Extras.AlertDialogs
|
||||
@using ShadcnBlazor.Extras.Common
|
||||
@using ShadcnBlazor.Extras.Dialogs
|
||||
@using ShadcnBlazor.Fields
|
||||
@using ShadcnBlazor.Selects
|
||||
@using ShadcnBlazor.Switches
|
||||
@inject HttpClient HttpClient
|
||||
@inject DialogService DialogService
|
||||
@inject AlertDialogService AlertDialogService
|
||||
|
||||
<div class="mt-5">
|
||||
<LazyLoader Load="LoadAsync">
|
||||
@if (StatusDto.IsEnabled)
|
||||
{
|
||||
if (StatusDto.IsReachable)
|
||||
{
|
||||
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5">
|
||||
<Card ClassName="col-span-1">
|
||||
<CardHeader>
|
||||
<CardTitle>Version</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<FieldGroup>
|
||||
<FieldSet>
|
||||
<Field>
|
||||
<FieldLabel>Version / Branch</FieldLabel>
|
||||
<FieldContent>
|
||||
<Select DefaultValue="@SelectedVersion" @bind-Value="SelectedVersion">
|
||||
<SelectTrigger ClassName="w-64">
|
||||
<SelectValue/>
|
||||
</SelectTrigger>
|
||||
<SelectContent ClassName="w-64">
|
||||
@foreach (var version in Versions)
|
||||
{
|
||||
var displayName = version.Identifier;
|
||||
|
||||
if (version.IsDevelopment)
|
||||
displayName += " (dev)";
|
||||
|
||||
if (version.IsPreRelease)
|
||||
displayName += " (beta)";
|
||||
|
||||
<SelectItem Value="@version.Identifier">
|
||||
@displayName
|
||||
</SelectItem>
|
||||
}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</FieldContent>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel>Bypass Build Cache</FieldLabel>
|
||||
<FieldContent>
|
||||
<Switch @bind-Value="NoBuildCache"/>
|
||||
</FieldContent>
|
||||
</Field>
|
||||
</FieldSet>
|
||||
<Field Orientation="FieldOrientation.Horizontal">
|
||||
<Button @onclick="AskApplyAsync">Apply</Button>
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card ClassName="col-span-1">
|
||||
<CardHeader>
|
||||
<CardTitle>Plugins</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Empty>
|
||||
<EmptyHeader>
|
||||
<EmptyMedia Variant="EmptyMediaVariant.Icon">
|
||||
<SearchIcon/>
|
||||
</EmptyMedia>
|
||||
<EmptyTitle>No Plugins found</EmptyTitle>
|
||||
<EmptyDescription>
|
||||
No plugins found in instance configuration
|
||||
</EmptyDescription>
|
||||
</EmptyHeader>
|
||||
</Empty>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<Empty>
|
||||
<EmptyHeader>
|
||||
<EmptyMedia Variant="EmptyMediaVariant.Icon">
|
||||
<CircleAlertIcon ClassName="text-red-500"/>
|
||||
</EmptyMedia>
|
||||
<EmptyTitle>Container Helper unreachable</EmptyTitle>
|
||||
<EmptyDescription>
|
||||
The container helper is unreachable. No management actions are available
|
||||
</EmptyDescription>
|
||||
</EmptyHeader>
|
||||
</Empty>
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
<Empty>
|
||||
<EmptyHeader>
|
||||
<EmptyMedia Variant="EmptyMediaVariant.Icon">
|
||||
<ToggleLeftIcon/>
|
||||
</EmptyMedia>
|
||||
<EmptyTitle>Container Helper is disabled</EmptyTitle>
|
||||
<EmptyDescription>
|
||||
The container helper is disabled on this instance.
|
||||
This might be due to running a multiple container moonlight setup
|
||||
</EmptyDescription>
|
||||
</EmptyHeader>
|
||||
</Empty>
|
||||
}
|
||||
</LazyLoader>
|
||||
</div>
|
||||
|
||||
@code
|
||||
{
|
||||
private string SelectedVersion = "v2.1";
|
||||
private bool NoBuildCache;
|
||||
|
||||
private ContainerHelperStatusDto StatusDto;
|
||||
private VersionDto[] Versions;
|
||||
|
||||
private async Task LoadAsync(LazyLoader _)
|
||||
{
|
||||
StatusDto = (await HttpClient.GetFromJsonAsync<ContainerHelperStatusDto>("api/admin/ch/status"))!;
|
||||
|
||||
var currentVersion = await HttpClient.GetFromJsonAsync<VersionDto>("api/admin/versions/instance");
|
||||
|
||||
if (currentVersion != null)
|
||||
SelectedVersion = currentVersion.Identifier;
|
||||
|
||||
Versions = (await HttpClient.GetFromJsonAsync<VersionDto[]>("api/admin/versions"))!;
|
||||
}
|
||||
|
||||
private async Task ApplyAsync()
|
||||
{
|
||||
await DialogService.LaunchAsync<UpdateInstanceModal>(
|
||||
parameters =>
|
||||
{
|
||||
parameters[nameof(UpdateInstanceModal.Version)] = SelectedVersion;
|
||||
parameters[nameof(UpdateInstanceModal.NoBuildCache)] = NoBuildCache;
|
||||
},
|
||||
model =>
|
||||
{
|
||||
model.ShowCloseButton = false;
|
||||
model.ClassName = "sm:max-w-4xl!";
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private async Task AskApplyAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(SelectedVersion))
|
||||
return;
|
||||
|
||||
var version = Versions.First(x => x.Identifier == SelectedVersion);
|
||||
|
||||
var shouldContinue = await ConfirmRiskyVersionAsync(
|
||||
"Moonlight Rebuild",
|
||||
"If you continue the moonlight instance will become unavailable during the rebuild process. This will impact users on this instance"
|
||||
);
|
||||
|
||||
if (!shouldContinue)
|
||||
return;
|
||||
|
||||
if (version.IsDevelopment)
|
||||
{
|
||||
shouldContinue = await ConfirmRiskyVersionAsync(
|
||||
"Development Version",
|
||||
"You are about to install development a version. This can break your instance. Continue at your own risk"
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (version.IsPreRelease)
|
||||
{
|
||||
shouldContinue = await ConfirmRiskyVersionAsync(
|
||||
"Beta / Pre-Release Version",
|
||||
"You are about to install a version marked as pre-release / beta. This can break your instance. Continue at your own risk"
|
||||
);
|
||||
}
|
||||
else
|
||||
shouldContinue = true;
|
||||
}
|
||||
|
||||
if (!shouldContinue)
|
||||
return;
|
||||
|
||||
await ApplyAsync();
|
||||
}
|
||||
|
||||
private async Task<bool> ConfirmRiskyVersionAsync(string title, string message)
|
||||
{
|
||||
var tcs = new TaskCompletionSource();
|
||||
var confirmed = false;
|
||||
|
||||
await AlertDialogService.ConfirmDangerAsync(
|
||||
title,
|
||||
message,
|
||||
() =>
|
||||
{
|
||||
confirmed = true;
|
||||
tcs.SetResult();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
);
|
||||
|
||||
await tcs.Task;
|
||||
|
||||
return confirmed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
@using System.Text.Json
|
||||
@using LucideBlazor
|
||||
@using Moonlight.Shared
|
||||
@using Moonlight.Shared.Admin.Sys.ContainerHelper
|
||||
@using ShadcnBlazor.Buttons
|
||||
@using ShadcnBlazor.Dialogs
|
||||
@using ShadcnBlazor.Progresses
|
||||
@using ShadcnBlazor.Spinners
|
||||
|
||||
@inject HttpClient HttpClient
|
||||
|
||||
@inherits ShadcnBlazor.Extras.Dialogs.DialogBase
|
||||
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
Updating instance to @Version...
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<div class="grid grid-cols-1 xl:grid-cols-2 w-full gap-5">
|
||||
<div class="text-base flex flex-col p-2 gap-y-1">
|
||||
@for (var i = 0; i < Steps.Length; i++)
|
||||
{
|
||||
if (CurrentStep == i)
|
||||
{
|
||||
<div class="flex flex-row items-center gap-x-1">
|
||||
@if (IsFailed)
|
||||
{
|
||||
<CircleXIcon ClassName="text-red-500 size-5"/>
|
||||
}
|
||||
else
|
||||
{
|
||||
<Spinner ClassName="size-5"/>
|
||||
}
|
||||
<span>
|
||||
@Steps[i]
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i < CurrentStep)
|
||||
{
|
||||
<div class="flex flex-row items-center gap-x-1 text-muted-foreground">
|
||||
<CircleCheckIcon ClassName="text-green-500 size-5"/>
|
||||
<span>
|
||||
@Steps[i]
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="text-muted-foreground flex flex-row items-center gap-x-1">
|
||||
<span class="size-5"></span>
|
||||
@Steps[i]
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
<div class="bg-black text-white rounded-lg font-mono h-96 flex flex-col-reverse overflow-auto p-3 scrollbar-thin">
|
||||
@for (var i = LogLines.Count - 1; i >= 0; i--)
|
||||
{
|
||||
<div>
|
||||
@LogLines[i]
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (CurrentStep == Steps.Length || IsFailed)
|
||||
{
|
||||
<DialogFooter ClassName="justify-end">
|
||||
<Button Variant="ButtonVariant.Outline" @onclick="CloseAsync">Close</Button>
|
||||
</DialogFooter>
|
||||
}
|
||||
else
|
||||
{
|
||||
<DialogFooter>
|
||||
<Progress ClassName="my-1" Value="@Progress"></Progress>
|
||||
</DialogFooter>
|
||||
}
|
||||
|
||||
@code
|
||||
{
|
||||
[Parameter] public string Version { get; set; }
|
||||
[Parameter] public bool NoBuildCache { get; set; }
|
||||
|
||||
private bool IsFailed;
|
||||
private int Progress;
|
||||
private int CurrentStep;
|
||||
|
||||
private readonly string[] Steps =
|
||||
[
|
||||
"Checking", // 0
|
||||
"Updating configuration files", // 1
|
||||
"Starting rebuild task", // 2
|
||||
"Building docker image", // 3
|
||||
"Redeploying container instance", // 4
|
||||
"Waiting for container instance to start up", // 5
|
||||
"Update complete" // 6
|
||||
];
|
||||
|
||||
private readonly List<string?> LogLines = new();
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (!firstRender)
|
||||
return;
|
||||
|
||||
// Checking
|
||||
CurrentStep = 0;
|
||||
Progress = 0;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
|
||||
await Task.Delay(2000);
|
||||
|
||||
// Update configuration
|
||||
CurrentStep = 1;
|
||||
Progress = 20;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
|
||||
await HttpClient.PostAsJsonAsync("api/admin/ch/version", new SetVersionDto
|
||||
{
|
||||
Version = Version
|
||||
}, SerializationContext.Default.Options);
|
||||
|
||||
// Starting rebuild task
|
||||
CurrentStep = 2;
|
||||
Progress = 30;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "api/admin/ch/rebuild");
|
||||
|
||||
request.Content = JsonContent.Create(
|
||||
new RequestRebuildDto(NoBuildCache),
|
||||
null,
|
||||
SerializationContext.Default.Options
|
||||
);
|
||||
|
||||
var response = await HttpClient.SendAsync(
|
||||
request,
|
||||
HttpCompletionOption.ResponseHeadersRead
|
||||
);
|
||||
|
||||
await using var responseStream = await response.Content.ReadAsStreamAsync();
|
||||
using var streamReader = new StreamReader(responseStream);
|
||||
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
var line = await streamReader.ReadLineAsync();
|
||||
|
||||
if (line == null)
|
||||
break;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
continue;
|
||||
|
||||
var data = line.Trim("data: ");
|
||||
var deserializedData = JsonSerializer.Deserialize<RebuildEventDto>(data, SerializationContext.Default.Options);
|
||||
|
||||
switch (deserializedData.Type)
|
||||
{
|
||||
case RebuildEventType.Log:
|
||||
LogLines.Add(deserializedData.Data);
|
||||
break;
|
||||
|
||||
case RebuildEventType.Step:
|
||||
|
||||
switch (deserializedData.Data)
|
||||
{
|
||||
case "BuildImage":
|
||||
|
||||
// Building docker image
|
||||
|
||||
CurrentStep = 3;
|
||||
Progress = 40;
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
break;
|
||||
|
||||
case "ServiceDown":
|
||||
|
||||
// Redeploying container instance
|
||||
|
||||
CurrentStep = 4;
|
||||
Progress = 60;
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case RebuildEventType.Failed:
|
||||
|
||||
IsFailed = true;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
break;
|
||||
}
|
||||
} while (true);
|
||||
|
||||
// Waiting for container instance to start up
|
||||
|
||||
CurrentStep = 5;
|
||||
Progress = 90;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
|
||||
// Wait some time for instance to shut down
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
// Ping instance until its reachable again
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
await HttpClient.GetStringAsync("api/ping");
|
||||
break;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignored
|
||||
}
|
||||
|
||||
await Task.Delay(3000);
|
||||
}
|
||||
|
||||
// Update complete
|
||||
CurrentStep = 7;
|
||||
Progress = 100;
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user