1
0
Fork 0
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:
Solomon Hykes 2013-03-25 18:48:57 -07:00
parent e4a69b1044
commit f70bc2c98c
2 changed files with 22 additions and 24 deletions

View file

@ -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) { if srv.runtime.graph.LookupRemoteImage(remote, srv.runtime.authConfig) {
fmt.Fprintf(stdout, "Pulling %s...\n", remote) if err := srv.runtime.graph.PullImage(stdout, remote, srv.runtime.authConfig); err != nil {
if err := srv.runtime.graph.PullImage(remote, srv.runtime.authConfig); err != nil {
return err return err
} }
fmt.Fprintf(stdout, "Pulled\n")
return nil return nil
} }
// FIXME: Allow pull repo:tag // 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 { if err := srv.runtime.graph.PullRepository(stdout, remote, "", srv.runtime.repositories, srv.runtime.authConfig); err != nil {
return err return err
} }
fmt.Fprintf(stdout, "Pull completed\n")
return nil return nil
} }

View file

@ -94,36 +94,38 @@ func (graph *Graph) LookupRemoteImage(imgId string, authConfig *auth.AuthConfig)
// Retrieve an image from the Registry. // Retrieve an image from the Registry.
// Returns the Image object as well as the layer as an Archive (io.Reader) // 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{} client := &http.Client{}
fmt.Fprintf(stdout, "Pulling %s metadata\n", imgId)
// Get the Json // Get the Json
req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil) req, err := http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/json", nil)
if err != 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) req.SetBasicAuth(authConfig.Username, authConfig.Password)
res, err := client.Do(req) res, err := client.Do(req)
if err != nil || res.StatusCode != 200 { if err != nil {
if res != nil { return nil, nil, fmt.Errorf("Failed to download json: %s", err)
return nil, nil, fmt.Errorf("Internal server error: %d trying to get image %s", res.StatusCode, imgId) }
} if res.StatusCode != 200 {
return nil, nil, err return nil, nil, fmt.Errorf("HTTP code %d", res.StatusCode)
} }
defer res.Body.Close() defer res.Body.Close()
jsonString, err := ioutil.ReadAll(res.Body) jsonString, err := ioutil.ReadAll(res.Body)
if err != nil { 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) img, err := NewImgJson(jsonString)
if err != nil { 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 img.Id = imgId
// Get the layer // Get the layer
fmt.Fprintf(stdout, "Pulling %s fs layer\n", imgId)
req, err = http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/layer", nil) req, err = http.NewRequest("GET", REGISTRY_ENDPOINT+"/images/"+imgId+"/layer", nil)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Error while getting from the server: %s\n", err) 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 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) history, err := graph.getRemoteHistory(imgId, authConfig)
if err != nil { if err != nil {
return err return err
@ -145,7 +147,7 @@ func (graph *Graph) PullImage(imgId string, authConfig *auth.AuthConfig) error {
// FIXME: Lunch the getRemoteImage() in goroutines // FIXME: Lunch the getRemoteImage() in goroutines
for _, j := range history { for _, j := range history {
if !graph.Exists(j.Id) { 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 { if err != nil {
// FIXME: Keep goging in case of error? // FIXME: Keep goging in case of error?
return err 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 { func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, repositories *TagStore, authConfig *auth.AuthConfig) error {
client := &http.Client{} 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 var repositoryTarget string
// If we are asking for 'root' repository, lookup on the Library's registry // 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) req.SetBasicAuth(authConfig.Username, authConfig.Password)
res, err := client.Do(req) res, err := client.Do(req)
if err != nil || res.StatusCode != 200 { if err != nil {
if res != nil {
return fmt.Errorf("Internal server error: %d trying to pull %s", res.StatusCode, remote)
}
return err return err
} }
if res.StatusCode != 200 {
return fmt.Errorf("HTTP code: %d", res.StatusCode)
}
defer res.Body.Close() defer res.Body.Close()
rawJson, err := ioutil.ReadAll(res.Body) rawJson, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
@ -194,7 +196,8 @@ func (graph *Graph) PullRepository(stdout io.Writer, remote, askedTag string, re
return err return err
} }
for tag, rev := range t { 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 return err
} }
if err = repositories.Set(remote, tag, rev, true); err != nil { 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) req.SetBasicAuth(authConfig.Username, authConfig.Password)
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { 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 { if res.StatusCode != 200 {
Debugf("Pushing return status: %d\n", res.StatusCode)
switch res.StatusCode { switch res.StatusCode {
case 204: case 204:
// Case where the image is already on the Registry // Case where the image is already on the Registry