Ver Fonte

* Remote API: send push/pull progress bar as json

Solomon Hykes há 12 anos atrás
pai
commit
9775f0bd14
5 ficheiros alterados com 69 adições e 29 exclusões
  1. 5 2
      api.go
  2. 23 2
      commands.go
  3. 1 1
      graph.go
  4. 16 17
      server.go
  5. 24 7
      utils/utils.go

+ 5 - 2
api.go

@@ -13,7 +13,7 @@ import (
 	"strings"
 )
 
-const API_VERSION = 1.0
+const API_VERSION = 1.1
 
 func hijackServer(w http.ResponseWriter) (io.ReadCloser, io.Writer, error) {
 	conn, _, err := w.(http.Hijacker).Hijack()
@@ -291,7 +291,10 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht
 
 	if image != "" { //pull
 		registry := r.Form.Get("registry")
-		if err := srv.ImagePull(image, tag, registry, w); err != nil {
+		if version > 1.0 {
+			w.Header().Set("Content-Type", "application/json")
+		}
+		if err := srv.ImagePull(image, tag, registry, w, version > 1.0); err != nil {
 			return err
 		}
 	} else { //import

+ 23 - 2
commands.go

@@ -1257,8 +1257,29 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
 		return fmt.Errorf("error: %s", body)
 	}
 
-	if _, err := io.Copy(out, resp.Body); err != nil {
-		return err
+	if resp.Header.Get("Content-Type") == "application/json" {
+		type Message struct {
+			Status   string `json:"status,omitempty"`
+			Progress string `json:"progress,omitempty"`
+		}
+		dec := json.NewDecoder(resp.Body)
+		for {
+			var m Message
+			if err := dec.Decode(&m); err == io.EOF {
+				break
+			} else if err != nil {
+				return err
+			}
+			if m.Progress != "" {
+				fmt.Fprintf(out, "Downloading %s\r", m.Progress)
+			} else {
+				fmt.Fprintf(out, "%s\n", m.Status)
+			}
+		}
+	} else {
+		if _, err := io.Copy(out, resp.Body); err != nil {
+			return err
+		}
 	}
 	return nil
 }

+ 1 - 1
graph.go

@@ -165,7 +165,7 @@ func (graph *Graph) TempLayerArchive(id string, compression Compression, output
 	if err != nil {
 		return nil, err
 	}
-	return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, "Buffering to disk %v/%v (%v)"), tmp.Root)
+	return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, "Buffering to disk %v/%v (%v)", false), tmp.Root)
 }
 
 // Mktemp creates a temporary sub-directory inside the graph's filesystem.

+ 16 - 17
server.go

@@ -91,7 +91,7 @@ func (srv *Server) ImageInsert(name, url, path string, out io.Writer) error {
 		return err
 	}
 
-	if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, "Downloading %v/%v (%v)"), path); err != nil {
+	if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, "Downloading %v/%v (%v)\r", false), path); err != nil {
 		return err
 	}
 	// FIXME: Handle custom repo, tag comment, author
@@ -291,8 +291,7 @@ func (srv *Server) ContainerTag(name, repo, tag string, force bool) error {
 	return nil
 }
 
-func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []string) error {
-	out = utils.NewWriteFlusher(out)
+func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []string, json bool) error {
 	history, err := srv.registry.GetRemoteHistory(imgId, registry, token)
 	if err != nil {
 		return err
@@ -302,7 +301,7 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri
 	// FIXME: Launch the getRemoteImage() in goroutines
 	for _, id := range history {
 		if !srv.runtime.graph.Exists(id) {
-			fmt.Fprintf(out, "Pulling %s metadata\r\n", id)
+			fmt.Fprintf(out, utils.FormatStatus("Pulling %s metadata", json), id)
 			imgJson, err := srv.registry.GetRemoteImageJson(id, registry, token)
 			if err != nil {
 				// FIXME: Keep goging in case of error?
@@ -314,12 +313,12 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri
 			}
 
 			// Get the layer
-			fmt.Fprintf(out, "Pulling %s fs layer\r\n", img.Id)
+			fmt.Fprintf(out, utils.FormatStatus("Pulling %s fs layer", json), id)
 			layer, contentLength, err := srv.registry.GetRemoteImageLayer(img.Id, registry, token)
 			if err != nil {
 				return err
 			}
-			if err := srv.runtime.graph.Register(utils.ProgressReader(layer, contentLength, out, "Downloading %v/%v (%v)"), false, img); err != nil {
+			if err := srv.runtime.graph.Register(utils.ProgressReader(layer, contentLength, out, utils.FormatProgress("%v/%v (%v)", json), json), false, img); err != nil {
 				return err
 			}
 		}
@@ -327,9 +326,8 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri
 	return nil
 }
 
-func (srv *Server) pullRepository(out io.Writer, remote, askedTag string) error {
-	out = utils.NewWriteFlusher(out)
-	fmt.Fprintf(out, "Pulling repository %s from %s\r\n", remote, auth.IndexServerAddress())
+func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, json bool) error {
+	fmt.Fprintf(out, utils.FormatStatus("Pulling repository %s from %s", json), remote, auth.IndexServerAddress())
 	repoData, err := srv.registry.GetRepositoryData(remote)
 	if err != nil {
 		return err
@@ -366,11 +364,11 @@ func (srv *Server) pullRepository(out io.Writer, remote, askedTag string) error
 			utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.Id)
 			continue
 		}
-		fmt.Fprintf(out, "Pulling image %s (%s) from %s\n", img.Id, img.Tag, remote)
+		fmt.Fprintf(out, utils.FormatStatus("Pulling image %s (%s) from %s", json), img.Id, img.Tag, remote)
 		success := false
 		for _, ep := range repoData.Endpoints {
-			if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens); err != nil {
-				fmt.Fprintf(out, "Error while retrieving image for tag: %s (%s); checking next endpoint\n", askedTag, err)
+			if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens, json); err != nil {
+				fmt.Fprintf(out, utils.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint\n", json), askedTag, err)
 				continue
 			}
 			success = true
@@ -395,15 +393,16 @@ func (srv *Server) pullRepository(out io.Writer, remote, askedTag string) error
 	return nil
 }
 
-func (srv *Server) ImagePull(name, tag, registry string, out io.Writer) error {
+func (srv *Server) ImagePull(name, tag, registry string, out io.Writer, json bool) error {
+	out = utils.NewWriteFlusher(out)
 	if registry != "" {
-		if err := srv.pullImage(out, name, registry, nil); err != nil {
+		if err := srv.pullImage(out, name, registry, nil, json); err != nil {
 			return err
 		}
 		return nil
 	}
 
-	if err := srv.pullRepository(out, name, tag); err != nil {
+	if err := srv.pullRepository(out, name, tag, json); err != nil {
 		return err
 	}
 
@@ -570,7 +569,7 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st
 	}
 
 	// Send the layer
-	if err := srv.registry.PushImageLayerRegistry(imgData.Id, utils.ProgressReader(layerData, int(layerData.Size), out, ""), ep, token); err != nil {
+	if err := srv.registry.PushImageLayerRegistry(imgData.Id, utils.ProgressReader(layerData, int(layerData.Size), out, "", false), ep, token); err != nil {
 		return err
 	}
 	return nil
@@ -621,7 +620,7 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write
 		if err != nil {
 			return err
 		}
-		archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, "Importing %v/%v (%v)")
+		archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, "Importing %v/%v (%v)\r", false)
 	}
 	img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil)
 	if err != nil {

+ 24 - 7
utils/utils.go

@@ -69,6 +69,7 @@ type progressReader struct {
 	readProgress int           // How much has been read so far (bytes)
 	lastUpdate   int           // How many bytes read at least update
 	template     string        // Template to print. Default "%v/%v (%v)"
+	json bool
 }
 
 func (r *progressReader) Read(p []byte) (n int, err error) {
@@ -84,15 +85,15 @@ func (r *progressReader) Read(p []byte) (n int, err error) {
 	}
 	if r.readProgress-r.lastUpdate > updateEvery || err != nil {
 		if r.readTotal > 0 {
-			fmt.Fprintf(r.output, r.template+"\r", r.readProgress, r.readTotal, fmt.Sprintf("%.0f%%", float64(r.readProgress)/float64(r.readTotal)*100))
+			fmt.Fprintf(r.output, r.template, r.readProgress, r.readTotal, fmt.Sprintf("%.0f%%", float64(r.readProgress)/float64(r.readTotal)*100))
 		} else {
-			fmt.Fprintf(r.output, r.template+"\r", r.readProgress, "?", "n/a")
+			fmt.Fprintf(r.output, r.template, r.readProgress, "?", "n/a")
 		}
 		r.lastUpdate = r.readProgress
 	}
 	// Send newline when complete
 	if err != nil {
-		fmt.Fprintf(r.output, "\n")
+		fmt.Fprintf(r.output, FormatStatus("", r.json))
 	}
 
 	return read, err
@@ -100,11 +101,11 @@ func (r *progressReader) Read(p []byte) (n int, err error) {
 func (r *progressReader) Close() error {
 	return io.ReadCloser(r.reader).Close()
 }
-func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string) *progressReader {
-	if template == "" {
-		template = "%v/%v (%v)"
+func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string, json bool) *progressReader {
+      	if template == "" {
+		template = "%v/%v (%v)\r"
 	}
-	return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template}
+	return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template, json}
 }
 
 // HumanDuration returns a human-readable approximation of a duration
@@ -555,3 +556,19 @@ func NewWriteFlusher(w io.Writer) *WriteFlusher {
 	}
 	return &WriteFlusher{w: w, flusher: flusher}
 }
+
+func FormatStatus(str string, json bool) string {
+	if json {
+		return "{\"status\" : \"" + str + "\"}"
+	}
+	return str + "\r\n"
+}
+
+func FormatProgress(str string, json bool) string {
+	if json {
+		return "{\"progress\" : \"" + str + "\"}"
+	}
+	return "Downloading " + str + "\r"
+}
+
+