extract the docker NewClientWithOpts, and add connectionhelper for DOCKER_HOST set to ssh://remote (#207)

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>

Co-authored-by: Casey Lee <cplee@nektos.com>
This commit is contained in:
Sven Dowideit
2020-05-04 14:15:42 +10:00
committed by GitHub
parent ef9fab9fad
commit 6196436f70
7 changed files with 42 additions and 15 deletions

View File

@@ -8,7 +8,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/builder/dockerignore"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/fileutils"
"github.com/nektos/act/pkg/common"
@@ -30,11 +29,10 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
return nil
}
cli, err := client.NewClientWithOpts(client.FromEnv)
cli, err := GetDockerClient(ctx)
if err != nil {
return err
}
cli.NegotiateAPIVersion(ctx)
logger.Debugf("Building image from '%v'", input.ContextDir)

View File

@@ -5,17 +5,15 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
// ImageExistsLocally returns a boolean indicating if an image with the
// requested name (and tag) exist in the local docker image store
func ImageExistsLocally(ctx context.Context, imageName string) (bool, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
cli, err := GetDockerClient(ctx)
if err != nil {
return false, err
}
cli.NegotiateAPIVersion(ctx)
filters := filters.NewArgs()
filters.Add("reference", imageName)

View File

@@ -6,7 +6,6 @@ import (
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/nektos/act/pkg/common"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
@@ -48,11 +47,10 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
imageRef := cleanImage(input.Image)
logger.Debugf("pulling image '%v'", imageRef)
cli, err := client.NewClientWithOpts(client.FromEnv)
cli, err := GetDockerClient(ctx)
if err != nil {
return err
}
cli.NegotiateAPIVersion(ctx)
reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{})
_ = logDockerResponse(logger, reader, err != nil)

View File

@@ -15,6 +15,7 @@ import (
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/docker/cli/cli/connhelper"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
@@ -137,16 +138,46 @@ type containerReference struct {
input *NewContainerInput
}
func GetDockerClient(ctx context.Context) (*client.Client, error) {
var err error
var cli *client.Client
// TODO: this should maybe need to be a global option, not hidden in here?
// though i'm not sure how that works out when there's another Executor :D
// I really would like something that works on OSX native for eg
dockerHost := os.Getenv("DOCKER_HOST")
if strings.HasPrefix(dockerHost, "ssh://") {
var helper *connhelper.ConnectionHelper
helper, err = connhelper.GetConnectionHelper(dockerHost)
if err != nil {
return nil, err
}
cli, err = client.NewClientWithOpts(
client.WithHost(helper.Host),
client.WithDialContext(helper.Dialer),
)
} else {
cli, err = client.NewClientWithOpts(client.FromEnv)
}
if err != nil {
return nil, errors.WithStack(err)
}
cli.NegotiateAPIVersion(ctx)
return cli, err
}
func (cr *containerReference) connect() common.Executor {
return func(ctx context.Context) error {
if cr.cli != nil {
return nil
}
cli, err := client.NewClientWithOpts(client.FromEnv)
cli, err := GetDockerClient(ctx)
if err != nil {
return errors.WithStack(err)
return err
}
cli.NegotiateAPIVersion(ctx)
cr.cli = cli
return nil
}

View File

@@ -3,7 +3,6 @@ package container
import (
"context"
"github.com/docker/docker/client"
"github.com/nektos/act/pkg/common"
)
@@ -17,11 +16,10 @@ func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
return nil
}
cli, err := client.NewClientWithOpts(client.FromEnv)
cli, err := GetDockerClient(ctx)
if err != nil {
return err
}
cli.NegotiateAPIVersion(ctx)
return cli.VolumeRemove(ctx, volume, force)
}