image_pull.go 936 B

12345678910111213141516171819202122232425262728293031323334
  1. package lib
  2. import (
  3. "io"
  4. "net/http"
  5. "net/url"
  6. "github.com/docker/docker/api/types"
  7. )
  8. // ImagePull request the docker host to pull an image from 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) ImagePull(options types.ImagePullOptions, privilegeFunc RequestPrivilegeFunc) (io.ReadCloser, error) {
  13. query := url.Values{}
  14. query.Set("fromImage", options.ImageID)
  15. if options.Tag != "" {
  16. query.Set("tag", options.Tag)
  17. }
  18. resp, err := cli.tryImageCreate(query, options.RegistryAuth)
  19. if resp.statusCode == http.StatusUnauthorized {
  20. newAuthHeader, privilegeErr := privilegeFunc()
  21. if privilegeErr != nil {
  22. return nil, privilegeErr
  23. }
  24. resp, err = cli.tryImageCreate(query, newAuthHeader)
  25. }
  26. if err != nil {
  27. return nil, err
  28. }
  29. return resp.body, nil
  30. }