tgstation-server 6.19.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
AdministrationAuthority.cs
Go to the documentation of this file.
1using System;
2using System.IO;
5using System.Web;
6
10
11using Octokit;
12
24
26{
29 {
33 const string OctokitException = "Bad GitHub API response, check configuration!";
34
38 static readonly object ReadCacheKey = new();
39
44
49
54
59
64
69
74
79
84
100 IDatabaseContext databaseContext,
111 : base(
112 databaseContext,
113 logger)
114 {
124 }
125
128 => new(
129 () => Flag(AdministrationRights.ChangeVersion),
130 async () =>
131 {
132 try
133 {
135 {
136 Version? greatestVersion = null;
137 Uri? repoUrl = null;
138 var scopeCancellationToken = CancellationToken.None; // DCT: None available
139 try
140 {
144
145 foreach (var kvp in releases)
146 {
147 var version = kvp.Key;
148 var release = kvp.Value;
149 if (version.Major > 3 // Forward/backward compatible but not before TGS4
150 && (greatestVersion == null || version > greatestVersion))
152 }
153
155 }
156 catch (NotFoundException e)
157 {
158 Logger.LogWarning(e, "Not found exception while retrieving upstream repository info!");
159 }
160
161 return new AdministrationResponse
162 {
163 LatestVersion = greatestVersion,
164 TrackedRepositoryUrl = repoUrl,
165 GeneratedAt = DateTimeOffset.UtcNow,
166 };
167 }
168
169 var ttl = TimeSpan.FromMinutes(30);
172 {
173 using var entry = cacheService.CreateEntry(ReadCacheKey);
175 entry.Value = task = CacheFactory();
176 }
177 else
179
180 var result = await task.WaitAsync(cancellationToken);
182 }
184 {
186 }
187 catch (ApiException e)
188 {
189 Logger.LogWarning(e, OctokitException);
191 new ErrorMessageResponse(ErrorCode.RemoteApiError)
192 {
193 AdditionalData = e.Message,
194 },
195 HttpFailureResponse.FailedDependency);
196 }
197 });
198
201 {
203
204 return new(
205 () =>
206 {
207 if (uploadZip)
208 return Flag(AdministrationRights.UploadVersion);
209
210 return Flag(AdministrationRights.ChangeVersion);
211 },
212 async () =>
213 {
214 if (targetVersion.Major < 4)
215 return BadRequest<ServerUpdateResponse>(ErrorCode.CannotChangeServerSuite);
216
219 new ErrorMessageResponse(ErrorCode.MissingHostWatchdog),
220 HttpFailureResponse.UnprocessableEntity);
221
224 : null;
225
227 try
228 {
229 try
230 {
232 }
233 catch
234 {
235 if (uploadZip)
236 await uploadTicket!.DisposeAsync();
237
238 throw;
239 }
240 }
242 {
244 }
245 catch (ApiException e)
246 {
247 Logger.LogWarning(e, OctokitException);
249 new ErrorMessageResponse(ErrorCode.RemoteApiError)
250 {
251 AdditionalData = e.Message,
252 },
253 HttpFailureResponse.FailedDependency);
254 }
255
256 return updateResult switch
257 {
262 new ErrorMessageResponse(ErrorCode.SwarmIntegrityCheckFailed),
263 HttpFailureResponse.FailedDependency),
264 _ => throw new InvalidOperationException($"Unexpected ServerUpdateResult: {updateResult}"),
265 };
266 });
267 }
268
271 => new(
272 () => Flag(AdministrationRights.RestartHost),
273 async () =>
274 {
276 {
277 Logger.LogDebug("Restart request failed due to lack of host watchdog!");
278 return new AuthorityResponse(
279 new ErrorMessageResponse(ErrorCode.MissingHostWatchdog),
280 HttpFailureResponse.UnprocessableEntity);
281 }
282
284 return new AuthorityResponse();
285 });
286
289 {
290 ArgumentNullException.ThrowIfNull(path);
291 return new(
292 () => Flag(AdministrationRights.DownloadLogs),
293 async () =>
294 {
295 path = HttpUtility.UrlDecode(path);
296
297 // guard against directory navigation
299 if (path != sanitizedPath)
301
304 path);
305 try
306 {
309 () => null,
310 null,
311 fullPath,
312 true));
313
315 {
316 Name = path,
317 LastModified = await ioManager.GetLastModified(fullPath, cancellationToken),
318 FileTicket = fileTransferTicket.FileTicket,
319 });
320 }
321 catch (IOException ex)
322 {
323 return Conflict<LogFileResponse>(ErrorCode.IOError, ex.ToString());
324 }
325 });
326 }
327 }
328}
Represents an error message returned by the server.
RequirementsGated< AuthorityResponse< LogFileResponse > > GetLog(string path, CancellationToken cancellationToken)
Get a ticket for downloading a log file at a given path .A RequirementsGated<TResult> LogFileResponse...
readonly IFileTransferTicketProvider fileTransferService
The IFileTransferTicketProvider for the AdministrationAuthority.
readonly IServerControl serverControl
The IServerControl for the AdministrationAuthority.
readonly IOptionsSnapshot< FileLoggingConfiguration > fileLoggingConfigurationOptions
The FileLoggingConfiguration for the AdministrationAuthority.
RequirementsGated< AuthorityResponse > TriggerServerRestart()
Triggers a restart of tgstation-server without terminating running game instances....
readonly IMemoryCache cacheService
The IMemoryCache for the AdministrationAuthority.
RequirementsGated< AuthorityResponse< ServerUpdateResponse > > TriggerServerVersionChange(Version targetVersion, bool uploadZip, CancellationToken cancellationToken)
Triggers a restart of tgstation-server without terminating running game instances,...
RequirementsGated< AuthorityResponse< AdministrationResponse > > GetUpdateInformation(bool forceFresh, CancellationToken cancellationToken)
Gets the AdministrationResponse containing server update information.A RequirementsGated<TResult> Adm...
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the AdministrationAuthority.
readonly IAssemblyInformationProvider assemblyInformationProvider
The IAssemblyInformationProvider for the AdministrationAuthority.
const string OctokitException
Default Exception.Message for ApiExceptions.
AdministrationAuthority(IDatabaseContext databaseContext, ILogger< UserAuthority > logger, IGitHubServiceFactory gitHubServiceFactory, IServerControl serverControl, IServerUpdateInitiator serverUpdateInitiator, IFileTransferTicketProvider fileTransferService, IMemoryCache cacheService, IAssemblyInformationProvider assemblyInformationProvider, IPlatformIdentifier platformIdentifier, IIOManager ioManager, IOptionsSnapshot< FileLoggingConfiguration > fileLoggingConfigurationOptions)
Initializes a new instance of the AdministrationAuthority class.
readonly IIOManager ioManager
The IIOManager for the AdministrationAuthority.
readonly IServerUpdateInitiator serverUpdateInitiator
The IServerUpdateInitiator for the AdministrationAuthority.
static readonly object ReadCacheKey
The IMemoryCache key for GetUpdateInformation(bool, CancellationToken).
readonly IGitHubServiceFactory gitHubServiceFactory
The IGitHubServiceFactory for the AdministrationAuthority.
ILogger< AuthorityBase > Logger
Gets the ILogger for the AuthorityBase.
Evaluates a set of IAuthorizationRequirements to be checked before executing a response.
Represents a file on disk to be downloaded.
IAuthority for administrative server operations.
Represents a service that may take an updated Host assembly and run it, stopping the current assembly...
bool WatchdogPresent
true if live updates are supported, false. TryStartUpdate(IServerUpdateExecutor, Version) and Restart...
ValueTask Restart()
Restarts the Host.
ValueTask< ServerUpdateResult > InitiateUpdate(IFileStreamProvider? fileStreamProvider, Version version, CancellationToken cancellationToken)
Start the process of downloading and applying an update to a new server version .
Interface for using filesystems.
Definition IIOManager.cs:14
string GetFileName(string path)
Gets the file name portion of a path .
string ConcatPath(params string[] paths)
Combines an array of strings into a path.
Task< DateTimeOffset > GetLastModified(string path, CancellationToken cancellationToken)
Get the DateTimeOffset of when a given path was last modified.
For identifying the current platform.
Service for temporarily storing files to be downloaded or uploaded.
FileTicketResponse CreateDownload(FileDownloadProvider fileDownloadProvider)
Create a FileTicketResponse for a download.
IFileUploadTicket CreateUpload(FileUploadStreamKind streamKind)
Create a IFileUploadTicket.
A FileTicketResponse that waits for a pending upload.
ValueTask< IGitHubService > CreateService(CancellationToken cancellationToken)
Create a IGitHubService.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12
@ List
User may list files if the Models.Instance allows it.
AdministrationRights
Administration rights for the server.
HttpFailureResponse
Indicates the type of HTTP status code an failing AuthorityResponse should generate.
HttpSuccessResponse
Indicates the type of HTTP status code a successful AuthorityResponse<TResult> should generate.
ServerUpdateResult
The result of a call to start a server update.
FileUploadStreamKind
Determines the type of global::System.IO.Stream returned from IFileUploadTicket's created from IFileT...