cs-codex-dist-tests/Tools/BiblioTech/Commands/AdminCommand.cs

384 lines
14 KiB
C#
Raw Normal View History

2023-10-24 13:25:45 +00:00
using BiblioTech.Options;
using CodexPlugin;
2023-10-29 08:25:50 +00:00
using Core;
using Newtonsoft.Json;
2023-10-24 12:07:15 +00:00
namespace BiblioTech.Commands
{
2023-10-24 13:25:45 +00:00
public class AdminCommand : BaseCommand
2023-10-24 12:07:15 +00:00
{
2023-10-25 09:25:27 +00:00
private readonly ClearUserAssociationCommand clearCommand = new ClearUserAssociationCommand();
private readonly ReportCommand reportCommand = new ReportCommand();
private readonly DeployListCommand deployListCommand = new DeployListCommand();
private readonly DeployUploadCommand deployUploadCommand = new DeployUploadCommand();
private readonly DeployRemoveCommand deployRemoveCommand = new DeployRemoveCommand();
private readonly WhoIsCommand whoIsCommand = new WhoIsCommand();
2023-10-29 08:25:50 +00:00
private readonly NetInfoCommand netInfoCommand;
2023-10-29 09:40:01 +00:00
private readonly DebugPeerCommand debugPeerCommand;
2023-10-29 08:25:50 +00:00
public AdminCommand(CoreInterface ci)
{
netInfoCommand = new NetInfoCommand(ci);
2023-10-29 09:40:01 +00:00
debugPeerCommand = new DebugPeerCommand(ci);
2023-10-29 08:25:50 +00:00
}
2023-10-24 13:25:45 +00:00
public override string Name => "admin";
public override string StartingMessage => "...";
public override string Description => "Admins only.";
2023-10-24 12:07:15 +00:00
2023-10-24 13:25:45 +00:00
public override CommandOption[] Options => new CommandOption[]
2023-10-24 12:07:15 +00:00
{
2023-10-24 13:25:45 +00:00
clearCommand,
reportCommand,
deployListCommand,
deployUploadCommand,
2023-10-25 09:25:27 +00:00
deployRemoveCommand,
whoIsCommand,
2023-10-29 09:40:01 +00:00
netInfoCommand,
debugPeerCommand
2023-10-24 13:25:45 +00:00
};
protected override async Task Invoke(CommandContext context)
{
if (!IsSenderAdmin(context.Command))
2023-10-24 12:07:15 +00:00
{
2023-10-25 08:54:26 +00:00
await context.Followup("You're not an admin.");
2023-10-24 13:25:45 +00:00
return;
2023-10-24 12:07:15 +00:00
}
2023-10-24 13:25:45 +00:00
if (!IsInAdminChannel(context.Command))
{
2023-10-25 08:54:26 +00:00
await context.Followup("Please use admin commands only in the admin channel.");
return;
}
2023-10-24 13:25:45 +00:00
await clearCommand.CommandHandler(context);
await reportCommand.CommandHandler(context);
await deployListCommand.CommandHandler(context);
await deployUploadCommand.CommandHandler(context);
2023-10-24 13:43:07 +00:00
await deployRemoveCommand.CommandHandler(context);
2023-10-25 09:25:27 +00:00
await whoIsCommand.CommandHandler(context);
2023-10-29 08:47:34 +00:00
await netInfoCommand.CommandHandler(context);
2023-10-29 09:40:01 +00:00
await debugPeerCommand.CommandHandler(context);
2023-10-24 12:07:15 +00:00
}
2023-10-24 13:25:45 +00:00
public class ClearUserAssociationCommand : SubCommandOption
{
2023-10-25 09:25:27 +00:00
private readonly UserOption userOption = new UserOption("User to clear Eth address for.", true);
2023-10-24 13:25:45 +00:00
public ClearUserAssociationCommand()
: base("clear", "Admin only. Clears current Eth address for a user, allowing them to set a new one.")
{
}
2023-10-24 12:07:15 +00:00
2023-10-25 09:25:27 +00:00
public override CommandOption[] Options => new[] { userOption };
2023-10-24 12:07:15 +00:00
2023-10-24 13:25:45 +00:00
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 09:25:27 +00:00
var user = userOption.GetUser(context);
if (user == null)
2023-10-24 13:25:45 +00:00
{
await context.Followup("Failed to get user ID");
2023-10-24 13:25:45 +00:00
return;
}
2023-10-25 09:25:27 +00:00
Program.UserRepo.ClearUserAssociatedAddress(user);
await context.Followup("Done.");
2023-10-24 13:25:45 +00:00
}
}
public class ReportCommand : SubCommandOption
2023-10-24 12:07:15 +00:00
{
2023-10-25 09:25:27 +00:00
private readonly UserOption userOption = new UserOption(
2023-10-24 13:25:45 +00:00
description: "User to report history for.",
isRequired: true);
public ReportCommand()
: base("report", "Admin only. Reports bot-interaction history for a user.")
{
}
2023-10-24 12:07:15 +00:00
2023-10-25 09:25:27 +00:00
public override CommandOption[] Options => new[] { userOption };
2023-10-24 13:25:45 +00:00
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 09:25:27 +00:00
var user = userOption.GetUser(context);
if (user == null)
2023-10-24 13:25:45 +00:00
{
await context.Followup("Failed to get user ID");
2023-10-24 13:25:45 +00:00
return;
}
2023-11-09 09:13:34 +00:00
var report = Program.UserRepo.GetInteractionReport(user);
2023-11-08 12:33:48 +00:00
await context.Followup(report);
2023-10-24 13:25:45 +00:00
}
}
public class DeployListCommand : SubCommandOption
2023-10-24 12:07:15 +00:00
{
2023-10-25 09:25:27 +00:00
public DeployListCommand()
2023-10-24 13:25:45 +00:00
: base("list", "Lists current deployments.")
{
}
protected override async Task onSubCommand(CommandContext context)
{
2023-10-25 09:25:27 +00:00
var deployments = Program.DeploymentFilesMonitor.GetDeployments();
2023-10-24 13:25:45 +00:00
//todo shows old deployments
2023-10-24 13:25:45 +00:00
if (!deployments.Any())
{
await context.Followup("No deployments available.");
2023-10-24 13:25:45 +00:00
return;
}
var nl = Environment.NewLine;
2023-11-09 09:13:34 +00:00
await context.Followup(deployments.Select(FormatDeployment).ToArray());
2023-10-24 13:25:45 +00:00
}
private string FormatDeployment(CodexDeployment deployment)
{
var m = deployment.Metadata;
2023-10-24 13:43:07 +00:00
return $"'{m.Name}' ({m.StartUtc.ToString("o")})";
2023-10-24 13:25:45 +00:00
}
}
public class DeployUploadCommand : SubCommandOption
{
private readonly FileAttachementOption fileOption = new FileAttachementOption(
name: "json",
description: "Codex-deployment json to add.",
isRequired: true);
2023-10-25 09:25:27 +00:00
public DeployUploadCommand()
2023-10-24 13:25:45 +00:00
: base("add", "Upload a new deployment JSON file.")
{
}
public override CommandOption[] Options => new[] { fileOption };
protected override async Task onSubCommand(CommandContext context)
{
var file = await fileOption.Parse(context);
if (file == null) return;
2023-10-25 09:25:27 +00:00
var result = await Program.DeploymentFilesMonitor.DownloadDeployment(file);
2023-10-24 13:43:07 +00:00
if (result)
{
await context.Followup("Success!");
2023-10-24 13:43:07 +00:00
}
else
{
await context.Followup("That didn't work.");
2023-10-24 13:43:07 +00:00
}
}
}
public class DeployRemoveCommand : SubCommandOption
{
private readonly StringOption stringOption = new StringOption(
2023-10-24 13:45:40 +00:00
name: "name",
2023-10-24 13:43:07 +00:00
description: "Name of deployment to remove.",
isRequired: true);
2023-10-25 09:25:27 +00:00
public DeployRemoveCommand()
2023-10-24 13:43:07 +00:00
: base("remove", "Removes a deployment file.")
{
}
2023-10-24 13:25:45 +00:00
2023-10-24 13:43:07 +00:00
public override CommandOption[] Options => new[] { stringOption };
protected override async Task onSubCommand(CommandContext context)
{
var str = await stringOption.Parse(context);
if (string.IsNullOrEmpty(str)) return;
2023-10-25 09:25:27 +00:00
var result = Program.DeploymentFilesMonitor.DeleteDeployment(str);
2023-10-24 13:43:07 +00:00
if (result)
{
await context.Followup("Success!");
2023-10-24 13:43:07 +00:00
}
else
{
await context.Followup("That didn't work.");
2023-10-24 13:43:07 +00:00
}
2023-10-24 13:25:45 +00:00
}
2023-10-24 12:07:15 +00:00
}
2023-10-25 09:25:27 +00:00
public class WhoIsCommand : SubCommandOption
{
private readonly UserOption userOption = new UserOption("User", isRequired: false);
private readonly EthAddressOption ethAddressOption = new EthAddressOption(isRequired: false);
public WhoIsCommand()
: base(name: "whois",
description: "Fetches info about a user or ethAddress in the testnet.")
{
}
public override CommandOption[] Options => new CommandOption[]
{
userOption,
ethAddressOption
};
protected override async Task onSubCommand(CommandContext context)
{
var user = userOption.GetUser(context);
var ethAddr = await ethAddressOption.Parse(context);
if (user != null)
{
await context.Followup(Program.UserRepo.GetUserReport(user));
2023-10-25 09:25:27 +00:00
}
if (ethAddr != null)
{
await context.Followup(Program.UserRepo.GetUserReport(ethAddr));
2023-10-25 09:25:27 +00:00
}
}
}
2023-10-29 08:25:50 +00:00
2023-10-29 09:40:01 +00:00
public abstract class AdminDeploymentCommand : SubCommandOption
2023-10-29 08:25:50 +00:00
{
private readonly CoreInterface ci;
2023-10-29 09:40:01 +00:00
public AdminDeploymentCommand(CoreInterface ci, string name, string description)
: base(name, description)
2023-10-29 08:25:50 +00:00
{
this.ci = ci;
}
2023-11-09 09:13:34 +00:00
protected async Task<T?> OnDeployment<T>(CommandContext context, Func<ICodexNodeGroup, string, T> action)
2023-10-29 08:25:50 +00:00
{
var deployment = Program.DeploymentFilesMonitor.GetDeployments().SingleOrDefault();
if (deployment == null)
{
await context.Followup("No deployment found.");
2023-11-09 09:13:34 +00:00
return default;
2023-10-29 08:25:50 +00:00
}
try
{
var group = ci.WrapCodexContainers(deployment.CodexInstances.Select(i => i.Containers).ToArray());
2023-11-09 09:13:34 +00:00
var result = action(group, deployment.Metadata.Name);
return result;
2023-10-29 09:40:01 +00:00
}
catch (Exception ex)
{
2023-11-09 09:13:34 +00:00
var message = new[]
{
"Failed to wrap nodes with exception: "
};
var exceptionMessage = ex.ToString().Split(Environment.NewLine);
await context.Followup(message.Concat(exceptionMessage).ToArray());
return default;
2023-10-29 09:40:01 +00:00
}
}
}
public class NetInfoCommand : AdminDeploymentCommand
{
public NetInfoCommand(CoreInterface ci)
: base(ci, name: "netinfo",
description: "Fetches info endpoints of codex nodes.")
{
}
protected override async Task onSubCommand(CommandContext context)
{
2023-11-09 09:13:34 +00:00
var report = await OnDeployment(context, CreateNetInfoReport);
if (report != null && report.Any())
2023-10-29 09:40:01 +00:00
{
2023-11-09 09:13:34 +00:00
await context.Followup(report);
}
}
private string[] CreateNetInfoReport(ICodexNodeGroup group, string name)
{
var content = new List<string>
{
$"{DateTime.UtcNow.ToString("o")} - {group.Count()} Codex nodes.",
$"Deployment name: '{name}'."
};
2023-11-09 09:13:34 +00:00
foreach (var node in group)
{
try
{
var info = node.GetDebugInfo();
var json = JsonConvert.SerializeObject(info, Formatting.Indented);
var jsonLines = json.Split(Environment.NewLine);
content.Add($"Node '{node.GetName()}' responded with:");
content.Add("---");
content.AddRange(jsonLines);
content.Add("---");
}
catch (Exception ex)
2023-10-29 08:25:50 +00:00
{
2023-11-09 09:13:34 +00:00
content.Add($"Node '{node.GetName()}' failed to respond with exception: " + ex);
2023-10-29 08:25:50 +00:00
}
2023-11-09 09:13:34 +00:00
}
2023-11-09 09:13:34 +00:00
return content.ToArray();
2023-10-29 09:40:01 +00:00
}
}
public class DebugPeerCommand : AdminDeploymentCommand
{
private readonly StringOption peerIdOption = new StringOption("peerid", "id of peer to try and reach.", true);
public DebugPeerCommand(CoreInterface ci)
: base(ci, name: "debugpeer",
description: "Calls debug/peer on each codex node.")
{
}
public override CommandOption[] Options => new[] { peerIdOption };
protected override async Task onSubCommand(CommandContext context)
{
var peerId = await peerIdOption.Parse(context);
if (string.IsNullOrEmpty(peerId)) return;
2023-11-09 09:13:34 +00:00
var report = await OnDeployment(context, (group, name) => CreateDebugPeerReport(group, name, peerId));
if (report != null && report.Any())
2023-10-29 08:25:50 +00:00
{
2023-11-09 09:13:34 +00:00
await context.Followup(report);
}
}
2023-11-08 12:33:48 +00:00
2023-11-09 09:13:34 +00:00
private string[] CreateDebugPeerReport(ICodexNodeGroup group, string name, string peerId)
{
var content = new List<string>
{
$"{DateTime.UtcNow.ToString("o")} - {group.Count()} Codex nodes.",
$"Deployment name: '{name}'.",
$"Calling debug/peer for '{peerId}'."
};
foreach (var node in group)
{
try
2023-10-29 09:40:01 +00:00
{
2023-11-09 09:13:34 +00:00
var info = node.GetDebugPeer(peerId);
var json = JsonConvert.SerializeObject(info, Formatting.Indented);
var jsonLines = json.Split(Environment.NewLine);
content.Add($"Node '{node.GetName()}' responded with:");
content.Add("---");
content.AddRange(jsonLines);
content.Add("---");
2023-10-29 09:40:01 +00:00
}
2023-11-09 09:13:34 +00:00
catch (Exception ex)
{
content.Add($"Node '{node.GetName()}' failed to respond with exception: " + ex);
}
}
2023-11-08 12:33:48 +00:00
2023-11-09 09:13:34 +00:00
return content.ToArray();
2023-10-29 08:25:50 +00:00
}
}
2023-10-24 12:07:15 +00:00
}
}