From 268db982658d4401fd91e84d049ac5ac38cd4164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Guid=C3=A9e?= Date: Sat, 9 Mar 2024 13:15:03 -0500 Subject: [PATCH] feat(server): send more container logs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Quentin Guidée --- .../apps/containers/adapter/runner_docker.go | 26 +++++++++++---- .../apps/containers/core/service/container.go | 33 +++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/server/apps/containers/adapter/runner_docker.go b/server/apps/containers/adapter/runner_docker.go index d0c47ba1..95e8d60e 100644 --- a/server/apps/containers/adapter/runner_docker.go +++ b/server/apps/containers/adapter/runner_docker.go @@ -87,8 +87,9 @@ func (a runnerDockerAdapter) Start( // Build stdout, err := a.buildImageFromName(ctx, c.GetImageNameWithTag()) if err != nil { - log.Error(err) + log.Error(err, vlog.String("step", "build")) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) return } @@ -105,8 +106,10 @@ func (a runnerDockerAdapter) Start( err := json.Unmarshal(scanner.Bytes(), &msg) if err != nil { log.Error(err, + vlog.String("step", "build"), vlog.String("text", scanner.Text()), vlog.String("id", c.ID.String())) + _, _ = wErr.Write([]byte(err.Error() + "\n")) continue } @@ -123,24 +126,30 @@ func (a runnerDockerAdapter) Start( progressJSON, err := json.Marshal(progress) if err != nil { log.Error(err, + vlog.String("step", "build"), vlog.String("text", scanner.Text()), vlog.String("id", c.ID.String())) + _, _ = wErr.Write([]byte(err.Error() + "\n")) continue } _, err = fmt.Fprintf(wOut, "%s %s\n", "DOWNLOAD", progressJSON) if err != nil { log.Error(err, + vlog.String("step", "build"), vlog.String("text", scanner.Text()), vlog.String("id", c.ID.String())) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) return } } if scanner.Err() != nil { log.Error(scanner.Err(), + vlog.String("step", "build"), vlog.String("id", c.ID.String())) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) return } }() @@ -182,13 +191,15 @@ func (a runnerDockerAdapter) Start( id, err = a.createContainer(ctx, opts) if err != nil { - log.Error(err) + log.Error(err, vlog.String("step", "create")) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) return } } else if err != nil { - log.Error(err) + log.Error(err, vlog.String("step", "create")) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) return } @@ -196,8 +207,9 @@ func (a runnerDockerAdapter) Start( cli := containersapi.NewContainersKernelClient(ctx) err = cli.StartContainer(context.Background(), id) if err != nil { - log.Error(err) + log.Error(err, vlog.String("step", "start")) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) return } setStatus(types.ContainerStatusRunning) @@ -205,8 +217,9 @@ func (a runnerDockerAdapter) Start( var stderr io.ReadCloser stdout, stderr, err = a.readLogs(ctx, id) if err != nil { - log.Error(err) + log.Error(err, vlog.String("step", "read logs")) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) return } @@ -234,8 +247,9 @@ func (a runnerDockerAdapter) Start( err = a.WaitCondition(ctx, c, types.WaitContainerCondition(container.WaitConditionNotRunning)) if err != nil { - log.Error(err) + log.Error(err, vlog.String("step", "wait condition")) setStatus(types.ContainerStatusError) + _, _ = wErr.Write([]byte(err.Error() + "\n")) } else { setStatus(types.ContainerStatusOff) } diff --git a/server/apps/containers/core/service/container.go b/server/apps/containers/core/service/container.go index 6559812b..8eefa8a3 100644 --- a/server/apps/containers/core/service/container.go +++ b/server/apps/containers/core/service/container.go @@ -388,6 +388,13 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error { stdout, stderr, err := s.runner.Start(ctx, c, ports, volumes, env, caps, sysctls, setStatus) if err != nil { s.setStatus(c, types.ContainerStatusError) + + s.ctx.DispatchEvent(types.EventContainerLog{ + ContainerID: id, + Kind: types.LogKindVertexErr, + Message: types.NewLogLineMessageString(err.Error()), + }) + return err } @@ -401,6 +408,13 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error { err := json.Unmarshal([]byte(msg), &downloadProgress) if err != nil { log.Error(err) + + s.ctx.DispatchEvent(types.EventContainerLog{ + ContainerID: id, + Kind: types.LogKindOut, + Message: types.NewLogLineMessageString(err.Error()), + }) + continue } @@ -420,6 +434,12 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error { } if scanner.Err() != nil { log.Error(scanner.Err()) + + s.ctx.DispatchEvent(types.EventContainerLog{ + ContainerID: id, + Kind: types.LogKindErr, + Message: types.NewLogLineMessageString(scanner.Err().Error()), + }) } }() @@ -432,8 +452,15 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error { Message: types.NewLogLineMessageString(scanner.Text()), }) } + if scanner.Err() != nil { log.Error(scanner.Err()) + + s.ctx.DispatchEvent(types.EventContainerLog{ + ContainerID: id, + Kind: types.LogKindErr, + Message: types.NewLogLineMessageString(scanner.Err().Error()), + }) } }() @@ -446,6 +473,12 @@ func (s *containerService) Start(ctx context.Context, id uuid.UUID) error { err := s.WaitStatus(ctx, id, types.ContainerStatusRunning) if err != nil { log.Error(err) + + s.ctx.DispatchEvent(types.EventContainerLog{ + ContainerID: id, + Kind: types.LogKindVertexErr, + Message: types.NewLogLineMessageString(err.Error()), + }) } }() wg.Wait()