mirror of https://github.com/docker/compose.git
Remove command.DockerCli dependency
Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
058c779378
commit
0f3c214b48
|
@ -29,8 +29,6 @@ import (
|
||||||
"github.com/docker/buildx/util/buildflags"
|
"github.com/docker/buildx/util/buildflags"
|
||||||
xprogress "github.com/docker/buildx/util/progress"
|
xprogress "github.com/docker/buildx/util/progress"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/docker/cli/cli/flags"
|
|
||||||
"github.com/docker/docker/client"
|
|
||||||
bclient "github.com/moby/buildkit/client"
|
bclient "github.com/moby/buildkit/client"
|
||||||
"github.com/moby/buildkit/session"
|
"github.com/moby/buildkit/session"
|
||||||
"github.com/moby/buildkit/session/auth/authprovider"
|
"github.com/moby/buildkit/session/auth/authprovider"
|
||||||
|
@ -193,22 +191,29 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
|
||||||
return images, nil
|
return images, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *composeService) serverInfo(ctx context.Context) (command.ServerInfo, error) {
|
||||||
|
ping, err := s.apiClient.Ping(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return command.ServerInfo{}, err
|
||||||
|
}
|
||||||
|
serverInfo := command.ServerInfo{
|
||||||
|
HasExperimental: ping.Experimental,
|
||||||
|
OSType: ping.OSType,
|
||||||
|
BuildkitVersion: ping.BuilderVersion,
|
||||||
|
}
|
||||||
|
return serverInfo, err
|
||||||
|
}
|
||||||
|
|
||||||
func (s *composeService) doBuild(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
|
func (s *composeService) doBuild(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
|
||||||
if len(opts) == 0 {
|
if len(opts) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
dockerCli, err := command.NewDockerCli()
|
serverInfo, err := s.serverInfo(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = dockerCli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(cli *command.DockerCli) (client.APIClient, error) {
|
if buildkitEnabled, err := command.BuildKitEnabled(serverInfo); err != nil || !buildkitEnabled {
|
||||||
return s.apiClient, nil
|
return s.doBuildClassic(ctx, opts)
|
||||||
}))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if buildkitEnabled, err := command.BuildKitEnabled(dockerCli.ServerInfo()); err != nil || !buildkitEnabled {
|
|
||||||
return s.doBuildClassic(ctx, dockerCli, opts)
|
|
||||||
}
|
}
|
||||||
return s.doBuildBuildkit(ctx, project, opts, mode)
|
return s.doBuildBuildkit(ctx, project, opts, mode)
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
buildx "github.com/docker/buildx/build"
|
buildx "github.com/docker/buildx/build"
|
||||||
"github.com/docker/cli/cli/command"
|
|
||||||
"github.com/docker/cli/cli/command/image/build"
|
"github.com/docker/cli/cli/command/image/build"
|
||||||
dockertypes "github.com/docker/docker/api/types"
|
dockertypes "github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/cli"
|
"github.com/docker/docker/cli"
|
||||||
|
@ -42,11 +41,11 @@ import (
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *composeService) doBuildClassic(ctx context.Context, dockerCli *command.DockerCli, opts map[string]buildx.Options) (map[string]string, error) {
|
func (s *composeService) doBuildClassic(ctx context.Context, opts map[string]buildx.Options) (map[string]string, error) {
|
||||||
var nameDigests = make(map[string]string)
|
var nameDigests = make(map[string]string)
|
||||||
var errs error
|
var errs error
|
||||||
for name, o := range opts {
|
for name, o := range opts {
|
||||||
digest, err := doBuildClassicSimpleImage(ctx, dockerCli, o)
|
digest, err := s.doBuildClassicSimpleImage(ctx, o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = multierror.Append(errs, err).ErrorOrNil()
|
errs = multierror.Append(errs, err).ErrorOrNil()
|
||||||
}
|
}
|
||||||
|
@ -57,7 +56,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, dockerCli *command.
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: gocyclo
|
// nolint: gocyclo
|
||||||
func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli, options buildx.Options) (string, error) {
|
func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options buildx.Options) (string, error) {
|
||||||
var (
|
var (
|
||||||
buildCtx io.ReadCloser
|
buildCtx io.ReadCloser
|
||||||
dockerfileCtx io.ReadCloser
|
dockerfileCtx io.ReadCloser
|
||||||
|
@ -70,8 +69,8 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
|
||||||
|
|
||||||
dockerfileName := options.Inputs.DockerfilePath
|
dockerfileName := options.Inputs.DockerfilePath
|
||||||
specifiedContext := options.Inputs.ContextPath
|
specifiedContext := options.Inputs.ContextPath
|
||||||
progBuff := dockerCli.Out()
|
progBuff := os.Stdout
|
||||||
buildBuff := dockerCli.Out()
|
buildBuff := os.Stdout
|
||||||
if options.ImageIDFile != "" {
|
if options.ImageIDFile != "" {
|
||||||
// Avoid leaving a stale file if we eventually fail
|
// Avoid leaving a stale file if we eventually fail
|
||||||
if err := os.Remove(options.ImageIDFile); err != nil && !os.IsNotExist(err) {
|
if err := os.Remove(options.ImageIDFile); err != nil && !os.IsNotExist(err) {
|
||||||
|
@ -144,24 +143,19 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup an upload progress bar
|
|
||||||
progressOutput := streamformatter.NewProgressOutput(progBuff)
|
|
||||||
if !dockerCli.Out().IsTerminal() {
|
|
||||||
progressOutput = &lastProgressOutput{output: progressOutput}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if up to this point nothing has set the context then we must have another
|
// if up to this point nothing has set the context then we must have another
|
||||||
// way for sending it(streaming) and set the context to the Dockerfile
|
// way for sending it(streaming) and set the context to the Dockerfile
|
||||||
if dockerfileCtx != nil && buildCtx == nil {
|
if dockerfileCtx != nil && buildCtx == nil {
|
||||||
buildCtx = dockerfileCtx
|
buildCtx = dockerfileCtx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
progressOutput := streamformatter.NewProgressOutput(progBuff)
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
if buildCtx != nil {
|
if buildCtx != nil {
|
||||||
body = progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
|
body = progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
|
||||||
}
|
}
|
||||||
|
|
||||||
configFile := dockerCli.ConfigFile()
|
configFile := s.configFile
|
||||||
creds, _ := configFile.GetAllCredentials()
|
creds, _ := configFile.GetAllCredentials()
|
||||||
authConfigs := make(map[string]dockertypes.AuthConfig, len(creds))
|
authConfigs := make(map[string]dockertypes.AuthConfig, len(creds))
|
||||||
for k, auth := range creds {
|
for k, auth := range creds {
|
||||||
|
@ -174,7 +168,7 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
response, err := dockerCli.Client().ImageBuild(ctx, body, buildOptions)
|
response, err := s.apiClient.ImageBuild(ctx, body, buildOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -184,13 +178,13 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
|
||||||
aux := func(msg jsonmessage.JSONMessage) {
|
aux := func(msg jsonmessage.JSONMessage) {
|
||||||
var result dockertypes.BuildResult
|
var result dockertypes.BuildResult
|
||||||
if err := json.Unmarshal(*msg.Aux, &result); err != nil {
|
if err := json.Unmarshal(*msg.Aux, &result); err != nil {
|
||||||
fmt.Fprintf(dockerCli.Err(), "Failed to parse aux message: %s", err)
|
fmt.Fprintf(os.Stderr, "Failed to parse aux message: %s", err)
|
||||||
} else {
|
} else {
|
||||||
imageID = result.ID
|
imageID = result.ID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, dockerCli.Out().FD(), dockerCli.Out().IsTerminal(), aux)
|
err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, progBuff.Fd(), true, aux)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if jerr, ok := err.(*jsonmessage.JSONError); ok {
|
if jerr, ok := err.(*jsonmessage.JSONError); ok {
|
||||||
// If no error code is set, default to 1
|
// If no error code is set, default to 1
|
||||||
|
@ -206,7 +200,7 @@ func doBuildClassicSimpleImage(ctx context.Context, dockerCli *command.DockerCli
|
||||||
// daemon isn't running Windows.
|
// daemon isn't running Windows.
|
||||||
if response.OSType != "windows" && runtime.GOOS == "windows" {
|
if response.OSType != "windows" && runtime.GOOS == "windows" {
|
||||||
// if response.OSType != "windows" && runtime.GOOS == "windows" && !options.quiet {
|
// if response.OSType != "windows" && runtime.GOOS == "windows" && !options.quiet {
|
||||||
fmt.Fprintln(dockerCli.Out(), "SECURITY WARNING: You are building a Docker "+
|
fmt.Fprintln(os.Stdout, "SECURITY WARNING: You are building a Docker "+
|
||||||
"image from Windows against a non-Windows Docker host. All files and "+
|
"image from Windows against a non-Windows Docker host. All files and "+
|
||||||
"directories added to build context will have '-rwxr-xr-x' permissions. "+
|
"directories added to build context will have '-rwxr-xr-x' permissions. "+
|
||||||
"It is recommended to double check and reset permissions for sensitive "+
|
"It is recommended to double check and reset permissions for sensitive "+
|
||||||
|
|
Loading…
Reference in New Issue