-
Notifications
You must be signed in to change notification settings - Fork 21
/
environment.go
317 lines (286 loc) · 8.56 KB
/
environment.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
// Copyright 2020 Matthew Holt
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package xk6
import (
"bytes"
"context"
"fmt"
"html/template"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
k6ModulePath, err := versionedModulePath(defaultK6ModulePath, b.K6Version)
if err != nil {
return nil, err
}
// clean up any SIV-incompatible module paths real quick
for i, p := range b.Extensions {
b.Extensions[i].PackagePath, err = versionedModulePath(p.PackagePath, p.Version)
if err != nil {
return nil, err
}
}
// create the context for the main module template
tplCtx := goModTemplateContext{
K6Module: k6ModulePath,
}
// evaluate the template for the main module
var buf bytes.Buffer
tpl, err := template.New("main").Parse(mainModuleTemplate)
if err != nil {
return nil, err
}
err = tpl.Execute(&buf, tplCtx)
if err != nil {
return nil, err
}
// create the folder in which the build environment will operate
tempFolder, err := newTempFolder()
if err != nil {
return nil, err
}
env := &environment{
k6Version: b.K6Version,
extensions: b.Extensions,
k6ModulePath: k6ModulePath,
tempFolder: tempFolder,
timeoutGoGet: b.TimeoutGet,
skipCleanup: b.SkipCleanup,
}
defer func() {
if err != nil {
err2 := env.Close()
if err2 != nil {
err = fmt.Errorf("%w; additionally, cleaning up folder: %v", err, err2)
}
}
}()
log.Printf("[INFO] Temporary folder: %s", tempFolder)
// initialize the go module
log.Println("[INFO] Initializing Go module")
cmd := env.newCommand("go", "mod", "init", "k6")
err = env.runCommand(ctx, cmd, 10*time.Second)
if err != nil {
return nil, err
}
// specify module replacements before pinning versions
replaced := make(map[string]string)
for _, r := range b.Replacements {
log.Printf("[INFO] Replace %s => %s", r.Old.String(), r.New.String())
err = env.execGoModReplace(ctx, r.Old.Param(), r.New.Param())
if err != nil {
return nil, err
}
replaced[r.Old.String()] = r.New.String()
}
// check for early abort
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
// pin versions by populating go.mod, first for k6 itself and then extensions
log.Println("[INFO] Pinning versions")
if b.K6Repo != "" {
// building with a forked repo, so get the main one and replace it with
// the fork
err = env.execGoModRequire(ctx, k6ModulePath, "")
if err != nil {
return nil, err
}
replace := b.K6Repo
if b.K6Version != "" {
replace = fmt.Sprintf("%s@%s", b.K6Repo, b.K6Version)
}
err = env.execGoModReplace(ctx, k6ModulePath, replace)
if err != nil {
return nil, err
}
}
nextExt:
for _, p := range b.Extensions {
err = env.writeExtensionImportFile(p.PackagePath)
if err != nil {
return nil, err
}
// if module is locally available, do not "go get" it;
// also note that we iterate and check prefixes, because
// an extension package may be a subfolder of a module, i.e.
// foo/a/extension is within module foo/a.
for repl := range replaced {
if strings.HasPrefix(p.PackagePath, repl) {
continue nextExt
}
}
err = env.execGoModRequire(ctx, p.PackagePath, p.Version)
if err != nil {
return nil, err
}
// check for early abort
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
}
// This is here as we could've not run go mod tidy due to a replace being the only extension
err = env.execGoModTidy(ctx)
if err != nil {
return nil, err
}
// write the main module file to temporary folder
// we do this last so we get the needed versions from all the replacements and extensions instead of k6 if possible
mainPath := filepath.Join(tempFolder, "main.go")
log.Printf("[INFO] Writing main module: %s", mainPath)
err = os.WriteFile(mainPath, buf.Bytes(), 0o600)
if err != nil {
return nil, err
}
// building with the default main repo
if b.K6Repo == "" && env.k6Version != "" {
// Only require a specific k6 version if provided. Otherwise extensions
// will require a version they depend on, and Go's module resolution
// algorithm will choose the highest one among all extensions.
err = env.execGoModRequire(ctx, k6ModulePath, env.k6Version)
if err != nil {
return nil, err
}
}
err = env.execGoModTidy(ctx)
if err != nil {
return nil, err
}
log.Println("[INFO] Build environment ready")
return env, nil
}
type environment struct {
k6Version string
extensions []Dependency
k6ModulePath string
tempFolder string
timeoutGoGet time.Duration
skipCleanup bool
}
// Close cleans up the build environment, including deleting
// the temporary folder from the disk.
func (env environment) Close() error {
if env.skipCleanup {
log.Printf("[INFO] Skipping cleanup as requested; leaving folder intact: %s", env.tempFolder)
return nil
}
log.Printf("[INFO] Cleaning up temporary folder: %s", env.tempFolder)
return os.RemoveAll(env.tempFolder)
}
func (env environment) writeExtensionImportFile(packagePath string) error {
fileContents := fmt.Sprintf(`package main
import _ %q
`, packagePath)
filePath := filepath.Join(env.tempFolder, strings.ReplaceAll(packagePath, "/", "_")+".go")
return os.WriteFile(filePath, []byte(fileContents), 0o600)
}
func (env environment) newCommand(command string, args ...string) *exec.Cmd {
cmd := exec.Command(command, args...)
cmd.Dir = env.tempFolder
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func (env environment) runCommand(ctx context.Context, cmd *exec.Cmd, timeout time.Duration) error {
log.Printf("[INFO] exec (timeout=%s): %+v ", timeout, cmd)
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
// start the command; if it fails to start, report error immediately
err := cmd.Start()
if err != nil {
return err
}
// wait for the command in a goroutine; the reason for this is
// very subtle: if, in our select, we do `case cmdErr := <-cmd.Wait()`,
// then that case would be chosen immediately, because cmd.Wait() is
// immediately available (even though it blocks for potentially a long
// time, it can be evaluated immediately). So we have to remove that
// evaluation from the `case` statement.
cmdErrChan := make(chan error)
go func() {
cmdErrChan <- cmd.Wait()
}()
// unblock either when the command finishes, or when the done
// channel is closed -- whichever comes first
select {
case cmdErr := <-cmdErrChan:
// process ended; report any error immediately
return cmdErr
case <-ctx.Done():
// context was canceled, either due to timeout or
// maybe a signal from higher up canceled the parent
// context; presumably, the OS also sent the signal
// to the child process, so wait for it to die
select {
case <-time.After(15 * time.Second):
_ = cmd.Process.Kill()
case <-cmdErrChan:
}
return ctx.Err()
}
}
// tidy the module to ensure go.mod will not have versions such as `latest`
func (env environment) execGoModTidy(ctx context.Context) error {
tidyCmd := env.newCommand("go", "mod", "tidy", "-compat=1.17")
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
}
func (env environment) execGoModRequire(ctx context.Context, modulePath, moduleVersion string) error {
mod := modulePath
if moduleVersion != "" {
mod += "@" + moduleVersion
} else {
mod += "@latest"
}
cmd := env.newCommand("go", "mod", "edit", "-require", mod)
err := env.runCommand(ctx, cmd, env.timeoutGoGet)
if err != nil {
return err
}
return env.execGoModTidy(ctx)
}
func (env environment) execGoModReplace(ctx context.Context, modulePath, replaceRepo string) error {
replace := fmt.Sprintf("%s=%s", modulePath, replaceRepo)
cmd := env.newCommand("go", "mod", "edit", "-replace", replace)
err := env.runCommand(ctx, cmd, env.timeoutGoGet)
if err != nil {
return err
}
return env.execGoModTidy(ctx)
}
type goModTemplateContext struct {
K6Module string
}
const mainModuleTemplate = `package main
import (
k6cmd "{{.K6Module}}/cmd"
// plug in k6 modules here
// TODO: Create /modules/standard dir structure?
// _ "{{.K6Module}}/modules/standard"
)
func main() {
k6cmd.Execute()
}
`