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
|
|
|
|
|
2023-10-25 08:38:21 +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.");
|
2023-10-25 08:38:21 +00:00
|
|
|
|
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
|
|
|
|
{
|
2023-11-02 14:04:53 +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);
|
2023-11-02 14:04:53 +00:00
|
|
|
|
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
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup("Failed to get user ID");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 14:04:53 +00:00
|
|
|
|
var report = string.Join(Environment.NewLine, Program.UserRepo.GetInteractionReport(user));
|
|
|
|
|
if (report.Length > 1900)
|
|
|
|
|
{
|
|
|
|
|
var filename = $"user-{user.Username}.log";
|
|
|
|
|
await context.FollowupWithAttachement(filename, report);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (!deployments.Any())
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup("No deployments available.");
|
2023-10-24 13:25:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 14:04:53 +00:00
|
|
|
|
var nl = Environment.NewLine;
|
|
|
|
|
await context.Followup($"Deployments:{nl}{string.Join(nl, deployments.Select(FormatDeployment))}");
|
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)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup("Success!");
|
2023-10-24 13:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup("Success!");
|
2023-10-24 13:43:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup(Program.UserRepo.GetUserReport(user));
|
2023-10-25 09:25:27 +00:00
|
|
|
|
}
|
|
|
|
|
if (ethAddr != null)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
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-02 14:04:53 +00:00
|
|
|
|
protected async Task OnDeployment(CommandContext context, Func<ICodexNodeGroup, string, Task> action)
|
2023-10-29 08:25:50 +00:00
|
|
|
|
{
|
|
|
|
|
var deployment = Program.DeploymentFilesMonitor.GetDeployments().SingleOrDefault();
|
|
|
|
|
if (deployment == null)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup("No deployment found.");
|
2023-10-29 08:25:50 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var group = ci.WrapCodexContainers(deployment.CodexInstances.Select(i => i.Container).ToArray());
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await action(group, deployment.Metadata.Name);
|
2023-10-29 09:40:01 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup("Failed to wrap nodes with exception: " + ex);
|
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-02 14:04:53 +00:00
|
|
|
|
await OnDeployment(context, async (group, name) =>
|
2023-10-29 09:40:01 +00:00
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
var nl = Environment.NewLine;
|
|
|
|
|
var content = new List<string>
|
|
|
|
|
{
|
|
|
|
|
$"{DateTime.UtcNow.ToString("o")} - {group.Count()} Codex nodes."
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-29 08:25:50 +00:00
|
|
|
|
foreach (var node in group)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var info = node.GetDebugInfo();
|
2023-10-29 09:02:51 +00:00
|
|
|
|
var json = JsonConvert.SerializeObject(info, Formatting.Indented);
|
|
|
|
|
var jsonInsert = $"{nl}```{nl}{json}{nl}```{nl}";
|
2023-11-02 14:04:53 +00:00
|
|
|
|
content.Add($"Node '{node.GetName()}' responded with {jsonInsert}");
|
2023-10-29 08:25:50 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
content.Add($"Node '{node.GetName()}' failed to respond with exception: " + ex);
|
2023-10-29 08:25:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-02 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
var filename = $"netinfo-{NoWhitespaces(name)}.log";
|
|
|
|
|
await context.FollowupWithAttachement(filename, string.Join(nl, 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-02 14:04:53 +00:00
|
|
|
|
await OnDeployment(context, async (group, name) =>
|
2023-10-29 08:25:50 +00:00
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup($"Calling debug/peer for '{peerId}' on {group.Count()} Codex nodes.");
|
2023-10-29 09:40:01 +00:00
|
|
|
|
foreach (var node in group)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var info = node.GetDebugPeer(peerId);
|
|
|
|
|
var nl = Environment.NewLine;
|
|
|
|
|
var json = JsonConvert.SerializeObject(info, Formatting.Indented);
|
|
|
|
|
var jsonInsert = $"{nl}```{nl}{json}{nl}```{nl}";
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup($"Node '{node.GetName()}' responded with {jsonInsert}");
|
2023-10-29 09:40:01 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-11-02 14:04:53 +00:00
|
|
|
|
await context.Followup($"Node '{node.GetName()}' failed to respond with exception: " + ex);
|
2023-10-29 09:40:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-10-29 08:25:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-02 14:04:53 +00:00
|
|
|
|
|
|
|
|
|
private static string NoWhitespaces(string s)
|
|
|
|
|
{
|
|
|
|
|
return s.Replace(" ", "-");
|
|
|
|
|
}
|
2023-10-24 12:07:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|