forked from ycrash/yc-360-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_linux.go
More file actions
61 lines (55 loc) · 1.09 KB
/
docker_linux.go
File metadata and controls
61 lines (55 loc) · 1.09 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
//go:build linux
// +build linux
package shell
import (
"bufio"
"bytes"
"github.com/mitchellh/go-ps"
"strconv"
"strings"
)
func GetDockerID(pid int) (id string, err error) {
pids, err := getPIDChain(pid)
if err != nil {
return
}
output, err := CommandCombinedOutput(DockerInfo)
if err != nil {
return
}
scanner := bufio.NewScanner(bytes.NewReader(output))
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
for _, pid := range pids {
prefix := strconv.Itoa(pid) + " "
if strings.HasPrefix(line, prefix) {
id = line[len(prefix):]
return
}
}
}
return
}
func getPIDChain(pid int) (pids []int, err error) {
var process ps.Process
for {
process, err = ps.FindProcess(pid)
if process == nil {
return
}
if err != nil {
return
}
pids = append(pids, pid)
pid = process.PPid()
}
}
func DockerCopy(dst string, src string) (err error) {
err = CommandRun(Append(DockerCP, src, dst))
return
}
func DockerExecute(args ...string) (output []byte, err error) {
output, err = CommandCombinedOutput(Append(DockerExec, args...))
return
}