2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-11-18 17:20:54 -05:00
|
|
|
"errors"
|
2015-12-06 15:37:54 -05:00
|
|
|
"io"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-12-06 15:37:54 -05:00
|
|
|
"github.com/docker/docker/api/client/lib"
|
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-12-06 15:37:54 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdPush pushes an image or repository to the registry.
|
|
|
|
//
|
|
|
|
// Usage: docker push NAME[:TAG]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdPush(args ...string) error {
|
2015-10-08 08:46:21 -04:00
|
|
|
cmd := Cli.Subcmd("push", []string{"NAME[:TAG]"}, Cli.DockerCommands["push"].Description, true)
|
2015-07-15 16:42:45 -04:00
|
|
|
addTrustedFlags(cmd, false)
|
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-03-24 23:57:23 -04:00
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
ref, err := reference.ParseNamed(cmd.Arg(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tag string
|
|
|
|
switch x := ref.(type) {
|
|
|
|
case reference.Digested:
|
|
|
|
return errors.New("cannot push a digest reference")
|
|
|
|
case reference.Tagged:
|
|
|
|
tag = x.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(ref)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Resolve the Auth config relevant for this server
|
2015-12-11 22:11:20 -05:00
|
|
|
authConfig := registry.ResolveAuthConfig(cli.configFile.AuthConfigs, repoInfo.Index)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-06 15:37:54 -05:00
|
|
|
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
|
2015-07-15 16:42:45 -04:00
|
|
|
if isTrusted() {
|
2015-12-06 15:37:54 -05:00
|
|
|
return cli.trustedPush(repoInfo, tag, authConfig, requestPrivilege)
|
2015-07-15 16:42:45 -04:00
|
|
|
}
|
|
|
|
|
2015-12-06 15:37:54 -05:00
|
|
|
return cli.imagePushPrivileged(authConfig, ref.Name(), tag, cli.out, requestPrivilege)
|
|
|
|
}
|
|
|
|
|
2015-12-11 23:11:42 -05:00
|
|
|
func (cli *DockerCli) imagePushPrivileged(authConfig types.AuthConfig, imageID, tag string, outputStream io.Writer, requestPrivilege lib.RequestPrivilegeFunc) error {
|
2015-12-11 21:14:52 -05:00
|
|
|
encodedAuth, err := encodeAuthToBase64(authConfig)
|
2015-12-06 15:37:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
options := types.ImagePushOptions{
|
|
|
|
ImageID: imageID,
|
|
|
|
Tag: tag,
|
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
}
|
|
|
|
|
|
|
|
responseBody, err := cli.client.ImagePush(options, requestPrivilege)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-12-06 15:37:54 -05:00
|
|
|
return jsonmessage.DisplayJSONMessagesStream(responseBody, outputStream, cli.outFd, cli.isTerminalOut)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|