From dc9d6c1c1f52eefba95920059f0607d3d57b2d07 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 15 May 2013 13:22:57 -0700 Subject: [PATCH] Upload images only when necessary --- registry/registry.go | 11 +++++------ server.go | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/registry/registry.go b/registry/registry.go index 03e977c374..d79b3e9f20 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -3,6 +3,7 @@ package registry import ( "bytes" "encoding/json" + "errors" "fmt" "github.com/dotcloud/docker/auth" "github.com/dotcloud/docker/utils" @@ -14,6 +15,8 @@ import ( "strings" ) +var ErrAlreadyExists error = errors.New("Image already exists") + func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) { for _, cookie := range c.Jar.Cookies(req.URL) { req.AddCookie(cookie) @@ -291,15 +294,13 @@ func (r *Registry) PushImageJsonRegistry(imgData *ImgData, jsonRaw []byte, regis if res.StatusCode != 200 { errBody, err := ioutil.ReadAll(res.Body) if err != nil { - return fmt.Errorf("HTTP code %d while uploading metadata and error when"+ - " trying to parse response body: %v", res.StatusCode, err) + return fmt.Errorf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err) } var jsonBody map[string]string if err := json.Unmarshal(errBody, &jsonBody); err != nil { errBody = []byte(err.Error()) } else if jsonBody["error"] == "Image already exists" { - utils.Debugf("Image %s already uploaded ; skipping\n", imgData.Id) - return nil + return ErrAlreadyExists } return fmt.Errorf("HTTP code %d while uploading metadata: %s", res.StatusCode, errBody) } @@ -338,8 +339,6 @@ func (r *Registry) PushRegistryTag(remote, revision, tag, registry string, token revision = "\"" + revision + "\"" registry = "https://" + registry + "/v1" - utils.Debugf("Pushing tags for rev [%s] on {%s}\n", revision, registry+"/users/"+remote+"/"+tag) - req, err := http.NewRequest("PUT", registry+"/repositories/"+remote+"/tags/"+tag, strings.NewReader(revision)) if err != nil { return err diff --git a/server.go b/server.go index 35c3599093..05be2a9167 100644 --- a/server.go +++ b/server.go @@ -478,10 +478,15 @@ func (srv *Server) pushRepository(out io.Writer, name string, localRepo map[stri fmt.Fprintf(out, "Pushing repository %s to %s (%d tags)\r\n", 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) + continue + } if err := srv.pushImage(out, name, elem.Id, ep, repoData.Tokens); 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) if err := srv.registry.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil { return err } @@ -511,6 +516,15 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st Checksum: checksum, } + // 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) + return nil + } + return err + } + // Retrieve the tarball to be sent var layerData *TempArchive // If the archive exists, use it @@ -537,10 +551,6 @@ 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 { - return err - } // Send the layer if err := srv.registry.PushImageLayerRegistry(imgData.Id, utils.ProgressReader(layerData, int(layerData.Size), out, ""), ep, token); err != nil { return err