tgstation-server 6.19.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 .Where(x => x.InstanceId == Instance.Id)
85 .FirstOrDefaultAsync(cancellationToken);
86
87 if (dreamMakerSettings == null)
88 return this.Gone();
89
90 return Json(dreamMakerSettings.ToApi());
91 }
92
101 [HttpGet("{id}")]
102 [TgsAuthorize(DreamMakerRights.CompileJobs)]
105 public async ValueTask<IActionResult> GetId(long id, CancellationToken cancellationToken)
106 {
108 .Where(x => x.Id == id)
109 .FirstOrDefaultAsync(cancellationToken);
110 if (compileJob == default)
111 return NotFound();
112 return Json(compileJob.ToApi());
113 }
114
124 [TgsAuthorize(DreamMakerRights.CompileJobs)]
128 () => ValueTask.FromResult<PaginatableResult<CompileJob>?>(
131 .OrderByDescending(x => x.Job.StoppedAt))),
132 null,
133 page,
134 pageSize,
135 cancellationToken);
136
143 [HttpPut]
147 {
149
151 job,
152 (core, databaseContextFactory, paramJob, progressReporter, jobCancellationToken)
153 => core!.DreamMaker.DeploymentProcess(paramJob, databaseContextFactory, progressReporter, jobCancellationToken),
154 cancellationToken);
155 return Accepted(job.ToApi());
156 }
157
167 [HttpPost]
169 DreamMakerRights.SetDme
170 | DreamMakerRights.SetApiValidationPort
171 | DreamMakerRights.SetSecurityLevel
172 | DreamMakerRights.SetApiValidationRequirement
173 | DreamMakerRights.SetTimeout
174 | DreamMakerRights.SetCompilerArguments)]
178#pragma warning disable CA1506 // TODO: Decomplexify
180 {
181 ArgumentNullException.ThrowIfNull(model);
182
183 if (model.ApiValidationPort == 0)
184 throw new InvalidOperationException("ApiValidationPort cannot be 0!");
185
188 .Where(x => x.InstanceId == Instance.Id)
189 .FirstOrDefaultAsync(cancellationToken);
190 if (hostModel == null)
191 return this.Gone();
192
194 if (model.ProjectName != null)
195 {
196 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetDme))
197 return Forbid();
198
199 if (model.ProjectName.Length == 0) // can't use isnullorwhitespace because linux memes
201 else
202 hostModel.ProjectName = model.ProjectName;
203 }
204
205 if (model.ApiValidationPort.HasValue)
206 {
207 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetApiValidationPort))
208 return Forbid();
209
210 if (model.ApiValidationPort.Value != hostModel.ApiValidationPort!.Value)
211 {
212 Logger.LogTrace(
213 "Triggering port allocator for DM-I:{instanceId} because model port {modelPort} doesn't match DB port {dbPort}...",
214 Instance.Id,
215 model.ApiValidationPort,
216 hostModel.ApiValidationPort);
219 model.ApiValidationPort.Value,
220 true,
221 cancellationToken);
222 if (verifiedPort != model.ApiValidationPort)
223 return Conflict(new ErrorMessageResponse(ErrorCode.PortNotAvailable));
224
225 hostModel.ApiValidationPort = model.ApiValidationPort;
226 }
227 }
228
229 if (model.ApiValidationSecurityLevel.HasValue)
230 {
231 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetSecurityLevel))
232 return Forbid();
233
234 hostModel.ApiValidationSecurityLevel = model.ApiValidationSecurityLevel;
235 }
236
237#pragma warning disable CS0618 // Type or member is obsolete
238 bool? legacyRequireDMApiValidation = model.RequireDMApiValidation;
239#pragma warning restore CS0618 // Type or member is obsolete
240 if (legacyRequireDMApiValidation.HasValue)
241 {
242 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetApiValidationRequirement))
243 return Forbid();
244
247 : DMApiValidationMode.Optional;
248 }
249
250 if (model.DMApiValidationMode.HasValue)
251 {
252 if (legacyRequireDMApiValidation.HasValue)
253 return BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure));
254
255 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetApiValidationRequirement))
256 return Forbid();
257
258 hostModel.DMApiValidationMode = model.DMApiValidationMode;
259 }
260
261 if (model.Timeout.HasValue)
262 {
263 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetTimeout))
264 return Forbid();
265
266 hostModel.Timeout = model.Timeout;
267 }
268
269 if (model.CompilerAdditionalArguments != null)
270 {
271 if (!dreamMakerRights.HasFlag(DreamMakerRights.SetCompilerArguments))
272 return Forbid();
273
274 var sanitizedArguments = model.CompilerAdditionalArguments.Trim();
275 if (sanitizedArguments.Length == 0)
277 else
279 }
280
281 await DatabaseContext.Save(cancellationToken);
282
283 if (!dreamMakerRights.HasFlag(DreamMakerRights.Read))
284 return NoContent();
285
286 return await Read(cancellationToken);
287 }
288#pragma warning restore CA1506
289
296 .Include(x => x.Job!)
297 .ThenInclude(x => x.StartedBy)
298 .Include(x => x.Job!)
299 .ThenInclude(x => x.Instance)
300 .Include(x => x.RevisionInformation!)
301 .ThenInclude(x => x.PrimaryTestMerge!)
302 .ThenInclude(x => x.MergedBy)
303 .Include(x => x.RevisionInformation)
304 .ThenInclude(x => x.ActiveTestMerges!)
305 .ThenInclude(x => x!.TestMerge)
306 .ThenInclude(x => x!.MergedBy)
307 .Where(x => x.Job.Instance!.Id == Instance.Id);
308 }
309}
virtual ? long Id
The ID of the entity.
Definition EntityId.cs:14
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.