github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/client/image_pull.go (about)

     1  package client
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"golang.org/x/net/context"
     9  
    10  	"github.com/docker/distribution/reference"
    11  	"github.com/docker/docker/api/types"
    12  )
    13  
    14  // ImagePull requests the docker host to pull an image from a remote registry.
    15  // It executes the privileged function if the operation is unauthorized
    16  // and it tries one more time.
    17  // It's up to the caller to handle the io.ReadCloser and close it properly.
    18  //
    19  // FIXME(vdemeester): there is currently used in a few way in docker/docker
    20  // - if not in trusted content, ref is used to pass the whole reference, and tag is empty
    21  // - if in trusted content, ref is used to pass the reference name, and tag for the digest
    22  func (cli *Client) ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
    23  	ref, err := reference.ParseNormalizedNamed(refStr)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	query := url.Values{}
    29  	query.Set("fromImage", reference.FamiliarName(ref))
    30  	if !options.All {
    31  		query.Set("tag", getAPITagFromNamedRef(ref))
    32  	}
    33  
    34  	resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
    35  	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
    36  		newAuthHeader, privilegeErr := options.PrivilegeFunc()
    37  		if privilegeErr != nil {
    38  			return nil, privilegeErr
    39  		}
    40  		resp, err = cli.tryImageCreate(ctx, query, newAuthHeader)
    41  	}
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return resp.body, nil
    46  }
    47  
    48  // getAPITagFromNamedRef returns a tag from the specified reference.
    49  // This function is necessary as long as the docker "server" api expects
    50  // digests to be sent as tags and makes a distinction between the name
    51  // and tag/digest part of a reference.
    52  func getAPITagFromNamedRef(ref reference.Named) string {
    53  	if digested, ok := ref.(reference.Digested); ok {
    54  		return digested.Digest().String()
    55  	}
    56  	ref = reference.TagNameOnly(ref)
    57  	if tagged, ok := ref.(reference.Tagged); ok {
    58  		return tagged.Tag()
    59  	}
    60  	return ""
    61  }