From 7a922259e1440a8d4f5fe7a9d722a6fc4004162d Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Sat, 25 May 2013 14:12:02 +0000 Subject: [PATCH 1/5] add error handling Upstream-commit: cb0bc4adc297baeea8e7b74269765c35457e04e7 Component: engine --- components/engine/api.go | 3 ++- components/engine/commands.go | 3 +++ components/engine/utils/utils.go | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/components/engine/api.go b/components/engine/api.go index 3164a886f6..e5816b255f 100644 --- a/components/engine/api.go +++ b/components/engine/api.go @@ -295,7 +295,8 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht w.Header().Set("Content-Type", "application/json") } if err := srv.ImagePull(image, tag, registry, w, version > 1.0); err != nil { - return err + fmt.Fprintf(w, utils.FormatError(err.Error(), version > 1.0)) + return nil } } else { //import if err := srv.ImageImport(src, repo, tag, r.Body, w); err != nil { diff --git a/components/engine/commands.go b/components/engine/commands.go index 6c4dcd14d6..01ab8a334f 100644 --- a/components/engine/commands.go +++ b/components/engine/commands.go @@ -1261,6 +1261,7 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e type Message struct { Status string `json:"status,omitempty"` Progress string `json:"progress,omitempty"` + Error string `json:"error,omitempty"` } dec := json.NewDecoder(resp.Body) for { @@ -1272,6 +1273,8 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e } if m.Progress != "" { fmt.Fprintf(out, "Downloading %s\r", m.Progress) + } else if m.Error != "" { + return fmt.Errorf(m.Error) } else { fmt.Fprintf(out, "%s\n", m.Status) } diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index 233c624b68..8588c52478 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -564,6 +564,13 @@ func FormatStatus(str string, json bool) string { return str + "\r\n" } +func FormatError(str string, json bool) string { + if json { + return "{\"error\" : \"" + str + "\"}" + } + return "Error: " + str + "\r\n" +} + func FormatProgress(str string, json bool) string { if json { return "{\"progress\" : \"" + str + "\"}" From 04ca200a24d2ee9076f6d9f4c6d58fa879b35dff Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Sat, 25 May 2013 15:09:46 +0000 Subject: [PATCH 2/5] imporved error, push, import insert Upstream-commit: c8c7094b2e8088210e2182e582264e09222c6e3d Component: engine --- components/engine/api.go | 44 ++++++++++++++----- components/engine/graph.go | 2 +- components/engine/runtime_test.go | 2 +- components/engine/server.go | 72 +++++++++++++++---------------- components/engine/utils/utils.go | 44 +++++++++++++------ 5 files changed, 101 insertions(+), 63 deletions(-) diff --git a/components/engine/api.go b/components/engine/api.go index e5816b255f..55fb8d5a66 100644 --- a/components/engine/api.go +++ b/components/engine/api.go @@ -289,17 +289,25 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht tag := r.Form.Get("tag") repo := r.Form.Get("repo") + if version > 1.0 { + w.Header().Set("Content-Type", "application/json") + } + sf := utils.NewStreamFormatter(version > 1.0) if image != "" { //pull registry := r.Form.Get("registry") - if version > 1.0 { - w.Header().Set("Content-Type", "application/json") - } - if err := srv.ImagePull(image, tag, registry, w, version > 1.0); err != nil { - fmt.Fprintf(w, utils.FormatError(err.Error(), version > 1.0)) - return nil + if err := srv.ImagePull(image, tag, registry, w, sf); err != nil { + if sf.Used() { + fmt.Fprintf(w, sf.FormatError(err)) + return nil + } + return err } } else { //import - if err := srv.ImageImport(src, repo, tag, r.Body, w); err != nil { + if err := srv.ImageImport(src, repo, tag, r.Body, w, sf); err != nil { + if sf.Used() { + fmt.Fprintf(w, sf.FormatError(err)) + return nil + } return err } } @@ -335,8 +343,15 @@ func postImagesInsert(srv *Server, version float64, w http.ResponseWriter, r *ht return fmt.Errorf("Missing parameter") } name := vars["name"] - - if err := srv.ImageInsert(name, url, path, w); err != nil { + if version > 1.0 { + w.Header().Set("Content-Type", "application/json") + } + sf := utils.NewStreamFormatter(version > 1.0) + if err := srv.ImageInsert(name, url, path, w, sf); err != nil { + if sf.Used() { + fmt.Fprintf(w, sf.FormatError(err)) + return nil + } return err } return nil @@ -352,8 +367,15 @@ func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http return fmt.Errorf("Missing parameter") } name := vars["name"] - - if err := srv.ImagePush(name, registry, w); err != nil { + if version > 1.0 { + w.Header().Set("Content-Type", "application/json") + } + sf := utils.NewStreamFormatter(version > 1.0) + if err := srv.ImagePush(name, registry, w, sf); err != nil { + if sf.Used() { + fmt.Fprintf(w, sf.FormatError(err)) + return nil + } return err } return nil diff --git a/components/engine/graph.go b/components/engine/graph.go index c0dd869227..0090d51636 100644 --- a/components/engine/graph.go +++ b/components/engine/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)", false), tmp.Root) + return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, "Buffering to disk %v/%v (%v)", utils.NewStreamFormatter(false)), tmp.Root) } // Mktemp creates a temporary sub-directory inside the graph's filesystem. diff --git a/components/engine/runtime_test.go b/components/engine/runtime_test.go index 6c4ec5ded4..0dd71c48a3 100644 --- a/components/engine/runtime_test.go +++ b/components/engine/runtime_test.go @@ -75,7 +75,7 @@ func init() { registry: registry.NewRegistry(runtime.root), } // Retrieve the Image - if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, false); err != nil { + if err := srv.ImagePull(unitTestImageName, "", "", os.Stdout, utils.NewStreamFormatter(false)); err != nil { panic(err) } } diff --git a/components/engine/server.go b/components/engine/server.go index 3303c7c5a1..3353796475 100644 --- a/components/engine/server.go +++ b/components/engine/server.go @@ -67,7 +67,7 @@ func (srv *Server) ImagesSearch(term string) ([]ApiSearch, error) { return outs, nil } -func (srv *Server) ImageInsert(name, url, path string, out io.Writer) error { +func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) error { out = utils.NewWriteFlusher(out) img, err := srv.runtime.repositories.LookupImage(name) if err != nil { @@ -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)\r", false), path); err != nil { + if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), path); err != nil { return err } // FIXME: Handle custom repo, tag comment, author @@ -99,7 +99,7 @@ func (srv *Server) ImageInsert(name, url, path string, out io.Writer) error { if err != nil { return err } - fmt.Fprintf(out, "%s\n", img.Id) + fmt.Fprintf(out, sf.FormatStatus("%s"), img.Id) return nil } @@ -291,7 +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, json bool) error { +func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []string, sf *utils.StreamFormatter) error { history, err := srv.registry.GetRemoteHistory(imgId, registry, token) if err != nil { return err @@ -301,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, utils.FormatStatus("Pulling %s metadata", json), id) + fmt.Fprintf(out, sf.FormatStatus("Pulling %s metadata"), id) imgJson, err := srv.registry.GetRemoteImageJson(id, registry, token) if err != nil { // FIXME: Keep goging in case of error? @@ -313,12 +313,12 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri } // Get the layer - fmt.Fprintf(out, utils.FormatStatus("Pulling %s fs layer", json), id) + fmt.Fprintf(out, sf.FormatStatus("Pulling %s fs layer"), 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, utils.FormatProgress("%v/%v (%v)", json), json), false, img); err != nil { + if err := srv.runtime.graph.Register(utils.ProgressReader(layer, contentLength, out, sf.FormatProgress("Downloading", "%v/%v (%v)"), sf), false, img); err != nil { return err } } @@ -326,8 +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, json bool) error { - fmt.Fprintf(out, utils.FormatStatus("Pulling repository %s from %s", json), remote, auth.IndexServerAddress()) +func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, sf *utils.StreamFormatter) error { + fmt.Fprintf(out, sf.FormatStatus("Pulling repository %s from %s"), remote, auth.IndexServerAddress()) repoData, err := srv.registry.GetRepositoryData(remote) if err != nil { return err @@ -364,11 +364,11 @@ func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, json b utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.Id) continue } - fmt.Fprintf(out, utils.FormatStatus("Pulling image %s (%s) from %s", json), img.Id, img.Tag, remote) + fmt.Fprintf(out, sf.FormatStatus("Pulling image %s (%s) from %s"), img.Id, img.Tag, remote) success := false for _, ep := range repoData.Endpoints { - 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) + if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens, sf); err != nil { + fmt.Fprintf(out, sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint\n"), askedTag, err) continue } success = true @@ -393,16 +393,16 @@ func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, json b return nil } -func (srv *Server) ImagePull(name, tag, registry string, out io.Writer, json bool) error { +func (srv *Server) ImagePull(name, tag, registry string, out io.Writer, sf *utils.StreamFormatter) error { out = utils.NewWriteFlusher(out) if registry != "" { - if err := srv.pullImage(out, name, registry, nil, json); err != nil { + if err := srv.pullImage(out, name, registry, nil, sf); err != nil { return err } return nil } - if err := srv.pullRepository(out, name, tag, json); err != nil { + if err := srv.pullRepository(out, name, tag, sf); err != nil { return err } @@ -475,14 +475,14 @@ func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgDat return imgList, nil } -func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[string]string) error { +func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[string]string, sf *utils.StreamFormatter) error { out = utils.NewWriteFlusher(out) - fmt.Fprintf(out, "Processing checksums\n") + fmt.Fprintf(out, sf.FormatStatus("Processing checksums")) imgList, err := srv.getImageList(localRepo) if err != nil { return err } - fmt.Fprintf(out, "Sending image list\n") + fmt.Fprintf(out, sf.FormatStatus("Sending image list")) repoData, err := srv.registry.PushImageJsonIndex(name, imgList, false) if err != nil { @@ -491,18 +491,18 @@ func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[stri // FIXME: Send only needed images for _, ep := range repoData.Endpoints { - fmt.Fprintf(out, "Pushing repository %s to %s (%d tags)\r\n", name, ep, len(localRepo)) + fmt.Fprintf(out, sf.FormatStatus("Pushing repository %s to %s (%d tags)"), name, ep, len(localRepo)) // For each image within the repo, push them for _, elem := range imgList { if _, exists := repoData.ImgList[elem.Id]; exists { - fmt.Fprintf(out, "Image %s already on registry, skipping\n", name) + fmt.Fprintf(out, sf.FormatStatus("Image %s already on registry, skipping"), name) continue } - if err := srv.pushImage(out, name, elem.Id, ep, repoData.Tokens); err != nil { + if err := srv.pushImage(out, name, elem.Id, ep, repoData.Tokens, sf); err != nil { // FIXME: Continue on error? return err } - fmt.Fprintf(out, "Pushing tags for rev [%s] on {%s}\n", elem.Id, ep+"/users/"+name+"/"+elem.Tag) + fmt.Fprintf(out, sf.FormatStatus("Pushing tags for rev [%s] on {%s}"), elem.Id, ep+"/users/"+name+"/"+elem.Tag) if err := srv.registry.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil { return err } @@ -515,13 +515,13 @@ func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[stri return nil } -func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []string) error { +func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []string, sf *utils.StreamFormatter) error { out = utils.NewWriteFlusher(out) jsonRaw, err := ioutil.ReadFile(path.Join(srv.runtime.graph.Root, imgId, "json")) if err != nil { return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err) } - fmt.Fprintf(out, "Pushing %s\r\n", imgId) + fmt.Fprintf(out, sf.FormatStatus("Pushing %s"), imgId) // Make sure we have the image's checksum checksum, err := srv.getChecksum(imgId) @@ -536,7 +536,7 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st // Send the json if err := srv.registry.PushImageJsonRegistry(imgData, jsonRaw, ep, token); err != nil { if err == registry.ErrAlreadyExists { - fmt.Fprintf(out, "Image %s already uploaded ; skipping\n", imgData.Id) + fmt.Fprintf(out, sf.FormatStatus("Image %s already uploaded ; skipping"), imgData.Id) return nil } return err @@ -569,20 +569,20 @@ 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, "", false), ep, token); err != nil { + if err := srv.registry.PushImageLayerRegistry(imgData.Id, utils.ProgressReader(layerData, int(layerData.Size), out, sf.FormatProgress("", "%v/%v (%v)"), sf), ep, token); err != nil { return err } return nil } -func (srv *Server) ImagePush(name, registry string, out io.Writer) error { +func (srv *Server) ImagePush(name, registry string, out io.Writer, sf *utils.StreamFormatter) error { out = utils.NewWriteFlusher(out) img, err := srv.runtime.graph.Get(name) if err != nil { - fmt.Fprintf(out, "The push refers to a repository [%s] (len: %d)\n", name, len(srv.runtime.repositories.Repositories[name])) + fmt.Fprintf(out, sf.FormatStatus("The push refers to a repository [%s] (len: %d)"), name, len(srv.runtime.repositories.Repositories[name])) // If it fails, try to get the repository if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists { - if err := srv.pushRepository(out, name, localRepo); err != nil { + if err := srv.pushRepository(out, name, localRepo, sf); err != nil { return err } return nil @@ -590,14 +590,14 @@ func (srv *Server) ImagePush(name, registry string, out io.Writer) error { return err } - fmt.Fprintf(out, "The push refers to an image: [%s]\n", name) - if err := srv.pushImage(out, name, img.Id, registry, nil); err != nil { + fmt.Fprintf(out, sf.FormatStatus("The push refers to an image: [%s]"), name) + if err := srv.pushImage(out, name, img.Id, registry, nil, sf); err != nil { return err } return nil } -func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer) error { +func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Writer, sf *utils.StreamFormatter) error { var archive io.Reader var resp *http.Response @@ -606,21 +606,21 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write } else { u, err := url.Parse(src) if err != nil { - fmt.Fprintf(out, "Error: %s\n", err) + return err } if u.Scheme == "" { u.Scheme = "http" u.Host = src u.Path = "" } - fmt.Fprintf(out, "Downloading from %s\n", u) + fmt.Fprintf(out, sf.FormatStatus("Downloading from %s"), u) // Download with curl (pretty progress bar) // If curl is not available, fallback to http.Get() resp, err = utils.Download(u.String(), out) if err != nil { return err } - archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, "Importing %v/%v (%v)\r", false) + archive = utils.ProgressReader(resp.Body, int(resp.ContentLength), out, sf.FormatProgress("Importing", "%v/%v (%v)"), sf) } img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src, "", nil) if err != nil { @@ -632,7 +632,7 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write return err } } - fmt.Fprintf(out, "%s\n", img.ShortId()) + fmt.Fprintf(out, sf.FormatStatus(img.ShortId())) return nil } diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index 8588c52478..ec05c657e3 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -69,7 +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 + sf *StreamFormatter } func (r *progressReader) Read(p []byte) (n int, err error) { @@ -93,7 +93,7 @@ func (r *progressReader) Read(p []byte) (n int, err error) { } // Send newline when complete if err != nil { - fmt.Fprintf(r.output, FormatStatus("", r.json)) + fmt.Fprintf(r.output, r.sf.FormatStatus("")) } return read, err @@ -101,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, json bool) *progressReader { +func ProgressReader(r io.ReadCloser, size int, output io.Writer, template string, sf *StreamFormatter) *progressReader { if template == "" { - template = "%v/%v (%v)\r" + template = "%v/%v (%v)" } - return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template, json} + return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template, sf} } // HumanDuration returns a human-readable approximation of a duration @@ -557,25 +557,41 @@ func NewWriteFlusher(w io.Writer) *WriteFlusher { return &WriteFlusher{w: w, flusher: flusher} } -func FormatStatus(str string, json bool) string { - if json { +type StreamFormatter struct { + json bool + used bool +} + +func NewStreamFormatter(json bool) *StreamFormatter { + return &StreamFormatter{json, false} +} + +func (sf *StreamFormatter) FormatStatus(str string) string { + sf.used = true + if sf.json { return "{\"status\" : \"" + str + "\"}" } return str + "\r\n" } -func FormatError(str string, json bool) string { - if json { - return "{\"error\" : \"" + str + "\"}" +func (sf *StreamFormatter) FormatError(err error) string { + sf.used = true + if sf.json { + return "{\"error\" : \"" + err.Error() + "\"}" } - return "Error: " + str + "\r\n" + return "Error: " + err.Error() + "\r\n" } -func FormatProgress(str string, json bool) string { - if json { +func (sf *StreamFormatter) FormatProgress(action, str string) string { + sf.used = true + if sf.json { return "{\"progress\" : \"" + str + "\"}" } - return "Downloading " + str + "\r" + return action + " " + str + "\r" +} + +func (sf *StreamFormatter) Used() bool { + return sf.used } From b088e2de7051ed3e18e763186947b0504293e4bb Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Sat, 25 May 2013 15:51:26 +0000 Subject: [PATCH 3/5] ensure valid json Upstream-commit: 14212930e40439d0cfc635d56e084736867f9e30 Component: engine --- components/engine/commands.go | 7 +------ components/engine/server.go | 2 +- components/engine/utils/utils.go | 24 +++++++++++++++++++++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/components/engine/commands.go b/components/engine/commands.go index 01ab8a334f..7a0a5be3ee 100644 --- a/components/engine/commands.go +++ b/components/engine/commands.go @@ -1258,14 +1258,9 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e } if resp.Header.Get("Content-Type") == "application/json" { - type Message struct { - Status string `json:"status,omitempty"` - Progress string `json:"progress,omitempty"` - Error string `json:"error,omitempty"` - } dec := json.NewDecoder(resp.Body) for { - var m Message + var m utils.JsonMessage if err := dec.Decode(&m); err == io.EOF { break } else if err != nil { diff --git a/components/engine/server.go b/components/engine/server.go index 3353796475..6ebf337dd0 100644 --- a/components/engine/server.go +++ b/components/engine/server.go @@ -368,7 +368,7 @@ func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, sf *ut success := false for _, ep := range repoData.Endpoints { if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens, sf); err != nil { - fmt.Fprintf(out, sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint\n"), askedTag, err) + fmt.Fprintf(out, sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint"), askedTag, err) continue } success = true diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index ec05c657e3..1fce76e5aa 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/sha256" "encoding/hex" + "encoding/json" "errors" "fmt" "index/suffixarray" @@ -557,6 +558,12 @@ func NewWriteFlusher(w io.Writer) *WriteFlusher { return &WriteFlusher{w: w, flusher: flusher} } +type JsonMessage struct { + Status string `json:"status,omitempty"` + Progress string `json:"progress,omitempty"` + Error string `json:"error,omitempty"` +} + type StreamFormatter struct { json bool used bool @@ -569,7 +576,11 @@ func NewStreamFormatter(json bool) *StreamFormatter { func (sf *StreamFormatter) FormatStatus(str string) string { sf.used = true if sf.json { - return "{\"status\" : \"" + str + "\"}" + b, err := json.Marshal(&JsonMessage{Status:str}); + if err != nil { + return sf.FormatError(err) + } + return string(b) } return str + "\r\n" } @@ -577,7 +588,10 @@ func (sf *StreamFormatter) FormatStatus(str string) string { func (sf *StreamFormatter) FormatError(err error) string { sf.used = true if sf.json { - return "{\"error\" : \"" + err.Error() + "\"}" + if b, err := json.Marshal(&JsonMessage{Error:err.Error()}); err == nil { + return string(b) + } + return "{\"error\":\"format error\"}" } return "Error: " + err.Error() + "\r\n" } @@ -585,7 +599,11 @@ func (sf *StreamFormatter) FormatError(err error) string { func (sf *StreamFormatter) FormatProgress(action, str string) string { sf.used = true if sf.json { - return "{\"progress\" : \"" + str + "\"}" + b, err := json.Marshal(&JsonMessage{Progress:str}) + if err != nil { + return sf.FormatError(err) + } + return string(b) } return action + " " + str + "\r" } From 037b9b61607feac8d063d28726b76b58c428e975 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Sun, 26 May 2013 23:45:45 +0000 Subject: [PATCH 4/5] fix json encoding, and use less casts Upstream-commit: 5a36efb61fae88a7fe71f67a6b1f8b9da6468014 Component: engine --- components/engine/api.go | 8 ++++---- components/engine/graph.go | 3 ++- components/engine/server.go | 34 ++++++++++++++++---------------- components/engine/utils/utils.go | 34 +++++++++++++++++--------------- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/components/engine/api.go b/components/engine/api.go index 55fb8d5a66..c5472fa2c2 100644 --- a/components/engine/api.go +++ b/components/engine/api.go @@ -297,7 +297,7 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht registry := r.Form.Get("registry") if err := srv.ImagePull(image, tag, registry, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err @@ -305,7 +305,7 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht } else { //import if err := srv.ImageImport(src, repo, tag, r.Body, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err @@ -349,7 +349,7 @@ func postImagesInsert(srv *Server, version float64, w http.ResponseWriter, r *ht sf := utils.NewStreamFormatter(version > 1.0) if err := srv.ImageInsert(name, url, path, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err @@ -373,7 +373,7 @@ func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http sf := utils.NewStreamFormatter(version > 1.0) if err := srv.ImagePush(name, registry, w, sf); err != nil { if sf.Used() { - fmt.Fprintf(w, sf.FormatError(err)) + w.Write(sf.FormatError(err)) return nil } return err diff --git a/components/engine/graph.go b/components/engine/graph.go index 0090d51636..befb5ace5e 100644 --- a/components/engine/graph.go +++ b/components/engine/graph.go @@ -165,7 +165,8 @@ 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)", utils.NewStreamFormatter(false)), tmp.Root) + sf := utils.NewStreamFormatter(false) + return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, sf.FormatProgress("Buffering to disk", "%v/%v (%v)"), sf), tmp.Root) } // Mktemp creates a temporary sub-directory inside the graph's filesystem. diff --git a/components/engine/server.go b/components/engine/server.go index 6ebf337dd0..8799c6dc51 100644 --- a/components/engine/server.go +++ b/components/engine/server.go @@ -99,7 +99,7 @@ func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils. if err != nil { return err } - fmt.Fprintf(out, sf.FormatStatus("%s"), img.Id) + out.Write(sf.FormatStatus(img.Id)) return nil } @@ -301,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, sf.FormatStatus("Pulling %s metadata"), id) + out.Write(sf.FormatStatus("Pulling %s metadata", id)) imgJson, err := srv.registry.GetRemoteImageJson(id, registry, token) if err != nil { // FIXME: Keep goging in case of error? @@ -313,7 +313,7 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri } // Get the layer - fmt.Fprintf(out, sf.FormatStatus("Pulling %s fs layer"), id) + out.Write(sf.FormatStatus("Pulling %s fs layer", id)) layer, contentLength, err := srv.registry.GetRemoteImageLayer(img.Id, registry, token) if err != nil { return err @@ -327,7 +327,7 @@ func (srv *Server) pullImage(out io.Writer, imgId, registry string, token []stri } func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, sf *utils.StreamFormatter) error { - fmt.Fprintf(out, sf.FormatStatus("Pulling repository %s from %s"), remote, auth.IndexServerAddress()) + out.Write(sf.FormatStatus("Pulling repository %s from %s", remote, auth.IndexServerAddress())) repoData, err := srv.registry.GetRepositoryData(remote) if err != nil { return err @@ -364,11 +364,11 @@ func (srv *Server) pullRepository(out io.Writer, remote, askedTag string, sf *ut utils.Debugf("(%s) does not match %s (id: %s), skipping", img.Tag, askedTag, img.Id) continue } - fmt.Fprintf(out, sf.FormatStatus("Pulling image %s (%s) from %s"), img.Id, img.Tag, remote) + out.Write(sf.FormatStatus("Pulling image %s (%s) from %s", img.Id, img.Tag, remote)) success := false for _, ep := range repoData.Endpoints { if err := srv.pullImage(out, img.Id, "https://"+ep+"/v1", repoData.Tokens, sf); err != nil { - fmt.Fprintf(out, sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint"), askedTag, err) + out.Write(sf.FormatStatus("Error while retrieving image for tag: %s (%s); checking next endpoint", askedTag, err)) continue } success = true @@ -477,12 +477,12 @@ func (srv *Server) getImageList(localRepo map[string]string) ([]*registry.ImgDat func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[string]string, sf *utils.StreamFormatter) error { out = utils.NewWriteFlusher(out) - fmt.Fprintf(out, sf.FormatStatus("Processing checksums")) + out.Write(sf.FormatStatus("Processing checksums")) imgList, err := srv.getImageList(localRepo) if err != nil { return err } - fmt.Fprintf(out, sf.FormatStatus("Sending image list")) + out.Write(sf.FormatStatus("Sending image list")) repoData, err := srv.registry.PushImageJsonIndex(name, imgList, false) if err != nil { @@ -491,18 +491,18 @@ func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[stri // FIXME: Send only needed images for _, ep := range repoData.Endpoints { - fmt.Fprintf(out, sf.FormatStatus("Pushing repository %s to %s (%d tags)"), name, ep, len(localRepo)) + out.Write(sf.FormatStatus("Pushing repository %s to %s (%d tags)", name, ep, len(localRepo))) // For each image within the repo, push them for _, elem := range imgList { if _, exists := repoData.ImgList[elem.Id]; exists { - fmt.Fprintf(out, sf.FormatStatus("Image %s already on registry, skipping"), name) + out.Write(sf.FormatStatus("Image %s already on registry, skipping", name)) continue } if err := srv.pushImage(out, name, elem.Id, ep, repoData.Tokens, sf); err != nil { // FIXME: Continue on error? return err } - fmt.Fprintf(out, sf.FormatStatus("Pushing tags for rev [%s] on {%s}"), elem.Id, ep+"/users/"+name+"/"+elem.Tag) + out.Write(sf.FormatStatus("Pushing tags for rev [%s] on {%s}", elem.Id, ep+"/users/"+name+"/"+elem.Tag)) if err := srv.registry.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil { return err } @@ -521,7 +521,7 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st if err != nil { return fmt.Errorf("Error while retreiving the path for {%s}: %s", imgId, err) } - fmt.Fprintf(out, sf.FormatStatus("Pushing %s"), imgId) + out.Write(sf.FormatStatus("Pushing %s", imgId)) // Make sure we have the image's checksum checksum, err := srv.getChecksum(imgId) @@ -536,7 +536,7 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st // Send the json if err := srv.registry.PushImageJsonRegistry(imgData, jsonRaw, ep, token); err != nil { if err == registry.ErrAlreadyExists { - fmt.Fprintf(out, sf.FormatStatus("Image %s already uploaded ; skipping"), imgData.Id) + out.Write(sf.FormatStatus("Image %s already uploaded ; skipping", imgData.Id)) return nil } return err @@ -579,7 +579,7 @@ func (srv *Server) ImagePush(name, registry string, out io.Writer, sf *utils.Str out = utils.NewWriteFlusher(out) img, err := srv.runtime.graph.Get(name) if err != nil { - fmt.Fprintf(out, sf.FormatStatus("The push refers to a repository [%s] (len: %d)"), name, len(srv.runtime.repositories.Repositories[name])) + out.Write(sf.FormatStatus("The push refers to a repository [%s] (len: %d)", name, len(srv.runtime.repositories.Repositories[name]))) // If it fails, try to get the repository if localRepo, exists := srv.runtime.repositories.Repositories[name]; exists { if err := srv.pushRepository(out, name, localRepo, sf); err != nil { @@ -590,7 +590,7 @@ func (srv *Server) ImagePush(name, registry string, out io.Writer, sf *utils.Str return err } - fmt.Fprintf(out, sf.FormatStatus("The push refers to an image: [%s]"), name) + out.Write(sf.FormatStatus("The push refers to an image: [%s]", name)) if err := srv.pushImage(out, name, img.Id, registry, nil, sf); err != nil { return err } @@ -613,7 +613,7 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write u.Host = src u.Path = "" } - fmt.Fprintf(out, sf.FormatStatus("Downloading from %s"), u) + out.Write(sf.FormatStatus("Downloading from %s", u)) // Download with curl (pretty progress bar) // If curl is not available, fallback to http.Get() resp, err = utils.Download(u.String(), out) @@ -632,7 +632,7 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write return err } } - fmt.Fprintf(out, sf.FormatStatus(img.ShortId())) + out.Write(sf.FormatStatus(img.ShortId())) return nil } diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index 1fce76e5aa..64aa111717 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -94,7 +94,7 @@ func (r *progressReader) Read(p []byte) (n int, err error) { } // Send newline when complete if err != nil { - fmt.Fprintf(r.output, r.sf.FormatStatus("")) + r.output.Write(r.sf.FormatStatus("")) } return read, err @@ -102,11 +102,12 @@ 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, sf *StreamFormatter) *progressReader { - if template == "" { - template = "%v/%v (%v)" +func ProgressReader(r io.ReadCloser, size int, output io.Writer, template []byte, sf *StreamFormatter) *progressReader { + tpl := string(template) + if tpl == "" { + tpl = "%v/%v (%v)" } - return &progressReader{r, NewWriteFlusher(output), size, 0, 0, template, sf} + return &progressReader{r, NewWriteFlusher(output), size, 0, 0, tpl, sf} } // HumanDuration returns a human-readable approximation of a duration @@ -573,39 +574,40 @@ func NewStreamFormatter(json bool) *StreamFormatter { return &StreamFormatter{json, false} } -func (sf *StreamFormatter) FormatStatus(str string) string { +func (sf *StreamFormatter) FormatStatus(format string, a ...interface{}) []byte { sf.used = true + str := fmt.Sprintf(format, a...) if sf.json { b, err := json.Marshal(&JsonMessage{Status:str}); if err != nil { return sf.FormatError(err) } - return string(b) + return b } - return str + "\r\n" + return []byte(str + "\r\n") } -func (sf *StreamFormatter) FormatError(err error) string { +func (sf *StreamFormatter) FormatError(err error) []byte { sf.used = true if sf.json { if b, err := json.Marshal(&JsonMessage{Error:err.Error()}); err == nil { - return string(b) + return b } - return "{\"error\":\"format error\"}" + return []byte("{\"error\":\"format error\"}") } - return "Error: " + err.Error() + "\r\n" + return []byte("Error: " + err.Error() + "\r\n") } -func (sf *StreamFormatter) FormatProgress(action, str string) string { +func (sf *StreamFormatter) FormatProgress(action, str string) []byte { sf.used = true if sf.json { b, err := json.Marshal(&JsonMessage{Progress:str}) if err != nil { - return sf.FormatError(err) + return nil } - return string(b) + return b } - return action + " " + str + "\r" + return []byte(action + " " + str + "\r") } func (sf *StreamFormatter) Used() bool { From 663bde65c37cca82d30db71912512f106ee51a28 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 28 May 2013 15:49:57 +0000 Subject: [PATCH 5/5] documentation Upstream-commit: 8699805756d25602e2412173aac7364fbe9627c7 Component: engine --- .../docs/sources/api/docker_remote_api.rst | 57 ++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/components/engine/docs/sources/api/docker_remote_api.rst b/components/engine/docs/sources/api/docker_remote_api.rst index 4c8ebe847f..3920aba983 100644 --- a/components/engine/docs/sources/api/docker_remote_api.rst +++ b/components/engine/docs/sources/api/docker_remote_api.rst @@ -15,10 +15,17 @@ Docker Remote API - Default port in the docker deamon is 4243 - The API tends to be REST, but for some complex commands, like attach or pull, the HTTP connection is hijacked to transport stdout stdin and stderr -2. Endpoints +2. Version +========== + +The current verson of the API is 1.1 +Calling /images//insert is the same as calling /v1.1/images//insert +You can still call an old version of the api using /v1.0/images//insert + +3. Endpoints ============ -2.1 Containers +3.1 Containers -------------- List containers @@ -459,7 +466,7 @@ Remove a container :statuscode 500: server error -2.2 Images +3.2 Images ---------- List Images @@ -548,7 +555,19 @@ Create an image POST /images/create?fromImage=base HTTP/1.1 - **Example response**: + **Example response v1.1**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Content-Type: application/json + + {"status":"Pulling..."} + {"progress":"1/? (n/a)"} + {"error":"Invalid..."} + ... + + **Example response v1.0**: .. sourcecode:: http @@ -579,7 +598,19 @@ Insert a file in a image POST /images/test/insert?path=/usr&url=myurl HTTP/1.1 - **Example response**: + **Example response v1.1**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Content-Type: application/json + + {"status":"Inserting..."} + {"progress":"1/? (n/a)"} + {"error":"Invalid..."} + ... + + **Example response v1.0**: .. sourcecode:: http @@ -694,7 +725,19 @@ Push an image on the registry POST /images/test/push HTTP/1.1 - **Example response**: + **Example response v1.1**: + + .. sourcecode:: http + + HTTP/1.1 200 OK + Content-Type: application/json + + {"status":"Pushing..."} + {"progress":"1/? (n/a)"} + {"error":"Invalid..."} + ... + + **Example response v1.0**: .. sourcecode:: http @@ -800,7 +843,7 @@ Search images :statuscode 500: server error -2.3 Misc +3.3 Misc -------- Build an image from Dockerfile via stdin