Skip to content

Commit

Permalink
Merge pull request docker-archive#1206 from jimmyxian/fix-pull-return…
Browse files Browse the repository at this point in the history
…-code

Fix pull return code
  • Loading branch information
vieux committed Sep 14, 2015
2 parents 7481e1d + a10a39c commit e6d81e1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
14 changes: 13 additions & 1 deletion api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,26 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) {
if tag := r.Form.Get("tag"); tag != "" {
image += ":" + tag
}
callback := func(what, status string) {

errorFound := false
callback := func(what, status string, err error) {
if err != nil {
errorFound = true
sendJSONMessage(wf, what, fmt.Sprintf("Pulling %s... : %s", image, err.Error()))
return
}
if status == "" {
sendJSONMessage(wf, what, fmt.Sprintf("Pulling %s...", image))
} else {
sendJSONMessage(wf, what, fmt.Sprintf("Pulling %s... : %s", image, status))
}
}
c.cluster.Pull(image, &authConfig, callback)

if errorFound {
sendErrorJSONMessage(wf, 1, "")
}

} else { //import
source := r.Form.Get("fromSrc")
repo := r.Form.Get("repo")
Expand Down
17 changes: 17 additions & 0 deletions api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ func sendJSONMessage(w io.Writer, id, status string) {
json.NewEncoder(w).Encode(message)
}

func sendErrorJSONMessage(w io.Writer, errorCode int, errorMessage string) {
error := struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}{
errorCode,
errorMessage,
}

message := struct {
Error interface{} `json:"errorDetail,omitempty"`
}{
&error,
}

json.NewEncoder(w).Encode(message)
}
func newClientAndScheme(tlsConfig *tls.Config) (*http.Client, string) {
if tlsConfig != nil {
return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}, "https"
Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Cluster interface {
// `callback` can be called multiple time
// `where` is where it is being pulled
// `status` is the current status, like "", "in progress" or "downloaded
Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string))
Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string, err error))

// Import image
// `callback` can be called multiple time
Expand Down
2 changes: 1 addition & 1 deletion cluster/mesos/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (c *Cluster) RemoveImage(image *cluster.Image) ([]*dockerclient.ImageDelete
}

// Pull will pull images on the cluster nodes
func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string)) {
func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string, err error)) {

}

Expand Down
8 changes: 4 additions & 4 deletions cluster/swarm/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (c *Cluster) RemoveImages(name string, force bool) ([]*dockerclient.ImageDe
}

// Pull is exported
func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string)) {
func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callback func(where, status string, err error)) {
var wg sync.WaitGroup

c.RLock()
Expand All @@ -307,14 +307,14 @@ func (c *Cluster) Pull(name string, authConfig *dockerclient.AuthConfig, callbac
defer wg.Done()

if callback != nil {
callback(engine.Name, "")
callback(engine.Name, "", nil)
}
err := engine.Pull(name, authConfig)
if callback != nil {
if err != nil {
callback(engine.Name, err.Error())
callback(engine.Name, "", err)
} else {
callback(engine.Name, "downloaded")
callback(engine.Name, "downloaded", nil)
}
}
}(e)
Expand Down
8 changes: 8 additions & 0 deletions test/integration/api/pull.bats
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ function teardown() {
[[ "${lines[1]}" == *"busybox"* ]]
done
}

@test "docker pull -check error code" {
start_docker 2
swarm_manage

run docker_swarm pull does_not_exist
[ "$status" -eq 1 ]
}

0 comments on commit e6d81e1

Please sign in to comment.