-
Notifications
You must be signed in to change notification settings - Fork 45
/
Sudo.Dotnet.cs
105 lines (95 loc) · 4.48 KB
/
Sudo.Dotnet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CompatBot.Database.Providers;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
namespace CompatBot.Commands;
internal partial class Sudo
{
[Group("dotnet")]
[Description("Commands to manage dotnet")]
public sealed partial class Dotnet : BaseCommandModuleCustom
{
[GeneratedRegex(@"\.NET( Core)? (?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(-.+)?", RegexOptions.ExplicitCapture | RegexOptions.Singleline)]
private static partial Regex DotnetVersionPattern();
[Command("update"), Aliases("upgrade")]
[Description("Updates dotnet, and then restarts the bot")]
public async Task Update(CommandContext ctx, [Description("Dotnet SDK version (e.g. `5.1`)")] string version = "")
{
if (await LockObj.WaitAsync(0).ConfigureAwait(false))
{
DiscordMessage? msg = null;
try
{
Config.Log.Info("Checking for available dotnet updates...");
msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Checking for dotnet updates...").ConfigureAwait(false);
var (updated, stdout) = await UpdateAsync(version).ConfigureAwait(false);
if (!string.IsNullOrEmpty(stdout))
await ctx.SendAutosplitMessageAsync($"```{stdout}```").ConfigureAwait(false);
if (!updated)
return;
msg = await ctx.Channel.SendMessageAsync("Saving state...").ConfigureAwait(false);
await StatsStorage.SaveAsync(true).ConfigureAwait(false);
msg = await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Restarting...").ConfigureAwait(false);
Bot.Restart(ctx.Channel.Id, "Restarted after successful dotnet update");
}
catch (Exception e)
{
await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Updating failed: " + e.Message).ConfigureAwait(false);
}
finally
{
LockObj.Release();
}
}
else
await ctx.Channel.SendMessageAsync("Update is already in progress").ConfigureAwait(false);
}
private static async Task<(bool updated, string stdout)> UpdateAsync(string version)
{
using var aptUpdate = new Process
{
StartInfo = new("apt-get", "update")
{
CreateNoWindow = true,
UseShellExecute = false,
},
};
aptUpdate.Start();
await aptUpdate.WaitForExitAsync().ConfigureAwait(false);
if (string.IsNullOrEmpty(version))
{
var versionMatch = DotnetVersionPattern().Match(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);
if (!versionMatch.Success)
throw new InvalidOperationException("Failed to resolve required dotnet sdk version");
version = $"{versionMatch.Groups["major"].Value}.{versionMatch.Groups["minor"].Value}";
}
using var aptUpgrade = new Process
{
StartInfo = new("apt-get", $"-y --allow-unauthenticated --only-upgrade install dotnet-sdk-{version}")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8,
},
};
aptUpgrade.Start();
var stdout = await aptUpgrade.StandardOutput.ReadToEndAsync().ConfigureAwait(false);
await aptUpgrade.WaitForExitAsync().ConfigureAwait(false);
if (string.IsNullOrEmpty(stdout))
return (false, stdout);
if (!stdout.Contains("dotnet-sdk-"))
return (false, stdout);
//var resultsMatch = Regex.Match(stdout, @"(?<upgraded>\d+) upgraded, (?<installed>\d+) newly installed");
if (stdout.Contains("is already the newest version", StringComparison.InvariantCultureIgnoreCase))
return (false, stdout);
return (true, stdout);
}
}
}