-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathlauncher.go
More file actions
309 lines (262 loc) · 8.17 KB
/
launcher.go
File metadata and controls
309 lines (262 loc) · 8.17 KB
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
package cmd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"os/exec"
"strings"
"syscall"
"unicode"
"unicode/utf8"
"github.com/Masterminds/semver/v3"
"github.com/grafana/k6provider"
"go.k6.io/k6/cloudapi"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/ext"
)
// commandExecutor executes the requested k6 command line command.
// It abstract the execution path from the concrete binary.
type commandExecutor interface {
run(*state.GlobalState) error
}
// provisioner defines the interface for provisioning a commandExecutor for a set of dependencies
type provisioner interface {
provision(map[string]string) (commandExecutor, error)
}
func constraintsMapToProvisionDependency(deps map[string]*semver.Constraints) k6provider.Dependencies {
result := make(k6provider.Dependencies)
for name, constraint := range deps {
if constraint == nil {
// If dependency's constraint is nil, assume it is "*" and consider it satisfied.
// See https://github.com/grafana/k6deps/issues/91
result[name] = "*"
continue
}
result[name] = constraint.String()
}
return result
}
// customBinary runs the requested commands on a different binary on a subprocess passing the
// original arguments
type customBinary struct {
// path represents the local file path
// on the file system of the binary
path string
}
//nolint:forbidigo
func (b *customBinary) run(gs *state.GlobalState) error {
cmd := exec.CommandContext(gs.Ctx, b.path, gs.CmdArgs[1:]...) //nolint:gosec
// we pass os stdout, err, in because passing them from GlobalState changes how
// the subprocess detects the type of terminal
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
// If stdin was used by the analyze function, the content has been preserved
// in `gs.Stdin` and should be passed to the command
cmd.Stdin = gs.Stdin
// Copy environment variables to the k6 process skipping auto extension resolution feature flag.
env := []string{}
for k, v := range gs.Env {
if k == state.AutoExtensionResolution {
continue
}
env = append(env, fmt.Sprintf("%s=%s", k, v))
}
// If auto extension resolution is enabled then
// this avoids unnecessary re-processing of dependencies in the sub-process.
env = append(env, state.AutoExtensionResolution+"=false")
cmd.Env = env
// handle signals
sigC := make(chan os.Signal, 2)
gs.SignalNotify(sigC, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
gs.Logger.Debug("Launching the provisioned k6 binary")
if err := cmd.Start(); err != nil {
gs.Logger.
WithError(err).
Error("Failed to run the provisioned k6 binary")
return err
}
// wait for the subprocess to end
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
for {
select {
case err := <-done:
var exitError *exec.ExitError
if errors.As(err, &exitError) {
return errext.WithExitCodeIfNone(errAlreadyReported, exitcodes.ExitCode(exitError.ExitCode())) //nolint:gosec
}
return err
case sig := <-sigC:
gs.Logger.
WithField("signal", sig.String()).
Debug("Signal received, waiting for the subprocess to handle it and return.")
}
}
}
// used just to signal we shouldn't print error again
var errAlreadyReported = fmt.Errorf("already reported error")
// isCustomBuildRequired checks if there is at least one dependency that are not satisfied by the binary
// considering the version of k6 and any built-in extension
func isCustomBuildRequired(deps dependencies, k6Version string, exts []*ext.Extension) bool {
if len(deps) == 0 {
return false
}
// collect modules that this binary contain, including k6 itself
builtIn := map[string]string{"k6": k6Version}
for _, e := range exts {
builtIn[e.Name] = e.Version
}
for name, constraint := range deps {
version, provided := builtIn[name]
// if the binary does not contain a required module, we need a custom
if !provided {
return true
}
// If dependency's constraint is null, assume it is "*" and consider it satisfied.
// See https://github.com/grafana/k6deps/issues/91
if constraint == nil {
continue
}
semver, err := semver.NewVersion(version)
if err != nil {
// ignore built in module if version is not a valid sem ver (e.g. a development version)
// if user wants to use this built-in, must disable the automatic extension resolution
return true
}
// if the current version does not satisfies the constrains, binary provisioning is required
if !constraint.Check(semver) {
return true
}
}
return false
}
// k6buildProvisioner provisions a k6 binary that satisfies the dependencies using the k6build service
type k6buildProvisioner struct {
gs *state.GlobalState
}
func newK6BuildProvisioner(gs *state.GlobalState) provisioner {
return &k6buildProvisioner{gs: gs}
}
func (p *k6buildProvisioner) provision(deps map[string]string) (commandExecutor, error) {
config := getProviderConfig(p.gs)
logger := slog.New(newLogrusSlogHandler(p.gs.Logger))
provider, err := k6provider.NewProviderWithLogger(config, logger)
if err != nil {
return nil, err
}
binary, err := provider.GetBinary(p.gs.Ctx, deps)
if err != nil {
return nil, err
}
return &customBinary{binary.Path}, nil
}
func getProviderConfig(gs *state.GlobalState) k6provider.Config {
config := k6provider.Config{
BuildServiceURL: gs.Flags.BuildServiceURL,
BinaryCacheDir: gs.Flags.BinaryCache,
}
token, err := extractToken(gs)
if err != nil {
gs.Logger.WithError(err).Debug("Failed to get cloud token")
}
if token != "" {
config.BuildServiceAuth = token
}
return config
}
// extractToken gets the cloud token required to access the build service
// from the environment or from the config file
func extractToken(gs *state.GlobalState) (string, error) {
diskConfig, err := readDiskConfig(gs)
if err != nil {
return "", err
}
config, _, err := cloudapi.GetConsolidatedConfig(diskConfig.Collectors["cloud"], gs.Env, "", nil)
if err != nil {
return "", err
}
return config.Token.String, nil
}
func processUseDirectives(name string, text []byte, deps dependencies) error {
directives := findDirectives(text)
for _, directive := range directives {
// normalize spaces
directive = strings.ReplaceAll(directive, " ", " ")
if !strings.HasPrefix(directive, "use k6") {
continue
}
directive = strings.TrimSpace(strings.TrimPrefix(directive, "use k6"))
dep := "k6"
constraint := directive
if strings.HasPrefix(directive, "with k6/x/") {
directive = strings.TrimSpace(strings.TrimPrefix(directive, "with "))
dep, constraint, _ = strings.Cut(directive, " ")
}
var con *semver.Constraints
var err error
if len(constraint) > 0 {
con, err = semver.NewConstraint(constraint)
if err != nil {
return fmt.Errorf("error while parsing use directives constraint %q for %q in %q: %w", constraint, dep, name, err)
}
}
err = deps.update(dep, con)
if err != nil {
return fmt.Errorf("error while parsing use directives in %q: %w", name, err)
}
}
return nil
}
func findDirectives(text []byte) []string {
// parse #! at beginning of file
if bytes.HasPrefix(text, []byte("#!")) {
_, text, _ = bytes.Cut(text, []byte("\n"))
}
var result []string
for i := 0; i < len(text); {
r, width := utf8.DecodeRune(text[i:])
switch {
case unicode.IsSpace(r) || r == rune(';'): // skip all spaces and ;
i += width
case r == '"' || r == '\'': // string literals
idx := bytes.IndexRune(text[i+width:], r)
if idx < 0 {
return result
}
result = append(result, string(text[i+width:i+width+idx]))
i += width + idx + 1
case bytes.HasPrefix(text[i:], []byte("//")):
idx := bytes.IndexRune(text[i+width:], '\n')
if idx < 0 {
return result
}
i += width + idx + 1
case bytes.HasPrefix(text[i:], []byte("/*")):
idx := bytes.Index(text[i+width:], []byte("*/"))
if idx < 0 {
return result
}
i += width + idx + 2
default:
return result
}
}
return result
}
func parseManifest(manifestString string) (dependencies, error) {
if manifestString == "" {
return nil, nil //nolint:nilnil
}
manifestMap := make(map[string]string)
if err := json.Unmarshal([]byte(manifestString), &manifestMap); err != nil {
return nil, fmt.Errorf("invalid dependencies manifest %w", err)
}
return dependenciesFromMap(manifestMap)
}