-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (111 loc) · 2.4 KB
/
main.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
package main
import (
"fmt"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"miniDocker/subsystems"
"os"
)
const usage = `a toy docker as a simple container runtime implementation`
func main() {
app := cli.NewApp()
app.Name = "minidocker"
app.Usage = usage
log.SetOutput(os.Stdout)
log.SetReportCaller(true)
setUpCommands(app)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
// 设置CLI命令
func setUpCommands(app *cli.App) {
// miniDocker run
runCommand := cli.Command{
Name: "run",
Usage: `Create a container`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "ti",
Usage: "run with terminal",
},
cli.StringFlag{
Name: "mem",
Usage: "set memory limit",
},
cli.StringFlag{
Name: "cpushare",
Usage: "set cpushare limit",
},
cli.StringFlag{
Name: "cpuset",
Usage: "set cpuset limit",
},
cli.StringFlag{
Name: "v",
Usage: "volume",
},
cli.StringFlag{
Name: "n",
Usage: "set container name",
},
},
Action: func(context *cli.Context) error {
if len(context.Args()) < 1 {
return fmt.Errorf("usage: minidocker run [cmd]")
}
var cmd []string
for _, arg := range context.Args() {
cmd = append(cmd, arg)
}
tty := context.Bool("ti")
res := &subsystems.ResourceConfig{
MemoryLimit: context.String("mem"),
CPUSet: context.String("cpuset"),
CPUShare: context.String("cpushare"),
}
volume := context.String("v")
name := context.String("n")
if name == "" {
name = uuid.NewV4().String()
}
RunHandler(tty, cmd, res, volume, name)
return nil
},
}
initCommand := cli.Command{
Name: "init",
Usage: `an inner command for container initiation`,
Action: func(context *cli.Context) error {
err := RunContainerInitProcess()
return err
},
}
commitCommand := cli.Command{
Name: "commit",
Usage: "commit a container into image",
Action: func(context *cli.Context) error {
if len(context.Args()) != 1 {
return fmt.Errorf("minidocker commit [containerName]")
}
imageName := context.Args().Get(0)
commitContainer(imageName)
return nil
},
}
listCommand := cli.Command{
Name: "ps",
Usage: "list containers",
Action: func(ctx *cli.Context) error {
listContainers()
return nil
},
}
app.Commands = []cli.Command{
runCommand,
initCommand,
commitCommand,
listCommand,
}
}