Пример #1
0
        public async Task <ServerStartResult> Start(Server server)
        {
            using (ClaimResource(server.ID))
            {
                try
                {
                    _logger.LogInformation($"About to start server. ServerID: {server.ID}");

                    if (server.Image == null)
                    {
                        throw new ArgumentNullException("server.Image", "No image has been set on server");
                    }

                    if (string.IsNullOrEmpty(server.ContainerID) ||
                        await DockerQueryHelper.GetContainer(_dockerClient, server.ContainerID) == null ||
                        server.NeedsRecreate)
                    {
                        _logger.LogInformation($"Needs to create container before starting. ServerID: {server.ID}");

                        if (server.Image.BuildStatus != null && !server.Image.BuildStatus.BuildSucceeded)
                        {
                            throw new Exception($"Unable to start server! Image build did not succeed. ServerID: {server.ID}");
                        }

                        if (string.IsNullOrEmpty(server.Image.DockerId) || server.Image.BuildStatus == null)
                        {
                            _logger.LogInformation($"Unable to create container underlying image not yet built. Marking server for later start. ServerID: {server.ID}");

                            if (!_context.StartupQueue.Any(x => x.ServerId == server.ID))
                            {
                                _context.StartupQueue.Add(new ServerStartupQueue
                                {
                                    ServerId = server.ID
                                });

                                await _context.SaveChangesAsync();
                            }

                            return(ServerStartResult.LaterStart);
                        }

                        if (!string.IsNullOrEmpty(server.ContainerID))
                        {
                            await DestroyContainerInternal(server);
                        }

                        await CreateContainer(server);
                    }

                    WriteServerProperties(server);

                    var result = await _dockerClient.Containers.StartContainerAsync(server.ContainerID, new ContainerStartParameters
                    {
                    });

                    if (!result)
                    {
                        throw new Exception("Server failed to start");
                    }

                    _logger.LogInformation($"Server started. ServerID: {server.ID}");

                    _context.Update(server);
                    await _context.SaveChangesAsync();

                    await _infoClient.ServerStarted(server.ID);

                    return(ServerStartResult.Success);
                }
                catch (Exception e)
                {
                    _logger.LogError(new EventId(), e, $"Error occurred when starting server. ServerID: {server.ID}");

                    return(ServerStartResult.Fail);
                }
            }
        }