-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
270 lines (262 loc) · 6.32 KB
/
image.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package command
import (
"encoding/json"
"fmt"
"github.com/songxinjianqwe/capsule/cli/util"
"github.com/songxinjianqwe/capsule/libcapsule/facade"
"github.com/songxinjianqwe/capsule/libcapsule/image"
"github.com/songxinjianqwe/capsule/libcapsule/network"
"github.com/urfave/cli"
"golang.org/x/sys/unix"
"os"
"strings"
"text/tabwriter"
"time"
)
var ImagesCommand = cli.Command{
Name: "image",
Usage: "image management",
Subcommands: []cli.Command{
imageCreateCommand,
imageDeleteCommand,
imageListCommand,
imageGetCommand,
imageRunContainerCommand,
imageDestroyContainerCommand,
},
}
var imageCreateCommand = cli.Command{
Name: "create",
Usage: "create a image, like create $image_name $tarPath",
Action: func(ctx *cli.Context) error {
if err := util.CheckArgs(ctx, 2, util.ExactArgs); err != nil {
return err
}
// 这里是将一个存在的tar文件作为新的rootfs
imageService, err := image.NewImageService(ctx.GlobalString("root"))
if err != nil {
return err
}
if err := imageService.Create(ctx.Args().First(), ctx.Args()[1]); err != nil {
return err
}
return nil
},
}
var imageDeleteCommand = cli.Command{
Name: "delete",
Usage: "delete a image",
Action: func(ctx *cli.Context) error {
if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
return err
}
imageService, err := image.NewImageService(ctx.GlobalString("root"))
if err != nil {
return err
}
if err := imageService.Delete(ctx.Args().First()); err != nil {
return err
}
return nil
},
}
var imageListCommand = cli.Command{
Name: "list",
Usage: "list images",
Action: func(ctx *cli.Context) error {
imageService, err := image.NewImageService(ctx.GlobalString("root"))
if err != nil {
return err
}
images, err := imageService.List()
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
fmt.Fprint(w, "ID\tLAYER_ID\tCREATED\tSIZE\n")
for _, item := range images {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n",
item.Id,
item.LayerId,
item.CreateTime.Format(time.RFC3339Nano),
fmt.Sprintf("%dMB", item.Size))
}
if err := w.Flush(); err != nil {
return err
}
return nil
},
}
var imageGetCommand = cli.Command{
Name: "get",
Usage: "get a image",
Action: func(ctx *cli.Context) error {
if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
return err
}
imageService, err := image.NewImageService(ctx.GlobalString("root"))
if err != nil {
return err
}
image, err := imageService.Get(ctx.Args().First())
if err != nil {
return err
}
data, err := json.MarshalIndent(image, "", " ")
if err != nil {
return err
}
fmt.Println(string(data))
return nil
},
}
// image run $image_name command
// -d
// -workdir $workdir
// -hostname $hostname
// -id $name
// -env a=b c=d
// -cpushare
// -memory
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~下面是spec里没有的,由capsule负责做的配置信息
// -link
// -volume a/a:b
// -network $network_name
// -port xx:xxx
// -label a=b
var imageRunContainerCommand = cli.Command{
Name: "runc",
Usage: "run container in image way",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "detach, d",
Usage: "detach from the container's process",
},
cli.StringFlag{
Name: "id",
Usage: "container unique id",
},
cli.StringFlag{
Name: "cwd",
Value: "/",
Usage: "current work directory",
},
cli.StringSliceFlag{
Name: "env, e",
Usage: "environment variables",
},
cli.StringFlag{
Name: "hostname, h",
Usage: "hostname",
},
cli.Int64Flag{
Name: "cpushare, c",
Value: 1024,
Usage: "cpushare",
},
cli.Int64Flag{
Name: "memory, m",
Value: 0,
Usage: "memory limit",
},
cli.StringFlag{
Name: "network, net",
Value: network.DefaultBridgeName,
Usage: "network name",
},
cli.StringSliceFlag{
Name: "port, p",
Usage: "port mappings",
},
cli.StringSliceFlag{
Name: "label, l",
Usage: "container label",
},
cli.StringSliceFlag{
Name: "volume, v",
Usage: "-v container_dir, or -v host_dir:container_dir",
},
cli.StringSliceFlag{
Name: "link",
Usage: "-link container_id:alias",
},
},
Action: func(ctx *cli.Context) error {
if err := util.CheckArgs(ctx, 2, util.MinArgs); err != nil {
return err
}
imageService, err := image.NewImageService(ctx.GlobalString("root"))
if err != nil {
return err
}
containerId := ctx.String("id")
if containerId == "" {
return fmt.Errorf("container id can not be empty")
}
hostname := ctx.String("hostname")
if hostname == "" {
hostname = containerId
}
annotations := make(map[string]string)
for _, label := range ctx.StringSlice("label") {
splits := strings.SplitN(label, "=", 2)
annotations[splits[0]] = splits[1]
}
args := ctx.Args()[1:]
if len(args) == 1 && strings.Contains(args[0], " ") {
args = strings.Split(args[0], " ")
}
if err := imageService.Run(&image.ImageRunArgs{
ImageId: ctx.Args().First(),
ContainerId: containerId,
Args: args,
Env: ctx.StringSlice("env"),
Cwd: ctx.String("cwd"),
Hostname: hostname,
Cpushare: ctx.Uint64("cpushare"),
Memory: ctx.Int64("memory"),
Annotations: annotations,
Network: ctx.String("network"),
PortMappings: ctx.StringSlice("port"),
Detach: ctx.Bool("detach"),
Volumes: ctx.StringSlice("volume"),
Links: ctx.StringSlice("link"),
}); err != nil {
return err
}
return nil
},
}
var imageDestroyContainerCommand = cli.Command{
Name: "destroyc",
Usage: "destroy container in image way",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "Force delete the container even it is still running (uses SIGKILL)",
},
},
Action: func(ctx *cli.Context) error {
if err := util.CheckArgs(ctx, 1, util.ExactArgs); err != nil {
return err
}
imageService, err := image.NewImageService(ctx.GlobalString("root"))
if err != nil {
return err
}
container, err := facade.GetContainer(ctx.GlobalString("root"), ctx.Args().First())
if err != nil {
return err
}
if ctx.Bool("force") {
_ = container.Signal(unix.SIGKILL)
for i := 0; i < 100; i++ {
time.Sleep(100 * time.Millisecond)
return imageService.Destroy(container)
}
return fmt.Errorf("waiting container dead timed out")
} else {
return imageService.Destroy(container)
}
},
}