- internal/hooks: Runner finds <mount>/.devbox/hooks/<event>, runs via bash (or .ps1->pwsh), injects DEVBOX_EVENT/MOUNT/SHARE/HOST/REMOTE/SNAPSHOT/CHANGED_FILES, 60s timeout (WaitDelay bounds post-kill cleanup), pre-* non-zero exit = veto - syncer fires pre-push/post-push (in Push) + pre-pull/post-pull/on-conflict (in Pull); pre-* veto -> PullResult/Result.Vetoed -> Sync skips, waits for next nudge - daemon builds a Runner per mount; cmd mount/publish pass nil (daemon drives hooks) - tests: env+changed-files injection, absent=no-op, pre-* veto, 60s timeout kill, post-pull fires with changed list, pre-push vetoes (head not advanced)
133 lines
3.6 KiB
Go
133 lines
3.6 KiB
Go
// Package hooks runs user lifecycle scripts from a mount's .devbox/hooks/ dir on
|
|
// sync events. A script runs via bash (or pwsh for a .ps1 hook) with the sync
|
|
// context injected as env vars; a pre-* hook's non-zero exit vetoes that step.
|
|
package hooks
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// DefaultTimeout bounds a hook; a hung hook is killed, never wedging the sync loop.
|
|
const DefaultTimeout = 60 * time.Second
|
|
|
|
// Event names (also the hook script filenames).
|
|
const (
|
|
PrePush = "pre-push"
|
|
PostPush = "post-push"
|
|
PrePull = "pre-pull"
|
|
PostPull = "post-pull"
|
|
OnConflict = "on-conflict"
|
|
)
|
|
|
|
// Runner runs hooks for one mount.
|
|
type Runner struct {
|
|
Root string // mount local root; hooks live at Root/.devbox/hooks/
|
|
Share string
|
|
Host string
|
|
Remote string
|
|
Timeout time.Duration
|
|
Stdout io.Writer // hook stdout (defaults to os.Stdout)
|
|
Stderr io.Writer // hook stderr (defaults to os.Stderr)
|
|
}
|
|
|
|
// New returns a Runner for a mount with the default timeout.
|
|
func New(root, share, host, remote string) *Runner {
|
|
return &Runner{Root: root, Share: share, Host: host, Remote: remote, Timeout: DefaultTimeout}
|
|
}
|
|
|
|
// Run executes the hook for event if a matching executable script exists. The
|
|
// changed file list is written to a temp file referenced by DEVBOX_CHANGED_FILES.
|
|
// Returns nil if no hook exists; otherwise the hook's error (non-nil from a pre-*
|
|
// hook means the caller should veto that step). Nil receiver is a no-op.
|
|
func (r *Runner) Run(event string, changed []string, snapshot string) error {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
script, interp, ok := r.find(event)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
tmp, err := os.CreateTemp("", "devbox-changes-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(tmp.Name())
|
|
if len(changed) > 0 {
|
|
_, _ = tmp.WriteString(strings.Join(changed, "\n") + "\n")
|
|
}
|
|
tmp.Close()
|
|
|
|
timeout := r.Timeout
|
|
if timeout <= 0 {
|
|
timeout = DefaultTimeout
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, interp, script)
|
|
cmd.Dir = r.Root
|
|
cmd.Env = append(os.Environ(),
|
|
"DEVBOX_EVENT="+event,
|
|
"DEVBOX_MOUNT="+r.Root,
|
|
"DEVBOX_SHARE="+r.Share,
|
|
"DEVBOX_HOST="+r.Host,
|
|
"DEVBOX_REMOTE="+r.Remote,
|
|
"DEVBOX_SNAPSHOT="+snapshot,
|
|
"DEVBOX_CHANGED_FILES="+tmp.Name(),
|
|
)
|
|
cmd.Stdout = orElse(r.Stdout, os.Stdout)
|
|
cmd.Stderr = orElse(r.Stderr, os.Stderr)
|
|
// ponytail: after a timeout kill, bound how long Wait blocks on a child that
|
|
// inherited the output pipe (e.g. a `sleep`). Killing the whole process group
|
|
// is the upgrade path if orphaned grandchildren become a problem.
|
|
cmd.WaitDelay = 2 * time.Second
|
|
|
|
runErr := cmd.Run()
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return fmt.Errorf("hook %s timed out after %s", event, timeout)
|
|
}
|
|
if runErr != nil {
|
|
return fmt.Errorf("hook %s: %w", event, runErr)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// find locates the hook script + interpreter: an executable <event>.ps1 (pwsh)
|
|
// or an executable <event> (bash).
|
|
func (r *Runner) find(event string) (script, interp string, ok bool) {
|
|
dir := filepath.Join(r.Root, ".devbox", "hooks")
|
|
if ps := filepath.Join(dir, event+".ps1"); executable(ps) {
|
|
return ps, "pwsh", true
|
|
}
|
|
if base := filepath.Join(dir, event); executable(base) {
|
|
return base, "bash", true
|
|
}
|
|
return "", "", false
|
|
}
|
|
|
|
func executable(path string) bool {
|
|
info, err := os.Stat(path)
|
|
if err != nil || info.IsDir() {
|
|
return false
|
|
}
|
|
if runtime.GOOS == "windows" {
|
|
return true // no exec bit on Windows
|
|
}
|
|
return info.Mode().Perm()&0o111 != 0
|
|
}
|
|
|
|
func orElse(w, def io.Writer) io.Writer {
|
|
if w == nil {
|
|
return def
|
|
}
|
|
return w
|
|
}
|