2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 14:46:37 -04:00
|
|
|
"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.
|
2017-01-25 19:54:18 -05:00
|
|
|
func (cli *Client) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(image)
|
2016-09-06 14:46:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
2016-09-06 14:46:37 -04:00
|
|
|
return nil, errors.New("cannot push a digest reference")
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
tag := ""
|
|
|
|
name := reference.FamiliarName(ref)
|
|
|
|
|
|
|
|
if nameTaggedRef, isNamedTagged := ref.(reference.NamedTagged); isNamedTagged {
|
2016-09-06 14:46:37 -04:00
|
|
|
tag = nameTaggedRef.Tag()
|
|
|
|
}
|
|
|
|
|
|
|
|
query := url.Values{}
|
|
|
|
query.Set("tag", tag)
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
|
2016-09-06 14:46:37 -04:00
|
|
|
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
|
|
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
|
|
|
if privilegeErr != nil {
|
|
|
|
return nil, privilegeErr
|
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
resp, err = cli.tryImagePush(ctx, name, query, newAuthHeader)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|