-
Notifications
You must be signed in to change notification settings - Fork 69
/
go_task_recipe_executor.go
128 lines (102 loc) · 3.17 KB
/
go_task_recipe_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
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
package execution
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/go-task/task/v3"
taskargs "github.com/go-task/task/v3/args"
"github.com/go-task/task/v3/taskfile"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/newrelic/newrelic-cli/internal/install/types"
)
// GoTaskRecipeExecutor is an implementation of the recipeExecutor interface that
// uses the go-task module to execute the steps defined in each recipe.
type GoTaskRecipeExecutor struct {
Stderr io.Writer
Stdin io.Reader
Stdout io.Writer
}
// NewGoTaskRecipeExecutor returns a new instance of GoTaskRecipeExecutor.
func NewGoTaskRecipeExecutor() *GoTaskRecipeExecutor {
return &GoTaskRecipeExecutor{
Stderr: os.Stderr,
Stdin: os.Stdin,
Stdout: os.Stdout,
}
}
func (re *GoTaskRecipeExecutor) ExecutePreInstall(ctx context.Context, r types.OpenInstallationRecipe, recipeVars types.RecipeVars) error {
return errors.New("not implemented")
}
func (re *GoTaskRecipeExecutor) Execute(ctx context.Context, r types.OpenInstallationRecipe, recipeVars types.RecipeVars) error {
log.Debugf("executing recipe %s", r.Name)
out := []byte(r.Install)
// Create a temporary task file.
file, err := ioutil.TempFile("", r.Name)
defer os.Remove(file.Name())
if err != nil {
return err
}
_, err = file.Write(out)
if err != nil {
return err
}
stdoutCapture := NewLineCaptureBuffer(re.Stdout)
stderrCapture := NewLineCaptureBuffer(re.Stderr)
e := task.Executor{
Entrypoint: file.Name(),
Stderr: stderrCapture,
Stdin: re.Stdin,
Stdout: stdoutCapture,
}
if err = e.Setup(); err != nil {
return fmt.Errorf("could not set up task executor: %s", err)
}
var tf taskfile.Taskfile
err = yaml.Unmarshal(out, &tf)
if err != nil {
return fmt.Errorf("could not unmarshal taskfile: %s", err)
}
calls, globals := taskargs.ParseV3()
e.Taskfile.Vars.Merge(globals)
for k, val := range recipeVars {
e.Taskfile.Vars.Set(k, taskfile.Var{Static: val})
}
if err := e.Run(ctx, calls...); err != nil {
log.WithFields(log.Fields{
"err": err,
}).Debug("Task execution returned error")
goTaskError := types.NewGoTaskGeneralError(err)
// go-task does not provide an error type to denote context cancelation
// Therefore we need to match inside the error message
if strings.Contains(err.Error(), "context canceled") {
return types.ErrInterrupt
}
// Recipe trigger a canceled event with specific exit code 130 used
if isExitStatusCode(130, err) {
return types.ErrInterrupt
}
// We return exit code 131 when a user attempts to
// install a recipe on an unsupported operating system.
if isExitStatusCode(131, err) {
return &types.UnsupportedOperatingSytemError{
Err: errors.New(stderrCapture.LastFullLine),
}
}
// Catchall error formatting for child process errors
if strings.Contains(err.Error(), "exit status") {
lastStderr := stderrCapture.LastFullLine
return types.NewNonZeroExitCode(goTaskError, lastStderr)
}
return goTaskError
}
return nil
}
func isExitStatusCode(exitCode int, err error) bool {
exitCodeString := fmt.Sprintf("exit status %d", exitCode)
return strings.Contains(err.Error(), exitCodeString)
}