-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
183 lines (164 loc) · 3.89 KB
/
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
package main
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"goocker/cgroups/subsystem"
"goocker/container"
"goocker/images"
)
//docker run -p 3306:3306 --restart=always --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=Coolshark1588 -d mysql:5.7
var runCommand = cli.Command{
Name: "运行容器Run Container",
Flags: []cli.Flag{
//cli.BoolFlag{} :有该参数则为true,没有为false。例如 -it -d
//cli.StringFlag{} 要获取参数后面紧跟的值。 例如 --name redis-client
//cli.StringSliceFlag{} 获取参数后面的多个值 例如:--p 8000:80 8080:8080 8081:8081
cli.BoolFlag{
Name: "d",
Usage: "守护式后台运行",
},
cli.BoolFlag{
Name: "it",
Usage: "交互式终端运行interface terminal",
},
cli.StringFlag{
Name: "m",
Usage: "memory limit",
},
cli.StringFlag{
Name: "cpushare",
Usage: "cpushare limit权重值",
},
cli.StringFlag{
Name: "cpuset",
Usage: "cpuset limit",
},
cli.StringFlag{
Name: "name",
Usage: "给容器起别名",
},
cli.StringFlag{
Name: "net",
Usage: "指定网络模式",
},
cli.StringFlag{
Name: "v",
Usage: "容器卷挂载",
},
cli.StringSliceFlag{
Name: "p",
Usage: "端口映射",
},
cli.StringSliceFlag{
Name: "e",
Usage: "指定容器运行环境env",
},
},
Action: func(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
return fmt.Errorf("args is nil")
}
resCfg := &subsystem.ResourceConfig{
MemoryLimit: ctx.String("m"),
CpuShare: ctx.String("cpushare"),
CpuSet: ctx.String("cpuset"),
}
d := ctx.Bool("d")
it := ctx.Bool("it")
if d && it {
return fmt.Errorf("不能同时有it和d参数")
}
p := ctx.StringSlice("p")
name := ctx.String("name")
imagesName := ctx.Args().Get(0)
v := ctx.String("v")
e := ctx.StringSlice("e")
net := ctx.String("net")
var cmdArray []string
for _, args := range ctx.Args().Tail() {
cmdArray = append(cmdArray, args)
}
container.Run(cmdArray, e, p, resCfg, imagesName, name, v, net, it)
return nil
},
}
var commitCommand = cli.Command{
Name: "commit",
Usage: "提交",
Action: func(ctx *cli.Context) error {
imageName := ctx.Args().Get(0)
container.CommitContainer(imageName)
return nil
},
}
var initCommand = cli.Command{
Name: "init",
Usage: "初始化",
Action: func(ctx *cli.Context) error {
logrus.Printf("init started")
return container.RunInitContainer(ctx)
},
}
var stopCommand = cli.Command{
Name: "stop",
Usage: "停止容器",
Action: func(ctx *cli.Context) error {
containerName := ctx.Args().Get(0)
container.StopContainer(containerName)
return nil
},
}
var rmCommand = cli.Command{
Name: "rm",
Usage: "删除容器",
Action: func(ctx *cli.Context) error {
imagesName := ctx.Args().Get(0)
container.RmContainer(imagesName)
return nil
},
}
var execCommand = cli.Command{
Name: "exec",
Usage: "在容器内执行命令",
Action: func(ctx *cli.Context) error {
imageName := ctx.Args().Get(0)
container.ExecContainer(imageName)
return nil
},
}
var pullCommand = cli.Command{
Name: "pull",
Usage: "拉取镜像",
Action: func(ctx *cli.Context) error {
imageName := ctx.Args().Get(0)
images.PullImage(imageName)
return nil
},
}
var pushCommand = cli.Command{
Name: "push",
Usage: "推送容器",
Action: func(ctx cli.Context) error {
imageName := ctx.Args().Get(0)
images.PushImage(imageName)
return nil
},
}
var psCommand = cli.Command{
Name: "ps",
Usage: "查看容器列表",
Action: func(ctx *cli.Context) error {
a := ctx.String("a")
container.PsContainer(a)
return nil
},
}
var logCommand = cli.Command{
Name: "log",
Usage: "容器日志",
Action: func(ctx *cli.Context) {
containerName := ctx.Args().Get(0)
container.GetContainerLog(containerName)
},
}