6using System.Threading.Tasks;
8using Microsoft.AspNetCore.Authorization;
9using Microsoft.AspNetCore.Mvc;
10using Microsoft.Extensions.Logging;
11using Microsoft.Extensions.Options;
28 [ApiExplorerSettings(IgnoreApi =
true)]
50 readonly ILogger<SwarmController>
logger;
67 IOptions<SwarmConfiguration> swarmConfigurationOptions,
68 ILogger<SwarmController>
logger)
72 swarmConfiguration = swarmConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(swarmConfigurationOptions));
85 ArgumentNullException.ThrowIfNull(registrationRequest);
88 if (registrationRequest.ServerVersion?.Major != swarmProtocolVersion.Major)
89 return StatusCode((
int)HttpStatusCode.UpgradeRequired);
92 if (registrationResult ==
null)
95 if (registrationRequest.ServerVersion != swarmProtocolVersion)
96 logger.LogWarning(
"Allowed node {identifier} to register despite having a slightly different swarm protocol version!", registrationRequest.Identifier);
98 return Json(registrationResult);
107 public async ValueTask<IActionResult>
UnregisterNode(CancellationToken cancellationToken)
136 [Produces(MediaTypeNames.Application.Octet)]
137 public ValueTask<IActionResult>
GetUpdatePackage([FromQuery]
string ticket, CancellationToken cancellationToken)
138 =>
transferService.GenerateDownloadResponse(
this, ticket, cancellationToken);
148 ArgumentNullException.ThrowIfNull(serversUpdateRequest);
150 if (serversUpdateRequest.SwarmServers ==
null)
169 ArgumentNullException.ThrowIfNull(updateRequest);
187 public async ValueTask<IActionResult>
CommitUpdate(CancellationToken cancellationToken)
213 protected override async ValueTask<IActionResult?>
HookExecuteAction(Func<Task> executeAction, CancellationToken cancellationToken)
217 logger.LogTrace(
"Swarm request from {remoteIP}...", Request.HttpContext.Connection.RemoteIpAddress);
220 logger.LogDebug(
"Attempted swarm request without private key configured!");
225 && apiKeyHeaderValues.Count == 1
228 logger.LogDebug(
"Unauthorized swarm request!");
233 && registrationHeaderValues.Count == 1
234 && Guid.TryParse(registrationHeaderValues.First(), out var registrationId)))
236 logger.LogDebug(
"Swarm request without registration ID!");
241 if (ModelState?.IsValid ==
false)
243 var errorMessages = ModelState
244 .SelectMany(x => x.Value!.Errors)
245 .Select(x => x.ErrorMessage);
248 "Swarm request model validation failed!{newLine}{messages}",
250 String.Join(Environment.NewLine, errorMessages));
254 logger.LogTrace(
"Starting swarm request processing...");
255 await executeAction();
Configuration for the server swarm system.
string? PrivateKey
The private key used for swarm communication.
Base class for all API style controllers.
For swarm server communication.
readonly ISwarmOperations swarmOperations
The ISwarmOperations for the SwarmController.
bool ValidateRegistration()
Check that the RequestRegistrationId is valid.
ValueTask< IActionResult > GetUpdatePackage([FromQuery] string ticket, CancellationToken cancellationToken)
Endpoint to retrieve server update packages.
async ValueTask< IActionResult > Register([FromBody] SwarmRegistrationRequest registrationRequest, CancellationToken cancellationToken)
Registration endpoint.
IActionResult HealthCheck()
Health check endpoint.
override async ValueTask< IActionResult?> HookExecuteAction(Func< Task > executeAction, CancellationToken cancellationToken)
Hook for executing a request.A ValueTask<TResult> resulting in an IActionResult that,...
IActionResult UpdateNodeList([FromBody] SwarmServersUpdateRequest serversUpdateRequest)
Node list update endpoint.
readonly SwarmConfiguration swarmConfiguration
The SwarmConfiguration for the SwarmController.
SwarmController(ISwarmOperations swarmOperations, IFileTransferStreamHandler transferService, IOptions< SwarmConfiguration > swarmConfigurationOptions, ILogger< SwarmController > logger)
Initializes a new instance of the SwarmController class.
readonly ILogger< SwarmController > logger
The ILogger for the SwarmController.
async ValueTask< IActionResult > PrepareUpdate([FromBody] SwarmUpdateRequest updateRequest, CancellationToken cancellationToken)
Update initiation endpoint.
async ValueTask< IActionResult > AbortUpdate()
Update abort endpoint.
readonly IFileTransferStreamHandler transferService
The IFileTransferStreamHandler for the SwarmController.
async ValueTask< IActionResult > CommitUpdate(CancellationToken cancellationToken)
Update commit endpoint.
async ValueTask< IActionResult > UnregisterNode(CancellationToken cancellationToken)
Deregistration endpoint.
Attribute for bringing in the master versions list from MSBuild that aren't embedded into assemblies ...
string RawSwarmProtocolVersion
The Version string of the TGS swarm protocol.
static MasterVersionsAttribute Instance
Return the Assembly's instance of the MasterVersionsAttribute.
Constants used by the swarm system.
const string ApiKeyHeader
The header used to pass in the Configuration.SwarmConfiguration.PrivateKey.
const string UpdateRoute
The route used for swarm updates.
const string ControllerRoute
The base route for Controllers.SwarmController.
const string RegisterRoute
The route used for swarm registration.
const string RegistrationIdHeader
The header used to pass in swarm registration IDs.
A request to register with a swarm controller.
A request to update a nodes list of SwarmServers.
A request to update the swarm's TGS version.
Helpers for manipulating the Serilog.Context.LogContext.
const string RequestPathContextProperty
The Serilog.Context.LogContext property name for Models.User Api.Models.EntityId.Ids.
Swarm service operations for the Controllers.SwarmController.
ValueTask UnregisterNode(Guid registrationId, CancellationToken cancellationToken)
Attempt to unregister a node with a given registrationId with the controller.
ValueTask< bool > RemoteCommitReceived(Guid registrationId, CancellationToken cancellationToken)
Notify the controller that the node with the given registrationId is ready to commit or notify the n...
ValueTask< bool > PrepareUpdateFromController(SwarmUpdateRequest updateRequest, CancellationToken cancellationToken)
Notify the node of an update request from the controller.
bool ValidateRegistration(Guid registrationId)
Validate a given registrationId .
ValueTask< SwarmRegistrationResponse?> RegisterNode(SwarmServer node, Guid registrationId, CancellationToken cancellationToken)
Attempt to register a given node with the controller.
void UpdateSwarmServersList(IEnumerable< SwarmServerInformation > swarmServers)
Pass in an updated list of swarmServers to the node.
ValueTask AbortUpdate()
Attempt to abort an uncommitted update.
Reads and writes to Streams associated with FileTicketResponses.
@ Unauthorized
The swarm private keys didn't match.