Ejemplo n.º 1
0
    public static async Task PipeContainerOutput(IOutputObserver outputObserver, MultiplexedStream stream, CancellationToken cancellationToken)
    {
        byte[] buffer = new byte[1024];
        while (!cancellationToken.IsCancellationRequested)
        {
            var result = await stream.ReadOutputAsync(buffer, 0, buffer.Length, cancellationToken);

            if (result.EOF)
            {
                break;
            }

            switch (result.Target)
            {
            case MultiplexedStream.TargetStream.StandardOut:
                await outputObserver.StandardOutput(buffer, result.Count);

                break;

            case MultiplexedStream.TargetStream.StandardError:
                await outputObserver.StandardError(buffer, result.Count);

                break;
            }
        }
    }
Ejemplo n.º 2
0
        public async Task <int> RunBuild(Protocol.RunDockerBuild build, IOutputObserver outputObserver, CancellationToken cancellationToken)
        {
            await CleanupNetworks(cancellationToken);

            CreateContainerResponse?proxyContainer = null;

            try {
                proxyContainer = await docker.Containers.CreateContainerAsync(new CreateContainerParameters {
                    Image = build.ProxyImage,
                    Env   = build.EnableNetwork
                        ? null
                        : new List <string> {
                        "HELIUM_PROXY_REPLAY=true"
                    },
                    HostConfig = new HostConfig {
                        AutoRemove  = true,
                        NetworkMode = build.EnableNetwork ? null : "none",
                        Isolation   = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "process" : null,
                    },
                }, cancellationToken);

                NetworksCreateResponse?network = null;
                try {
                    var networkName = networkTag + "-" + proxyContainer.ID;
                    network = await docker.Networks.CreateNetworkAsync(
                        new NetworksCreateParameters {
                        Name     = networkName,
                        Internal = true,
                    },
                        cancellationToken
                        );

                    await docker.Networks.ConnectNetworkAsync(
                        network.ID,
                        new NetworkConnectParameters {
                        Container      = proxyContainer.ID,
                        EndpointConfig = new EndpointSettings {
                            Aliases = new List <string> {
                                buildProxyHostname,
                            },
                        },
                    },
                        cancellationToken
                        );


                    var    buildMapping = new Dictionary <string, string>();
                    string?imageId      = null;

                    foreach (var imageBuild in build.Dockerfile.Builds)
                    {
                        var id = await RunSingleImageBuild(imageBuild, network.ID, buildMapping, outputObserver, cancellationToken);

                        if (imageBuild.FromCommand.AsName != null)
                        {
                            buildMapping.Add(imageBuild.FromCommand.AsName, id);
                        }

                        imageId = id;
                    }

                    if (imageId == null)
                    {
                        throw new Exception("No images were built");
                    }

                    await using (var imageFile = File.Create(build.OutputFile)) {
                        await docker.Images.SaveImageAsync(imageId, cancellationToken);
                    }

                    return(0);
                }
                finally {
                    if (network != null)
                    {
                        await docker.Networks.DeleteNetworkAsync(network.ID, cancellationToken);
                    }
                }
            }
            catch (RunCommandFailedException ex) {
                await outputObserver.StandardOutput(ex.Message);

                return(ex.ExitCode);
            }
            catch (Exception ex) {
                await outputObserver.StandardOutput(ex.Message);

                return(1);
            }
            finally {
                if (proxyContainer != null)
                {
                    await docker.Containers.RemoveContainerAsync(
                        proxyContainer.ID,
                        new ContainerRemoveParameters { Force = true },
                        cancellationToken
                        );
                }
            }
        }