Skip to content

Commit

Permalink
Add steps logs to shared output (saved in artifact).
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Nov 29, 2023
1 parent a0d98d8 commit 853ab47
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
17 changes: 7 additions & 10 deletions provisioner/internal/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"log/slog"
"os"
"sync"
"time"

Expand All @@ -17,7 +16,6 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/gammadia/alfred/proto"
"github.com/gammadia/alfred/scheduler"
"github.com/samber/lo"
Expand Down Expand Up @@ -309,17 +307,16 @@ func RunContainer(
return fmt.Errorf("failed to start docker container for step %d: %w", stepIndex, err)
}

out, err := docker.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Timestamps: true, Details: true})
if err != nil {
return fmt.Errorf("failed to get docker container logs for step %d: %w", stepIndex, err)
}
defer out.Close()

_, _ = stdcopy.StdCopy(os.Stdout, os.Stderr, out)

// Wait for the container to finish
select {
case status = <-wait:
tryTo(
"save container logs",
func() error {
return taskFs.SaveContainerLogs(resp.ID, fmt.Sprintf("/output/step-%d.log", stepIndex))
},
)

// Container is done
if status.StatusCode != 0 {
return fmt.Errorf("step %d failed with status: %d", stepIndex, status.StatusCode)
Expand Down
5 changes: 5 additions & 0 deletions provisioner/internal/workspacefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type WorkspaceFS interface {
HostPath(p string) string
MkDir(p string) error
SaveContainerLogs(containerId, p string) error
Archive(p string) (io.ReadCloser, error)
Delete(p string) error
Scope(p string) WorkspaceFS
Expand All @@ -29,6 +30,10 @@ func (f *ScopedFS) MkDir(p string) error {
return f.Parent.MkDir(path.Join(f.Prefix, p))
}

func (f *ScopedFS) SaveContainerLogs(containerId, p string) error {
return f.Parent.SaveContainerLogs(containerId, path.Join(f.Prefix, p))
}

func (f *ScopedFS) Archive(p string) (io.ReadCloser, error) {
return f.Parent.Archive(path.Join(f.Prefix, p))
}
Expand Down
11 changes: 11 additions & 0 deletions provisioner/local/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ func (f *fs) MkDir(p string) error {
return os.MkdirAll(f.HostPath(p), 0777)
}

func (f *fs) SaveContainerLogs(containerId, p string) error {
cmd := exec.Command("docker", "logs", "--timestamps", containerId)
if out, err := os.Create(f.HostPath(p)); err != nil {
return err
} else {
cmd.Stdout = out
cmd.Stderr = out
}
return cmd.Run()
}

func (f *fs) Archive(p string) (rc io.ReadCloser, err error) {
cmd := exec.Command("tar", "-c", "-f", "-", "-z", "-C", f.root, strings.TrimLeft(p, "/"))
if rc, err = cmd.StdoutPipe(); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions provisioner/openstack/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ func (f *fs) MkDir(p string) error {
})
}

func (f *fs) SaveContainerLogs(containerId, p string) error {
session, err := f.ssh.NewSession()
if err != nil {
return fmt.Errorf("failed to create SSH session: %w", err)
}
defer session.Close()

return session.Run(fmt.Sprintf(
"docker logs --timestamps %s 2>&1 > %s",
shellescape.Quote(containerId),
shellescape.Quote(f.HostPath(p)),
))
}

// Archive returns a .tar.gz of the given path
func (f *fs) Archive(p string) (io.ReadCloser, error) {
session, err := f.ssh.NewSession()
Expand Down

0 comments on commit 853ab47

Please sign in to comment.