From e2a4d60d8e4f4e2025c179c4ca8a31803d107776 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 12 Dec 2023 12:37:08 +0100 Subject: [PATCH] fix(pkg): fixed docker multiplexed output function. Signed-off-by: Federico Di Pierro --- pkg/driverbuilder/docker.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/pkg/driverbuilder/docker.go b/pkg/driverbuilder/docker.go index 935f065d..e5d604e8 100644 --- a/pkg/driverbuilder/docker.go +++ b/pkg/driverbuilder/docker.go @@ -29,6 +29,7 @@ import ( "os" "runtime" "strconv" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" @@ -286,7 +287,7 @@ func (bp *DockerBuildProcessor) Start(b *builder.Build) error { return err } - hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{Tty: false}) + hr, err := cli.ContainerExecAttach(ctx, edata.ID, types.ExecStartCheck{}) if err != nil { return err } @@ -396,20 +397,43 @@ func forwardLogs(logPipe io.Reader) { func multiplexedForwardLogs(logPipe io.Reader) { hdr := make([]byte, 8) for { + // Load size of message _, err := logPipe.Read(hdr) if err == io.EOF { break } if err != nil { slog.With("err", err.Error()).Error("log pipe error") + return } count := binary.BigEndian.Uint32(hdr[4:]) + + // Read message dat := make([]byte, count) - _, err = logPipe.Read(dat) - if err != nil { - slog.With("err", err.Error()).Error("log pipe error") + var readCnt int + for uint32(readCnt) < count { + readBytes, err := logPipe.Read(dat[readCnt:]) + readCnt += readBytes + if err == io.EOF { + if uint32(readCnt) == count { + break + } + slog.With("err", err.Error()).Error("log pipe error") + return + } + if err != nil { + slog.With("err", err.Error()).Error("log pipe error") + return + } + } + + // Print message line by line + lines := strings.Split(string(dat), "\n") + for _, line := range lines { + if line != "" { + slog.Debug(line) + } } - slog.Debug(string(dat)) } slog.Debug("log pipe close") }