/
exec.go
235 lines (206 loc) · 6.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package app
import (
"bytes"
"fmt"
"golang.org/x/sys/unix"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/gookit/color"
"github.com/sirupsen/logrus"
"golang.org/x/exp/slices"
)
const (
MissingExternalToolMessageKey = "MissingExternalTool"
)
type externalExe string
// known external programs that swctl can execute
const (
cmdDocker externalExe = "docker"
cmdVppProbe externalExe = "vpp-probe"
cmdAgentCtl externalExe = "agentctl"
)
func (ee externalExe) installPath() string {
return filepath.Join(binaryToolsInstallDir, string(ee))
}
func (ee externalExe) validateUsability(cli *CLI) error {
// check for file existence
fileInfo, err := os.Stat(ee.installPath())
if err != nil {
if os.IsNotExist(err) {
if messageOverride, found := cli.customizations[MissingExternalToolMessageKey]; found {
return fmt.Errorf(messageOverride.(string), ee.installPath())
}
return fmt.Errorf("%s is not installed, try running "+
"`swctl dependency install-tools`", ee.installPath())
}
return fmt.Errorf("existence of %s could not be determined due to %w", ee.installPath(), err)
}
// check for execution permissions
if uint32(fileInfo.Mode()&0111) != 0111 {
// someone can execute the file (file owner or owner's group or someone else)
// -> need to check if this process can execute it with more expensive OS call
if unix.Access(ee.installPath(), unix.X_OK) != nil {
return fmt.Errorf("%s can't be executed by this process/user", ee.installPath())
}
}
return nil
}
type externalCmd struct {
cli *CLI
exe externalExe
name string
args []string
env []string
color bool
}
func newExternalCmd(cmd externalExe, args []string, cli *CLI) *externalCmd {
ecmd := &externalCmd{
exe: cmd,
name: string(cmd),
cli: cli,
color: cli.GlobalOptions().Color != "never" && cli.Out().IsTerminal(),
}
ecmd.args = append(ecmd.args, args...)
ecmd.setDebugArg(cli.GlobalOptions().Debug)
ecmd.setLogLevelArg(logrus.GetLevel())
ecmd.setColorEnv()
ecmd.setMiscFlags()
return ecmd
}
func (ec *externalCmd) setDebugArg(debug bool) {
switch ec.exe {
case cmdAgentCtl, cmdDocker, cmdVppProbe:
if debug {
ec.prependUniqueArg("--debug", "", "-D")
}
}
}
func (ec *externalCmd) setLogLevelArg(loglvl logrus.Level) {
if loglvl < logrus.FatalLevel {
loglvl = logrus.FatalLevel
}
if loglvl > logrus.DebugLevel {
loglvl = logrus.DebugLevel
}
switch ec.exe {
case cmdAgentCtl, cmdDocker:
ec.prependUniqueArg("--log-level", loglvl.String(), "-l")
case cmdVppProbe:
ec.prependUniqueArg("--loglevel", loglvl.String(), "-L")
}
}
// https://no-color.org/
func (ec *externalCmd) setColorEnv() {
if !ec.color {
ec.env = append(ec.env, "NO_COLOR=1")
}
}
func (ec *externalCmd) setMiscFlags() {
switch ec.exe {
case cmdVppProbe:
if ec.color {
ec.prependUniqueArg("--color", "always")
}
case cmdAgentCtl:
ec.prependUniqueArg("--host", ec.cli.client.GetHost(), "-H")
case cmdDocker:
if i := slices.Index(ec.args, "compose"); i >= 0 {
var argVals []string
for _, cf := range ec.cli.GlobalOptions().ComposeFiles {
argVals = append(argVals, fmt.Sprintf("--file=%s", cf))
}
ec.args = tryInsertArgVals(ec.args, i+1, argVals...)
}
}
}
type ExecResult struct {
Took time.Duration
Status int
Stdout string
Stderr string
}
func (ec *externalCmd) exec(liveOutput bool) (*ExecResult, error) {
var stdout, stderr bytes.Buffer
// compute executable name that will define used filepath to executable file
executable := ec.name // just file name -> using PATH to resolve to absolute path
if ec.exe == cmdVppProbe || ec.exe == cmdAgentCtl {
// this is override of executed command to take the properly installed version of external command
// instead of whetever is in the linux PATH
executable = filepath.Join(binaryToolsInstallDir, string(ec.exe)) // absolute path to external tool
if err := ec.exe.validateUsability(ec.cli); err != nil {
return nil, fmt.Errorf("can't use external tool %s due to: %w", string(ec.exe), err)
}
}
cmd := exec.Command(executable, ec.args...)
if liveOutput {
stdoutMultiWriter := io.MultiWriter(&stdout, ec.cli.out)
stderrMultiWriter := io.MultiWriter(&stderr, ec.cli.err)
cmd.Stdout = stdoutMultiWriter
cmd.Stderr = stderrMultiWriter
} else {
cmd.Stdout = &stdout
cmd.Stderr = &stderr
}
cmd.Env = ec.env
now := time.Now()
logrus.Tracef("[%s] %q", color.Gray.Sprint("EXEC"), cmd.String())
err := cmd.Run()
took := time.Since(now)
l := logrus.WithField("took", took.Round(1*time.Millisecond).String())
var execRes ExecResult
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
l.Tracef("[%s] %q (%v)\n%s\n", color.Red.Sprint("ERR"), cmd.Args, color.Red.Sprint(err), color.LightRed.Sprint(stderr.String()))
execRes.Status = ee.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
} else {
return nil, err
}
}
execRes.Took = took
execRes.Stdout = strings.TrimRight(stdout.String(), "\n")
execRes.Stderr = strings.TrimRight(stderr.String(), "\n")
l.Tracef("[%s] %q\n%s\n", color.Green.Sprint("OK"), cmd.Args, color.FgGray.Sprint(execRes.Stdout))
return &execRes, nil
}
func (ec *externalCmd) prependUniqueArg(arg string, val string, aliases ...string) {
ec.args = tryInsertUniqueArg(ec.args, 0, arg, val, aliases...)
}
func tryInsertUniqueArg(args []string, pos int, arg string, val string, aliases ...string) []string {
if hasAnyPrefix(args, arg) || hasAnyPrefix(args, aliases...) {
return args
}
return tryInsertArg(args, pos, arg, val)
}
func tryInsertArg(args []string, pos int, arg string, val string) []string {
var argVal string
if val == "" {
argVal = arg
} else {
argVal = fmt.Sprintf("%s=%s", arg, val)
}
return tryInsertArgVals(args, pos, argVal)
}
func tryInsertArgVals(args []string, pos int, argVals ...string) []string {
if pos < 0 || pos > len(args) {
return args
}
return slices.Insert(args, pos, argVals...)
}
func anyPrefixIndex(elems []string, prefixes ...string) int {
for i, elem := range elems {
for _, pref := range prefixes {
if strings.HasPrefix(elem, pref) {
return i
}
}
}
return -1
}
func hasAnyPrefix(elems []string, prefixes ...string) bool {
return anyPrefixIndex(elems, prefixes...) >= 0
}