forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
381 lines (347 loc) · 7.58 KB
/
cmd.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package integration
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"text/template"
"time"
"github.com/mattn/go-shellwords"
"github.com/pkg/errors"
"github.com/tsuru/tsuru/safe"
)
const (
integrationEnvID = "TSURU_INTEGRATION_"
)
type safeWriter struct {
io.Writer
mu sync.Mutex
}
func (w *safeWriter) Write(data []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.Writer.Write(data)
}
type prefixWriter struct {
io.Writer
prefix string
}
func (w *prefixWriter) Write(data []byte) (int, error) {
newData := bytes.TrimSpace(data)
newData = bytes.Replace(newData, []byte("\r"), []byte("\n"), -1)
newData = bytes.Replace(newData, []byte("\n"), []byte("\n"+w.prefix), -1)
newData = append([]byte(w.prefix), append(newData, '\n')...)
_, err := w.Writer.Write(newData)
return len(data), err
}
var (
safeStdout = &safeWriter{Writer: os.Stdout}
safeStderr = &safeWriter{Writer: os.Stderr}
)
type Command struct {
Command string
Args []string
Input string
Timeout time.Duration
}
type Result struct {
Cmd *exec.Cmd
Command *Command
ExitCode int
Error error
Timeout bool
Stdout safe.Buffer
Stderr safe.Buffer
Env *Environment
}
type Expected struct {
ExitCode int
Timeout bool
Err string
Stderr string
Stdout string
}
type Environment struct {
mu *sync.Mutex
data map[string][]string
local map[string][]string
}
func NewEnvironment() *Environment {
e := Environment{
mu: &sync.Mutex{},
data: make(map[string][]string),
local: make(map[string][]string),
}
envs := os.Environ()
for _, env := range envs {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
continue
}
if strings.HasPrefix(parts[0], integrationEnvID) {
key := strings.Replace(parts[0], integrationEnvID, "", 1)
e.data[key] = strings.Split(parts[1], ",")
}
}
return &e
}
func (e *Environment) Clone() *Environment {
newEnv := *e
newEnv.local = make(map[string][]string)
return &newEnv
}
func (e *Environment) String() string {
e.mu.Lock()
defer e.mu.Unlock()
ret := fmt.Sprintln("Env vars:")
for k, v := range e.data {
ret += fmt.Sprintf(" %s: %#v\n", k, v)
}
ret += fmt.Sprintln("Local vars:")
for k, v := range e.local {
ret += fmt.Sprintf(" %s: %#v\n", k, v)
}
return ret[:len(ret)-1]
}
func (e *Environment) flatData() map[string]string {
e.mu.Lock()
defer e.mu.Unlock()
ret := map[string]string{}
for _, m := range []map[string][]string{e.data, e.local} {
for k, v := range m {
if len(v) > 0 {
ret[k] = v[0]
}
}
}
return ret
}
func (e *Environment) SetLocal(k string, v ...string) {
e.local[k] = v
}
func (e *Environment) Set(k string, v ...string) {
e.mu.Lock()
defer e.mu.Unlock()
e.data[k] = v
}
func (e *Environment) Add(k string, v string) {
e.mu.Lock()
defer e.mu.Unlock()
e.data[k] = append(e.data[k], v)
}
func (e *Environment) All(k string) []string {
e.mu.Lock()
defer e.mu.Unlock()
return append(e.local[k], e.data[k]...)
}
func (e *Environment) Get(k string) string {
if len(e.local[k]) > 0 {
return e.local[k][0]
}
e.mu.Lock()
defer e.mu.Unlock()
if len(e.data[k]) > 0 {
return e.data[k][0]
}
return ""
}
func (e *Environment) Has(k string) bool {
e.mu.Lock()
defer e.mu.Unlock()
return len(e.local[k]) > 0 || len(e.data[k]) > 0
}
func (e *Environment) IsDry() bool {
return e.Get("dryrun") != ""
}
func (e *Environment) VerboseLevel() int {
v, _ := strconv.Atoi(e.Get("verbose"))
return v
}
func (r *Result) SetError(err error) {
if err == nil {
return
}
r.Error = err
if exiterr, ok := r.Error.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
r.ExitCode = status.ExitStatus()
}
}
}
func (r *Result) String() string {
var args []string
if r.Cmd != nil {
args = r.Cmd.Args
}
var input string
if r.Command != nil {
input = r.Command.Input
}
return fmt.Sprintf(`--- Command %v ---
ExitCode: %d
Error: %v
Timeout: %v
Stdin: %q
Stdout: %q
Stderr: %q
%v
----------
`, args,
r.ExitCode,
r.Error,
r.Timeout,
input,
r.Stdout.String(),
r.Stderr.String(),
r.Env)
}
func (r *Result) Compare(expected Expected) error {
if expected.Timeout && !r.Timeout {
return errors.New("expected to timeout")
}
if expected.ExitCode != r.ExitCode {
return errors.Errorf("expected exitcode %d, got %d", expected.ExitCode, r.ExitCode)
}
matchRegex := func(exp, curr, field string) error {
if exp == "" {
return nil
}
var err error
exp, err = transformArgTemplate(r.Env, exp)
if err != nil {
return err
}
re, err := regexp.Compile(exp)
if err != nil {
return err
}
v := strings.TrimRight(curr, "\n")
if !re.MatchString(v) {
return errors.Errorf("expected %s to match %q: %q", field, exp, v)
}
return nil
}
var err error
if expected.Err != "" {
var errorStr string
if r.Error != nil {
errorStr = r.Error.Error()
}
err = matchRegex(expected.Err, errorStr, "err")
if err != nil {
return err
}
}
err = matchRegex(expected.Stderr, r.Stderr.String(), "stderr")
if err != nil {
return err
}
return matchRegex(expected.Stdout, r.Stdout.String(), "stdout")
}
func NewCommand(cmd string, args ...string) *Command {
return &Command{Command: cmd, Args: args, Timeout: 10 * time.Minute}
}
func (c *Command) WithArgs(args ...string) *Command {
c2 := *c
c2.Args = append([]string{}, append(c.Args, args...)...)
return &c2
}
func (c *Command) WithInput(input string) *Command {
c2 := *c
c2.Input = input
return &c2
}
func (c *Command) WithTimeout(timeout time.Duration) *Command {
c2 := *c
c2.Timeout = timeout
return &c2
}
func transformArgTemplate(e *Environment, val string) (string, error) {
tpl, err := template.New("tpl").Parse(val)
if err != nil {
return "", err
}
out := &bytes.Buffer{}
err = tpl.Execute(out, e.flatData())
if err != nil {
return "", err
}
return out.String(), nil
}
func (c *Command) Run(e *Environment) *Result {
res := &Result{Command: c, Env: e}
args := c.Args
input := c.Input
if e != nil {
args = nil
for i := range c.Args {
transformed, err := transformArgTemplate(e, c.Args[i])
if err != nil {
res.SetError(err)
return res
}
parts, _ := shellwords.Parse(transformed)
args = append(args, parts...)
}
var err error
input, err = transformArgTemplate(e, c.Input)
if err != nil {
res.SetError(err)
return res
}
}
execCmd := exec.Command(c.Command, args...)
execCmd.Stdin = strings.NewReader(input)
var stdout, stderr io.Writer
stdout = &res.Stdout
stderr = &res.Stderr
if e.VerboseLevel() > 1 {
prefix := fmt.Sprintf("%+v: ", execCmd.Args)
stdout = io.MultiWriter(stdout, &prefixWriter{safeStdout, prefix})
stderr = io.MultiWriter(stderr, &prefixWriter{safeStderr, prefix})
}
execCmd.Stdout = stdout
execCmd.Stderr = stderr
res.Cmd = execCmd
done := make(chan error, 1)
if e.IsDry() {
close(done)
fmt.Fprintf(safeStdout, "Would run: %+v\n", execCmd.Args)
} else {
if e.VerboseLevel() > 0 {
fmt.Fprintf(safeStdout, "Running: %+v\n", execCmd.Args)
}
err := res.Cmd.Start()
if err != nil {
res.SetError(err)
return res
}
if c.Timeout == 0 {
res.SetError(res.Cmd.Wait())
return res
}
go func() {
done <- res.Cmd.Wait()
}()
}
select {
case <-time.After(c.Timeout):
killErr := res.Cmd.Process.Kill()
if killErr != nil {
fmt.Fprintf(safeStderr, "failed to kill (pid=%d): %v\n", res.Cmd.Process.Pid, killErr)
}
res.Timeout = true
case err := <-done:
res.SetError(err)
}
return res
}