Allow instance attaching/detaching

This commit is contained in:
Cyberboss
2018-08-15 14:04:09 -04:00
parent bbe112b741
commit 7c223f428c
@@ -29,6 +29,11 @@ namespace Tgstation.Server.Host.Controllers
[Route(Routes.InstanceManager)]
public sealed class InstanceController : ModelController<Api.Models.Instance>
{
/// <summary>
/// File name to allow attaching instances
/// </summary>
const string InstanceAttachFileName = "TGS4_ALLOW_INSTANCE_ATTACH";
/// <summary>
/// The <see cref="IJobManager"/> for the <see cref="InstanceController"/>
/// </summary>
@@ -107,7 +112,8 @@ namespace Tgstation.Server.Host.Controllers
NormalizeModelPath(model, out var rawPath);
var dirExistsTask = ioManager.DirectoryExists(model.Path, cancellationToken);
if (await ioManager.FileExists(model.Path, cancellationToken).ConfigureAwait(false) || await dirExistsTask.ConfigureAwait(false))
return Conflict(new ErrorMessage { Message = "Path not empty!" });
if(!await ioManager.FileExists(ioManager.ConcatPath(model.Path, InstanceAttachFileName), cancellationToken).ConfigureAwait(false))
return Conflict(new ErrorMessage { Message = "Path not empty!" });
var newInstance = new Models.Instance
{
@@ -154,7 +160,8 @@ namespace Tgstation.Server.Host.Controllers
try
{
//actually reserve it now
await ioManager.CreateDirectory(rawPath, default).ConfigureAwait(false);
await ioManager.CreateDirectory(rawPath, cancellationToken).ConfigureAwait(false);
await ioManager.DeleteFile(ioManager.ConcatPath(rawPath, InstanceAttachFileName), cancellationToken).ConfigureAwait(false);
}
catch
{
@@ -169,7 +176,7 @@ namespace Tgstation.Server.Host.Controllers
{
return Conflict(new ErrorMessage { Message = e.Message });
}
catch (DbUpdateConcurrencyException e)
catch (DbUpdateException e)
{
return Conflict(new ErrorMessage{ Message = e.Message });
}
@@ -201,7 +208,17 @@ namespace Tgstation.Server.Host.Controllers
}
DatabaseContext.Instances.Remove(originalModel);
await DatabaseContext.Save(cancellationToken).ConfigureAwait(false); //cascades everything
var attachFileName = ioManager.ConcatPath(originalModel.Path, InstanceAttachFileName);
await ioManager.WriteAllBytes(attachFileName, Array.Empty<byte>(), default).ConfigureAwait(false);
try
{
await DatabaseContext.Save(cancellationToken).ConfigureAwait(false); //cascades everything
}
catch (DbUpdateException e)
{
return Conflict(new ErrorMessage { Message = e.Message });
}
return Ok();
}