mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Cleaned up error checking of 'docker pull', to be symmetrical to 'docker push'
This commit is contained in:
parent
e4a69b1044
commit
f70bc2c98c
2 changed files with 22 additions and 24 deletions
|
@ -472,19 +472,15 @@ func (srv *Server) CmdPull(stdin io.ReadCloser, stdout io.Writer, args ...string
|
|||
}
|
||||
|
||||
if srv.runtime.graph.LookupRemoteImage(remote, srv.runtime.authConfig) {
|
||||
fmt.Fprintf(stdout, "Pulling %s...\n", remote)
|
||||
if err := srv.runtime.graph.PullImage(remote, srv.runtime.authConfig); err != nil {
|
||||
if err := srv.runtime.graph.PullImage(stdout, remote, srv.runtime.authConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(stdout, "Pulled\n")
|
||||
return nil
|
||||
}
|
||||
// FIXME: Allow pull repo:tag
|
||||
fmt.Fprintf(stdout, "Pulling %s...\n", remote)
|
||||
if err := srv.runtime.graph.PullRepository(stdout, remote, "", srv.runtime.repositories, srv.runtime.authConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(stdout, "Pull completed\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
40
registry.go
40
registry.go
|
@ -94,36 +94,38 @@ func (graph *Graph) LookupRemoteImage(imgId string, authConfig *auth.AuthConfig)
|
|||
|
||||
// Retrieve an image from the Registry.
|
||||
// Returns the Image object as well as the layer as an Archive (io.Reader)
|
||||
func (graph *Graph) getRemoteImage(imgId string, authConfig *auth.AuthConfig) (*Image, Archive, error) {
|
||||
func (graph *Graph) getRemoteImage(stdout io.Writer, imgId string, authConfig *auth.AuthConfig) (*Image, Archive, error) {
|
||||
client := &http.Client{}
|
||||
|
||||
fmt.Fprintf(stdout, "Pulling %s metadata\n", imgId)
|
||||
// Get the Json
|
||||
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error while getting from the server: %s\n", err)
|
||||
return nil, nil, fmt.Errorf("Failed to download json: %s", err)
|
||||
}
|
||||
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
||||
res, err := client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
if res != nil {
|
||||
return nil, nil, fmt.Errorf("Internal server error: %d trying to get image %s", res.StatusCode, imgId)
|
||||
}
|
||||
return nil, nil, err
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Failed to download json: %s", err)
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
return nil, nil, fmt.Errorf("HTTP code %d", res.StatusCode)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
jsonString, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error while reading the http response: %s\n", err)
|
||||
return nil, nil, fmt.Errorf("Failed to download json: %s", err)
|
||||
}
|
||||
|
||||
img, err := NewImgJson(jsonString)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error while parsing the json: %s\n", err)
|
||||
return nil, nil, fmt.Errorf("Failed to parse json: %s", err)
|
||||
}
|
||||
img.Id = imgId
|
||||
|
||||
// Get the layer
|
||||
fmt.Fprintf(stdout, "Pulling %s fs layer\n", imgId)
|
||||
req, err = http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/layer", nil)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error while getting from the server: %s\n", err)
|
||||
|
@ -136,7 +138,7 @@ func (graph *Graph) getRemoteImage(imgId string, authConfig *auth.AuthConfig) (*
|
|||
return img, res.Body, nil
|
||||
}
|
||||
|
||||
func (graph *Graph) PullImage(imgId string, authConfig *auth.AuthConfig) error {
|
||||
func (graph *Graph) PullImage(stdout io.Writer, imgId string, authConfig *auth.AuthConfig) error {
|
||||
history, err := graph.getRemoteHistory(imgId, authConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -145,7 +147,7 @@ func (graph *Graph) PullImage(imgId string, authConfig *auth.AuthConfig) error {
|
|||
// FIXME: Lunch the getRemoteImage() in goroutines
|
||||
for _, j := range history {
|
||||
if !graph.Exists(j.Id) {
|
||||
img, layer, err := graph.getRemoteImage(j.Id, authConfig)
|
||||
img, layer, err := graph.getRemoteImage(stdout, j.Id, authConfig)
|
||||
if err != nil {
|
||||
// FIXME: Keep goging in case of error?
|
||||
return err
|
||||
|
@ -162,7 +164,7 @@ func (graph *Graph) PullImage(imgId string, authConfig *auth.AuthConfig) error {
|
|||
func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, repositories *TagStore, authConfig *auth.AuthConfig) error {
|
||||
client := &http.Client{}
|
||||
|
||||
fmt.Fprintf(stdout, "Pulling repo: %s\n", REGISTRY_ENDPOINT+"/users/"+remote)
|
||||
fmt.Fprintf(stdout, "Pulling repository %s\n", remote)
|
||||
|
||||
var repositoryTarget string
|
||||
// If we are asking for 'root' repository, lookup on the Library's registry
|
||||
|
@ -178,12 +180,12 @@ func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, re
|
|||
}
|
||||
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
||||
res, err := client.Do(req)
|
||||
if err != nil || res.StatusCode != 200 {
|
||||
if res != nil {
|
||||
return fmt.Errorf("Internal server error: %d trying to pull %s", res.StatusCode, remote)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
return fmt.Errorf("HTTP code: %d", res.StatusCode)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
rawJson, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
|
@ -194,7 +196,8 @@ func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, re
|
|||
return err
|
||||
}
|
||||
for tag, rev := range t {
|
||||
if err = graph.PullImage(rev, authConfig); err != nil {
|
||||
fmt.Fprintf(stdout, "Pulling tag %s:%s\n", remote, tag)
|
||||
if err = graph.PullImage(stdout, rev, authConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = repositories.Set(remote, tag, rev, true); err != nil {
|
||||
|
@ -232,10 +235,9 @@ func (graph *Graph) PushImage(stdout io.Writer, imgOrig *Image, authConfig *auth
|
|||
req.SetBasicAuth(authConfig.Username, authConfig.Password)
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to upload json: %s", err)
|
||||
return fmt.Errorf("Failed to upload metadata: %s", err)
|
||||
}
|
||||
if res.StatusCode != 200 {
|
||||
Debugf("Pushing return status: %d\n", res.StatusCode)
|
||||
switch res.StatusCode {
|
||||
case 204:
|
||||
// Case where the image is already on the Registry
|
||||
|
|
Loading…
Add table
Reference in a new issue