diff --git a/api/handlers.go b/api/handlers.go index 98ea62f728..142210157d 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -4,15 +4,15 @@ import ( "bytes" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" + "os" + "path" "runtime" "sort" "strconv" "strings" - "os" - "io" - "path" dockerfilters "github.com/docker/docker/pkg/parsers/filters" "github.com/docker/swarm/cluster" @@ -275,36 +275,36 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) { // POST /images/load func postImagesLoad(c *context, w http.ResponseWriter, r *http.Request) { - //cache tar file - tmpImageDir, err := ioutil.TempDir("", "docker-import-") - if err != nil { - httpError(w, err.Error(), http.StatusInternalServerError) - return - } - defer os.RemoveAll(tmpImageDir) + //cache tar file + tmpImageDir, err := ioutil.TempDir("", "docker-import-") + if err != nil { + httpError(w, err.Error(), http.StatusInternalServerError) + return + } + defer os.RemoveAll(tmpImageDir) - repoTarFile := path.Join(tmpImageDir, "repo.tar") - tarFile, err := os.Create(repoTarFile) - if err != nil { - httpError(w, err.Error(), http.StatusInternalServerError) - return - } - if _, err := io.Copy(tarFile, r.Body); err != nil { - httpError(w, err.Error(), http.StatusInternalServerError) - return - } - tarFile.Close() - - // call cluster to load image on every node - wf := NewWriteFlusher(w) - callback := func(what, status string) { - if status == "" { - fmt.Fprintf(wf, "%s:Loading Image...\n", what) - } else { - fmt.Fprintf(wf, "%s:Loading Image... %s\n", what,status) - } - } - c.cluster.Load(repoTarFile, callback) + repoTarFile := path.Join(tmpImageDir, "repo.tar") + tarFile, err := os.Create(repoTarFile) + if err != nil { + httpError(w, err.Error(), http.StatusInternalServerError) + return + } + if _, err := io.Copy(tarFile, r.Body); err != nil { + httpError(w, err.Error(), http.StatusInternalServerError) + return + } + tarFile.Close() + + // call cluster to load image on every node + wf := NewWriteFlusher(w) + callback := func(what, status string) { + if status == "" { + fmt.Fprintf(wf, "%s:Loading Image...\n", what) + } else { + fmt.Fprintf(wf, "%s:Loading Image... %s\n", what, status) + } + } + c.cluster.Load(repoTarFile, callback) } // GET /events diff --git a/cluster/cluster.go b/cluster/cluster.go index 4fb1a9891d..63ded4a30c 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -33,11 +33,11 @@ type Cluster interface { // `status` is the current status, like "", "in progress" or "downloaded Pull(name string, callback func(what, status string)) - // Load images - // `callback` can be called multiple time - // `what` is what is being loaded - // `status` is the current status, like "", "in progress" or "loaded" - Load(tarFile string, callback func(what, status string)) + // Load images + // `callback` can be called multiple time + // `what` is what is being loaded + // `status` is the current status, like "", "in progress" or "loaded" + Load(tarFile string, callback func(what, status string)) // Return some info about the cluster, like nb or containers / images // It is pretty open, so the implementation decides what to return. diff --git a/cluster/engine.go b/cluster/engine.go index 9b7084094c..f36c04ef58 100644 --- a/cluster/engine.go +++ b/cluster/engine.go @@ -5,10 +5,10 @@ import ( "errors" "fmt" "net" + "os" "strings" "sync" "time" - "os" log "github.com/Sirupsen/logrus" "github.com/samalba/dockerclient" @@ -414,15 +414,15 @@ func (e *Engine) Pull(image string) error { // Load an image on the engine func (e *Engine) Load(tarFile string) error { - file, err := os.Open(tarFile) - if err != nil { - return err - } + file, err := os.Open(tarFile) + if err != nil { + return err + } - if err := e.client.LoadImage(file); err != nil { - return err - } - return nil + if err := e.client.LoadImage(file); err != nil { + return err + } + return nil } // RegisterEventHandler registers an event handler. diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index dac0385db3..ada1e8c0e8 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -240,27 +240,27 @@ func (c *Cluster) Pull(name string, callback func(what, status string)) { // Load image func (c *Cluster) Load(tarFile string, callback func(what, status string)) { - size := len(c.engines) - done := make(chan bool, size) - for _, n := range c.engines { - go func(nn *cluster.Engine) { - if callback != nil { - callback(nn.Name, "") - } - err := nn.Load(tarFile) - if callback != nil { - if err != nil { - callback(nn.Name, err.Error()) - } else { - callback(nn.Name, "loaded") - } - } - done <- true - }(n) - } - for i := 0; i < size; i++ { - <-done - } + size := len(c.engines) + done := make(chan bool, size) + for _, n := range c.engines { + go func(nn *cluster.Engine) { + if callback != nil { + callback(nn.Name, "") + } + err := nn.Load(tarFile) + if callback != nil { + if err != nil { + callback(nn.Name, err.Error()) + } else { + callback(nn.Name, "loaded") + } + } + done <- true + }(n) + } + for i := 0; i < size; i++ { + <-done + } } // Containers returns all the containers in the cluster.