-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
85 lines (75 loc) · 2.03 KB
/
Copy pathrun.go
File metadata and controls
85 lines (75 loc) · 2.03 KB
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
package main
import (
"fmt"
log "github.com/Sirupsen/logrus"
"math/rand"
"mycontainer/cgroups"
"mycontainer/cgroups/subsystems"
"mycontainer/container"
"os"
"strings"
"time"
"mycontainer/network"
"strconv"
)
func Run(containerName string, tty bool, cmdArray []string, res *subsystems.ResourceConfig, volume string, imageName string, envSlice []string, net string, portMapping []string) {
id := randStringBytes(10)
if containerName == "" {
containerName = id
}
fmt.Fprintln(os.Stdout, containerName)
parent, writePipe := container.NewParentProcess(containerName, tty, volume, imageName, envSlice)
if parent == nil {
log.Error("New parent process error")
return
}
if err := parent.Start(); err != nil {
log.Error(err)
}
// record container info
containerName, err := container.RecordContainerInfo(id, parent.Process.Pid, containerName, cmdArray, volume, portMapping)
if err != nil {
log.Errorf("Record container info error: %v", err)
return
}
// cgroups
cgroupManager := cgroups.NewCgroupManager(id)
defer cgroupManager.Destroy()
cgroupManager.Set(res)
cgroupManager.Apply(parent.Process.Pid)
if net != "" {
network.Init()
containerInfo := &container.ContainerInfo{
Id: id,
Pid: strconv.Itoa(parent.Process.Pid),
Name: containerName,
PortMapping: portMapping,
}
if err := network.Connect(net, containerInfo); err != nil {
log.Errorf("Error connect network: %v", err)
return
}
}
sendInitCommand(cmdArray, writePipe)
if tty {
parent.Wait()
container.DeleteWorkSpace(volume, containerName)
container.DeleteContainerInfo(containerName)
os.Exit(0)
}
}
func sendInitCommand(cmdArray []string, writePipe *os.File) {
command := strings.Join(cmdArray, " ")
log.Infof("command all is %s", command)
writePipe.WriteString(command)
writePipe.Close()
}
func randStringBytes(n int) string {
letterBytes := "1234567890abcdefghijklmnopqrstuvwxyz"
rand.Seed(time.Now().UnixNano())
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}