diff --git a/api/handlers.go b/api/handlers.go index 39ab133980..832e01d2e7 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -62,6 +62,59 @@ func getVersion(c *context, w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(version) } +// GET /images/get +func getImages(c *context, w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + httpError(w, err.Error(), http.StatusInternalServerError) + return + } + names := r.Form["names"] + + // find an engine which has all images + // Engine.Addr : [found names] + dict := make(map[string][]string) + bFoundEngine := false + var foundEngineAddr string + for _, image := range c.cluster.Images() { + for _, name := range names { + if image.Match(name) { + // check if engine addr already exists + value, exists := dict[image.Engine.Addr] + if exists { + // check if name already exists + found := false + for _, tempName := range value { + if tempName == name { + found = true + } + } + if found == false { + dict[image.Engine.Addr] = append(value, name) + if len(names) == len(dict[image.Engine.Addr]) { + bFoundEngine = true + foundEngineAddr = image.Engine.Addr + } + } + } else { + dict[image.Engine.Addr] = []string{name} + } + } + if bFoundEngine { + break + } + } + if bFoundEngine { + break + } + } + + if bFoundEngine { + proxy(c.tlsConfig, foundEngineAddr, w, r) + } else { + httpError(w, fmt.Sprintf("Not found an engine which has all images: %s", names), http.StatusNotFound) + } +} + // GET /images/json func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { diff --git a/api/router.go b/api/router.go index a0722f66c0..fbf95025e0 100644 --- a/api/router.go +++ b/api/router.go @@ -28,7 +28,7 @@ var routes = map[string]map[string]handler{ "/images/json": getImagesJSON, "/images/viz": notImplementedHandler, "/images/search": proxyRandom, - "/images/get": notImplementedHandler, + "/images/get": getImages, "/images/{name:.*}/get": proxyImage, "/images/{name:.*}/history": proxyImage, "/images/{name:.*}/json": proxyImage, diff --git a/test/integration/api/save.bats b/test/integration/api/save.bats index 5c2e009644..25dc27ee2a 100644 --- a/test/integration/api/save.bats +++ b/test/integration/api/save.bats @@ -42,3 +42,31 @@ function teardown() { rm -f $temp_file_name rm -f $temp_file_name_o } + +@test "docker save muti-images" { + start_docker_with_busybox 1 + start_docker 1 + swarm_manage + + # tag busybox + run docker_swarm tag busybox testimage + [ "$status" -eq 0 ] + + # make sure image exists + run docker_swarm images + [ "$status" -eq 0 ] + [[ "${output}" == *"busybox"* ]] + [[ "${output}" == *"testimage"* ]] + + temp_file_name=$(mktemp) + + docker_swarm save busybox testimage > $temp_file_name + + # saved image file exists, not empty and is tar file + [ -s $temp_file_name ] + run file $temp_file_name + [ "$status" -eq 0 ] + [[ "${output}" == *"tar archive"* ]] + + rm -f $temp_file_name +}