-
Notifications
You must be signed in to change notification settings - Fork 8
/
executor.go
87 lines (73 loc) · 3.45 KB
/
executor.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
78
79
80
81
82
83
84
85
86
87
package ns
import (
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/longhorn/go-common-libs/exec"
"github.com/longhorn/go-common-libs/proc"
"github.com/longhorn/go-common-libs/types"
)
// Executor is a struct resonpsible for executing commands in a specific
// namespace using nsenter.
type Executor struct {
namespaces []types.Namespace // The namespaces to enter.
nsDirectory string // The directory of the namespace.
executor exec.ExecuteInterface // An interface for executing commands. This allows mocking for unit tests.
}
// NewNamespaceExecutor creates a new namespace executor for the given process name,
// namespaces and proc directory. If the process name is not empty, it will try to
// use the process namespace directory. Otherwise, it will use the host namespace
// directory. The namespaces are the namespaces to enter. The proc directory is
// the directory where the process information is stored. It will also verify the
// existence of the nsenter binary.
func NewNamespaceExecutor(processName, procDirectory string, namespaces []types.Namespace) (*Executor, error) {
nsDir, err := proc.GetProcessNamespaceDirectory(processName, procDirectory)
if err != nil {
return nil, err
}
NamespaceExecutor := &Executor{
namespaces: namespaces,
nsDirectory: nsDir,
executor: exec.NewExecutor(),
}
if _, err := NamespaceExecutor.executor.Execute(nil, types.NsBinary, []string{"-V"}, types.ExecuteDefaultTimeout); err != nil {
return nil, errors.Wrap(err, "cannot find nsenter for namespace switching")
}
return NamespaceExecutor, nil
}
// prepareCommandArgs prepares the nsenter command arguments.
func (nsexec *Executor) prepareCommandArgs(binary string, args, envs []string) []string {
cmdArgs := []string{}
for _, ns := range nsexec.namespaces {
nsPath := filepath.Join(nsexec.nsDirectory, ns.String())
switch ns {
case types.NamespaceIpc:
cmdArgs = append(cmdArgs, "--ipc="+nsPath)
case types.NamespaceMnt:
cmdArgs = append(cmdArgs, "--mount="+nsPath)
case types.NamespaceNet:
cmdArgs = append(cmdArgs, "--net="+nsPath)
}
}
if len(envs) > 0 {
cmdArgs = append(cmdArgs, "env", "-i")
cmdArgs = append(cmdArgs, envs...)
}
cmdArgs = append(cmdArgs, binary)
return append(cmdArgs, args...)
}
// Execute executes the command in the namespace. If NsDirectory is empty,
// it will execute the command in the current namespace.
func (nsexec *Executor) Execute(envs []string, binary string, args []string, timeout time.Duration) (string, error) {
return nsexec.executor.Execute(nil, types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), timeout)
}
// ExecuteWithStdin executes the command in the namespace with stdin.
// If NsDirectory is empty, it will execute the command in the current namespace.
func (nsexec *Executor) ExecuteWithStdin(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error) {
return nsexec.executor.ExecuteWithStdin(types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), stdinString, timeout)
}
// ExecuteWithStdinPipe executes the command in the namespace with stdin pipe.
// If NsDirectory is empty, it will execute the command in the current namespace.
func (nsexec *Executor) ExecuteWithStdinPipe(envs []string, binary string, args []string, stdinString string, timeout time.Duration) (string, error) {
return nsexec.executor.ExecuteWithStdinPipe(types.NsBinary, nsexec.prepareCommandArgs(binary, args, envs), stdinString, timeout)
}