-
Notifications
You must be signed in to change notification settings - Fork 1
/
commit.go
163 lines (134 loc) · 3.75 KB
/
commit.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"github.com/RedDragonet/rocker/container"
log "github.com/RedDragonet/rocker/pkg/pidlog"
)
//镜像打包
/*
待打包 Layer
.
|-- busybox
|-- diff
| |-- ******
|-- lower
|-- 5d81f475ce28f0005821946a7e76d23dfcc28bb67263a9d5a8cb24126021feb8
|-- diff
| |-- ******
|-- lower
只打包 diff 目录
==========
打包后的目录
./
./busybox/
./busybox/layer.tar
./5d81f475ce28f0005821946a7e76d23dfcc28bb67263a9d5a8cb24126021feb8/
./5d81f475ce28f0005821946a7e76d23dfcc28bb67263a9d5a8cb24126021feb8/layer.tar
./repositories
./manifest.json
*/
func commit(id, output string) error {
tmpCommitDir := path.Join("/tmp/rocker/commit/", id)
defer func() {
_ = os.RemoveAll(tmpCommitDir)
}()
log.Infof("容器打包镜像 %s", id)
//遍历所有层的ID
layers, err := getLayers(id)
if err != nil {
return err
}
layersJarFile := make([]string, len(layers))
for index, layer := range layers {
layerPath := path.Join(tmpCommitDir, layer)
err := os.MkdirAll(layerPath, 0700)
if err != nil {
return err
}
jarFile := path.Join(layer, "layer.tar")
layersJarFile[index] = jarFile
//将每一层的diff目录,打包成 *ID*/layer.tar
if _, err := exec.Command("tar", "-C", container.GetDiffPath(layer), "-cf", path.Join(tmpCommitDir, jarFile), ".").CombinedOutput(); err != nil {
log.Errorf("容器打包镜像 layer error %s %v", layerPath, err)
return fmt.Errorf("容器打包镜像 layer error %s %v", layerPath, err)
}
}
err = createRepositoriesFile(tmpCommitDir, id, output)
if err != nil {
return err
}
err = createLayersFile(tmpCommitDir, layers)
if err != nil {
return err
}
//最终打包
//所有的 jarFile file
//******** 列出所有 files 打包符合 docker images 的规范
/*
[root@localhost ~]# tar -tf 3.tar
busybox/layer.tar
9701a03c637547cc19b465ea26323ee23071804e9cc8a03d38e2eacb9a5102fe/layer.tar
repositories
manifest.json
******直接打包文件会出现
[root@localhost ~]# tar -tf 3.tar
./
./busybox/layer.tar
./9701a03c637547cc19b465ea26323ee23071804e9cc8a03d38e2eacb9a5102fe/layer.tar
./repositories
./manifest.json
导致 tar 去查找文件,如manifest.json时,需要制定查找的文件为 ./manifest.json
*/
//直接打包目录会出现
//
files := append(layersJarFile, "repositories", "manifest.json")
args := append([]string{"-C", tmpCommitDir, "-cf", output + ".tar"}, files...)
if _, err := exec.Command("tar", args...).CombinedOutput(); err != nil {
log.Errorf("容器打包镜像 error %s %s %s %v %v", id, output, err)
return fmt.Errorf("容器打包镜像 error %s %s %s %v %v", id, output, err)
}
return nil
}
func getLayers(id string) ([]string, error) {
//lowers 层也同时打包
lowerDirs, err := container.GetLowerDirs(id)
if err != nil {
return nil, err
}
layers := make([]string, len(lowerDirs)+1)
for index, lowerDiffPath := range lowerDirs {
layers[index] = path.Dir(lowerDiffPath)
}
//当前层在最后
layers[len(layers)-1] = id
return layers, nil
}
//创建当前容器描述文件
func createRepositoriesFile(dir, id, name string) error {
//{"image_name":{"latest":"id"}}
fileData := fmt.Sprintf(`{"%s":{"latest":"%s"}}`, name, id)
err := ioutil.WriteFile(path.Join(dir, "repositories"), []byte(fileData), 0600)
if err != nil {
return err
}
return nil
}
//Layers
func createLayersFile(dir string, layers []string) error {
//[{"Layers":["12sbv","qe123","123sdf"]}]
layersJson, err := json.Marshal(layers)
if err != nil {
return err
}
fileData := fmt.Sprintf(`[{"Layers":%s}]`, string(layersJson))
err = ioutil.WriteFile(path.Join(dir, "manifest.json"), []byte(fileData), 0600)
if err != nil {
return err
}
return nil
}