Implemented "roundtrip" status checking. Added node ssl field.
This commit is contained in:
@@ -16,6 +16,7 @@ public class Node
|
||||
public string Token { get; set; }
|
||||
public int HttpPort { get; set; }
|
||||
public int FtpPort { get; set; }
|
||||
public bool UseSsl { get; set; }
|
||||
|
||||
// Misc
|
||||
public bool EnableTransparentMode { get; set; }
|
||||
|
||||
437
MoonlightServers.ApiServer/Database/Migrations/20241213181416_AddedNodeSslField.Designer.cs
generated
Normal file
437
MoonlightServers.ApiServer/Database/Migrations/20241213181416_AddedNodeSslField.Designer.cs
generated
Normal file
@@ -0,0 +1,437 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using MoonlightServers.ApiServer.Database;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MoonlightServers.ApiServer.Database.Migrations
|
||||
{
|
||||
[DbContext(typeof(ServersDataContext))]
|
||||
[Migration("20241213181416_AddedNodeSslField")]
|
||||
partial class AddedNodeSslField
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasDefaultSchema("Servers")
|
||||
.HasAnnotation("ProductVersion", "8.0.11")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||
|
||||
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Allocation", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("IpAddress")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("NodeId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Port")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int?>("ServerId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("NodeId");
|
||||
|
||||
b.HasIndex("ServerId");
|
||||
|
||||
b.ToTable("Allocations", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Node", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<bool>("EnableDynamicFirewall")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<bool>("EnableTransparentMode")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("Fqdn")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("FtpPort")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("HttpPort")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Token")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<bool>("UseSsl")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Nodes", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Server", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Bandwidth")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Cpu")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Disk")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("DockerImageIndex")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Memory")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("NodeId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("OwnerId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("StarId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("StartupOverride")
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<bool>("UseVirtualDisk")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("NodeId");
|
||||
|
||||
b.HasIndex("StarId");
|
||||
|
||||
b.ToTable("Servers", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerBackup", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<bool>("Completed")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<DateTime>("CompletedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
b.Property<long>("Size")
|
||||
.HasColumnType("bigint");
|
||||
|
||||
b.Property<bool>("Successful")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("ServerBackups", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerVariable", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Key")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("ServerId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ServerId");
|
||||
|
||||
b.ToTable("ServerVariables", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Star", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<bool>("AllowDockerImageChange")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("Author")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("DefaultDockerImage")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("DonateUrl")
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("InstallDockerImage")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("InstallScript")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("InstallShell")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("OnlineDetection")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("ParseConfiguration")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("RequiredAllocations")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("StartupCommand")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("StopCommand")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("UpdateUrl")
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Version")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Stars", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarDockerImage", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<bool>("AutoPulling")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("DisplayName")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Identifier")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("StarId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("StarId");
|
||||
|
||||
b.ToTable("StarDockerImages", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarVariable", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<bool>("AllowEditing")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<bool>("AllowViewing")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.Property<string>("DefaultValue")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Filter")
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Key")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<int>("StarId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Type")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("StarId");
|
||||
|
||||
b.ToTable("StarVariables", "Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Allocation", b =>
|
||||
{
|
||||
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Node", "Node")
|
||||
.WithMany("Allocations")
|
||||
.HasForeignKey("NodeId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Server", "Server")
|
||||
.WithMany("Allocations")
|
||||
.HasForeignKey("ServerId");
|
||||
|
||||
b.Navigation("Node");
|
||||
|
||||
b.Navigation("Server");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Server", b =>
|
||||
{
|
||||
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Node", "Node")
|
||||
.WithMany("Servers")
|
||||
.HasForeignKey("NodeId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Star", "Star")
|
||||
.WithMany()
|
||||
.HasForeignKey("StarId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Node");
|
||||
|
||||
b.Navigation("Star");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.ServerVariable", b =>
|
||||
{
|
||||
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Server", "Server")
|
||||
.WithMany()
|
||||
.HasForeignKey("ServerId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Server");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarDockerImage", b =>
|
||||
{
|
||||
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Star", "Star")
|
||||
.WithMany("DockerImages")
|
||||
.HasForeignKey("StarId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Star");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.StarVariable", b =>
|
||||
{
|
||||
b.HasOne("MoonlightServers.ApiServer.Database.Entities.Star", "Star")
|
||||
.WithMany("Variables")
|
||||
.HasForeignKey("StarId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Star");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Node", b =>
|
||||
{
|
||||
b.Navigation("Allocations");
|
||||
|
||||
b.Navigation("Servers");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Server", b =>
|
||||
{
|
||||
b.Navigation("Allocations");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MoonlightServers.ApiServer.Database.Entities.Star", b =>
|
||||
{
|
||||
b.Navigation("DockerImages");
|
||||
|
||||
b.Navigation("Variables");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace MoonlightServers.ApiServer.Database.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddedNodeSslField : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "UseSsl",
|
||||
schema: "Servers",
|
||||
table: "Nodes",
|
||||
type: "tinyint(1)",
|
||||
nullable: false,
|
||||
defaultValue: false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "UseSsl",
|
||||
schema: "Servers",
|
||||
table: "Nodes");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,9 @@ namespace MoonlightServers.ApiServer.Database.Migrations
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<bool>("UseSsl")
|
||||
.HasColumnType("tinyint(1)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Nodes", "Servers");
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Helpers;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.ApiServer.Services;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.Nodes.Sys;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Admin.Nodes;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/servers/nodes")]
|
||||
public class NodeSystemController : Controller
|
||||
{
|
||||
private readonly DatabaseRepository<Node> NodeRepository;
|
||||
private readonly NodeService NodeService;
|
||||
|
||||
public NodeSystemController(DatabaseRepository<Node> nodeRepository, NodeService nodeService)
|
||||
{
|
||||
NodeRepository = nodeRepository;
|
||||
NodeService = nodeService;
|
||||
}
|
||||
|
||||
[HttpGet("{nodeId}/system/status")]
|
||||
public async Task<NodeSystemStatusResponse> GetStatus([FromRoute] int nodeId)
|
||||
{
|
||||
var node = GetNode(nodeId);
|
||||
|
||||
NodeSystemStatusResponse response;
|
||||
|
||||
var sw = new Stopwatch();
|
||||
sw.Start();
|
||||
|
||||
try
|
||||
{
|
||||
var statusResponse = await NodeService.GetSystemStatus(node);
|
||||
|
||||
sw.Stop();
|
||||
|
||||
response = new()
|
||||
{
|
||||
Version = statusResponse.Version,
|
||||
RoundtripError = statusResponse.TripError,
|
||||
RoundtripSuccess = statusResponse.TripSuccess,
|
||||
RoundtripTime = statusResponse.TripTime + sw.Elapsed,
|
||||
RoundtripRemoteFailure = !statusResponse.TripSuccess // When the remote trip failed, it's the remotes fault
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
sw.Stop();
|
||||
|
||||
response = new()
|
||||
{
|
||||
Version = "Unknown",
|
||||
RoundtripError = e.Message,
|
||||
RoundtripSuccess = false,
|
||||
RoundtripTime = sw.Elapsed,
|
||||
RoundtripRemoteFailure = false
|
||||
};
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private Node GetNode(int nodeId)
|
||||
{
|
||||
var result = NodeRepository
|
||||
.Get()
|
||||
.FirstOrDefault(x => x.Id == nodeId);
|
||||
|
||||
if (result == null)
|
||||
throw new HttpApiException("A node with this id could not be found", 404);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Http.Controllers.Remote.Node;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/servers/remote/node")]
|
||||
public class NodeTripController : Controller
|
||||
{
|
||||
[HttpGet("trip")]
|
||||
public Task Get() => Task.CompletedTask;
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MoonlightServers.DaemonShared\MoonlightServers.DaemonShared.csproj" />
|
||||
<ProjectReference Include="..\MoonlightServers.Frontend\MoonlightServers.Frontend.csproj"/>
|
||||
<ProjectReference Include="..\MoonlightServers.Shared\MoonlightServers.Shared.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
52
MoonlightServers.ApiServer/Services/NodeService.cs
Normal file
52
MoonlightServers.ApiServer/Services/NodeService.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using MoonCore.Attributes;
|
||||
using MoonCore.Helpers;
|
||||
using MoonlightServers.ApiServer.Database.Entities;
|
||||
using MoonlightServers.DaemonShared.Http.Responses.Sys;
|
||||
|
||||
namespace MoonlightServers.ApiServer.Services;
|
||||
|
||||
[Singleton]
|
||||
public class NodeService
|
||||
{
|
||||
public async Task<HttpApiClient> CreateApiClient(Node node)
|
||||
{
|
||||
string url = "";
|
||||
|
||||
if (node.UseSsl)
|
||||
url += "https://";
|
||||
else
|
||||
url += "http://";
|
||||
|
||||
url += $"{node.Fqdn}:{node.HttpPort}/";
|
||||
|
||||
var httpClient = new HttpClient()
|
||||
{
|
||||
BaseAddress = new Uri(url)
|
||||
};
|
||||
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {node.Token}");
|
||||
|
||||
return new HttpApiClient(httpClient);
|
||||
}
|
||||
|
||||
public async Task<SystemInfoResponse> GetSystemInfo(Node node)
|
||||
{
|
||||
using var apiClient = await CreateApiClient(node);
|
||||
|
||||
return await apiClient.GetJson<SystemInfoResponse>("api/system/info");
|
||||
}
|
||||
|
||||
public async Task<SystemStatusResponse> GetSystemStatus(Node node)
|
||||
{
|
||||
using var apiClient = await CreateApiClient(node);
|
||||
|
||||
return await apiClient.GetJson<SystemStatusResponse>("api/system/status");
|
||||
}
|
||||
|
||||
public async Task<SystemDataUsageResponse> GetSystemDataUsage(Node node)
|
||||
{
|
||||
using var apiClient = await CreateApiClient(node);
|
||||
|
||||
return await apiClient.GetJson<SystemDataUsageResponse>("api/system/dataUsage");
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,12 @@ public class AppConfiguration
|
||||
public DockerData Docker { get; set; } = new();
|
||||
public StorageData Storage { get; set; } = new();
|
||||
public SecurityData Security { get; set; } = new();
|
||||
public RemoteData Remote { get; set; } = new();
|
||||
|
||||
public class RemoteData
|
||||
{
|
||||
public string Url { get; set; }
|
||||
}
|
||||
|
||||
public class DockerData
|
||||
{
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MoonlightServers.Daemon.Services;
|
||||
using MoonlightServers.DaemonShared.Http.Responses.Sys;
|
||||
|
||||
namespace MoonlightServers.Daemon.Http.Controllers.Sys;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/system/status")]
|
||||
public class SystemStatusController : Controller
|
||||
{
|
||||
private readonly RemoteService RemoteService;
|
||||
|
||||
public SystemStatusController(RemoteService remoteService)
|
||||
{
|
||||
RemoteService = remoteService;
|
||||
}
|
||||
|
||||
public async Task<SystemStatusResponse> Get()
|
||||
{
|
||||
SystemStatusResponse response;
|
||||
|
||||
var sw = new Stopwatch();
|
||||
sw.Start();
|
||||
|
||||
try
|
||||
{
|
||||
await RemoteService.GetStatus();
|
||||
|
||||
sw.Stop();
|
||||
|
||||
response = new()
|
||||
{
|
||||
TripSuccess = true,
|
||||
TripTime = sw.Elapsed,
|
||||
Version = "2.1.0" // TODO: Set global
|
||||
};
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
sw.Stop();
|
||||
|
||||
response = new()
|
||||
{
|
||||
TripError = e.Message,
|
||||
TripTime = sw.Elapsed,
|
||||
TripSuccess = false,
|
||||
Version = "2.1.0" // TODO: Set global
|
||||
};
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
40
MoonlightServers.Daemon/Services/RemoteService.cs
Normal file
40
MoonlightServers.Daemon/Services/RemoteService.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using MoonCore.Attributes;
|
||||
using MoonCore.Helpers;
|
||||
using MoonlightServers.Daemon.Configuration;
|
||||
|
||||
namespace MoonlightServers.Daemon.Services;
|
||||
|
||||
[Singleton]
|
||||
public class RemoteService
|
||||
{
|
||||
private readonly AppConfiguration Configuration;
|
||||
|
||||
public RemoteService(AppConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public Task<HttpApiClient> CreateHttpClient()
|
||||
{
|
||||
var formattedUrl = Configuration.Remote.Url.EndsWith('/')
|
||||
? Configuration.Remote.Url
|
||||
: Configuration.Remote.Url + "/";
|
||||
|
||||
var httpClient = new HttpClient()
|
||||
{
|
||||
BaseAddress = new Uri(formattedUrl)
|
||||
};
|
||||
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Configuration.Security.Token}");
|
||||
|
||||
var apiClient = new HttpApiClient(httpClient);
|
||||
|
||||
return Task.FromResult(apiClient);
|
||||
}
|
||||
|
||||
public async Task GetStatus()
|
||||
{
|
||||
using var apiClient = await CreateHttpClient();
|
||||
await apiClient.Get("api/servers/remote/node/trip");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace MoonlightServers.DaemonShared.Http.Responses.Sys;
|
||||
|
||||
public class SystemStatusResponse
|
||||
{
|
||||
public bool TripSuccess { get; set; }
|
||||
public TimeSpan TripTime { get; set; }
|
||||
public string? TripError { get; set; }
|
||||
public string Version { get; set; }
|
||||
}
|
||||
21
MoonlightServers.Frontend/Services/NodeService.cs
Normal file
21
MoonlightServers.Frontend/Services/NodeService.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using MoonCore.Attributes;
|
||||
using MoonCore.Helpers;
|
||||
using MoonlightServers.Shared.Http.Responses.Admin.Nodes.Sys;
|
||||
|
||||
namespace MoonlightServers.Frontend.Services;
|
||||
|
||||
[Scoped]
|
||||
public class NodeService
|
||||
{
|
||||
private readonly HttpApiClient HttpApiClient;
|
||||
|
||||
public NodeService(HttpApiClient httpApiClient)
|
||||
{
|
||||
HttpApiClient = httpApiClient;
|
||||
}
|
||||
|
||||
public async Task<NodeSystemStatusResponse> GetSystemStatus(int nodeId)
|
||||
{
|
||||
return await HttpApiClient.GetJson<NodeSystemStatusResponse>($"api/admin/servers/nodes/{nodeId}/system/status");
|
||||
}
|
||||
}
|
||||
20
MoonlightServers.Frontend/Startup/PluginStartup.cs
Normal file
20
MoonlightServers.Frontend/Startup/PluginStartup.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
using MoonCore.Extensions;
|
||||
using Moonlight.Client.Interfaces;
|
||||
|
||||
namespace MoonlightServers.Frontend.Startup;
|
||||
|
||||
public class PluginStartup : IAppStartup
|
||||
{
|
||||
public Task BuildApp(WebAssemblyHostBuilder builder)
|
||||
{
|
||||
builder.Services.AutoAddServices<PluginStartup>();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task ConfigureApp(WebAssemblyHost app)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,19 @@
|
||||
@page "/admin/servers/nodes"
|
||||
|
||||
@using System.Diagnostics
|
||||
@using MoonCore.Blazor.Tailwind.Alerts
|
||||
@using MoonCore.Blazor.Tailwind.MinimalCrud
|
||||
@using MoonCore.Helpers
|
||||
@using MoonCore.Models
|
||||
@using MoonCore.Blazor.Tailwind.DataTable
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes
|
||||
@using MoonCore.Blazor.Tailwind.Components
|
||||
@using MoonlightServers.Frontend.Services
|
||||
@using MoonlightServers.Shared.Http.Responses.Admin.Nodes.Sys
|
||||
|
||||
@inject HttpApiClient ApiClient
|
||||
@inject NodeService NodeService
|
||||
@inject AlertService AlertService
|
||||
|
||||
<div class="mb-3">
|
||||
<NavTabs Index="2" Names="@UiConstants.AdminNavNames" Links="@UiConstants.AdminNavLinks"/>
|
||||
@@ -20,10 +26,56 @@
|
||||
<DataColumn TItem="NodeDetailResponse" Field="@(x => x.Fqdn)" Title="Fqdn"/>
|
||||
<DataColumn TItem="NodeDetailResponse" Field="@(x => x.Fqdn)" Title="Status">
|
||||
<Template>
|
||||
<LazyLoader Load="_ => LoadNodeStatus(context.Id)">
|
||||
@{
|
||||
bool isFetched;
|
||||
NodeSystemStatusResponse? data;
|
||||
|
||||
lock (Responses)
|
||||
isFetched = Responses.TryGetValue(context.Id, out data);
|
||||
}
|
||||
|
||||
@if (isFetched)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
<span class="text-danger-500">
|
||||
<i class="icon-server-offg text-base me-1 align-middle"></i>
|
||||
<span class="align-middle">
|
||||
API Error
|
||||
</span>
|
||||
</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.RoundtripSuccess)
|
||||
{
|
||||
<span class="text-success-500">
|
||||
<i class="icon-check text-base me-1 align-middle"></i>
|
||||
<span class="align-middle">Online (v.2.1.0)</span>
|
||||
<span class="align-middle">Online (@(data.Version))</span>
|
||||
</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="text-danger-500">
|
||||
<i class="icon-server-off text-base me-1 align-middle"></i>
|
||||
<span class="align-middle">
|
||||
Error
|
||||
<a @onclick="() => ShowErrorDetails(context.Id)" @onclick:preventDefault
|
||||
href="#" class="ms-1 text-gray-600">Details</a>
|
||||
</span>
|
||||
</span>
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="text-gray-500">
|
||||
<i class="icon-loader text-base me-1 align-middle"></i>
|
||||
<span class="align-middle">Loading</span>
|
||||
</span>
|
||||
}
|
||||
</LazyLoader>
|
||||
</Template>
|
||||
</DataColumn>
|
||||
<DataColumn TItem="NodeDetailResponse"
|
||||
@@ -63,10 +115,49 @@
|
||||
</MinimalCrud>
|
||||
|
||||
@code
|
||||
|
||||
|
||||
|
||||
{
|
||||
private Dictionary<int, NodeSystemStatusResponse?> Responses = new();
|
||||
|
||||
private Task LoadNodeStatus(int node)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var status = await NodeService.GetSystemStatus(node);
|
||||
|
||||
lock (Responses)
|
||||
Responses[node] = status;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
lock (Responses)
|
||||
Responses[node] = null;
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task ShowErrorDetails(int id)
|
||||
{
|
||||
NodeSystemStatusResponse? data;
|
||||
|
||||
lock (Responses)
|
||||
data = Responses.GetValueOrDefault(id);
|
||||
|
||||
if (data == null)
|
||||
return;
|
||||
|
||||
var message = $"Failed after {Math.Round(data.RoundtripTime.TotalSeconds, 2)} seconds: " +
|
||||
(data.RoundtripRemoteFailure ? "(Failed at node)" : "(Failed at api server)") +
|
||||
$" {data.RoundtripError}";
|
||||
|
||||
await AlertService.Danger("Node error details", message);
|
||||
}
|
||||
|
||||
private void OnConfigure(MinimalCrudOptions<NodeDetailResponse> options)
|
||||
{
|
||||
options.Title = "Nodes";
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MoonlightServers.Shared.Http.Responses.Admin.Nodes.Sys;
|
||||
|
||||
public class NodeSystemStatusResponse
|
||||
{
|
||||
public bool RoundtripSuccess { get; set; }
|
||||
public bool RoundtripRemoteFailure { get; set; }
|
||||
public TimeSpan RoundtripTime { get; set; }
|
||||
public string? RoundtripError { get; set; }
|
||||
public string Version { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user