From bf3207d84d72f3f574aefa173565331b04aef284 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Wed, 15 Aug 2018 16:10:26 -0400 Subject: [PATCH] Various things: Remove rights => int conversions Clean up DreamDaemonController Watchdog API docs --- docs/API.dox | 26 +++++++++- .../Controllers/ByondController.cs | 2 +- .../Controllers/ChatController.cs | 4 +- .../Controllers/DreamDaemonController.cs | 48 +++++++++---------- .../Controllers/DreamMakerController.cs | 2 +- .../Controllers/InstanceController.cs | 2 +- .../Controllers/RepositoryController.cs | 4 +- 7 files changed, 56 insertions(+), 32 deletions(-) diff --git a/docs/API.dox b/docs/API.dox index ee17463c05..eab8120881 100644 --- a/docs/API.dox +++ b/docs/API.dox @@ -17,6 +17,8 @@ The TGS4 API's canonical definitions are provided as a .NET Standard library in An all inclusive TAP interface for using the API is also provided in this package: https://www.nuget.org/packages/Tgstation.Server.Client +Last off, if anything in this API doesn't seem to hold true when tested against the server, please open an issue on the repository. + @subsection api_interp Interpreting C# Models This document will reference the canonical C# models in the @ref Tgstation.Server.Api.Models namespace. Note that these models are built to mirror the JSON requests and responses with a couple caveats. @@ -404,4 +406,26 @@ When creating a file, only @ref Tgstation.Server.Api.Models.ConfigurationFile.Pa If the file already exists, the @ref Tgstation.Server.Api.Models.ConfigurationFile.LastReadHash field must also be present with the last version recieved from the server for that file. If this does not match at the time of the request, 409 will be returned, indicating the file has changed since it was last viewed by the client. -To delete a file set @ref Tgstation.Server.Api.Models.ConfigurationFile.Content to null in the request. If deleting a file leaves a directory empty, they too will be deleted. \ No newline at end of file +To delete a file set @ref Tgstation.Server.Api.Models.ConfigurationFile.Content to null in the request. If deleting a file leaves a directory empty, they too will be deleted. + +@subsection api_dog Watchdog + +To read watchdog status use the following request: + +I GET "/DreamDaemon" => @ref Tgstation.Server.Api.Models.DreamDaemon + +To update watchdog settings use the following request: + +I POST "/DreamDaemon" => @ref Tgstation.Server.Api.Models.DreamDaemon + +Note that soft restart/shutdown operations cannot be cancelled via this method, they can be changed from one to another however. + +To start the watchdog use the following request: + +I PUT "/DreamDaemon" Empty => @ref Tgstation.Server.Api.Models.Job + +The returned job represents the startup process for the watchdog + +To stop the watchdog use the following request: + +I DELETE "/DreamDaemon" => OK diff --git a/src/Tgstation.Server.Host/Controllers/ByondController.cs b/src/Tgstation.Server.Host/Controllers/ByondController.cs index e685bc7a9a..11c0191d24 100644 --- a/src/Tgstation.Server.Host/Controllers/ByondController.cs +++ b/src/Tgstation.Server.Host/Controllers/ByondController.cs @@ -92,7 +92,7 @@ namespace Tgstation.Server.Host.Controllers Description = String.Format(CultureInfo.InvariantCulture, "Install BYOND version {0}", installingVersion), StartedBy = AuthenticationContext.User, CancelRightsType = RightsType.Byond, - CancelRight = (int)ByondRights.CancelInstall, + CancelRight = (ulong)ByondRights.CancelInstall, Instance = Instance }; await jobManager.RegisterOperation(job, (paramJob, serviceProvicer, progressHandler, ct) => byondManager.ChangeVersion(installingVersion, ct), cancellationToken).ConfigureAwait(false); diff --git a/src/Tgstation.Server.Host/Controllers/ChatController.cs b/src/Tgstation.Server.Host/Controllers/ChatController.cs index 3b823ea0ad..f1cb99d06c 100644 --- a/src/Tgstation.Server.Host/Controllers/ChatController.cs +++ b/src/Tgstation.Server.Host/Controllers/ChatController.cs @@ -153,7 +153,7 @@ namespace Tgstation.Server.Host.Controllers var results = await query.ToListAsync(cancellationToken).ConfigureAwait(false); - var connectionStrings = (AuthenticationContext.GetRight(RightsType.ChatBots) & (int)ChatBotRights.ReadConnectionString) != 0; + var connectionStrings = (AuthenticationContext.GetRight(RightsType.ChatBots) & (ulong)ChatBotRights.ReadConnectionString) != 0; if (!connectionStrings) foreach (var I in results) @@ -172,7 +172,7 @@ namespace Tgstation.Server.Host.Controllers if (results == default) return NotFound(); - var connectionStrings = (AuthenticationContext.GetRight(RightsType.ChatBots) & (int)ChatBotRights.ReadConnectionString) != 0; + var connectionStrings = (AuthenticationContext.GetRight(RightsType.ChatBots) & (ulong)ChatBotRights.ReadConnectionString) != 0; if (!connectionStrings) results.ConnectionString = null; diff --git a/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs b/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs index dbdee8146b..5e81eab21b 100644 --- a/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs +++ b/src/Tgstation.Server.Host/Controllers/DreamDaemonController.cs @@ -61,7 +61,7 @@ namespace Tgstation.Server.Host.Controllers var job = new Models.Job { Description = "Launch DreamDaemon", - CancelRight = (int)DreamDaemonRights.Shutdown, + CancelRight = (ulong)DreamDaemonRights.Shutdown, CancelRightsType = RightsType.DreamDaemon, Instance = Instance, StartedBy = AuthenticationContext.User @@ -72,8 +72,6 @@ namespace Tgstation.Server.Host.Controllers var result = await instance.Watchdog.Launch(innerCt).ConfigureAwait(false); if (result == null) throw new InvalidOperationException("Watchdog already running!"); - if (!instance.Watchdog.Running) - throw new Exception("Failed to launch watchdog!"); }, cancellationToken).ConfigureAwait(false); return Json(job); @@ -81,20 +79,30 @@ namespace Tgstation.Server.Host.Controllers /// [TgsAuthorize(DreamDaemonRights.ReadMetadata | DreamDaemonRights.ReadRevision)] - public override async Task Read(CancellationToken cancellationToken) - { + public override Task Read(CancellationToken cancellationToken) => ReadImpl(null, cancellationToken); + + /// + /// Implementation of + /// + /// The to operate on if any + /// The for the operation + /// A resulting in the of the operation + async Task ReadImpl(DreamDaemonSettings settings, CancellationToken cancellationToken) + { var instance = instanceManager.GetInstance(Instance); var dd = instance.Watchdog; - var metadata = (AuthenticationContext.GetRight(RightsType.DreamDaemon) & (int)DreamDaemonRights.ReadMetadata) != 0; - var revision = (AuthenticationContext.GetRight(RightsType.DreamDaemon) & (int)DreamDaemonRights.ReadRevision) != 0; + var metadata = (AuthenticationContext.GetRight(RightsType.DreamDaemon) & (ulong)DreamDaemonRights.ReadMetadata) != 0; + var revision = (AuthenticationContext.GetRight(RightsType.DreamDaemon) & (ulong)DreamDaemonRights.ReadRevision) != 0; - var settings = await DatabaseContext.Instances.Where(x => x.Id == Instance.Id).Select(x => x.DreamDaemonSettings).FirstAsync(cancellationToken).ConfigureAwait(false); + if (settings == null) + settings = await DatabaseContext.Instances.Where(x => x.Id == Instance.Id).Select(x => x.DreamDaemonSettings).FirstAsync(cancellationToken).ConfigureAwait(false); var result = new DreamDaemon(); if(metadata) { var alphaActive = dd.AlphaIsActive; var llp = dd.LastLaunchParameters; + var rstate = dd.RebootState; result.AutoStart = settings.AutoStart; result.CurrentPort = alphaActive ? llp.PrimaryPort : llp.SecondaryPort; result.CurrentSecurity = llp.SecurityLevel; @@ -102,9 +110,8 @@ namespace Tgstation.Server.Host.Controllers result.PrimaryPort = dd.ActiveLaunchParameters.PrimaryPort; result.AllowWebClient = dd.ActiveLaunchParameters.AllowWebClient; result.Running = dd.Running; - result.SecondaryPort = dd.LastLaunchParameters.SecondaryPort; - result.SecurityLevel = dd.LastLaunchParameters.SecurityLevel; - var rstate = dd.RebootState; + result.SecondaryPort = llp.SecondaryPort; + result.SecurityLevel = llp.SecurityLevel; result.SoftRestart = rstate == RebootState.Restart; result.SoftShutdown = rstate == RebootState.Shutdown; }; @@ -126,10 +133,6 @@ namespace Tgstation.Server.Host.Controllers { //alias for stopping DD var instance = instanceManager.GetInstance(Instance); - - if (!instance.Watchdog.Running) - return StatusCode((int)HttpStatusCode.Gone); - await instance.Watchdog.Terminate(false, cancellationToken).ConfigureAwait(false); return Ok(); } @@ -178,21 +181,18 @@ namespace Tgstation.Server.Host.Controllers return BadRequest(new ErrorMessage { Message = "TGS does not support the ultrasafe DreamDaemon configuration!" }); var wd = instanceManager.GetInstance(Instance).Watchdog; - - var changeSettingsTask = wd.ChangeSettings(current, cancellationToken); + + //run these in parallel because they are equally as important + await Task.WhenAll(DatabaseContext.Save(cancellationToken), wd.ChangeSettings(current, cancellationToken)).ConfigureAwait(false); //soft shutdown/restart can't be cancelled because of how many things rely on them //They can be alternated though if (!oldSoftRestart.Value && current.SoftRestart.Value) - await Task.WhenAll(changeSettingsTask, wd.Restart(true, cancellationToken)).ConfigureAwait(false); + await wd.Restart(true, cancellationToken).ConfigureAwait(false); else if (!oldSoftShutdown.Value && current.SoftShutdown.Value) - await Task.WhenAll(changeSettingsTask, wd.Terminate(true, cancellationToken)).ConfigureAwait(false); - else - await changeSettingsTask.ConfigureAwait(false); + await wd.Terminate(true, cancellationToken).ConfigureAwait(false); - await DatabaseContext.Save(default).ConfigureAwait(false); - - return Ok(); + return await ReadImpl(current, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs b/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs index 50e088ef68..2c05a5704b 100644 --- a/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs +++ b/src/Tgstation.Server.Host/Controllers/DreamMakerController.cs @@ -91,7 +91,7 @@ namespace Tgstation.Server.Host.Controllers Description = "Compile active repository code", StartedBy = AuthenticationContext.User, CancelRightsType = RightsType.DreamMaker, - CancelRight = (int)DreamMakerRights.CancelCompile, + CancelRight = (ulong)DreamMakerRights.CancelCompile, Instance = Instance }; await jobManager.RegisterOperation(job, (paramJob, serviceProvider, progressReporter, ct) => RunCompile(paramJob, serviceProvider, Instance, ct), cancellationToken).ConfigureAwait(false); diff --git a/src/Tgstation.Server.Host/Controllers/InstanceController.cs b/src/Tgstation.Server.Host/Controllers/InstanceController.cs index 7a3f465f90..902d1c54c6 100644 --- a/src/Tgstation.Server.Host/Controllers/InstanceController.cs +++ b/src/Tgstation.Server.Host/Controllers/InstanceController.cs @@ -319,7 +319,7 @@ namespace Tgstation.Server.Host.Controllers Description = String.Format(CultureInfo.InvariantCulture, "Move instance ID {0} from {1} to {2}", Instance.Id, Instance.Path, rawPath), Instance = Instance, CancelRightsType = RightsType.InstanceManager, - CancelRight = (int)InstanceManagerRights.CancelMove, + CancelRight = (ulong)InstanceManagerRights.CancelMove, StartedBy = AuthenticationContext.User }; diff --git a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs index 34e38cdfbe..beac22b255 100644 --- a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs +++ b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs @@ -153,7 +153,7 @@ namespace Tgstation.Server.Host.Controllers Description = String.Format(CultureInfo.InvariantCulture, "Clone branch {1} of repository {0}", origin, cloneBranch ?? "master"), StartedBy = AuthenticationContext.User, CancelRightsType = RightsType.Repository, - CancelRight = (int)RepositoryRights.CancelClone, + CancelRight = (ulong)RepositoryRights.CancelClone, Instance = Instance }; var api = currentModel.ToApi(); @@ -307,7 +307,7 @@ namespace Tgstation.Server.Host.Controllers StartedBy = AuthenticationContext.User, Instance = Instance, CancelRightsType = RightsType.Repository, - CancelRight = (int)RepositoryRights.CancelPendingChanges, + CancelRight = (ulong)RepositoryRights.CancelPendingChanges, }; await jobManager.RegisterOperation(job, async (paramJob, serviceProvider, progressReporter, ct) =>