-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.go
220 lines (181 loc) · 6.14 KB
/
script.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
package script
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
gexec "os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/ovh/utask"
"github.com/ovh/utask/pkg/plugins/builtin/scriptutil"
"github.com/ovh/utask/pkg/plugins/taskplugin"
)
// the script plugin execute scripts
var (
Plugin = taskplugin.New("script", "0.2", exec,
taskplugin.WithConfig(validConfig, Config{}),
taskplugin.WithContextFunc(ctx),
taskplugin.WithResources(resourcesscript),
)
)
const (
exitCodeMetadataKey string = "exit_code"
processStateMetadataKey string = "process_state"
outputMetadataKey string = "output"
executionTimeMetadataKey string = "execution_time"
errorMetadataKey string = "error"
)
// Metadata represents the metadata of script execution
type Metadata struct {
ExitCode string `json:"exit_code"`
ProcessState string `json:"process_state"`
Output string `json:"output"`
ExecutionTime string `json:"execution_time"`
Error string `json:"error"`
}
// Config is the configuration needed to execute a script
type Config struct {
File string `json:"file_path"`
Argv []string `json:"argv,omitempty"`
Timeout string `json:"timeout,omitempty"`
Stdin string `json:"stdin,omitempty"`
OutputMode string `json:"output_mode"`
OutputManualDelimiters []string `json:"output_manual_delimiters"`
ExitCodesUnrecoverable []string `json:"exit_codes_unrecoverable"`
}
// ScriptContext is the metadata inherited from the task
type ScriptContext struct {
TaskID string `json:"task_id"`
ResolutionID string `json:"resolution_id"`
}
func ctx(stepName string) interface{} {
return &ScriptContext{
TaskID: "{{ .task.task_id }}",
ResolutionID: "{{ .task.resolution_id }}",
}
}
func resourcesscript(i interface{}) []string {
cfg := i.(*Config)
return []string{
"fork",
fmt.Sprintf("script:%s", cfg.File),
}
}
func validConfig(config interface{}) error {
cfg := config.(*Config)
if cfg.File == "" {
return errors.New("file is missing")
}
scriptPath := filepath.Join(utask.FScriptsFolder, cfg.File)
f, err := os.Stat(scriptPath)
if err != nil {
return fmt.Errorf("can't stat %q: %s", scriptPath, err.Error())
}
if f.Mode()&0111 == 0 {
return fmt.Errorf("%q is not executable", scriptPath)
}
if cfg.Timeout != "" {
if cfg.Timeout[0] == '-' {
return errors.New("timeout must be positive")
}
if _, err := time.ParseDuration(cfg.Timeout); err != nil {
return fmt.Errorf("can't parse timeout field %q: %s", cfg.Timeout, err.Error())
}
}
switch cfg.OutputMode {
case "":
// default will have to be reset in exec as config modification will not be persisted
cfg.OutputMode = scriptutil.OutputModeManualLastLine
case scriptutil.OutputModeDisabled, scriptutil.OutputModeManualDelimiters, scriptutil.OutputModeManualLastLine:
default:
return fmt.Errorf("invalid value %q for output_mode, allowed values are: %s", cfg.OutputMode, strings.Join([]string{scriptutil.OutputModeDisabled, scriptutil.OutputModeManualDelimiters, scriptutil.OutputModeManualLastLine}, ", "))
}
if cfg.OutputManualDelimiters != nil && cfg.OutputMode != scriptutil.OutputModeManualDelimiters {
return fmt.Errorf("invalid parameter \"output_manual_delimiters\", output_mode is configured to %q", cfg.OutputMode)
}
if cfg.OutputMode == scriptutil.OutputModeManualDelimiters && (cfg.OutputManualDelimiters == nil || len(cfg.OutputManualDelimiters) != 2) {
length := 0
if cfg.OutputManualDelimiters != nil {
length = len(cfg.OutputManualDelimiters)
}
return fmt.Errorf("wrong number of output_manual_delimiters, 2 expected, found %d", length)
}
if cfg.OutputManualDelimiters != nil {
if _, err := scriptutil.GenerateOutputDelimitersRegexp(cfg.OutputManualDelimiters[0], cfg.OutputManualDelimiters[1]); err != nil {
return fmt.Errorf("unable to compile output_manual_delimiters regexp: %s", err)
}
}
if err := scriptutil.ValidateExitCodesUnreachable(cfg.ExitCodesUnrecoverable); err != nil {
return err
}
return nil
}
func exec(stepName string, config interface{}, ctx interface{}) (interface{}, interface{}, error) {
cfg := config.(*Config)
scriptContext := ctx.(*ScriptContext)
var timeout time.Duration
if cfg.Timeout != "" {
timeout, _ = time.ParseDuration(cfg.Timeout)
} else {
// default is 2 * 1 minute = 2 minutes
timeout = 2 * time.Minute
}
if cfg.OutputMode == "" {
cfg.OutputMode = scriptutil.OutputModeManualLastLine
}
ctxe, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := gexec.CommandContext(ctxe, fmt.Sprintf("./%s", cfg.File), cfg.Argv...)
cmd.Env = append(
os.Environ(),
fmt.Sprintf("UTASK_TASK_ID=%s", scriptContext.TaskID),
fmt.Sprintf("UTASK_RESOLUTION_ID=%s", scriptContext.ResolutionID),
fmt.Sprintf("UTASK_STEP_NAME=%s", stepName),
)
cmd.Dir = utask.FScriptsFolder
cmd.Stdin = strings.NewReader(cfg.Stdin)
exitCode := 0
metaError := ""
// start exec time timer
timer := time.Now()
// execute script
out, err := cmd.CombinedOutput()
// evaluate exec time
execTime := time.Since(timer)
if err != nil {
if exitError, ok := err.(*gexec.ExitError); ok {
exitCode = exitError.Sys().(syscall.WaitStatus).ExitStatus()
} else {
exitCode = 1
}
metaError = err.Error()
} else {
exitCode = cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
}
pState := cmd.ProcessState.String()
outStr := string(out)
metadata := map[string]interface{}{
exitCodeMetadataKey: fmt.Sprint(exitCode),
processStateMetadataKey: pState,
outputMetadataKey: outStr,
executionTimeMetadataKey: execTime.String(),
errorMetadataKey: metaError,
}
output := make(map[string]interface{})
if resultLine, err := scriptutil.ParseOutput(outStr, cfg.OutputMode, cfg.OutputManualDelimiters); err != nil {
return nil, metadata, err
} else if resultLine != "" {
err = json.Unmarshal([]byte(resultLine), &output)
if err != nil && exitCode == 0 {
return nil, metadata, err
}
}
if exitCode != 0 {
return output, metadata, scriptutil.FormatErrorExitCode(exitCode, cfg.ExitCodesUnrecoverable, err)
}
return output, metadata, nil
}