From a10a39c462e7a07b01b3e3b03972a1430d48cc6a Mon Sep 17 00:00:00 2001 From: Xian Chaobo Date: Thu, 10 Sep 2015 04:10:28 -0400 Subject: [PATCH] fix pull return code Signed-off-by: Xian Chaobo --- api/handlers.go | 14 +++++++++++++- api/utils.go | 17 +++++++++++++++++ cluster/cluster.go | 2 +- cluster/mesos/cluster.go | 2 +- cluster/swarm/cluster.go | 8 ++++---- test/integration/api/pull.bats | 8 ++++++++ 6 files changed, 44 insertions(+), 7 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index f3b89d7ba9..45d55bd332 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -434,7 +434,14 @@ 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 { @@ -442,6 +449,11 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) { } } c.cluster.Pull(image, &authConfig, callback) + + if errorFound { + sendErrorJSONMessage(wf, 1, "") + } + } else { //import source := r.Form.Get("fromSrc") repo := r.Form.Get("repo") diff --git a/api/utils.go b/api/utils.go index f6f34927cd..c2ae3d0f15 100644 --- a/api/utils.go +++ b/api/utils.go @@ -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" diff --git a/cluster/cluster.go b/cluster/cluster.go index 30899f8502..696912c640 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -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 diff --git a/cluster/mesos/cluster.go b/cluster/mesos/cluster.go index 4c21b90fdb..c7bf14b56e 100644 --- a/cluster/mesos/cluster.go +++ b/cluster/mesos/cluster.go @@ -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)) { } diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 11dd53923c..a8c60e29cd 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -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() @@ -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) diff --git a/test/integration/api/pull.bats b/test/integration/api/pull.bats index 1e27c4549a..9d9e7c87cc 100644 --- a/test/integration/api/pull.bats +++ b/test/integration/api/pull.bats @@ -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 ] +}