1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Implement docker image create with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-12-03 11:14:07 -05:00
parent 1b2b91ba43
commit 1698fe01f5
2 changed files with 43 additions and 14 deletions

View file

@ -10,8 +10,10 @@ import (
"strings"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/client/lib"
"github.com/docker/docker/api/types"
Cli "github.com/docker/docker/cli"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/registry"
"github.com/docker/docker/runconfig"
tagpkg "github.com/docker/docker/tag"
@ -22,8 +24,6 @@ func (cli *DockerCli) pullImage(image string) error {
}
func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
v := url.Values{}
ref, err := reference.ParseNamed(image)
if err != nil {
return err
@ -40,9 +40,6 @@ func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
tag = tagpkg.DefaultTag
}
v.Set("fromImage", ref.Name())
v.Set("tag", tag)
// Resolve the Repository name from fqn to RepositoryInfo
repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
@ -56,18 +53,19 @@ func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
return err
}
registryAuthHeader := []string{
base64.URLEncoding.EncodeToString(buf),
options := lib.CreateImageOptions{
Parent: ref.Name(),
Tag: tag,
RegistryAuth: base64.URLEncoding.EncodeToString(buf),
}
sopts := &streamOpts{
rawTerminal: true,
out: out,
headers: map[string][]string{"X-Registry-Auth": registryAuthHeader},
}
if _, err := cli.stream("POST", "/images/create?"+v.Encode(), sopts); err != nil {
responseBody, err := cli.client.CreateImage(options)
if err != nil {
return err
}
return nil
defer responseBody.Close()
return jsonmessage.DisplayJSONMessagesStream(responseBody, out, cli.outFd, cli.isTerminalOut)
}
type cidFile struct {

View file

@ -0,0 +1,31 @@
package lib
import (
"io"
"net/url"
)
// CreateImageOptions holds information to create images
type CreateImageOptions struct {
// Parent is the image to create this image from
Parent string
// Tag is the name to tag this image
Tag string
// RegistryAuth is the base64 encoded credentials for this server
RegistryAuth string
}
// CreateImage creates a new image based in the parent options.
// It returns the JSON content in the response body.
func (cli *Client) CreateImage(options CreateImageOptions) (io.ReadCloser, error) {
var 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)
if err != nil {
return nil, err
}
return resp.body, nil
}