forked from lixd/mydocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.go
77 lines (68 loc) · 2.15 KB
/
exec.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"mydocker/container"
// 需要导入nsenter包,以触发C代码
_ "mydocker/nsenter"
log "github.com/sirupsen/logrus"
)
// nsenter里的C代码里已经出现mydocker_pid和mydocker_cmd这两个Key,主要是为了控制是否执行C代码里面的setns.
const (
EnvExecPid = "mydocker_pid"
EnvExecCmd = "mydocker_cmd"
)
func ExecContainer(containerName string, comArray []string) {
// 根据传进来的容器名获取对应的PID
pid, err := getContainerPidByName(containerName)
if err != nil {
log.Errorf("Exec container getContainerPidByName %s error %v", containerName, err)
return
}
cmd := exec.Command("/proc/self/exe", "exec")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// 把命令拼接成字符串,便于传递
cmdStr := strings.Join(comArray, " ")
log.Infof("container pid:%s command:%s", pid, cmdStr)
_ = os.Setenv(EnvExecPid, pid)
_ = os.Setenv(EnvExecCmd, cmdStr)
// 把指定PID进程的环境变量传递给新启动的进程,实现通过exec命令也能查询到容器的环境变量
containerEnvs := getEnvsByPid(pid)
cmd.Env = append(os.Environ(), containerEnvs...)
if err = cmd.Run(); err != nil {
log.Errorf("Exec container %s error %v", containerName, err)
}
}
func getContainerPidByName(containerName string) (string, error) {
// 拼接出记录容器信息的文件路径
dirURL := fmt.Sprintf(container.InfoLocFormat, containerName)
configFilePath := dirURL + container.ConfigName
// 读取内容并解析
contentBytes, err := ioutil.ReadFile(configFilePath)
if err != nil {
return "", err
}
var containerInfo container.Info
if err = json.Unmarshal(contentBytes, &containerInfo); err != nil {
return "", err
}
return containerInfo.Pid, nil
}
// getEnvsByPid 读取指定PID进程的环境变量
func getEnvsByPid(pid string) []string {
path := fmt.Sprintf("/proc/%s/environ", pid)
contentBytes, err := ioutil.ReadFile(path)
if err != nil {
log.Errorf("Read file %s error %v", path, err)
return nil
}
// env split by \u0000
envs := strings.Split(string(contentBytes), "\u0000")
return envs
}