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