2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/registry"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-03-27 21:21:59 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type pullOptions struct {
|
|
|
|
remote string
|
|
|
|
all bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPullCommand creates a new `docker pull` command
|
|
|
|
func NewPullCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
var opts pullOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "pull [OPTIONS] NAME[:TAG|@DIGEST]",
|
|
|
|
Short: "Pull an image or a repository from a registry",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.remote = args[0]
|
|
|
|
return runPull(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
|
2017-01-17 09:46:07 -05:00
|
|
|
command.AddTrustVerificationFlags(flags)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPull(dockerCli *command.DockerCli, opts pullOptions) error {
|
2017-01-11 16:54:52 -05:00
|
|
|
distributionRef, err := reference.ParseNormalizedNamed(opts.remote)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if opts.all && !reference.IsNameOnly(distributionRef) {
|
|
|
|
return errors.New("tag can't be used with --all-tags/-a")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !opts.all && reference.IsNameOnly(distributionRef) {
|
2017-01-25 19:54:18 -05:00
|
|
|
distributionRef = reference.TagNameOnly(distributionRef)
|
|
|
|
if tagged, ok := distributionRef.(reference.Tagged); ok {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", tagged.Tag())
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-09-09 15:38:00 -04:00
|
|
|
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
|
|
|
|
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "pull")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-12-06 14:27:27 -05:00
|
|
|
// Check if reference has a digest
|
|
|
|
_, isCanonical := distributionRef.(reference.Canonical)
|
|
|
|
if command.IsTrusted() && !isCanonical {
|
|
|
|
err = trustedPull(ctx, dockerCli, repoInfo, distributionRef, authConfig, requestPrivilege)
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
2017-01-11 16:54:52 -05:00
|
|
|
err = imagePullPrivileged(ctx, dockerCli, authConfig, reference.FamiliarString(distributionRef), requestPrivilege, opts.all)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2017-01-10 22:27:55 -05:00
|
|
|
if strings.Contains(err.Error(), "when fetching 'plugin'") {
|
2016-09-08 13:11:39 -04:00
|
|
|
return errors.New(err.Error() + " - Use `docker plugin install`")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|