-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexec.go
More file actions
563 lines (503 loc) · 16.8 KB
/
exec.go
File metadata and controls
563 lines (503 loc) · 16.8 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package internal
import (
"errors"
"fmt"
"os"
osExec "os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/google/uuid"
tuikitIO "github.com/flowexec/tuikit/io"
"github.com/flowexec/tuikit/views"
"github.com/gen2brain/beeep"
"github.com/spf13/cobra"
"github.com/flowexec/flow/cmd/internal/flags"
"github.com/flowexec/flow/internal/io"
"github.com/flowexec/flow/internal/runner"
"github.com/flowexec/flow/internal/runner/engine"
"github.com/flowexec/flow/internal/runner/exec"
"github.com/flowexec/flow/internal/runner/launch"
"github.com/flowexec/flow/internal/runner/parallel"
"github.com/flowexec/flow/internal/runner/render"
"github.com/flowexec/flow/internal/runner/request"
"github.com/flowexec/flow/internal/runner/serial"
"github.com/flowexec/flow/internal/utils/env"
"github.com/flowexec/flow/pkg/context"
flowErrors "github.com/flowexec/flow/pkg/errors"
"github.com/flowexec/flow/pkg/filesystem"
"github.com/flowexec/flow/pkg/logger"
"github.com/flowexec/flow/pkg/store"
"github.com/flowexec/flow/types/executable"
"github.com/flowexec/flow/types/workspace"
)
const (
// backgroundRunIDEnv is set on child processes spawned by --background.
backgroundRunIDEnv = "FLOW_BACKGROUND_RUN_ID"
)
func RegisterExecCmd(ctx *context.Context, rootCmd *cobra.Command) {
subCmd := &cobra.Command{
Use: "exec EXECUTABLE_ID [args...]",
Aliases: executable.SortedValidVerbs(),
Short: "Execute any executable by reference.",
Long: execDocumentation + fmt.Sprintf(
"\n\nSee %s for more information on executable verbs and "+
"%s for more information on executable IDs.\n\n%s",
io.TypesDocsURL("flowfile", "executableverb"),
io.TypesDocsURL("flowfile", "executableref"),
execExamples,
),
Args: cobra.ArbitraryArgs,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
verbStr := cmd.CalledAs()
verb := executable.Verb(verbStr)
execList, err := ctx.ExecutableCache.GetExecutableList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
execIDs := make([]string, 0, len(execList))
for _, e := range execList {
if e.Verb.Equals(verb) {
execIDs = append(execIDs, e.ID())
}
}
return execIDs, cobra.ShellCompDirectiveNoFileComp
},
PreRun: func(cmd *cobra.Command, args []string) {
logMode := flags.ValueFor[string](cmd, *flags.LogModeFlag, false)
if err := tuikitIO.LogMode(logMode).Validate(); err != nil {
logger.Log().FatalErr(err)
}
execPreRun(ctx, cmd, args)
},
Run: func(cmd *cobra.Command, args []string) {
verbStr := cmd.CalledAs()
verb := executable.Verb(verbStr)
execFunc(ctx, cmd, verb, args)
},
}
RegisterFlag(ctx, subCmd, *flags.ParameterValueFlag)
RegisterFlag(ctx, subCmd, *flags.LogModeFlag)
RegisterFlag(ctx, subCmd, *flags.BackgroundFlag)
rootCmd.AddCommand(subCmd)
}
func execPreRun(_ *context.Context, _ *cobra.Command, _ []string) {
runner.RegisterRunner(exec.NewRunner())
runner.RegisterRunner(launch.NewRunner())
runner.RegisterRunner(request.NewRunner())
runner.RegisterRunner(render.NewRunner())
runner.RegisterRunner(serial.NewRunner())
runner.RegisterRunner(parallel.NewRunner())
}
func execFunc(ctx *context.Context, cmd *cobra.Command, verb executable.Verb, args []string) {
logMode := flags.ValueFor[string](cmd, *flags.LogModeFlag, false)
if logMode != "" {
logger.Log().SetMode(tuikitIO.LogMode(logMode))
}
if err := verb.Validate(); err != nil {
logger.Log().FatalErr(err)
}
var ref executable.Ref
if len(args) == 0 {
ref = context.ExpandRef(ctx, executable.NewRef("", verb))
} else {
idArg := args[0]
ref = context.ExpandRef(ctx, executable.NewRef(idArg, verb))
}
e, err := ctx.ExecutableCache.GetExecutableByRef(ref)
if err != nil && errors.Is(flowErrors.NewExecutableNotFoundError(ref.String()), err) {
logger.Log().Debugf("Executable %s not found in cache, syncing cache", ref)
if err := ctx.ExecutableCache.Update(); err != nil {
logger.Log().FatalErr(err)
}
e, err = ctx.ExecutableCache.GetExecutableByRef(ref)
}
if err != nil {
logger.Log().FatalErr(err)
}
if err := e.Validate(); err != nil {
logger.Log().FatalErr(err)
}
if !e.IsExecutableFromWorkspace(ctx.CurrentWorkspace.AssignedName()) {
logger.Log().FatalErr(fmt.Errorf(
"executable '%s' cannot be executed from workspace %s",
ref,
ctx.Config.CurrentWorkspace,
))
}
// Handle --background: spawn a detached child process and return immediately.
background := flags.ValueFor[bool](cmd, *flags.BackgroundFlag, false)
if background {
launchBackground(ctx, ref, verb, args)
return
}
// If this is a background child process, eagerly record the log archive path
// so that `logs --running` can stream output while we're still executing.
bgRunID := os.Getenv(backgroundRunIDEnv)
if bgRunID != "" {
linkBackgroundArchive(ctx, bgRunID)
}
if ctx.DataStore != nil {
if err := ctx.DataStore.CreateProcessBucket(ref.String()); err != nil {
logger.Log().FatalErr(err)
}
_ = os.Setenv(store.BucketEnv, ref.String())
}
envMap := buildExecEnv(ctx, cmd, e)
var execArgs []string
if len(args) >= 2 {
execArgs = args[1:]
}
startTime := time.Now()
eng := engine.NewExecEngine()
runErr := runner.Exec(ctx, e, eng, envMap, execArgs)
dur := time.Since(startTime)
cleanupProcessStore(ctx)
recordExecution(ctx, ref, startTime, dur, runErr)
// Update background run record if this is a child process.
if bgRunID != "" {
finalizeBackgroundRun(ctx, bgRunID, runErr)
}
if runErr != nil {
logger.Log().FatalErr(runErr)
}
logger.Log().Debug(fmt.Sprintf("%s flow completed", ref), "Elapsed", dur.Round(time.Millisecond))
sendCompletionNotifications(ctx, cmd, dur)
}
// launchBackground spawns a detached flow process for the given executable and returns immediately.
func launchBackground(ctx *context.Context, ref executable.Ref, verb executable.Verb, args []string) {
runID := uuid.New().String()[:8]
// Build the child command: same verb + args. Stdout/stderr are set to nil so
// Go redirects them to /dev/null — terminal output is suppressed but the tuikit
// archive handler still writes to the log file normally.
childArgs := []string{string(verb)}
if len(args) > 0 {
childArgs = append(childArgs, args...)
}
flowBin, err := os.Executable()
if err != nil {
logger.Log().FatalErr(fmt.Errorf("unable to find flow binary: %w", err))
}
child := osExec.Command(flowBin, childArgs...)
child.Env = append(os.Environ(), fmt.Sprintf("%s=%s", backgroundRunIDEnv, runID))
child.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
child.Stdout = nil
child.Stderr = nil
child.Stdin = nil
if err := child.Start(); err != nil {
logger.Log().FatalErr(fmt.Errorf("failed to start background process: %w", err))
}
run := store.BackgroundRun{
ID: runID,
PID: child.Process.Pid,
Ref: ref.String(),
StartedAt: time.Now(),
Status: store.BackgroundRunning,
}
if ctx.DataStore != nil {
if err := ctx.DataStore.SaveBackgroundRun(run); err != nil {
logger.Log().Errorf("failed to save background run record: %v", err)
}
}
// Release the child process so it survives parent exit.
_ = child.Process.Release()
logger.Log().Println(fmt.Sprintf("Started background run %s (PID %d) for %s", runID, run.PID, ref))
}
// linkBackgroundArchive eagerly writes the log archive path into the background run
// record so that `logs attach` can stream output while the child is still executing.
// Unlike findArchiveByID, this scans the log directory directly without skipping empty
// files — the archive file exists at startup but may not have content yet.
func linkBackgroundArchive(ctx *context.Context, runID string) {
if ctx.DataStore == nil || ctx.LogArchiveID == "" {
return
}
archivePath := findArchiveFileByID(ctx.LogArchiveID)
if archivePath == "" {
return
}
run, err := ctx.DataStore.GetBackgroundRun(runID)
if err != nil {
return
}
run.LogArchiveID = archivePath
_ = ctx.DataStore.SaveBackgroundRun(run)
}
// findArchiveFileByID scans the logs directory for a file whose name starts with the
// given archive ID. Unlike ListArchiveEntries, this does not skip empty files.
func findArchiveFileByID(archiveID string) string {
logsDir := filesystem.LogsDir()
files, err := os.ReadDir(logsDir)
if err != nil {
return ""
}
for _, f := range files {
if f.IsDir() {
continue
}
if strings.HasPrefix(f.Name(), archiveID) {
return filepath.Join(logsDir, f.Name())
}
}
return ""
}
// finalizeBackgroundRun updates the background run record with the final status.
func finalizeBackgroundRun(ctx *context.Context, runID string, runErr error) {
if ctx.DataStore == nil {
return
}
run, err := ctx.DataStore.GetBackgroundRun(runID)
if err != nil {
logger.Log().Debug("failed to load background run for finalization", "err", err)
return
}
now := time.Now()
run.CompletedAt = &now
run.LogArchiveID = findArchiveByID(ctx.LogArchiveID)
if runErr != nil {
run.Status = store.BackgroundFailed
run.Error = runErr.Error()
} else {
run.Status = store.BackgroundCompleted
}
if err := ctx.DataStore.SaveBackgroundRun(run); err != nil {
logger.Log().Debug("failed to finalize background run", "err", err)
}
}
func buildExecEnv(ctx *context.Context, cmd *cobra.Command, e *executable.Executable) map[string]string {
envMap := make(map[string]string)
if wsData, err := ctx.WorkspacesCache.GetWorkspaceConfigList(); err != nil {
logger.Log().Errorf("failed to get workspace cache data, skipping env file resolution: %v", err)
} else {
if wsCfg := wsData.FindByName(e.Workspace()); wsCfg == nil {
logger.Log().Warnf("workspace %s not found in cache, skipping env file resolution", e.Workspace())
} else {
applyWorkspaceParameterOverrides(wsCfg, envMap)
}
}
paramOverrides := flags.ValueFor[[]string](cmd, *flags.ParameterValueFlag, false)
applyParameterOverrides(paramOverrides, envMap)
textInputs := pendingFormFields(ctx, e, envMap)
if len(textInputs) > 0 {
form, err := views.NewForm(logger.Theme(ctx.Config.Theme.String()), ctx.StdIn(), ctx.StdOut(), textInputs...)
if err != nil {
logger.Log().FatalErr(err)
}
if err := form.Run(ctx); err != nil {
logger.Log().FatalErr(err)
}
for key, val := range form.ValueMap() {
envMap[key] = fmt.Sprintf("%v", val)
}
}
return envMap
}
func cleanupProcessStore(ctx *context.Context) {
if ctx.DataStore != nil {
if err := ctx.DataStore.DeleteProcessBucket(store.EnvironmentBucket()); err != nil {
logger.Log().Errorf("failed clearing process store\n%v", err)
}
}
}
func recordExecution(ctx *context.Context, ref executable.Ref, startTime time.Time, dur time.Duration, runErr error) {
record := store.ExecutionRecord{
Ref: ref.String(),
StartedAt: startTime,
Duration: dur,
}
if runErr != nil {
record.ExitCode = 1
record.Error = runErr.Error()
}
record.LogArchiveID = findArchiveByID(ctx.LogArchiveID)
if ctx.DataStore != nil {
if recErr := ctx.DataStore.RecordExecution(record); recErr != nil {
logger.Log().Debug("failed to record execution history", "err", recErr)
}
}
}
// findArchiveByID searches log archive entries for one matching the given ID.
// Returns the entry's path if found, empty string otherwise.
func findArchiveByID(archiveID string) string {
if archiveID == "" {
return ""
}
entries, err := tuikitIO.ListArchiveEntries(filesystem.LogsDir())
if err != nil {
return ""
}
for _, e := range entries {
if e.ID == archiveID {
return e.Path
}
}
return ""
}
func sendCompletionNotifications(ctx *context.Context, cmd *cobra.Command, dur time.Duration) {
if !TUIEnabled(ctx, cmd) || dur <= 1*time.Minute {
return
}
if ctx.Config.SendSoundNotification() {
_ = beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
}
if ctx.Config.SendTextNotification() {
_ = beeep.Notify("Flow", "Flow completed", "")
}
}
func runByRef(ctx *context.Context, cmd *cobra.Command, argsStr string) error {
s := strings.Split(argsStr, " ")
if len(s) != 2 {
return fmt.Errorf("invalid reference string %s", argsStr)
}
verbStr := s[0]
verb := executable.Verb(verbStr)
id := s[1]
cmds := cmd.Root().Commands()
var execCmd *cobra.Command
for _, c := range cmds {
if c.Name() == "exec" {
execCmd = c
break
}
}
if execCmd == nil {
return errors.New("exec command not found")
}
execCmd.SetArgs([]string{verbStr, id})
execCmd.SetOut(ctx.StdOut())
execCmd.SetErr(ctx.StdOut())
execCmd.SetIn(ctx.StdIn())
execPreRun(ctx, execCmd, []string{id})
execFunc(ctx, execCmd, verb, []string{id})
ctx.Cancel()
return nil
}
//nolint:gocognit
func pendingFormFields(
ctx *context.Context, rootExec *executable.Executable, envMap map[string]string,
) []*views.FormField {
pending := make([]*views.FormField, 0)
switch {
case rootExec.Exec != nil:
for _, param := range rootExec.Exec.Params {
_, exists := envMap[param.EnvKey]
if param.Prompt != "" && !exists {
pending = append(pending, &views.FormField{Key: param.EnvKey, Title: param.Prompt})
}
}
case rootExec.Launch != nil:
for _, param := range rootExec.Launch.Params {
_, exists := envMap[param.EnvKey]
if param.Prompt != "" && !exists {
pending = append(pending, &views.FormField{Key: param.EnvKey, Title: param.Prompt})
}
}
case rootExec.Request != nil:
for _, param := range rootExec.Request.Params {
_, exists := envMap[param.EnvKey]
if param.Prompt != "" && !exists {
pending = append(pending, &views.FormField{Key: param.EnvKey, Title: param.Prompt})
}
}
case rootExec.Render != nil:
for _, param := range rootExec.Render.Params {
_, exists := envMap[param.EnvKey]
if param.Prompt != "" && !exists {
pending = append(pending, &views.FormField{Key: param.EnvKey, Title: param.Prompt})
}
}
case rootExec.Serial != nil:
for _, param := range rootExec.Serial.Params {
_, exists := envMap[param.EnvKey]
if param.Prompt != "" && !exists {
pending = append(pending, &views.FormField{Key: param.EnvKey, Title: param.Prompt})
}
}
for _, child := range rootExec.Serial.Execs {
if child.Ref != "" {
childExec, err := ctx.ExecutableCache.GetExecutableByRef(child.Ref)
if err != nil {
continue
}
childPending := pendingFormFields(ctx, childExec, envMap)
pending = append(pending, childPending...)
}
}
case rootExec.Parallel != nil:
for _, param := range rootExec.Parallel.Params {
if param.Prompt != "" {
pending = append(pending, &views.FormField{Key: param.EnvKey, Title: param.Prompt})
}
}
for _, child := range rootExec.Parallel.Execs {
if child.Ref != "" {
childExec, err := ctx.ExecutableCache.GetExecutableByRef(child.Ref)
if err != nil {
continue
}
childPending := pendingFormFields(ctx, childExec, envMap)
pending = append(pending, childPending...)
}
}
}
return pending
}
//nolint:nestif
func applyWorkspaceParameterOverrides(ws *workspace.Workspace, envMap map[string]string) {
if len(ws.EnvFiles) > 0 {
loaded, err := env.LoadEnvFromFiles(ws.EnvFiles, ws.Location())
if err != nil {
logger.Log().Errorf("failed loading env files for workspace %s: %v", ws.AssignedName(), err)
}
for k, v := range loaded {
envMap[k] = v
}
} else {
rootEnvFile := filepath.Join(ws.Location(), ".env")
if _, err := os.Stat(rootEnvFile); err == nil {
loaded, err := env.LoadEnvFromFiles([]string{rootEnvFile}, ws.Location())
if err != nil {
logger.Log().Errorf("failed loading root env file %s: %v", rootEnvFile, err)
} else {
for k, v := range loaded {
envMap[k] = v
}
}
}
}
}
func applyParameterOverrides(overrides []string, envMap map[string]string) {
for _, override := range overrides {
parts := strings.SplitN(override, "=", 2)
if len(parts) != 2 {
continue // skip invalid overrides
}
key, value := parts[0], parts[1]
envMap[key] = value
}
}
var (
//nolint:lll
execDocumentation = `
Execute an executable where EXECUTABLE_ID is the target executable's ID in the form of 'ws/ns:name'.
The flow subcommand used should match the target executable's verb or one of its aliases.
If the target executable accept arguments, they can be passed in the form of flag or positional arguments.
Flag arguments are specified with the format 'flag=value' and positional arguments are specified as values without any prefix.
`
execExamples = `
#### Examples
**Execute a nameless flow in the current workspace with the 'install' verb**
flow install
**Execute a nameless flow in the 'ws' workspace with the 'test' verb**
flow test ws/
**Execute the 'build' flow in the current workspace and namespace**
flow exec build
flow run build (Equivalent to the above since 'run' is an alias for the 'exec' verb)
**Execute the 'docs' flow with the 'show' verb in the current workspace and namespace**
flow show docs
**Execute the 'build' flow in the 'ws' workspace and 'ns' namespace**
flow exec ws/ns:build
**Execute the 'build' flow in the 'ws' workspace and 'ns' namespace with flag and positional arguments**
flow exec ws/ns:build flag1=value1 flag2=value2 value3 value4
`
)