This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathrun.go
354 lines (304 loc) · 9.5 KB
/
run.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
package run
import (
"context"
"crypto/md5"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/sourcegraph/conc/pool"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/std"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"
)
type cmdRunner struct {
*std.Output
cmds []SGConfigCommand
parentEnv map[string]string
verbose bool
}
func Commands(ctx context.Context, parentEnv map[string]string, verbose bool, cmds []SGConfigCommand) (err error) {
if len(cmds) == 0 {
// Exit early if there are no commands to run.
return nil
}
std.Out.WriteLine(output.Styled(output.StylePending, fmt.Sprintf("Starting %d cmds", len(cmds))))
repoRoot := cmds[0].GetConfig().RepositoryRoot
// binaries get installed to <repository-root>/.bin. If the binary is installed with go build, then go
// will create .bin directory. Some binaries (like docsite) get downloaded instead of built and therefore
// need the directory to exist before hand.
binDir := filepath.Join(repoRoot, ".bin")
if err := os.Mkdir(binDir, 0755); err != nil && !os.IsExist(err) {
return err
}
if err := writePid(); err != nil {
return err
}
runner := cmdRunner{
std.Out,
cmds,
parentEnv,
verbose,
}
return runner.run(ctx)
}
func (runner *cmdRunner) run(ctx context.Context) error {
p := pool.New().WithContext(ctx).WithCancelOnError().WithFirstError()
// Start each command concurrently
for _, cmd := range runner.cmds {
cmd := cmd
p.Go(func(ctx context.Context) error {
config := cmd.GetConfig()
std.Out.WriteLine(output.Styledf(output.StylePending, "Running %s...", config.Name))
// Start watching the commands dependencies
wantRestart, err := cmd.StartWatch(ctx)
if err != nil {
runner.printError(cmd, err)
return err
}
// start up the binary
proc, err := runner.start(ctx, cmd)
if err != nil {
runner.printError(cmd, err)
return errors.Wrapf(err, "failed to start command %q", config.Name)
}
defer proc.cancel()
// Wait forever until we're asked to stop or that restarting returns an error.
for {
select {
// Handle context cancelled
case <-ctx.Done():
return ctx.Err()
// Handle process exit
case err := <-proc.Exit():
// If the process failed, we exit immediately
if err != nil {
return err
}
runner.WriteLine(output.Styledf(output.StyleSuccess, "%s%s exited without error%s", output.StyleBold, config.Name, output.StyleReset))
// If we shouldn't restart when the process exits, return
if !config.ContinueWatchOnExit {
return nil
}
// handle file watcher triggered
case <-wantRestart:
// If the command has an installer, re-run the install and determine if we should restart
runner.WriteLine(output.Styledf(output.StylePending, "Change detected. Reloading %s...", config.Name))
shouldRestart, err := runner.reinstall(ctx, cmd)
if err != nil {
runner.printError(cmd, err)
return err
}
if shouldRestart {
runner.WriteLine(output.Styledf(output.StylePending, "Restarting %s...", config.Name))
proc.cancel()
proc, err = runner.start(ctx, cmd)
if err != nil {
return err
}
defer proc.cancel()
} else {
runner.WriteLine(output.Styledf(output.StylePending, "Binary for %s did not change. Not restarting.", config.Name))
}
}
}
})
}
return p.Wait()
}
func (runner *cmdRunner) printError(cmd SGConfigCommand, err error) {
printCmdError(runner.Output.Output, cmd.GetConfig().Name, err)
}
func (runner *cmdRunner) debug(msg string, args ...any) { //nolint currently unused but a handy tool for debugginlg
if runner.verbose {
message := fmt.Sprintf(msg, args...)
runner.WriteLine(output.Styledf(output.StylePending, "%s[DEBUG]: %s %s", output.StyleBold, output.StyleReset, message))
}
}
func (runner *cmdRunner) start(ctx context.Context, cmd SGConfigCommand) (*startedCmd, error) {
return startSgCmd(ctx, cmd, runner.parentEnv)
}
func (runner *cmdRunner) reinstall(ctx context.Context, cmd SGConfigCommand) (bool, error) {
if installer, ok := cmd.(Installer); !ok {
// If there is no installer, then we always restart
return true, nil
} else {
bin, err := cmd.GetBinaryLocation()
if err != nil {
// If the command doesn't have a CheckBinary, we just ignore it
if errors.Is(err, noBinaryError{}) {
return false, nil
} else {
return false, err
}
}
oldHash, err := md5HashFile(bin)
if err != nil {
return false, err
}
if err := installer.RunInstall(ctx, runner.parentEnv); err != nil {
runner.printError(cmd, err)
return false, err
}
newHash, err := md5HashFile(bin)
if err != nil {
return false, err
}
return oldHash != newHash, nil
}
}
// installErr is returned by runWatch if the cmd.Install step fails.
type installErr struct {
cmdName string
output string
originalErr error
}
func (e installErr) Error() string {
return fmt.Sprintf("install of %s failed: %s", e.cmdName, e.output)
}
// runErr is used internally by runWatch to print a message when a
// command failed to reinstall.
type runErr struct {
cmdName string
exitCode int
stderr string
stdout string
}
func (e runErr) Error() string {
return fmt.Sprintf("failed to run %s.\nstderr:\n%s\nstdout:\n%s\n", e.cmdName, e.stderr, e.stdout)
}
func printCmdError(out *output.Output, cmdName string, err error) {
// Don't log context canceled errors because they are not the root issue
if errors.Is(err, context.Canceled) {
return
}
var message, cmdOut string
switch e := errors.Cause(err).(type) {
case installErr:
message = "Failed to build " + cmdName
if e.originalErr != nil {
if errWithout, ok := e.originalErr.(errorWithoutOutputer); ok {
// If we can, let's strip away the output, otherwise this gets
// too noisy.
message += ": " + errWithout.ErrorWithoutOutput()
} else {
message += ": " + e.originalErr.Error()
}
}
cmdOut = e.output
case runErr:
message = "Failed to run " + cmdName
cmdOut = fmt.Sprintf("Exit code: %d\n\n", e.exitCode)
if len(strings.TrimSpace(e.stdout)) > 0 {
formattedStdout := "\t" + strings.Join(strings.Split(e.stdout, "\n"), "\n\t")
cmdOut += fmt.Sprintf("Standard out:\n%s\n", formattedStdout)
}
if len(strings.TrimSpace(e.stderr)) > 0 {
formattedStderr := "\t" + strings.Join(strings.Split(e.stderr, "\n"), "\n\t")
cmdOut += fmt.Sprintf("Standard err:\n%s\n", formattedStderr)
}
default:
var exc *exec.ExitError
// recurse if it is an exit error
if errors.As(err, &exc) {
printCmdError(out, cmdName, runErr{
cmdName: cmdName,
exitCode: exc.ExitCode(),
stderr: string(exc.Stderr),
})
return
} else {
message = fmt.Sprintf("Failed to run %s: %+v", cmdName, err)
}
}
separator := strings.Repeat("-", 80)
if cmdOut != "" {
line := output.Linef(
"", output.StyleWarning,
"%s\n%s%s:\n%s%s%s%s%s",
separator, output.StyleBold, message, output.StyleReset,
cmdOut, output.StyleWarning, separator, output.StyleReset,
)
out.WriteLine(line)
} else {
line := output.Linef(
"", output.StyleWarning,
"%s\n%s%s\n%s%s",
separator, output.StyleBold, message,
separator, output.StyleReset,
)
out.WriteLine(line)
}
}
// makeEnv merges environments starting from the left, meaning the first environment will be overriden by the second one, skipping
// any key that has been explicitly defined in the current environment of this process. This enables users to manually overrides
// environment variables explictly, i.e FOO=1 sg start will have FOO=1 set even if a command or commandset sets FOO.
func makeEnv(envs ...map[string]string) (combined []string) {
for k, v := range makeEnvMap(envs...) {
combined = append(combined, fmt.Sprintf("%s=%s", k, v))
}
return combined
}
func makeEnvMap(envs ...map[string]string) map[string]string {
combined := map[string]string{}
for _, pair := range os.Environ() {
elems := strings.SplitN(pair, "=", 2)
if len(elems) != 2 {
panic("space/time continuum wrong")
}
combined[elems[0]] = elems[1]
}
for _, env := range envs {
for k, v := range env {
if _, ok := os.LookupEnv(k); ok {
// If the key is already set in the process env, we don't
// overwrite it. That way we can do something like:
//
// SRC_LOG_LEVEL=debug sg run enterprise-frontend
//
// to overwrite the default value in sg.config.yaml
continue
}
// Expand env vars and keep track of previously set env vars
// so they can be used when expanding too.
// TODO: using range to iterate over the env is not stable and thus
// this won't work
expanded := os.Expand(v, func(lookup string) string {
// If we're looking up the key that we're trying to define, we
// skip the self-reference and look in the OS
if lookup == k {
return os.Getenv(lookup)
}
if e, ok := env[lookup]; ok {
return e
}
return os.Getenv(lookup)
})
combined[k] = expanded
}
}
return combined
}
func md5HashFile(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", err
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return string(h.Sum(nil)), nil
}
func Test(ctx context.Context, cmd SGConfigCommand, parentEnv map[string]string) error {
name := cmd.GetConfig().Name
std.Out.WriteLine(output.Styledf(output.StylePending, "Starting testsuite %q.", name))
proc, err := startSgCmd(ctx, cmd, parentEnv)
if err != nil {
printCmdError(std.Out.Output, name, err)
}
return proc.Wait()
}