Skip to content

Commit

Permalink
Improve log attachment at buildtime
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jun 10, 2018
1 parent 46d7ac5 commit 0640e32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
22 changes: 2 additions & 20 deletions daemon/inertiad/project/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,10 @@ func dockerCompose(d *Deployment, cli *docker.Client, out io.Writer) (func() err
if err != nil {
return nil, err
}

// Attach logs and report build progress until container exits
reader, err := ContainerLogs(cli, LogOptions{
Container: resp.ID, Stream: true,
NoTimestamps: true,
})
if err != nil {
return nil, err
}
stop := make(chan struct{})
go log.FlushRoutine(out, reader, stop)
go StreamContainerLogs(cli, resp.ID, out, stop)
status, err := cli.ContainerWait(ctx, resp.ID)
close(stop)
reader.Close()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -250,18 +240,10 @@ func herokuishBuild(d *Deployment, cli *docker.Client, out io.Writer) (func() er
}

// Attach logs and report build progress until container exits
reader, err := ContainerLogs(cli, LogOptions{
Container: resp.ID, Stream: true,
NoTimestamps: true,
})
if err != nil {
return nil, err
}
stop := make(chan struct{})
go log.FlushRoutine(out, reader, stop)
go StreamContainerLogs(cli, resp.ID, out, stop)
status, err := cli.ContainerWait(ctx, resp.ID)
close(stop)
reader.Close()
if err != nil {
return nil, err
}
Expand Down
18 changes: 18 additions & 0 deletions daemon/inertiad/project/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
docker "github.com/docker/docker/client"
"github.com/ubclaunchpad/inertia/daemon/inertiad/log"
)

var (
Expand Down Expand Up @@ -85,3 +86,20 @@ func stopActiveContainers(cli *docker.Client, out io.Writer) error {
_, err = cli.ContainersPrune(ctx, filters.Args{})
return err
}

// StreamContainerLogs streams logs from given container ID. Best used as a
// goroutine.
func StreamContainerLogs(client *docker.Client, id string, out io.Writer,
stop chan struct{}) error {
// Attach logs and report build progress until container exits
reader, err := ContainerLogs(client, LogOptions{
Container: id, Stream: true,
NoTimestamps: true,
})
if err != nil {
return err
}
defer reader.Close()
log.FlushRoutine(out, reader, stop)
return nil
}

0 comments on commit 0640e32

Please sign in to comment.