Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional cancellable context #309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions action/operation_configs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action

import (
"context"
"os"

"github.com/cnabio/cnab-go/driver"
Expand Down Expand Up @@ -33,5 +34,10 @@ func (cfgs OperationConfigs) ApplyConfig(op *driver.Operation) error {
op.Err = os.Stderr
}

// if no context were provided
if op.CTX == nil {
op.CTX = context.Background()
}

return nil
}
33 changes: 19 additions & 14 deletions driver/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func (d *Driver) initializeDockerCli() (command.Cli, error) {
}

func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
ctx := context.Background()

cli, err := d.initializeDockerCli()
if err != nil {
Expand All @@ -193,12 +192,12 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
return driver.OperationResult{}, nil
}
if d.config["PULL_ALWAYS"] == "1" {
if err := pullImage(ctx, cli, op.Image.Image); err != nil {
if err := pullImage(op.CTX, cli, op.Image.Image); err != nil {
return driver.OperationResult{}, err
}
}

ii, err := d.inspectImage(ctx, op.Image)
ii, err := d.inspectImage(op.CTX, op.Image)
if err != nil {
return driver.OperationResult{}, err
}
Expand All @@ -212,13 +211,13 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
return driver.OperationResult{}, err
}

resp, err := cli.Client().ContainerCreate(ctx, &d.containerCfg, &d.containerHostCfg, nil, nil, "")
resp, err := cli.Client().ContainerCreate(op.CTX, &d.containerCfg, &d.containerHostCfg, nil, nil, "")
if err != nil {
return driver.OperationResult{}, fmt.Errorf("cannot create container: %v", err)
}

if d.config["CLEANUP_CONTAINERS"] == "true" {
defer cli.Client().ContainerRemove(ctx, resp.ID, container.RemoveOptions{})
defer cli.Client().ContainerRemove(op.CTX, resp.ID, container.RemoveOptions{})
}

containerUID := getContainerUserID(ii.Config.User)
Expand All @@ -231,12 +230,12 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
}
// This copies the tar to the root of the container. The tar has been assembled using the
// path from the given file, starting at the /.
err = cli.Client().CopyToContainer(ctx, resp.ID, "/", tarContent, options)
err = cli.Client().CopyToContainer(op.CTX, resp.ID, "/", tarContent, options)
if err != nil {
return driver.OperationResult{}, fmt.Errorf("error copying to / in container: %s", err)
}

attach, err := cli.Client().ContainerAttach(ctx, resp.ID, container.AttachOptions{
attach, err := cli.Client().ContainerAttach(op.CTX, resp.ID, container.AttachOptions{
Stream: true,
Stdout: true,
Stderr: true,
Expand Down Expand Up @@ -269,28 +268,34 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
}
}()

if err = cli.Client().ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
if err = cli.Client().ContainerStart(op.CTX, resp.ID, container.StartOptions{}); err != nil {
return driver.OperationResult{}, fmt.Errorf("cannot start container: %v", err)
}
statusc, errc := cli.Client().ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
statusc, errc := cli.Client().ContainerWait(op.CTX, resp.ID, container.WaitConditionNotRunning)
select {
case <-op.CTX.Done():
err = cli.Client().ContainerStop(context.Background(), resp.ID, container.StopOptions{})
if err != nil {
return driver.OperationResult{}, err
}
return driver.OperationResult{}, op.CTX.Err()
case err := <-errc:
if err != nil {
opResult, fetchErr := d.fetchOutputs(ctx, resp.ID, op)
opResult, fetchErr := d.fetchOutputs(op.CTX, resp.ID, op)
return opResult, containerError("error in container", err, fetchErr)
}
case s := <-statusc:
if s.StatusCode == 0 {
return d.fetchOutputs(ctx, resp.ID, op)
return d.fetchOutputs(op.CTX, resp.ID, op)
}
if s.Error != nil {
opResult, fetchErr := d.fetchOutputs(ctx, resp.ID, op)
opResult, fetchErr := d.fetchOutputs(op.CTX, resp.ID, op)
return opResult, containerError(fmt.Sprintf("container exit code: %d, message", s.StatusCode), err, fetchErr)
}
opResult, fetchErr := d.fetchOutputs(ctx, resp.ID, op)
opResult, fetchErr := d.fetchOutputs(op.CTX, resp.ID, op)
return opResult, containerError(fmt.Sprintf("container exit code: %d, message", s.StatusCode), err, fetchErr)
}
opResult, fetchErr := d.fetchOutputs(ctx, resp.ID, op)
opResult, fetchErr := d.fetchOutputs(op.CTX, resp.ID, op)
if fetchErr != nil {
return opResult, fmt.Errorf("fetching outputs failed: %s", fetchErr)
}
Expand Down
2 changes: 2 additions & 0 deletions driver/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package driver

import (
"context"
"fmt"
"io"

Expand Down Expand Up @@ -40,6 +41,7 @@ type Operation struct {
Err io.Writer `json:"-"`
// Bundle represents the bundle information for use by the operation
Bundle *bundle.Bundle
CTX context.Context
}

// ResolvedCred is a credential that has been resolved and is ready for injection into the runtime.
Expand Down
Loading