mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Upload images only when necessary
This commit is contained in:
parent
97880a223e
commit
dc9d6c1c1f
2 changed files with 19 additions and 10 deletions
|
@ -3,6 +3,7 @@ package registry
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/auth"
|
"github.com/dotcloud/docker/auth"
|
||||||
"github.com/dotcloud/docker/utils"
|
"github.com/dotcloud/docker/utils"
|
||||||
|
@ -14,6 +15,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrAlreadyExists error = errors.New("Image already exists")
|
||||||
|
|
||||||
func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) {
|
func doWithCookies(c *http.Client, req *http.Request) (*http.Response, error) {
|
||||||
for _, cookie := range c.Jar.Cookies(req.URL) {
|
for _, cookie := range c.Jar.Cookies(req.URL) {
|
||||||
req.AddCookie(cookie)
|
req.AddCookie(cookie)
|
||||||
|
@ -291,15 +294,13 @@ func (r *Registry) PushImageJsonRegistry(imgData *ImgData, jsonRaw []byte, regis
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
errBody, err := ioutil.ReadAll(res.Body)
|
errBody, err := ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("HTTP code %d while uploading metadata and error when"+
|
return fmt.Errorf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err)
|
||||||
" trying to parse response body: %v", res.StatusCode, err)
|
|
||||||
}
|
}
|
||||||
var jsonBody map[string]string
|
var jsonBody map[string]string
|
||||||
if err := json.Unmarshal(errBody, &jsonBody); err != nil {
|
if err := json.Unmarshal(errBody, &jsonBody); err != nil {
|
||||||
errBody = []byte(err.Error())
|
errBody = []byte(err.Error())
|
||||||
} else if jsonBody["error"] == "Image already exists" {
|
} else if jsonBody["error"] == "Image already exists" {
|
||||||
utils.Debugf("Image %s already uploaded ; skipping\n", imgData.Id)
|
return ErrAlreadyExists
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
return fmt.Errorf("HTTP code %d while uploading metadata: %s", res.StatusCode, errBody)
|
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 + "\""
|
revision = "\"" + revision + "\""
|
||||||
registry = "https://" + registry + "/v1"
|
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))
|
req, err := http.NewRequest("PUT", registry+"/repositories/"+remote+"/tags/"+tag, strings.NewReader(revision))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
18
server.go
18
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))
|
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 each image within the repo, push them
|
||||||
for _, elem := range imgList {
|
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 {
|
if err := srv.pushImage(out, name, elem.Id, ep, repoData.Tokens); err != nil {
|
||||||
// FIXME: Continue on error?
|
// FIXME: Continue on error?
|
||||||
return err
|
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 {
|
if err := srv.registry.PushRegistryTag(name, elem.Id, elem.Tag, ep, repoData.Tokens); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -511,6 +516,15 @@ func (srv *Server) pushImage(out io.Writer, remote, imgId, ep string, token []st
|
||||||
Checksum: checksum,
|
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
|
// Retrieve the tarball to be sent
|
||||||
var layerData *TempArchive
|
var layerData *TempArchive
|
||||||
// If the archive exists, use it
|
// 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
|
// 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, ""), ep, token); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue