From d8c1f81023c359c3d26960437d26dbc5b2d609ff Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Wed, 12 Jan 2022 13:03:30 +0100 Subject: [PATCH] Initial Build Multiarch docker build Unified logging (replaced some crashes with error logs) Replace all console writes with log formatted writes. Changelog: added --- MinecraftDiscordBot/ConnectedComputer.cs | 24 ++- MinecraftDiscordBot/Dockerfile | 6 +- .../MinecraftDiscordBot.csproj | 7 +- MinecraftDiscordBot/Program.cs | 175 ++++++++++++------ build.py | 34 ++++ checkcompat.py | 28 +++ 6 files changed, 202 insertions(+), 72 deletions(-) create mode 100644 build.py create mode 100644 checkcompat.py diff --git a/MinecraftDiscordBot/ConnectedComputer.cs b/MinecraftDiscordBot/ConnectedComputer.cs index 7455e84..1566b4c 100644 --- a/MinecraftDiscordBot/ConnectedComputer.cs +++ b/MinecraftDiscordBot/ConnectedComputer.cs @@ -15,12 +15,11 @@ public class ConnectedComputer { } private void OnMessage(string message) { - var msg = JsonConvert.DeserializeObject(message); - if (msg is null) throw new InvalidProgramException("Unexpected Message!"); + if (JsonConvert.DeserializeObject(message) is not ReplyMessage msg) return; IChunkWaiter? waiter; lock (_syncRoot) { if (!_waits.TryGetValue(msg.AnswerId, out waiter)) { - Console.WriteLine($"Invalid wait id '{msg.AnswerId}'!"); + Program.LogWarning("Socket", $"Invalid wait id '{msg.AnswerId}'!"); return; } } @@ -62,9 +61,16 @@ public class ConnectedComputer { public void AddChunk(int chunkId, int totalChunks, string value) { lock (_syncRoot) { if (_chunks is null) _chunks = new string[totalChunks]; - else if (_chunks.Length != totalChunks) throw new InvalidOperationException("Different numbers of chunks in same message ID!"); + else if (_chunks.Length != totalChunks) { + Program.LogError(Program.WebSocketSource, new InvalidOperationException("Different numbers of chunks in same message ID!")); + return; + } ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed - chunk = chunk is null ? value : throw new InvalidOperationException($"Chunk with ID {chunkId} was already received!"); + if (chunk is not null) { + Program.LogError(Program.WebSocketSource, new InvalidOperationException($"Chunk with ID {chunkId} was already received!")); + return; + } + chunk = value; } if (++_receivedChunks == totalChunks) FinalizeResult(_chunks); } @@ -75,13 +81,13 @@ public class ConnectedComputer { } protected int GetFreeId() { - int i = 10; - while (i-- >= 0) { + var attempts = 0; + while (true) { var id = _rnd.Next(); if (!_waits.ContainsKey(id)) return id; + Program.LogWarning(Program.WebSocketSource, $"Could not get a free ID after {++attempts} attempts!"); } - throw new InvalidOperationException("Could not get a free ID after many attempts!"); } protected ChunkWaiter GetWaiter(Func resultParser, CancellationToken ct) { @@ -285,9 +291,7 @@ public class ModItemId { if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name)); ModName = name[..colon]; ModItem = name[(colon + 1)..]; -#if DEBUG if (ToString() != name) throw new InvalidProgramException("Bad Parsing!"); -#endif } public override string ToString() => $"{ModName}:{ModItem}"; public string ModName { get; } diff --git a/MinecraftDiscordBot/Dockerfile b/MinecraftDiscordBot/Dockerfile index 28c3073..2db0ef0 100644 --- a/MinecraftDiscordBot/Dockerfile +++ b/MinecraftDiscordBot/Dockerfile @@ -3,16 +3,16 @@ FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base WORKDIR /app -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim-amd64 AS build WORKDIR /src COPY ["MinecraftDiscordBot/MinecraftDiscordBot.csproj", "MinecraftDiscordBot/"] RUN dotnet restore "MinecraftDiscordBot/MinecraftDiscordBot.csproj" COPY . . WORKDIR "/src/MinecraftDiscordBot" -RUN dotnet build "MinecraftDiscordBot.csproj" -c Release -o /app/build +RUN dotnet build "MinecraftDiscordBot.csproj" -c Release -o /app/build /p:UseAppHost=false FROM build AS publish -RUN dotnet publish "MinecraftDiscordBot.csproj" -c Release -o /app/publish +RUN dotnet publish "MinecraftDiscordBot.csproj" -c Release -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app diff --git a/MinecraftDiscordBot/MinecraftDiscordBot.csproj b/MinecraftDiscordBot/MinecraftDiscordBot.csproj index 4c0a87b..1ecb7a8 100644 --- a/MinecraftDiscordBot/MinecraftDiscordBot.csproj +++ b/MinecraftDiscordBot/MinecraftDiscordBot.csproj @@ -1,4 +1,4 @@ - + Exe @@ -9,10 +9,15 @@ + + + + + diff --git a/MinecraftDiscordBot/Program.cs b/MinecraftDiscordBot/Program.cs index a4ccd29..226db04 100644 --- a/MinecraftDiscordBot/Program.cs +++ b/MinecraftDiscordBot/Program.cs @@ -6,103 +6,138 @@ using Fleck; using Newtonsoft.Json; using System.Collections.Concurrent; using System.Reflection; +using System.Runtime.CompilerServices; namespace MinecraftDiscordBot; -public class Program { - private const string WebSocketSource = "WebSocket"; - private readonly object _logLock = new(); +public class Program : IDisposable { + public const string WebSocketSource = "WebSocket"; + public const string BotSource = "Bot"; + private static readonly object LogLock = new(); private readonly DiscordSocketClient _client = new(new() { - LogLevel = LogSeverity.Verbose + LogLevel = LogSeverity.Verbose, + GatewayIntents = GatewayIntents.AllUnprivileged & ~(GatewayIntents.GuildScheduledEvents | GatewayIntents.GuildInvites) }); private readonly WebSocketServer _wssv; private readonly BotConfiguration _config; private readonly HashSet _whitelistedChannels; private readonly ConcurrentDictionary _connections = new(); private static readonly char[] WhiteSpace = new char[] { '\t', '\n', ' ', '\r' }; + public ITextChannel[] _channels = Array.Empty(); private RefinedStorageComputer? _rsSystem = null; + private bool disposedValue; + public RefinedStorageComputer? RsSystem { + get => _rsSystem; set { + if (_rsSystem != value) { + _rsSystem = value; + _ = Task.Run(() => Broadcast(i => i.SendMessageAsync(value is null + ? $"The Refined Storage went offline. Please check the server!" + : $"The Refined Storage is back online!"))); + } + } + } + + private async Task Broadcast(Func> message) => _ = await Task.WhenAll(_channels.Select(message)); public Program(BotConfiguration config) { _config = config; - _client.Log += Log; + _client.Log += LogAsync; _client.MessageReceived += (msg) => DiscordMessageReceived(msg); _wssv = new WebSocketServer($"ws://0.0.0.0:{config.Port}") { RestartAfterListenError = true }; + FleckLog.LogAction = LogWebSocket; _whitelistedChannels = config.Channels.ToHashSet(); } + private void LogWebSocket(LogLevel level, string message, Exception exception) => Log(new(level switch { + LogLevel.Debug => LogSeverity.Debug, + LogLevel.Info => LogSeverity.Info, + LogLevel.Warn => LogSeverity.Warning, + LogLevel.Error => LogSeverity.Error, + _ => LogSeverity.Critical // Unknown logging states should behave critical + }, WebSocketSource, message, exception)); + + public static Task Main(string[] args) => JsonConvert.DeserializeObject(File.ReadAllText("config.json")) is BotConfiguration config ? new Program(config).RunAsync() : throw new InvalidProgramException("Configuration file missing!"); public async Task RunAsync() { - _wssv.Start(socket => { - socket.OnOpen = async () => await SocketOpened(socket); - socket.OnClose = async () => await SocketClosed(socket); - socket.OnMessage = async message => await SocketReceived(socket, message); - }); await _client.LoginAsync(TokenType.Bot, _config.Token); await _client.StartAsync(); -#if !DEBUG await VerifyTextChannels(); -#endif + StartWebSocketServer(); // Block this task until the program is closed. await Task.Delay(-1); return 0; } - private async Task VerifyTextChannels() { - var channels = await Task.WhenAll(_whitelistedChannels.Select(id => _client.GetChannelAsync(id).AsTask()).ToArray()); - await Task.WhenAll(channels.Where(i => i is ITextChannel { Guild: RestGuild }).Select(i => ((RestGuild)((ITextChannel)i).Guild).UpdateAsync())); - foreach (var channel in channels) { - if (channel is ITextChannel tchannel) Console.WriteLine($"Whitelisted in channel: {channel.Name} [{channel.Id}] on server {tchannel.Guild.Name} [{tchannel.Guild.Id}]"); - else throw new InvalidProgramException($"Cannot use this bot on non-text channel {channel.Name} [{channel.Id}]!"); + private void StartWebSocketServer() => _wssv.Start(socket => { + socket.OnOpen = async () => await SocketOpened(socket); + socket.OnClose = async () => await SocketClosed(socket); + socket.OnMessage = async message => await SocketReceived(socket, message); + }); + private async IAsyncEnumerable GetValidChannels(IEnumerable ids) { + foreach (var channelId in ids) { + var channel = await _client.GetChannelAsync(channelId); + if (channel is not ITextChannel textChannel) { + if (channel is null) await LogWarning(BotSource, $"Channel with id [{channelId}] does not exist!"); + else await LogWarning(BotSource, $"Channel is not a text channels and will not be used: {channel.Name} [{channel.Id}]!"); + continue; + } + + if (textChannel.Guild is RestGuild guild) { + await guild.UpdateAsync(); + await LogInfo(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on server {guild.Name} [{guild.Id}]"); + } else { + await LogWarning(BotSource, $"Whitelisted in channel: {channel.Name} [{channel.Id}] on unknown server!"); + } + yield return textChannel; } } - private async Task SocketReceived(IWebSocketConnection socket, string message) { - var capability = JsonConvert.DeserializeObject(message); + private async Task VerifyTextChannels() => _channels = await GetValidChannels(_whitelistedChannels).ToArrayAsync(); - if (capability is null) return; - var pc = capability.Role switch { - RefinedStorageComputer.Role => new RefinedStorageComputer(socket), - string role => throw new ArgumentException($"Invalid role '{role}'!") - }; - AddComputerSocket(socket, pc); - await Log(new LogMessage(LogSeverity.Info, WebSocketSource, $"[{socket.ConnectionInfo.Id}] Presented capability as {pc.GetType().Name}")).ConfigureAwait(false); + private async Task SocketReceived(IWebSocketConnection socket, string message) { + if (JsonConvert.DeserializeObject(message) is not CapabilityMessage capability) return; + + try { + var pc = capability.Role switch { + RefinedStorageComputer.Role => new RefinedStorageComputer(socket), + string role => throw new ArgumentException($"Invalid role '{role}'!") + }; + AddComputerSocket(socket, pc); + await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Presented capability as {pc.GetType().Name}"); + } catch (ArgumentException e) { + await LogError(WebSocketSource, e); + } } - private void AddComputerSocket(IWebSocketConnection socket, RefinedStorageComputer pc) { - _connections[socket.ConnectionInfo.Id] = pc; - if (pc is not null) _rsSystem = pc; + private void AddComputerSocket(IWebSocketConnection socket, ConnectedComputer pc) { + if (pc is RefinedStorageComputer rs) RsSystem = rs; } private void RemoveComputerSocket(IWebSocketConnection socket) { - if (!_connections.TryRemove(socket.ConnectionInfo.Id, out _)) - throw new InvalidProgramException("Could not remove non-existing client!"); - if (_rsSystem?.ConnectionInfo.Id == socket.ConnectionInfo.Id) _rsSystem = null; + if (RsSystem?.ConnectionInfo.Id == socket.ConnectionInfo.Id) RsSystem = null; } private async Task SocketClosed(IWebSocketConnection socket) { RemoveComputerSocket(socket); - await Log(new LogMessage(LogSeverity.Info, WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!")).ConfigureAwait(false); + await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client disconnected!"); } - private async Task SocketOpened(IWebSocketConnection socket) => await Log(new LogMessage(LogSeverity.Info, WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!")).ConfigureAwait(false); + private static async Task SocketOpened(IWebSocketConnection socket) + => await LogInfo(WebSocketSource, $"[{socket.ConnectionInfo.Id}] Client connected from {socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}!"); private async Task DiscordMessageReceived(SocketMessage arg, int timeout = 10000) { if (arg is not SocketUserMessage message) return; if (message.Author.IsBot) return; if (!IsChannelWhitelisted(arg.Channel)) return; - var cts = new CancellationTokenSource(timeout -#if DEBUG - * 1000 -#endif - ); + var cts = new CancellationTokenSource(timeout); if (IsCommand(message, out var argPos)) { var parameters = message.Content[argPos..].Split(WhiteSpace, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); @@ -110,8 +145,8 @@ public class Program { return; } - await Log(new LogMessage(LogSeverity.Info, "Discord", $"[{arg.Author.Username}] {arg.Content}")).ConfigureAwait(false); - await SendToAll(JsonConvert.SerializeObject(new TextMessage(arg))); + await LogInfo("Discord", $"[{arg.Author.Username}] {arg.Content}"); + // TODO: Relay Message to Chat Receiver } private Task HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) @@ -123,9 +158,9 @@ public class Program { : message.ReplyAsync($"You really think an empty command works?"); private Task HandleRefinedStorageCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) - => _rsSystem is null - ? message.ReplyAsync("The Refined Storage system is currently unavailable!") - : _rsSystem.HandleCommand(message, parameters, ct); + => RsSystem is RefinedStorageComputer rs + ? rs.HandleCommand(message, parameters, ct) + : message.ReplyAsync("The Refined Storage system is currently unavailable!"); private bool IsCommand(SocketUserMessage message, out int argPos) { argPos = 0; @@ -134,20 +169,44 @@ public class Program { private bool IsChannelWhitelisted(ISocketMessageChannel channel) => _whitelistedChannels.Contains(channel.Id); - private async Task SendToAll(string message) { - async Task SendToClient(KeyValuePair cp) { - try { - await cp.Value.Send(message); - } catch (Exception e) { - await Log(new LogMessage(LogSeverity.Warning, WebSocketSource, $"[{cp.Key}] Sending message failed!", e)).ConfigureAwait(false); - } - } - await Task.WhenAll(_connections.Select(SendToClient).ToArray()); - } + public static ConfiguredTaskAwaitable LogInfo(string source, string message) => LogAsync(new(LogSeverity.Info, source, message)).ConfigureAwait(false); + public static ConfiguredTaskAwaitable LogWarning(string source, string message) => LogAsync(new(LogSeverity.Warning, source, message)).ConfigureAwait(false); + public static ConfiguredTaskAwaitable LogError(string source, Exception exception) => LogAsync(new(LogSeverity.Error, source, exception?.Message, exception)).ConfigureAwait(false); - private async Task Log(LogMessage msg) { - lock (_logLock) - Console.WriteLine(msg.ToString()); + private static async Task LogAsync(LogMessage msg) { + Log(msg); await Task.CompletedTask; } + + private static void Log(LogMessage msg) { + lock (LogLock) + Console.WriteLine(msg.ToString()); + } + + protected virtual void Dispose(bool disposing) { + if (!disposedValue) { + if (disposing) { + // TODO: dispose managed state (managed objects) + _wssv.Dispose(); + _client.Dispose(); + } + + // TODO: free unmanaged resources (unmanaged objects) and override finalizer + // TODO: set large fields to null + disposedValue = true; + } + } + + // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources + // ~Program() + // { + // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + // Dispose(disposing: false); + // } + + public void Dispose() { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + Dispose(disposing: true); + GC.SuppressFinalize(this); + } } diff --git a/build.py b/build.py new file mode 100644 index 0000000..e382fa7 --- /dev/null +++ b/build.py @@ -0,0 +1,34 @@ +#!python +import subprocess +import argparse +from itertools import chain + +dockercmd = 'docker' + +parser = argparse.ArgumentParser(description='Create custom recumock images.') +parser.add_argument('tags', metavar='TAG', nargs='+', help='Version tags to build.') + +args = parser.parse_args() + +platforms = ['linux/amd64', 'linux/arm64', 'linux/arm/v7'] + +def pull(image): + subprocess.run([dockercmd, 'pull', baseimage], check=True) + +def build(image, directory, platforms, build_args = None): + if build_args is None: + build_args = [] + build_args = list(chain.from_iterable(['--build-arg', f'{arg}={val}'] for (arg, val) in build_args)) + platformlist = ','.join(platforms) + subprocess.run([dockercmd, 'buildx', 'build', '-f', 'MinecraftDiscordBot/Dockerfile', '--platform', platformlist, '-t', image] + build_args + ['--push', directory], check=True) + + +for tag in args.tags: + targetimage = f'chenio/mcdiscordbot:{tag}' + baseimage = f'mcr.microsoft.com/dotnet/runtime:6.0' + + #print(f'Pulling base image {baseimage}') + #pull(baseimage) + print(f'Building image {targetimage} from {baseimage}.') + build(targetimage, '.', platforms, [('TAG', tag)]) + \ No newline at end of file diff --git a/checkcompat.py b/checkcompat.py new file mode 100644 index 0000000..00f5f93 --- /dev/null +++ b/checkcompat.py @@ -0,0 +1,28 @@ +#!python +import subprocess +import argparse + +parser = argparse.ArgumentParser(description='Check platform compatibility with Python.') +parser.add_argument('tag', metavar='TAG', help='Version tag to build.') + +args = parser.parse_args() +tag = args.tag +sourcetag = tag +baseimage = f'mcr.microsoft.com/dotnet/runtime:6.0' +targetimage = f'chenio/mcdiscordbot:{tag}' + +platforms = ['linux/amd64', 'linux/arm64', 'linux/riscv64', 'linux/ppc64le', 'linux/s390x', 'linux/386', 'linux/mips64le', 'linux/mips64', 'linux/arm/v7', 'linux/arm/v6'] + +compatible_archs = [] + +print(f'Pulling base image {baseimage}') +subprocess.run(['docker', 'pull', baseimage], check=True) +for platform in platforms: + print(f'Try building image {targetimage} for architecture {platform}.') + proc = subprocess.run(['docker', 'buildx', 'build', '-f', 'MinecraftDiscordBot/Dockerfile', '--platform', platform, '-t', targetimage, '.']) + if proc.returncode == 0: + compatible_archs.append(platform) + +print(f'Successful platforms for {baseimage}:') +for platform in compatible_archs: + print(f'\t- {platform}') \ No newline at end of file