Skip to content

Commit

Permalink
fix: docker service
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Sep 12, 2022
1 parent fb2d8f5 commit 964e61f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
6 changes: 6 additions & 0 deletions GZCTF/Controllers/EditController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CTFServer.Extensions;
using CTFServer.Middlewares;
using CTFServer.Models;
using CTFServer.Models.Request.Edit;
using CTFServer.Models.Request.Game;
using CTFServer.Models.Request.Info;
Expand All @@ -23,6 +24,7 @@ namespace CTFServer.Controllers;
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status403Forbidden)]
public class EditController : Controller
{
private readonly ILogger<EditController> logger;
private readonly UserManager<UserInfo> userManager;
private readonly IPostRepository postRepository;
private readonly IGameNoticeRepository gameNoticeRepository;
Expand All @@ -33,6 +35,7 @@ public class EditController : Controller
private readonly IContainerRepository containerRepository;

public EditController(UserManager<UserInfo> _userManager,
ILogger<EditController> _logger,
IPostRepository _postRepository,
IContainerRepository _containerRepository,
IChallengeRepository _challengeRepository,
Expand All @@ -41,6 +44,7 @@ public EditController(UserManager<UserInfo> _userManager,
IContainerService _containerService,
IFileRepository _fileService)
{
logger = _logger;
fileService = _fileService;
userManager = _userManager;
gameRepository = _gameRepository;
Expand Down Expand Up @@ -582,6 +586,8 @@ public async Task<IActionResult> CreateTestContainer([FromRoute] int id, [FromRo
challenge.TestContainer = container;
await challengeRepository.SaveAsync(token);

logger.Log($"成功创建测试容器 {container.ContainerId}", user, TaskStatus.Success);

return Ok(ContainerInfoModel.FromContainer(container));
}

Expand Down
44 changes: 23 additions & 21 deletions GZCTF/Services/DockerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public DockerService(IOptions<DockerConfig> _options, IOptions<RegistryConfig> _
DockerClientConfiguration cfg = string.IsNullOrEmpty(this.options.Uri) ? new() : new(new Uri(this.options.Uri));

// TODO: Docker Auth Required
// TODO: Docker Swarm Support

dockerClient = cfg.CreateClient();

// Auth for registry
Expand Down Expand Up @@ -72,7 +70,7 @@ public async Task DestroyContainer(Container container, CancellationToken token
}
catch (Exception e)
{
logger.LogError(e, "删除容器失败");
logger.LogError(e, $"容器 {container.ContainerId} 删除失败");
return;
}

Expand Down Expand Up @@ -164,7 +162,7 @@ private ServiceCreateParameters GetServiceCreateParameters(ContainerConfig confi
}
catch (Exception e)
{
logger.LogError(e.Message, e);
logger.LogError(e, $"容器 {parameters.Service.Name} 删除失败");
return null;
}

Expand All @@ -174,13 +172,20 @@ private ServiceCreateParameters GetServiceCreateParameters(ContainerConfig confi
Image = config.Image,
};

var res = await dockerClient.Swarm.InspectServiceAsync(serviceRes.ID, token);

if (res?.Endpoint?.Ports is null || res.Endpoint.Ports.Count == 0)
retry = 0;
SwarmService? res;
do
{
logger.SystemLog($"容器 {parameters.Service.Name} 创建后未获取到端口暴露信息,这可能是意料外的行为", TaskStatus.Fail, LogLevel.Warning);
return null;
}
res = await dockerClient.Swarm.InspectServiceAsync(serviceRes.ID, token);
retry++;
if (retry == 3)
{
logger.SystemLog($"容器 {parameters.Service.Name} 创建后未获取到端口暴露信息,创建失败", TaskStatus.Fail, LogLevel.Warning);
return null;
}
if (res is not { Endpoint.Ports.Count: > 0 })
await Task.Delay(500, token);
} while (res is not { Endpoint.Ports.Count: > 0 });

var port = res.Endpoint.Ports.First();

Expand Down Expand Up @@ -222,17 +227,14 @@ await dockerClient.Images.CreateImageAsync(new()
return null;
}

if (containerRes is null)
try
{
try
{
containerRes = await dockerClient.Containers.CreateContainerAsync(parameters, token);
}
catch (Exception e)
{
logger.LogError(e.Message, e);
return null;
}
containerRes ??= await dockerClient.Containers.CreateContainerAsync(parameters, token);
}
catch (Exception e)
{
logger.LogError(e.Message, e);
return null;
}

Container container = new()
Expand All @@ -250,7 +252,7 @@ await dockerClient.Images.CreateImageAsync(new()
retry++;
if (retry == 3)
{
logger.SystemLog($"启动容器实例 {container.Id[..12]} ({config.Image.Split("/").LastOrDefault()}) 失败", TaskStatus.Fail, LogLevel.Warning);
logger.SystemLog($"启动容器实例 {container.Id} ({config.Image.Split("/").LastOrDefault()}) 失败", TaskStatus.Fail, LogLevel.Warning);
return null;
}
if (!started)
Expand Down

0 comments on commit 964e61f

Please sign in to comment.