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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 15 deletions

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/Microsoft/hcsshim v0.8.7 // indirect
github.com/andreaskoch/go-fswatch v1.0.0
github.com/containerd/containerd v1.3.3 // indirect
github.com/docker/cli v0.0.0-20190822175708-578ab52ece34
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v0.0.0-20200229013735-71373c6105e3
github.com/docker/go-connections v0.4.0 // indirect

3
go.sum
View File

@ -34,6 +34,9 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/cli v0.0.0-20190822175708-578ab52ece34 h1:H/dVI9lW9zuagcDsmBz2cj8E8paBX5FarjO7oQCpbVA=
github.com/docker/cli v0.0.0-20190822175708-578ab52ece34/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v17.12.1-ce-rc2+incompatible h1:ESUycEAqvFuLglAHkUW66rCc2djYtd3i1x231svLq9o=
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v0.0.0-20200229013735-71373c6105e3 h1:hq9QaRK9JJOg7GItpuSSA3MrBoEN3c3llxQappEq9Zo=

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)
}