mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Finished BackupServer and UpdateServer
This commit is contained in:
parent
824daed0d1
commit
0f3c6e6be9
28 changed files with 618 additions and 216 deletions
|
|
@ -1,11 +1,15 @@
|
|||
using QueryMaster;
|
||||
using ServerManagerTool.Common.Utils;
|
||||
using ServerManagerTool.DiscordBot.Enums;
|
||||
using ServerManagerTool.Enums;
|
||||
using ServerManagerTool.Lib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using WPFSharp.Globalizer;
|
||||
|
||||
namespace ServerManagerTool.Utils
|
||||
|
|
@ -15,6 +19,10 @@ namespace ServerManagerTool.Utils
|
|||
private static readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance;
|
||||
private static bool _runningCommand = false;
|
||||
|
||||
private static readonly Dictionary<string, CommandType> _currentProfileCommands = new Dictionary<string, CommandType>();
|
||||
|
||||
public static bool HasRunningCommands => _currentProfileCommands.Count > 0;
|
||||
|
||||
public static IList<string> HandleDiscordCommand(CommandType commandType, string serverId, string channelId, string profileId)
|
||||
{
|
||||
// check if incoming values are valid
|
||||
|
|
@ -57,7 +65,8 @@ namespace ServerManagerTool.Utils
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new string[] { ex.Message };
|
||||
var message = ex.InnerException is null ? ex.Message : ex.InnerException.Message;
|
||||
return new string[] { message };
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -74,54 +83,68 @@ namespace ServerManagerTool.Utils
|
|||
{
|
||||
if (string.IsNullOrWhiteSpace(profileId))
|
||||
{
|
||||
return new List<string> { _globalizer.GetResourceString("DiscordBot_CommandProfileMissing") };
|
||||
return new List<string> { string.Format(_globalizer.GetResourceString("DiscordBot_ProfileMissing"), CommandType.Info) };
|
||||
}
|
||||
|
||||
var serverName = string.Empty;
|
||||
var serverIp = IPAddress.Loopback;
|
||||
var queryPort = 0;
|
||||
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
// check if another command is being run against the profile
|
||||
if (_currentProfileCommands.ContainsKey(profileId))
|
||||
{
|
||||
var server = ServerManager.Instance.Servers.Where(s => Equals(channelId, s.Profile.DiscordChannelId) && Equals(profileId, s.Profile.ProfileID)).FirstOrDefault();
|
||||
|
||||
if (server is null)
|
||||
{
|
||||
throw new Exception(string.Format(_globalizer.GetResourceString("DiscordBot_CommandProfileNotFound"), profileId));
|
||||
}
|
||||
|
||||
serverName = server.Profile.ServerName;
|
||||
if (!string.IsNullOrWhiteSpace(server.Profile.ServerIP))
|
||||
{
|
||||
IPAddress.TryParse(server.Profile.ServerIP, out serverIp);
|
||||
}
|
||||
queryPort = server.Profile.QueryPort;
|
||||
}).Wait();
|
||||
|
||||
List<string> response = new List<string>();
|
||||
return new List<string> { string.Format(_globalizer.GetResourceString("DiscordBot_CommandRunningProfile"), _currentProfileCommands[profileId], profileId) };
|
||||
}
|
||||
_currentProfileCommands.Add(profileId, CommandType.Info);
|
||||
|
||||
try
|
||||
{
|
||||
using (var gameServer = ServerQuery.GetServerInstance(EngineType.Source, new IPEndPoint(serverIp, queryPort)))
|
||||
var serverName = string.Empty;
|
||||
var serverIp = IPAddress.Loopback;
|
||||
var queryPort = 0;
|
||||
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
var info = gameServer?.GetInfo();
|
||||
if (info is null)
|
||||
var server = ServerManager.Instance.Servers.FirstOrDefault(s => Equals(channelId, s.Profile.DiscordChannelId) && Equals(profileId, s.Profile.ProfileID));
|
||||
|
||||
if (server is null)
|
||||
{
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_CommandInfoFailed"), serverName));
|
||||
throw new Exception(string.Format(_globalizer.GetResourceString("DiscordBot_ProfileNotFound"), profileId));
|
||||
}
|
||||
else
|
||||
|
||||
serverName = server.Profile.ServerName;
|
||||
if (!string.IsNullOrWhiteSpace(server.Profile.ServerIP))
|
||||
{
|
||||
var mapName = _globalizer.GetResourceString($"Map_{info.Map}") ?? info.Map;
|
||||
response.Add($"```{info.Name}\n{_globalizer.GetResourceString("DiscordBot_MapLabel")} {mapName}\n{_globalizer.GetResourceString("ServerSettings_PlayersLabel")} {info.Players} / {info.MaxPlayers}```");
|
||||
IPAddress.TryParse(server.Profile.ServerIP, out serverIp);
|
||||
}
|
||||
queryPort = server.Profile.QueryPort;
|
||||
}).Wait();
|
||||
|
||||
List<string> response = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
using (var gameServer = ServerQuery.GetServerInstance(EngineType.Source, new IPEndPoint(serverIp, queryPort)))
|
||||
{
|
||||
var info = gameServer?.GetInfo();
|
||||
if (info is null)
|
||||
{
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_InfoFailed"), serverName));
|
||||
}
|
||||
else
|
||||
{
|
||||
var mapName = _globalizer.GetResourceString($"Map_{info.Map}") ?? info.Map;
|
||||
response.Add($"```{info.Name}\n{_globalizer.GetResourceString("DiscordBot_MapLabel")} {mapName}\n{_globalizer.GetResourceString("ServerSettings_PlayersLabel")} {info.Players} / {info.MaxPlayers}```");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_CommandInfoFailed"), serverName));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_InfoFailed"), serverName));
|
||||
}
|
||||
|
||||
return response;
|
||||
return response;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_currentProfileCommands.Remove(profileId);
|
||||
}
|
||||
}
|
||||
|
||||
private static IList<string> GetServerList(string channelId)
|
||||
|
|
@ -162,7 +185,63 @@ namespace ServerManagerTool.Utils
|
|||
|
||||
private static IList<string> BackupServer(string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), CommandType.Backup) };
|
||||
if (string.IsNullOrWhiteSpace(profileId))
|
||||
{
|
||||
return new List<string> { string.Format(_globalizer.GetResourceString("DiscordBot_ProfileMissing"), CommandType.Backup) };
|
||||
}
|
||||
|
||||
// check if another command is being run against the profile
|
||||
if (_currentProfileCommands.ContainsKey(profileId))
|
||||
{
|
||||
return new List<string> { string.Format(_globalizer.GetResourceString("DiscordBot_CommandRunningProfile"), _currentProfileCommands[profileId], profileId) };
|
||||
}
|
||||
_currentProfileCommands.Add(profileId, CommandType.Backup);
|
||||
|
||||
ServerProfileSnapshot profile = null;
|
||||
Task task = null;
|
||||
|
||||
try
|
||||
{
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
var server = ServerManager.Instance.Servers.FirstOrDefault(s => Equals(channelId, s.Profile.DiscordChannelId) && Equals(profileId, s.Profile.ProfileID));
|
||||
|
||||
if (server is null)
|
||||
{
|
||||
throw new Exception(string.Format(_globalizer.GetResourceString("DiscordBot_ProfileNotFound"), profileId));
|
||||
}
|
||||
|
||||
profile = ServerProfileSnapshot.Create(server.Profile);
|
||||
}).Wait();
|
||||
|
||||
List<string> response = new List<string>();
|
||||
|
||||
var app = new ServerApp(true)
|
||||
{
|
||||
DeleteOldServerBackupFiles = !Config.Default.AutoBackup_EnableBackup,
|
||||
OutputLogs = false,
|
||||
SendAlerts = true,
|
||||
SendEmails = false,
|
||||
ServerProcess = ServerProcessType.Backup,
|
||||
};
|
||||
|
||||
task = Task.Run(() =>
|
||||
{
|
||||
app.PerformProfileBackup(profile);
|
||||
_currentProfileCommands.Remove(profileId);
|
||||
});
|
||||
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_BackupRequested"), profile.ServerName));
|
||||
|
||||
return response;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (task is null)
|
||||
{
|
||||
_currentProfileCommands.Remove(profileId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IList<string> ShutdownServer(string channelId, string profileId)
|
||||
|
|
@ -182,7 +261,87 @@ namespace ServerManagerTool.Utils
|
|||
|
||||
private static IList<string> UpdateServer(string channelId, string profileId)
|
||||
{
|
||||
return new List<string>() { string.Format(_globalizer.GetResourceString("DiscordBot_CommandUnknown"), CommandType.Update) };
|
||||
if (string.IsNullOrWhiteSpace(profileId))
|
||||
{
|
||||
return new List<string> { string.Format(_globalizer.GetResourceString("DiscordBot_ProfileMissing"), CommandType.Update) };
|
||||
}
|
||||
|
||||
// check if another command is being run against the profile
|
||||
if (_currentProfileCommands.ContainsKey(profileId))
|
||||
{
|
||||
return new List<string> { string.Format(_globalizer.GetResourceString("DiscordBot_CommandRunningProfile"), _currentProfileCommands[profileId], profileId) };
|
||||
}
|
||||
_currentProfileCommands.Add(profileId, CommandType.Update);
|
||||
|
||||
ServerProfileSnapshot profile = null;
|
||||
bool performRestart = false;
|
||||
Task task = null;
|
||||
|
||||
try
|
||||
{
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
var server = ServerManager.Instance.Servers.FirstOrDefault(s => Equals(channelId, s.Profile.DiscordChannelId) && Equals(profileId, s.Profile.ProfileID));
|
||||
|
||||
if (server is null)
|
||||
{
|
||||
throw new Exception(string.Format(_globalizer.GetResourceString("DiscordBot_ProfileNotFound"), profileId));
|
||||
}
|
||||
|
||||
switch (server.Runtime.Status)
|
||||
{
|
||||
case ServerStatus.Initializing:
|
||||
case ServerStatus.Stopping:
|
||||
case ServerStatus.Unknown:
|
||||
throw new Exception(string.Format(_globalizer.GetResourceString("DiscordBot_ProfileBadStatus"), profileId, ServerRuntime.GetServerStatusString(ServerStatus.Stopped)));
|
||||
|
||||
case ServerStatus.Running:
|
||||
performRestart = true;
|
||||
break;
|
||||
|
||||
case ServerStatus.Updating:
|
||||
throw new Exception(string.Format(_globalizer.GetResourceString("DiscordBot_ProfileUpdating"), profileId));
|
||||
}
|
||||
|
||||
profile = ServerProfileSnapshot.Create(server.Profile);
|
||||
}).Wait();
|
||||
|
||||
List<string> response = new List<string>();
|
||||
|
||||
var app = new ServerApp(true)
|
||||
{
|
||||
DeleteOldServerBackupFiles = !Config.Default.AutoBackup_EnableBackup,
|
||||
OutputLogs = false,
|
||||
SendAlerts = true,
|
||||
SendEmails = false,
|
||||
ServerProcess = ServerProcessType.Update,
|
||||
ServerStatusChangeCallback = (ServerStatus serverStatus) =>
|
||||
{
|
||||
TaskUtils.RunOnUIThreadAsync(() =>
|
||||
{
|
||||
var server = ServerManager.Instance.Servers.FirstOrDefault(s => Equals(channelId, s.Profile.DiscordChannelId) && Equals(profileId, s.Profile.ProfileID));
|
||||
server.Runtime.UpdateServerStatus(serverStatus, true);
|
||||
}).Wait();
|
||||
}
|
||||
};
|
||||
|
||||
task = Task.Run(() =>
|
||||
{
|
||||
app.PerformProfileShutdown(profile, performRestart, true, false, false, CancellationToken.None);
|
||||
_currentProfileCommands.Remove(profileId);
|
||||
});
|
||||
|
||||
response.Add(string.Format(_globalizer.GetResourceString("DiscordBot_UpdateRequested"), profile.ServerName));
|
||||
|
||||
return response;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (task is null)
|
||||
{
|
||||
_currentProfileCommands.Remove(profileId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue