2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-12-06 15:37:54 -05:00
|
|
|
"io"
|
2015-03-24 23:57:23 -04:00
|
|
|
|
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: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"
|
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/types"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Resolve the Auth config relevant for this server
|
|
|
|
authConfig := cli.resolveAuthConfig(ctx, repoInfo.Index)
|
2015-12-06 15:37:54 -05:00
|
|
|
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
|
2016-05-21 09:57:57 -04:00
|
|
|
|
2015-07-15 16:42:45 -04:00
|
|
|
if isTrusted() {
|
2016-05-21 09:57:57 -04:00
|
|
|
return cli.trustedPush(ctx, repoInfo, ref, authConfig, requestPrivilege)
|
2015-07-15 16:42:45 -04:00
|
|
|
}
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
responseBody, err := cli.imagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
|
2015-12-21 18:02:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
|
|
|
return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
|
2015-12-06 15:37:54 -05:00
|
|
|
}
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
func (cli *DockerCli) imagePushPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc) (io.ReadCloser, error) {
|
2015-12-11 21:14:52 -05:00
|
|
|
encodedAuth, err := encodeAuthToBase64(authConfig)
|
2015-12-06 15:37:54 -05:00
|
|
|
if err != nil {
|
2015-12-21 18:02:44 -05:00
|
|
|
return nil, err
|
2015-12-06 15:37:54 -05:00
|
|
|
}
|
|
|
|
options := types.ImagePushOptions{
|
2016-04-13 04:33:46 -04:00
|
|
|
RegistryAuth: encodedAuth,
|
|
|
|
PrivilegeFunc: requestPrivilege,
|
2015-12-06 15:37:54 -05:00
|
|
|
}
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
return cli.client.ImagePush(ctx, ref, options)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|