(dron-254) handle wait for log transfer
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/drone-runners/drone-runner-docker/internal/docker/errors"
|
"github.com/drone-runners/drone-runner-docker/internal/docker/errors"
|
||||||
@@ -202,11 +203,24 @@ func (e *Docker) Run(ctx context.Context, specv runtime.Spec, stepv runtime.Step
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.TrimExtraInfo(err)
|
return nil, errors.TrimExtraInfo(err)
|
||||||
}
|
}
|
||||||
|
// this is an experimental feature that closes logging as the last step
|
||||||
|
var allowDeferTailLog = os.Getenv("DRONE_DEFER_TAIL_LOG") == "true"
|
||||||
|
if allowDeferTailLog {
|
||||||
// tail the container
|
// tail the container
|
||||||
|
logger.FromContext(ctx).
|
||||||
|
WithField("step id", step.ID).
|
||||||
|
Debugln("using deferred docker tail")
|
||||||
|
logs, tailErr := e.deferTail(ctx, step.ID, output)
|
||||||
|
if tailErr != nil {
|
||||||
|
return nil, errors.TrimExtraInfo(tailErr)
|
||||||
|
}
|
||||||
|
defer logs.Close()
|
||||||
|
} else {
|
||||||
err = e.tail(ctx, step.ID, output)
|
err = e.tail(ctx, step.ID, output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.TrimExtraInfo(err)
|
return nil, errors.TrimExtraInfo(err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// wait for the response
|
// wait for the response
|
||||||
return e.waitRetry(ctx, step.ID)
|
return e.waitRetry(ctx, step.ID)
|
||||||
}
|
}
|
||||||
@@ -343,8 +357,31 @@ func (e *Docker) wait(ctx context.Context, id string) (*runtime.State, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper function emulates the `docker logs -f` command, streaming
|
// helper function emulates the `docker logs -f` command, streaming all container logs until the container stops.
|
||||||
// all container logs until the container stops.
|
func (e *Docker) deferTail(ctx context.Context, id string, output io.Writer) (logs io.ReadCloser, err error) {
|
||||||
|
opts := types.ContainerLogsOptions{
|
||||||
|
Follow: true,
|
||||||
|
ShowStdout: true,
|
||||||
|
ShowStderr: true,
|
||||||
|
Details: false,
|
||||||
|
Timestamps: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
logs, err = e.client.ContainerLogs(ctx, id, opts)
|
||||||
|
if err != nil {
|
||||||
|
logger.FromContext(ctx).
|
||||||
|
WithError(err).
|
||||||
|
WithField("container", id).
|
||||||
|
Debugln("failed to stream logs")
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
stdcopy.StdCopy(output, output, logs)
|
||||||
|
|
||||||
|
return logs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// helper function emulates the `docker logs -f` command, streaming all container logs until the container stops.
|
||||||
func (e *Docker) tail(ctx context.Context, id string, output io.Writer) error {
|
func (e *Docker) tail(ctx context.Context, id string, output io.Writer) error {
|
||||||
opts := types.ContainerLogsOptions{
|
opts := types.ContainerLogsOptions{
|
||||||
Follow: true,
|
Follow: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user