2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-11-18 22:20:54 +00:00
|
|
|
"errors"
|
2015-03-25 03:57:23 +00:00
|
|
|
"fmt"
|
|
|
|
|
2016-02-03 23:41:26 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-05-05 04:18:28 +00:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-12-06 20:17:34 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-12-04 21:55:15 +00:00
|
|
|
"github.com/docker/docker/reference"
|
2015-03-25 03:57:23 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2015-03-25 03:57:23 +00:00
|
|
|
)
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdPull pulls an image or a repository from the registry.
|
|
|
|
//
|
|
|
|
// Usage: docker pull [OPTIONS] IMAGENAME[:TAG|@DIGEST]
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdPull(args ...string) error {
|
2015-10-08 12:46:21 +00:00
|
|
|
cmd := Cli.Subcmd("pull", []string{"NAME[:TAG|@DIGEST]"}, Cli.DockerCommands["pull"].Description, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
|
2015-07-15 20:42:45 +00:00
|
|
|
addTrustedFlags(cmd, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
cmd.Require(flag.Exact, 1)
|
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-07-15 20:42:45 +00:00
|
|
|
remote := cmd.Arg(0)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
distributionRef, err := reference.ParseNamed(remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-10 19:01:34 +00:00
|
|
|
if *allTags && !reference.IsNameOnly(distributionRef) {
|
|
|
|
return errors.New("tag can't be used with --all-tags/-a")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*allTags && reference.IsNameOnly(distributionRef) {
|
|
|
|
distributionRef = reference.WithDefaultTag(distributionRef)
|
|
|
|
fmt.Fprintf(cli.out, "Using default tag: %s\n", reference.DefaultTag)
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
|
|
|
|
var tag string
|
|
|
|
switch x := distributionRef.(type) {
|
2015-12-04 21:55:15 +00:00
|
|
|
case reference.Canonical:
|
2015-11-18 22:20:54 +00:00
|
|
|
tag = x.Digest().String()
|
2015-12-04 21:55:15 +00:00
|
|
|
case reference.NamedTagged:
|
2015-11-18 22:20:54 +00:00
|
|
|
tag = x.Tag()
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 08:33:46 +00:00
|
|
|
registryRef := registry.ParseReference(tag)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
2015-11-18 22:20:54 +00:00
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:55:17 +00:00
|
|
|
authConfig := cli.resolveAuthConfig(repoInfo.Index)
|
2015-12-06 20:17:34 +00:00
|
|
|
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
|
|
|
|
|
2016-04-13 08:33:46 +00:00
|
|
|
if isTrusted() && !registryRef.HasDigest() {
|
2015-07-15 20:42:45 +00:00
|
|
|
// Check if tag is digest
|
2016-04-13 08:33:46 +00:00
|
|
|
return cli.trustedPull(repoInfo, registryRef, authConfig, requestPrivilege)
|
2015-12-06 20:17:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:46:04 +00:00
|
|
|
return cli.imagePullPrivileged(authConfig, distributionRef.String(), requestPrivilege, *allTags)
|
2015-12-06 20:17:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:46:04 +00:00
|
|
|
func (cli *DockerCli) imagePullPrivileged(authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
|
2015-12-06 20:17:34 +00:00
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
encodedAuth, err := encodeAuthToBase64(authConfig)
|
2015-12-06 20:17:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
options := types.ImagePullOptions{
|
2016-04-13 08:33:46 +00:00
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
PrivilegeFunc: requestPrivilege,
|
2016-05-01 12:46:04 +00:00
|
|
|
All: all,
|
2015-07-15 20:42:45 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 08:33:46 +00:00
|
|
|
responseBody, err := cli.client.ImagePull(context.Background(), ref, options)
|
2015-12-06 20:17:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
2015-07-15 20:42:45 +00:00
|
|
|
|
2015-12-21 23:02:44 +00:00
|
|
|
return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|