diff options
author | Michael Crosby <crosbymichael@gmail.com> | 2016-09-06 11:46:37 -0700 |
---|---|---|
committer | Michael Crosby <crosbymichael@gmail.com> | 2016-09-07 11:05:58 -0700 |
commit | 7c36a1af031b510cd990cf488ee5998a3efb450f (patch) | |
tree | 477f7c37b71fbc6dc641d7b294b68e17d6c869ec /client/image_push.go | |
parent | 91e197d614547f0202e6ae9b8a24d88ee131d950 (diff) | |
download | docker-7c36a1af031b510cd990cf488ee5998a3efb450f.tar.gz |
Move engine-api client package
This moves the engine-api client package to `/docker/docker/client`.
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
Diffstat (limited to 'client/image_push.go')
-rw-r--r-- | client/image_push.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/client/image_push.go b/client/image_push.go new file mode 100644 index 0000000000..8e73d28f56 --- /dev/null +++ b/client/image_push.go @@ -0,0 +1,54 @@ +package client + +import ( + "errors" + "io" + "net/http" + "net/url" + + "golang.org/x/net/context" + + distreference "github.com/docker/distribution/reference" + "github.com/docker/docker/api/types" +) + +// ImagePush requests the docker host to push an image to a remote registry. +// It executes the privileged function if the operation is unauthorized +// and it tries one more time. +// It's up to the caller to handle the io.ReadCloser and close it properly. +func (cli *Client) ImagePush(ctx context.Context, ref string, options types.ImagePushOptions) (io.ReadCloser, error) { + distributionRef, err := distreference.ParseNamed(ref) + if err != nil { + return nil, err + } + + if _, isCanonical := distributionRef.(distreference.Canonical); isCanonical { + return nil, errors.New("cannot push a digest reference") + } + + var tag = "" + if nameTaggedRef, isNamedTagged := distributionRef.(distreference.NamedTagged); isNamedTagged { + tag = nameTaggedRef.Tag() + } + + query := url.Values{} + query.Set("tag", tag) + + resp, err := cli.tryImagePush(ctx, distributionRef.Name(), query, options.RegistryAuth) + if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil { + newAuthHeader, privilegeErr := options.PrivilegeFunc() + if privilegeErr != nil { + return nil, privilegeErr + } + resp, err = cli.tryImagePush(ctx, distributionRef.Name(), query, newAuthHeader) + } + if err != nil { + return nil, err + } + return resp.body, nil +} + +func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (serverResponse, error) { + headers := map[string][]string{"X-Registry-Auth": {registryAuth}} + return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers) +} |