mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
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:
parent
a09b46b898
commit
dea1ed5769
5 changed files with 38 additions and 15 deletions
|
@ -8,7 +8,6 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/builder/dockerignore"
|
"github.com/docker/docker/builder/dockerignore"
|
||||||
"github.com/docker/docker/client"
|
|
||||||
"github.com/docker/docker/pkg/archive"
|
"github.com/docker/docker/pkg/archive"
|
||||||
"github.com/docker/docker/pkg/fileutils"
|
"github.com/docker/docker/pkg/fileutils"
|
||||||
"github.com/nektos/act/pkg/common"
|
"github.com/nektos/act/pkg/common"
|
||||||
|
@ -30,11 +29,10 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cli.NegotiateAPIVersion(ctx)
|
|
||||||
|
|
||||||
logger.Debugf("Building image from '%v'", input.ContextDir)
|
logger.Debugf("Building image from '%v'", input.ContextDir)
|
||||||
|
|
||||||
|
|
|
@ -5,17 +5,15 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/client"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImageExistsLocally returns a boolean indicating if an image with the
|
// ImageExistsLocally returns a boolean indicating if an image with the
|
||||||
// requested name (and tag) exist in the local docker image store
|
// requested name (and tag) exist in the local docker image store
|
||||||
func ImageExistsLocally(ctx context.Context, imageName string) (bool, error) {
|
func ImageExistsLocally(ctx context.Context, imageName string) (bool, error) {
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
cli.NegotiateAPIVersion(ctx)
|
|
||||||
|
|
||||||
filters := filters.NewArgs()
|
filters := filters.NewArgs()
|
||||||
filters.Add("reference", imageName)
|
filters.Add("reference", imageName)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/client"
|
|
||||||
"github.com/nektos/act/pkg/common"
|
"github.com/nektos/act/pkg/common"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -48,11 +47,10 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
|
||||||
imageRef := cleanImage(input.Image)
|
imageRef := cleanImage(input.Image)
|
||||||
logger.Debugf("pulling image '%v'", imageRef)
|
logger.Debugf("pulling image '%v'", imageRef)
|
||||||
|
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cli.NegotiateAPIVersion(ctx)
|
|
||||||
|
|
||||||
reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{})
|
reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{})
|
||||||
_ = logDockerResponse(logger, reader, err != nil)
|
_ = logDockerResponse(logger, reader, err != nil)
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/go-git/go-billy/v5/osfs"
|
"github.com/go-git/go-billy/v5/osfs"
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
"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"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/mount"
|
"github.com/docker/docker/api/types/mount"
|
||||||
|
@ -137,16 +138,46 @@ type containerReference struct {
|
||||||
input *NewContainerInput
|
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 {
|
func (cr *containerReference) connect() common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
if cr.cli != nil {
|
if cr.cli != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithStack(err)
|
return err
|
||||||
}
|
}
|
||||||
cli.NegotiateAPIVersion(ctx)
|
|
||||||
cr.cli = cli
|
cr.cli = cli
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package container
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/docker/docker/client"
|
|
||||||
"github.com/nektos/act/pkg/common"
|
"github.com/nektos/act/pkg/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,11 +16,10 @@ func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cli.NegotiateAPIVersion(ctx)
|
|
||||||
|
|
||||||
return cli.VolumeRemove(ctx, volume, force)
|
return cli.VolumeRemove(ctx, volume, force)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue