Implement docker pull with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-12-06 15:17:34 -05:00
Родитель 39fa453fb0
Коммит 0c3f68b576
4 изменённых файлов: 57 добавлений и 3 удалений

Просмотреть файл

@ -13,11 +13,14 @@ func (cli *Client) ImageCreate(options types.ImageCreateOptions) (io.ReadCloser,
query := url.Values{}
query.Set("fromImage", options.Parent)
query.Set("tag", options.Tag)
headers := map[string][]string{"X-Registry-Auth": {options.RegistryAuth}}
resp, err := cli.post("/images/create", query, nil, headers)
resp, err := cli.tryImageCreate(query, options.RegistryAuth)
if err != nil {
return nil, err
}
return resp.body, nil
}
func (cli *Client) tryImageCreate(query url.Values, registryAuth string) (*serverResponse, error) {
headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
return cli.post("/images/create", query, nil, headers)
}

34
client/image_pull.go Normal file
Просмотреть файл

@ -0,0 +1,34 @@
package lib
import (
"io"
"net/http"
"net/url"
"github.com/docker/docker/api/types"
)
// ImagePull request the docker host to pull an image from 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.
func (cli *Client) ImagePull(options types.ImagePullOptions, privilegeFunc RequestPrivilegeFunc) (io.ReadCloser, error) {
query := url.Values{}
query.Set("fromImage", options.ImageID)
if options.Tag != "" {
query.Set("tag", options.Tag)
}
resp, err := cli.tryImageCreate(query, options.RegistryAuth)
if resp.statusCode == http.StatusUnauthorized {
newAuthHeader, privilegeErr := privilegeFunc()
if privilegeErr != nil {
return nil, privilegeErr
}
resp, err = cli.tryImageCreate(query, newAuthHeader)
}
if err != nil {
return nil, err
}
return resp.body, nil
}

9
client/privileged.go Normal file
Просмотреть файл

@ -0,0 +1,9 @@
package lib
// RequestPrivilegeFunc is a function interface that
// clients can supply to retry operations after
// getting an authorization error.
// This function returns the registry authentication
// header value in base 64 format, or an error
// if the privilege request fails.
type RequestPrivilegeFunc func() (string, error)

Просмотреть файл

@ -179,6 +179,14 @@ type ImageListOptions struct {
Filters filters.Args
}
// ImagePullOptions holds information to pull images.
type ImagePullOptions struct {
ImageID string
Tag string
// RegistryAuth is the base64 encoded credentials for this server
RegistryAuth string
}
// ImageRemoveOptions holds parameters to remove images.
type ImageRemoveOptions struct {
ImageID string