Esempio n. 1
0
        private async Task StartBuildInternal(Project project, string userId, int buildId)
        {
            var destination = new DirectoryInfo(Path.GetTempPath()).ResolveDir("portal_artifact").ResolveDir(project.Id).ResolveDir(buildId.ToString());

            if (!destination.Exists)
            {
                destination.Create();
            }
            _logger.LogInformation("Creating container...");
            var repo      = _dockerSetting.ImageName;
            var tag       = $"mc-{project.MinecraftVersion.Version}-{project.MinecraftVersion.DockerImageVersion}";
            var container = await _client.Containers.CreateContainerAsync(new CreateContainerParameters
            {
                //Name = "portal-build-daemon",
                Image      = $"{repo}:{tag}",
                HostConfig = new HostConfig
                {
                    Binds = new List <string>
                    {
                        $"{_storageSetting.GetProjectStorageSetting().GetRootDirectory().ResolveDir(project.Id).FullName}:{_dockerSetting.SourceDir}:ro",
                        $"{destination.FullName}:{_dockerSetting.BuildDir}:rw"
                    }
                },
                AttachStdout = true,
                AttachStderr = true,
                Tty          = true
            });

            var buffer = new byte[_dockerSetting.OutputBufferSize];

            using (var stream = await _client.Containers.AttachContainerAsync(container.ID, true, new ContainerAttachParameters
            {
                Stdout = true,
                Stderr = true,
                Stream = true
            }))
            {
                await _client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters());

                ReadResult result;
                do
                {
                    result = await stream.ReadOutputAsync(buffer, 0, buffer.Length, default(CancellationToken));

                    var text = Encoding.UTF8.GetString(buffer, 0, result.Count);
                    Console.Write(result.Target + text);
                    _connectionManager.SendMessage(project.Id, userId, text);
                } while (!result.EOF);
            }
            _logger.LogInformation("Build finished. Removing container...");
            await _client.Containers.RemoveContainerAsync(container.ID, new ContainerRemoveParameters());

            _logger.LogInformation("Container removed.");
            var artifactFile = Util.FindBuildArtifact(destination);

            if (artifactFile == null)
            {
                _logger.LogWarning($"Artifact file not found. ( project: {project.Id}, buildId: {buildId})");
                return;
            }
            await _storageSetting.GetArtifactStorageSetting().AfterBuildAsync(project.Id, artifactFile, buildId);

            if (_dockerSetting.DeleteTempAfterBuild)
            {
                destination.Delete(true);
            }
        }