...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package bpm
15
16 import (
17 "context"
18 "os/exec"
19 "strings"
20 "syscall"
21
22 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
23 )
24
25
26 func (b *ProcessBuilder) Build() *ManagedProcess {
27 args := b.args
28 cmd := b.cmd
29
30 if len(b.nsOptions) > 0 {
31 args = append([]string{"--", cmd}, args...)
32 for _, option := range b.nsOptions {
33 args = append([]string{"-" + nsArgMap[option.Typ], option.Path}, args...)
34 }
35
36 if b.localMnt {
37 args = append([]string{"-l"}, args...)
38 }
39 cmd = nsexecPath
40 }
41
42 if b.pause {
43 args = append([]string{cmd}, args...)
44 cmd = pausePath
45 }
46
47 if c := mock.On("MockProcessBuild"); c != nil {
48 f := c.(func(context.Context, string, ...string) *exec.Cmd)
49 return &ManagedProcess{
50 Cmd: f(b.ctx, cmd, args...),
51 Identifier: b.identifier,
52 }
53 }
54
55 log.Info("build command", "command", cmd+" "+strings.Join(args, " "))
56
57 command := exec.CommandContext(b.ctx, cmd, args...)
58 command.Env = b.env
59 command.SysProcAttr = &syscall.SysProcAttr{}
60 command.SysProcAttr.Pdeathsig = syscall.SIGTERM
61
62 if b.stdin != nil {
63 command.Stdin = b.stdin
64 }
65
66 if b.stdout != nil {
67 command.Stdout = b.stdout
68 }
69
70 if b.stderr != nil {
71 command.Stderr = b.stderr
72 }
73
74 return &ManagedProcess{
75 Cmd: command,
76 Identifier: b.identifier,
77 }
78 }
79