tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
DreamMakerController.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
5
9
23
25{
31 {
36
41
53 IDatabaseContext databaseContext,
54 IAuthenticationContext authenticationContext,
59 IApiHeadersProvider apiHeaders)
60 : base(
61 databaseContext,
62 authenticationContext,
63 logger,
65 apiHeaders)
66 {
69 }
70
77 [HttpGet]
81 {
84 .AsQueryable()
85 .Where(x => x.InstanceId == Instance.Id)
86 .FirstOrDefaultAsync(cancellationToken);
87
88 if (dreamMakerSettings == null)
89 return this.Gone();
90
91 return Json(dreamMakerSettings.ToApi());
92 }
93
102 [HttpGet("{id}")]
103 [TgsAuthorize(DreamMakerRights.CompileJobs)]
106 public async ValueTask<IActionResult> GetId(long id, CancellationToken cancellationToken)
107 {
109 .Where(x => x.Id == id)
110 .FirstOrDefaultAsync(cancellationToken);
111 if (compileJob == default)
112 return NotFound();
113 return Json(compileJob.ToApi());
114 }
115
125 [TgsAuthorize(DreamMakerRights.CompileJobs)]
129 () => ValueTask.FromResult(
132 .OrderByDescending(x => x.Job.StoppedAt))),
133 null,
134 page,
135 pageSize,
137
144 [HttpPut]
148 {
150
152 job,
153 (core, databaseContextFactory, paramJob, progressReporter, jobCancellationToken)
154 => core!.DreamMaker.DeploymentProcess(paramJob, databaseContextFactory, progressReporter, jobCancellationToken),
156 return Accepted(job.ToApi());
157 }
158
168 [HttpPost]
170 DreamMakerRights.SetDme
171 | DreamMakerRights.SetApiValidationPort
172 | DreamMakerRights.SetSecurityLevel
173 | DreamMakerRights.SetApiValidationRequirement
174 | DreamMakerRights.SetTimeout
175 | DreamMakerRights.SetCompilerArguments)]
179#pragma warning disable CA1506 // TODO: Decomplexify
181 {
182 ArgumentNullException.ThrowIfNull(model);
183
184 if (model.ApiValidationPort == 0)
185 throw new InvalidOperationException("ApiValidationPort cannot be 0!");
186
189 .AsQueryable()
190 .Where(x => x.InstanceId == Instance.Id)
191 .FirstOrDefaultAsync(cancellationToken);
192 if (hostModel == null)
193 return this.Gone();
194
196 if (model.ProjectName != null)
197 {
198 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetDme))
199 return Forbid();
200
201 if (model.ProjectName.Length == 0) // can't use isnullorwhitespace because linux memes
203 else
204 hostModel.ProjectName = model.ProjectName;
205 }
206
207 if (model.ApiValidationPort.HasValue)
208 {
209 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetApiValidationPort))
210 return Forbid();
211
212 if (model.ApiValidationPort.Value != hostModel.ApiValidationPort!.Value)
213 {
214 Logger.LogTrace(
215 "Triggering port allocator for DM-I:{instanceId} because model port {modelPort} doesn't match DB port {dbPort}...",
216 Instance.Id,
217 model.ApiValidationPort,
218 hostModel.ApiValidationPort);
221 model.ApiValidationPort.Value,
222 true,
224 if (verifiedPort != model.ApiValidationPort)
225 return Conflict(new ErrorMessageResponse(ErrorCode.PortNotAvailable));
226
227 hostModel.ApiValidationPort = model.ApiValidationPort;
228 }
229 }
230
231 if (model.ApiValidationSecurityLevel.HasValue)
232 {
233 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetSecurityLevel))
234 return Forbid();
235
236 hostModel.ApiValidationSecurityLevel = model.ApiValidationSecurityLevel;
237 }
238
239#pragma warning disable CS0618 // Type or member is obsolete
240 bool? legacyRequireDMApiValidation = model.RequireDMApiValidation;
241#pragma warning restore CS0618 // Type or member is obsolete
242 if (legacyRequireDMApiValidation.HasValue)
243 {
244 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetApiValidationRequirement))
245 return Forbid();
246
249 : DMApiValidationMode.Optional;
250 }
251
252 if (model.DMApiValidationMode.HasValue)
253 {
254 if (legacyRequireDMApiValidation.HasValue)
255 return BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure));
256
257 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetApiValidationRequirement))
258 return Forbid();
259
260 hostModel.DMApiValidationMode = model.DMApiValidationMode;
261 }
262
263 if (model.Timeout.HasValue)
264 {
265 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetTimeout))
266 return Forbid();
267
268 hostModel.Timeout = model.Timeout;
269 }
270
271 if (model.CompilerAdditionalArguments != null)
272 {
273 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetCompilerArguments))
274 return Forbid();
275
276 var sanitizedArguments = model.CompilerAdditionalArguments.Trim();
277 if (sanitizedArguments.Length == 0)
279 else
281 }
282
284
285 if (!dreamMakerRights.HasFlag(DreamMakerRights.Read))
286 return NoContent();
287
289 }
290#pragma warning restore CA1506
291
298 .AsQueryable()
299 .Include(x => x.Job!)
300 .ThenInclude(x => x.StartedBy)
301 .Include(x => x.Job!)
302 .ThenInclude(x => x.Instance)
303 .Include(x => x.RevisionInformation!)
304 .ThenInclude(x => x.PrimaryTestMerge!)
305 .ThenInclude(x => x.MergedBy)
306 .Include(x => x.RevisionInformation)
307 .ThenInclude(x => x.ActiveTestMerges!)
308 .ThenInclude(x => x!.TestMerge)
309 .ThenInclude(x => x!.MergedBy)
310 .Where(x => x.Job.Instance!.Id == Instance.Id);
311 }
312}
virtual ? long Id
The ID of the entity.
Definition EntityId.cs:13
Metadata about a server instance.
Definition Instance.cs:9
DreamMakerRights? DreamMakerRights
The Rights.DreamMakerRights of the InstancePermissionSet.
A request to the DreamMaker controller.
Represents an error message returned by the server.
Represents a long running job on the server. Model is read-only, updates attempt to cancel the job.
Definition JobResponse.cs:7
Routes to a server actions.
Definition Routes.cs:9
const string List
The postfix for list operations.
Definition Routes.cs:113
const string DreamMaker
The deployment controller.
Definition Routes.cs:98
new NotFoundObjectResult NotFound()
Generic 404 response.
ILogger< ApiController > Logger
The ILogger for the ApiController.
readonly IInstanceManager instanceManager
The IInstanceManager for the ComponentInterfacingController.
ApiController for managing the deployment system.
async ValueTask< IActionResult > GetId(long id, CancellationToken cancellationToken)
Get a CompileJob specified by a given id .
readonly IPortAllocator portAllocator
The IPortAllocator for the DreamMakerController.
async ValueTask< IActionResult > Read(CancellationToken cancellationToken)
Read current deployment settings.
async ValueTask< IActionResult > Update([FromBody] DreamMakerRequest model, CancellationToken cancellationToken)
Update deployment settings.
IQueryable< CompileJob > BaseCompileJobsQuery()
Base query for pulling in all required CompileJob fields.
ValueTask< IActionResult > List([FromQuery] int? page, [FromQuery] int? pageSize, CancellationToken cancellationToken)
List all CompileJob EntityIds for the instance.
async ValueTask< IActionResult > Create(CancellationToken cancellationToken)
Begin deploying repository code.
readonly IJobManager jobManager
The IJobManager for the DreamMakerController.
DreamMakerController(IDatabaseContext databaseContext, IAuthenticationContext authenticationContext, ILogger< DreamMakerController > logger, IInstanceManager instanceManager, IJobManager jobManager, IPortAllocator portAllocator, IApiHeadersProvider apiHeaders)
Initializes a new instance of the DreamMakerController class.
ComponentInterfacingController for operations that require an instance.
Backend abstract implementation of IDatabaseContext.
DbSet< DreamMakerSettings > DreamMakerSettings
The Models.DreamMakerSettings in the DatabaseContext.
DbSet< CompileJob > CompileJobs
The CompileJobs in the DatabaseContext.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext.A Task representing the running operation.
static Job Create(JobCode code, User? startedBy, Api.Models.Instance instance)
Creates a new job for registering in the Jobs.IJobService.
Manages the runtime of Jobs.
ValueTask RegisterOperation(Job job, JobEntrypoint operation, CancellationToken cancellationToken)
Registers a given Job and begins running it.
For creating and accessing authentication contexts.
Gets unassigned ports for use by TGS.
ValueTask< ushort?> GetAvailablePort(ushort basePort, bool checkOne, CancellationToken cancellationToken)
Gets a port not currently in use by TGS.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12
JobCode
The different types of Response.JobResponse.
Definition JobCode.cs:9
DMApiValidationMode
The DMAPI validation setting for deployments.
@ Read
User can read all chat bot properties except Models.Internal.ChatBotSettings.ConnectionString.
@ List
User may list files if the Models.Instance allows it.
DreamMakerRights
Rights for deployment.