-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmain_command.go
264 lines (246 loc) · 6.78 KB
/
main_command.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
package main
import (
"fmt"
"mydocker/cgroups/resource"
"mydocker/network"
"os"
"mydocker/container"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var runCommand = cli.Command{
Name: "run",
Usage: `Create a container with namespace and cgroups limit
mydocker run -it [command]
mydocker run -d -name [containerName] [imageName] [command]`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "it", // 简单起见,这里把 -i 和 -t 参数合并成一个
Usage: "enable tty",
},
cli.StringFlag{
Name: "mem", // 限制进程内存使用量,为了避免和 stress 命令的 -m 参数冲突 这里使用 -mem,到时候可以看下解决冲突的方法
Usage: "memory limit,e.g.: -mem 100m",
},
cli.StringFlag{
Name: "cpu",
Usage: "cpu quota,e.g.: -cpu 100", // 限制进程 cpu 使用率
},
cli.StringFlag{
Name: "cpuset",
Usage: "cpuset limit,e.g.: -cpuset 2,4", // 限制进程 cpu 使用率
},
cli.StringFlag{ // 数据卷
Name: "v",
Usage: "volume,e.g.: -v /ect/conf:/etc/conf",
},
cli.BoolFlag{
Name: "d",
Usage: "detach container,run background",
},
// 提供run后面的-name指定容器名字参数
cli.StringFlag{
Name: "name",
Usage: "container name,e.g.: -name mycontainer",
},
cli.StringSliceFlag{
Name: "e",
Usage: "set environment,e.g. -e name=mydocker",
},
cli.StringFlag{
Name: "net",
Usage: "container network,e.g. -net testbr",
},
cli.StringSliceFlag{
Name: "p",
Usage: "port mapping,e.g. -p 8080:80 -p 30336:3306",
},
},
/*
这里是run命令执行的真正函数。
1.判断参数是否包含command
2.获取用户指定的command
3.调用Run function去准备启动容器:
*/
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("missing container command")
}
var cmdArray []string
for _, arg := range context.Args() {
cmdArray = append(cmdArray, arg)
}
// get image name
imageName := cmdArray[0] // 镜像名称
cmdArray = cmdArray[1:]
tty := context.Bool("it")
detach := context.Bool("d")
if tty && detach {
return fmt.Errorf("it and d flag can not both provided")
}
if !detach { // 如果不是指定后台运行,就默认前台运行
tty = true
}
log.Infof("createTty %v", tty)
resConf := &resource.ResourceConfig{
MemoryLimit: context.String("mem"),
CpuSet: context.String("cpuset"),
CpuCfsQuota: context.Int("cpu"),
}
log.Info("resConf:", resConf)
volume := context.String("v")
containerName := context.String("name")
envSlice := context.StringSlice("e")
network := context.String("net")
portMapping := context.StringSlice("p")
Run(tty, cmdArray, envSlice, resConf, volume, containerName, imageName, network, portMapping)
return nil
},
}
var initCommand = cli.Command{
Name: "init",
Usage: "Init container process run user's process in container. Do not call it outside",
Action: func(context *cli.Context) error {
log.Infof("init come on")
err := container.RunContainerInitProcess()
return err
},
}
var commitCommand = cli.Command{
Name: "commit",
Usage: "commit container to image,e.g. mydocker commit 123456789 myimage",
Action: func(context *cli.Context) error {
if len(context.Args()) < 2 {
return fmt.Errorf("missing container name and image name")
}
containerID := context.Args().Get(0)
imageName := context.Args().Get(1)
return commitContainer(containerID, imageName)
},
}
var listCommand = cli.Command{
Name: "ps",
Usage: "list all the containers",
Action: func(context *cli.Context) error {
ListContainers()
return nil
},
}
var logCommand = cli.Command{
Name: "logs",
Usage: "print logs of a container",
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("please input your container id")
}
containerName := context.Args().Get(0)
logContainer(containerName)
return nil
},
}
var execCommand = cli.Command{
Name: "exec",
Usage: "exec a command into container,mydocker exec 123456789 /bin/sh",
Action: func(context *cli.Context) error {
// 如果环境变量存在,说明C代码已经运行过了,即setns系统调用已经执行了,这里就直接返回,避免重复执行
if os.Getenv(EnvExecPid) != "" {
log.Infof("pid callback pid %v", os.Getgid())
return nil
}
// 格式:mydocker exec 容器名字 命令,因此至少会有两个参数
if len(context.Args()) < 2 {
return fmt.Errorf("missing container name or command")
}
containerName := context.Args().Get(0)
// 将除了容器名之外的参数作为命令部分
commandArray := context.Args().Tail()
ExecContainer(containerName, commandArray)
return nil
},
}
var stopCommand = cli.Command{
Name: "stop",
Usage: "stop a container,e.g. mydocker stop 1234567890",
Action: func(context *cli.Context) error {
// 期望输入是:mydocker stop 容器Id,如果没有指定参数直接打印错误
if len(context.Args()) < 1 {
return fmt.Errorf("missing container id")
}
containerId := context.Args().Get(0)
stopContainer(containerId)
return nil
},
}
var removeCommand = cli.Command{
Name: "rm",
Usage: "remove unused containers,e.g. mydocker rm 1234567890",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "f", // 强制删除
Usage: "force delete running container,",
}},
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("missing container id")
}
containerId := context.Args().Get(0)
force := context.Bool("f")
removeContainer(containerId, force)
return nil
},
}
var networkCommand = cli.Command{
Name: "network",
Usage: "container network commands",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create a container network",
Flags: []cli.Flag{
cli.StringFlag{
Name: "driver",
Usage: "network driver",
},
cli.StringFlag{
Name: "subnet",
Usage: "subnet cidr",
},
},
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("missing network name")
}
driver := context.String("driver")
subnet := context.String("subnet")
name := context.Args()[0]
err := network.CreateNetwork(driver, subnet, name)
if err != nil {
return fmt.Errorf("create network error: %+v", err)
}
return nil
},
},
{
Name: "list",
Usage: "list container network",
Action: func(context *cli.Context) error {
network.ListNetwork()
return nil
},
},
{
Name: "remove",
Usage: "remove container network",
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("missing network name")
}
err := network.DeleteNetwork(context.Args()[0])
if err != nil {
return fmt.Errorf("remove network error: %+v", err)
}
return nil
},
},
},
}