image_push.go 1.1 KB

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