mirror of https://github.com/docker/docs.git
Add support Load Image
Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
parent
1aa61f9f36
commit
ccc32635d5
|
@ -10,6 +10,9 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"os"
|
||||||
|
"io"
|
||||||
|
"path"
|
||||||
|
|
||||||
dockerfilters "github.com/docker/docker/pkg/parsers/filters"
|
dockerfilters "github.com/docker/docker/pkg/parsers/filters"
|
||||||
"github.com/docker/swarm/cluster"
|
"github.com/docker/swarm/cluster"
|
||||||
|
@ -270,6 +273,40 @@ 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)
|
||||||
|
|
||||||
|
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
|
// GET /events
|
||||||
func getEvents(c *context, w http.ResponseWriter, r *http.Request) {
|
func getEvents(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
c.eventsHandler.Add(r.RemoteAddr, w)
|
c.eventsHandler.Add(r.RemoteAddr, w)
|
||||||
|
|
|
@ -33,6 +33,12 @@ type Cluster interface {
|
||||||
// `status` is the current status, like "", "in progress" or "downloaded
|
// `status` is the current status, like "", "in progress" or "downloaded
|
||||||
Pull(name string, callback func(what, status string))
|
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))
|
||||||
|
|
||||||
// Return some info about the cluster, like nb or containers / images
|
// Return some info about the cluster, like nb or containers / images
|
||||||
// It is pretty open, so the implementation decides what to return.
|
// It is pretty open, so the implementation decides what to return.
|
||||||
Info() [][2]string
|
Info() [][2]string
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"os"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/samalba/dockerclient"
|
"github.com/samalba/dockerclient"
|
||||||
|
@ -411,6 +412,19 @@ func (e *Engine) Pull(image string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load an image on the engine
|
||||||
|
func (e *Engine) Load(tarFile string) error {
|
||||||
|
file, err := os.Open(tarFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.client.LoadImage(file); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// RegisterEventHandler registers an event handler.
|
// RegisterEventHandler registers an event handler.
|
||||||
func (e *Engine) RegisterEventHandler(h EventHandler) error {
|
func (e *Engine) RegisterEventHandler(h EventHandler) error {
|
||||||
if e.eventHandler != nil {
|
if e.eventHandler != nil {
|
||||||
|
|
|
@ -238,6 +238,31 @@ func (c *Cluster) Pull(name string, callback func(what, status string)) {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Containers returns all the containers in the cluster.
|
// Containers returns all the containers in the cluster.
|
||||||
func (c *Cluster) Containers() []*cluster.Container {
|
func (c *Cluster) Containers() []*cluster.Container {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
|
|
Loading…
Reference in New Issue