image_push.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package client
  2. import (
  3. "io"
  4. "net/http"
  5. "net/url"
  6. "golang.org/x/net/context"
  7. "github.com/docker/engine-api/types"
  8. )
  9. // ImagePush request the docker host to push an image to a remote registry.
  10. // It executes the privileged function if the operation is unauthorized
  11. // and it tries one more time.
  12. // It's up to the caller to handle the io.ReadCloser and close it properly.
  13. func (cli *Client) ImagePush(ctx context.Context, options types.ImagePushOptions, privilegeFunc RequestPrivilegeFunc) (io.ReadCloser, error) {
  14. query := url.Values{}
  15. query.Set("tag", options.Tag)
  16. resp, err := cli.tryImagePush(ctx, options.ImageID, query, options.RegistryAuth)
  17. if resp.statusCode == http.StatusUnauthorized {
  18. newAuthHeader, privilegeErr := privilegeFunc()
  19. if privilegeErr != nil {
  20. return nil, privilegeErr
  21. }
  22. resp, err = cli.tryImagePush(ctx, options.ImageID, query, newAuthHeader)
  23. }
  24. if err != nil {
  25. return nil, err
  26. }
  27. return resp.body, nil
  28. }
  29. func (cli *Client) tryImagePush(ctx context.Context, imageID string, query url.Values, registryAuth string) (*serverResponse, error) {
  30. headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
  31. return cli.post(ctx, "/images/"+imageID+"/push", query, nil, headers)
  32. }