-
Notifications
You must be signed in to change notification settings - Fork 1
/
pr-loadimage
92 lines (79 loc) · 1.93 KB
/
pr-loadimage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
api.go
"os"
"io"
"path"
// POST /images/load
func postLoad(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)
}
"/images/load": postLoad,
cluster.go
// 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))
cluster.go
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
}
}
engine.go
"os"
// 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
}