forked from lixd/mydocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
142 lines (130 loc) · 4.14 KB
/
run.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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
"mydocker/cgroups"
"mydocker/cgroups/subsystems"
"mydocker/constant"
"mydocker/container"
"mydocker/network"
log "github.com/sirupsen/logrus"
)
// Run 执行具体 command
/*
这里的Start方法是真正开始执行由NewParentProcess构建好的command的调用,它首先会clone出来一个namespace隔离的
进程,然后在子进程中,调用/proc/self/exe,也就是调用自己,发送init参数,调用我们写的init方法,
去初始化容器的一些资源。
*/
func Run(tty bool, comArray []string, res *subsystems.ResourceConfig, containerName, volume, imageName string,
envSlice []string, nw string, portMapping []string) {
containerID := randStringBytes(container.IDLength)
if containerName == "" {
containerName = containerID
}
parent, writePipe := container.NewParentProcess(tty, containerName, volume, imageName, envSlice)
if parent == nil {
log.Errorf("New parent process error")
return
}
if err := parent.Start(); err != nil {
log.Errorf("Run parent.Start err:%v", err)
}
// record container info
err := recordContainerInfo(parent.Process.Pid, comArray, containerName, containerID, volume)
if err != nil {
log.Errorf("Record container info error %v", err)
return
}
// 创建cgroup manager, 并通过调用set和apply设置资源限制并使限制在容器上生效
cgroupManager := cgroups.NewCgroupManager("mydocker-cgroup")
defer cgroupManager.Destroy()
_ = cgroupManager.Set(res)
_ = cgroupManager.Apply(parent.Process.Pid, res)
if nw != "" {
// config container network
network.Init()
containerInfo := &container.Info{
Id: containerID,
Pid: strconv.Itoa(parent.Process.Pid),
Name: containerName,
PortMapping: portMapping,
}
if err = network.Connect(nw, containerInfo); err != nil {
log.Errorf("Error Connect Network %v", err)
return
}
}
// 在子进程创建后才能通过管道来发送参数
sendInitCommand(comArray, writePipe)
if tty { // 如果是tty,那么父进程等待
_ = parent.Wait()
deleteContainerInfo(containerName)
_ = container.DeleteWorkSpace(volume, containerName)
}
}
// sendInitCommand 通过writePipe将指令发送给子进程
func sendInitCommand(comArray []string, writePipe *os.File) {
command := strings.Join(comArray, " ")
log.Infof("command all is %s", command)
_, _ = writePipe.WriteString(command)
_ = writePipe.Close()
}
func recordContainerInfo(containerPID int, commandArray []string, containerName, containerID, volume string) error {
// 以当前时间作为容器创建时间
createTime := time.Now().Format("2006-01-02 15:04:05")
command := strings.Join(commandArray, "")
containerInfo := &container.Info{
Id: containerID,
Pid: strconv.Itoa(containerPID),
Command: command,
CreatedTime: createTime,
Status: container.RUNNING,
Name: containerName,
Volume: volume,
}
jsonBytes, err := json.Marshal(containerInfo)
if err != nil {
log.Errorf("Record container info error %v", err)
return err
}
jsonStr := string(jsonBytes)
// 拼接出存储容器信息文件的路径,如果目录不存在则级联创建
dirUrl := fmt.Sprintf(container.InfoLocFormat, containerName)
if err = os.MkdirAll(dirUrl, constant.Perm0622); err != nil {
log.Errorf("Mkdir error %s error %v", dirUrl, err)
return err
}
// 将容器信息写入文件
fileName := dirUrl + "/" + container.ConfigName
file, err := os.Create(fileName)
defer file.Close()
if err != nil {
log.Errorf("Create file %s error %v", fileName, err)
return err
}
if _, err = file.WriteString(jsonStr); err != nil {
log.Errorf("File write string error %v", err)
return err
}
return nil
}
func deleteContainerInfo(containerName string) {
dirURL := fmt.Sprintf(container.InfoLocFormat, containerName)
if err := os.RemoveAll(dirURL); err != nil {
log.Errorf("Remove dir %s error %v", dirURL, err)
}
}
func randStringBytes(n int) string {
letterBytes := "1234567890"
rand.Seed(time.Now().UnixNano())
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}