Adds netinfo command

This commit is contained in:
benbierens 2023-10-29 09:25:50 +01:00
parent d5b87253ae
commit f17d123af8
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
2 changed files with 54 additions and 1 deletions

View File

@ -1,5 +1,7 @@
using BiblioTech.Options;
using CodexPlugin;
using Core;
using Newtonsoft.Json;
namespace BiblioTech.Commands
{
@ -11,6 +13,12 @@ namespace BiblioTech.Commands
private readonly DeployUploadCommand deployUploadCommand = new DeployUploadCommand();
private readonly DeployRemoveCommand deployRemoveCommand = new DeployRemoveCommand();
private readonly WhoIsCommand whoIsCommand = new WhoIsCommand();
private readonly NetInfoCommand netInfoCommand;
public AdminCommand(CoreInterface ci)
{
netInfoCommand = new NetInfoCommand(ci);
}
public override string Name => "admin";
public override string StartingMessage => "...";
@ -24,6 +32,7 @@ namespace BiblioTech.Commands
deployUploadCommand,
deployRemoveCommand,
whoIsCommand,
netInfoCommand
};
protected override async Task Invoke(CommandContext context)
@ -221,5 +230,49 @@ namespace BiblioTech.Commands
}
}
}
public class NetInfoCommand : SubCommandOption
{
private readonly CoreInterface ci;
public NetInfoCommand(CoreInterface ci)
: base(name: "netinfo",
description: "Fetches info endpoints of codex nodes.")
{
this.ci = ci;
}
protected override async Task onSubCommand(CommandContext context)
{
var deployment = Program.DeploymentFilesMonitor.GetDeployments().SingleOrDefault();
if (deployment == null)
{
await context.AdminFollowup("No deployment found.");
return;
}
try
{
var group = ci.WrapCodexContainers(deployment.CodexInstances.Select(i => i.Container).ToArray());
foreach (var node in group)
{
try
{
var info = node.GetDebugInfo();
var json = JsonConvert.SerializeObject(info);
await context.AdminFollowup($"Node '{node.GetName()}' responded with '{json}'");
}
catch (Exception ex)
{
await context.AdminFollowup($"Node '{node.GetName()}' failed to respond with exception: " + ex);
}
}
}
catch (Exception ex)
{
await context.AdminFollowup("Failed to wrap nodes with exception: " + ex);
}
}
}
}
}

View File

@ -51,7 +51,7 @@ namespace BiblioTech
new GetBalanceCommand(ci, associateCommand),
new MintCommand(ci, associateCommand),
associateCommand,
new AdminCommand()
new AdminCommand(ci)
);
await client.LoginAsync(TokenType.Bot, Config.ApplicationToken);