-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
49 lines (45 loc) · 1.06 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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"os/exec"
"syscall"
)
func NewParentProcess(context *cli.Context) {
command := context.Args().Get(0)
args := []string{"init", command}
cmd := exec.Command("/proc/self/exe", args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS | syscall.CLONE_NEWNET | syscall.CLONE_NEWIPC,
}
tty := context.Bool("ti")
if tty {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
if err := cmd.Start(); err != nil {
log.Error(err)
}
// 记录容器信息
containerName := context.String("name")
containerName, err := recordContainerINfo(cmd.Process.Pid, command, containerName)
if err != nil {
log.Error(err)
}
limitMemory := context.String("m")
if limitMemory != "" {
MemoryLimit(cmd.Process.Pid, limitMemory)
}
if tty {
err := cmd.Wait()
// 终端模式下退出后,删除信息
deleteContainerinfo(containerName)
if err != nil {
fmt.Println(err)
return
}
}
}