Ver código fonte

Implement docker image create with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera 9 anos atrás
pai
commit
1698fe01f5
2 arquivos alterados com 43 adições e 14 exclusões
  1. 12 14
      api/client/create.go
  2. 31 0
      api/client/lib/image_create.go

+ 12 - 14
api/client/create.go

@@ -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 {

+ 31 - 0
api/client/lib/image_create.go

@@ -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
+}