feat(M7): devbox doctor, stop, and hook edit/list 🩺⏹️🪝
- devbox doctor: checks identity/join, hub reachability + bearer acceptance, bash for hooks, fsnotify watcher (catches inotify exhaustion), and each mount's writability; exits non-zero on any failure - devbox stop + pidfile: start writes ~/.config/devbox/daemon.pid and refuses a second daemon; stop SIGTERMs it (liveness-checked, cleans stale pidfiles) - devbox hook edit <share> <event>: scaffolds an executable, documented bash sample then opens $EDITOR/$VISUAL; hook list shows installed vs available - hooks: AllEvents/IsEvent/Dir/Sample helpers
This commit is contained in:
parent
38766fdcd5
commit
bf40d209c1
3 changed files with 340 additions and 8 deletions
|
|
@ -4,9 +4,13 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -15,10 +19,13 @@ import (
|
||||||
|
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/config"
|
"git.shoemoney.ai/shoemoney/devbox/internal/config"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/daemon"
|
"git.shoemoney.ai/shoemoney/devbox/internal/daemon"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hooks"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/identity"
|
"git.shoemoney.ai/shoemoney/devbox/internal/identity"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/syncer"
|
"git.shoemoney.ai/shoemoney/devbox/internal/syncer"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/transport"
|
"git.shoemoney.ai/shoemoney/devbox/internal/transport"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/watch"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/pkg/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "0.0.0-dev"
|
var version = "0.0.0-dev"
|
||||||
|
|
@ -38,10 +45,12 @@ func main() {
|
||||||
logCmd(),
|
logCmd(),
|
||||||
restoreCmd(),
|
restoreCmd(),
|
||||||
deployCmd(),
|
deployCmd(),
|
||||||
|
hookCmd(),
|
||||||
startCmd(),
|
startCmd(),
|
||||||
|
stopCmd(),
|
||||||
statusCmd(),
|
statusCmd(),
|
||||||
|
doctorCmd(),
|
||||||
versionCmd(),
|
versionCmd(),
|
||||||
stub("stop", "⏹️ stop the sync daemon", "M7"),
|
|
||||||
)
|
)
|
||||||
if err := root.Execute(); err != nil {
|
if err := root.Execute(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "error:", err)
|
fmt.Fprintln(os.Stderr, "error:", err)
|
||||||
|
|
@ -465,6 +474,13 @@ func startCmd() *cobra.Command {
|
||||||
if len(cfg.Mounts) == 0 {
|
if len(cfg.Mounts) == 0 {
|
||||||
return fmt.Errorf("no mounts — run: devbox mount <share> <dir>")
|
return fmt.Errorf("no mounts — run: devbox mount <share> <dir>")
|
||||||
}
|
}
|
||||||
|
if pid, ok := runningPid(dir); ok {
|
||||||
|
return fmt.Errorf("daemon already running (pid %d) — run: devbox stop", pid)
|
||||||
|
}
|
||||||
|
if err := writePid(dir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer removePid(dir)
|
||||||
host, _ := os.Hostname()
|
host, _ := os.Hostname()
|
||||||
dmn, err := daemon.New(dir, cfg, host, nil)
|
dmn, err := daemon.New(dir, cfg, host, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -529,12 +545,296 @@ func short(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func stub(use, short, milestone string) *cobra.Command {
|
func stopCmd() *cobra.Command {
|
||||||
return &cobra.Command{
|
return &cobra.Command{
|
||||||
Use: use,
|
Use: "stop",
|
||||||
Short: short,
|
Short: "⏹️ stop the running sync daemon",
|
||||||
RunE: func(*cobra.Command, []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
return fmt.Errorf("%q is not implemented yet (arrives in %s)", use, milestone)
|
dir, err := config.Dir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
pid, ok := runningPid(dir)
|
||||||
|
if !ok {
|
||||||
|
removePid(dir) // clean up a stale pidfile if present
|
||||||
|
return fmt.Errorf("no running daemon")
|
||||||
|
}
|
||||||
|
p, err := os.FindProcess(pid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := p.Signal(syscall.SIGTERM); err != nil {
|
||||||
|
return fmt.Errorf("signaling daemon (pid %d): %w", pid, err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(cmd.OutOrStdout(), "⏹️ sent stop to daemon (pid %d)\n", pid)
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- pidfile: lets `devbox stop` find a running `devbox start` ---
|
||||||
|
|
||||||
|
func pidPath(dir string) string { return filepath.Join(dir, "daemon.pid") }
|
||||||
|
|
||||||
|
func writePid(dir string) error {
|
||||||
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.WriteFile(pidPath(dir), []byte(strconv.Itoa(os.Getpid())), 0o600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func removePid(dir string) { _ = os.Remove(pidPath(dir)) }
|
||||||
|
|
||||||
|
// runningPid reports the pid in the pidfile, and whether that process is alive.
|
||||||
|
// A stale pidfile (process gone) returns ok=false.
|
||||||
|
func runningPid(dir string) (int, bool) {
|
||||||
|
b, err := os.ReadFile(pidPath(dir))
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
pid, err := strconv.Atoi(strings.TrimSpace(string(b)))
|
||||||
|
if err != nil || pid <= 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
p, err := os.FindProcess(pid)
|
||||||
|
if err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
// On unix signal 0 tests liveness without delivering a signal.
|
||||||
|
if err := p.Signal(syscall.Signal(0)); err != nil {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
return pid, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func doctorCmd() *cobra.Command {
|
||||||
|
return &cobra.Command{
|
||||||
|
Use: "doctor",
|
||||||
|
Short: "🩺 diagnose this device's devbox setup (config, hub, mounts, watcher)",
|
||||||
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
|
out := cmd.OutOrStdout()
|
||||||
|
dir, err := config.Dir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var failed bool
|
||||||
|
check := func(ok bool, warn bool, name, detail string) {
|
||||||
|
glyph := "✅"
|
||||||
|
if !ok {
|
||||||
|
if warn {
|
||||||
|
glyph = "⚠️ "
|
||||||
|
} else {
|
||||||
|
glyph = "❌"
|
||||||
|
failed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if detail != "" {
|
||||||
|
fmt.Fprintf(out, "%s %s — %s\n", glyph, name, detail)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(out, "%s %s\n", glyph, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(out, "devbox %s · %s/%s\n", version, runtime.GOOS, runtime.GOARCH)
|
||||||
|
fmt.Fprintf(out, "config: %s\n\n", dir)
|
||||||
|
|
||||||
|
// Identity + join state.
|
||||||
|
if _, err := identity.Load(dir); err != nil {
|
||||||
|
check(false, false, "identity", "not joined — run: devbox join <hub> <token>")
|
||||||
|
return fmt.Errorf("device is not joined")
|
||||||
|
}
|
||||||
|
check(true, false, "identity", "device keypair present")
|
||||||
|
|
||||||
|
d, err := config.LoadDaemon(dir)
|
||||||
|
if err != nil {
|
||||||
|
check(false, false, "config", err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
check(d.Hub != "", false, "hub configured", d.Hub)
|
||||||
|
check(d.Bearer != "", false, "bearer token", "present")
|
||||||
|
|
||||||
|
// Hub reachability (unauth) + bearer validity (authed).
|
||||||
|
if d.Hub != "" {
|
||||||
|
hc := &http.Client{Timeout: 5 * time.Second}
|
||||||
|
resp, err := hc.Get(strings.TrimRight(d.Hub, "/") + proto.PathMetrics)
|
||||||
|
if err != nil {
|
||||||
|
check(false, false, "hub reachable", err.Error())
|
||||||
|
} else {
|
||||||
|
resp.Body.Close()
|
||||||
|
check(true, false, "hub reachable", d.Hub+proto.PathMetrics)
|
||||||
|
c := transport.New(d.Hub)
|
||||||
|
c.SetBearer(d.Bearer)
|
||||||
|
if _, err := c.Have(nil); err != nil {
|
||||||
|
check(false, false, "bearer accepted", err.Error())
|
||||||
|
} else {
|
||||||
|
check(true, false, "bearer accepted", "hub authorized this device")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// bash — required for lifecycle hooks.
|
||||||
|
if _, err := exec.LookPath("bash"); err != nil {
|
||||||
|
check(false, true, "bash", "not found — lifecycle hooks won't run")
|
||||||
|
} else {
|
||||||
|
check(true, false, "bash", "available for hooks")
|
||||||
|
}
|
||||||
|
|
||||||
|
// fsnotify watcher (catches inotify exhaustion on Linux).
|
||||||
|
probe := filepath.Join(os.TempDir(), "devbox-doctor-watch")
|
||||||
|
_ = os.MkdirAll(probe, 0o755)
|
||||||
|
if w, err := watch.New(probe, 100*time.Millisecond); err != nil {
|
||||||
|
check(false, false, "file watcher", err.Error())
|
||||||
|
} else {
|
||||||
|
w.Close()
|
||||||
|
check(true, false, "file watcher", "fsnotify ok")
|
||||||
|
}
|
||||||
|
_ = os.RemoveAll(probe)
|
||||||
|
|
||||||
|
// Mounts: dir exists + writable.
|
||||||
|
check(len(d.Mounts) > 0, len(d.Mounts) == 0, "mounts", fmt.Sprintf("%d configured", len(d.Mounts)))
|
||||||
|
for _, m := range d.Mounts {
|
||||||
|
label := fmt.Sprintf("mount %s -> %s", m.Share, m.Local)
|
||||||
|
if writable(m.Local) {
|
||||||
|
mode := "rw"
|
||||||
|
if m.ReadOnly {
|
||||||
|
mode = "ro"
|
||||||
|
}
|
||||||
|
if m.Pinned {
|
||||||
|
mode += ",pinned"
|
||||||
|
}
|
||||||
|
check(true, false, label, mode)
|
||||||
|
} else {
|
||||||
|
check(false, false, label, "local dir missing or not writable")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintln(out)
|
||||||
|
if failed {
|
||||||
|
return fmt.Errorf("doctor found problems (see ❌ above)")
|
||||||
|
}
|
||||||
|
fmt.Fprintln(out, "🎉 all checks passed")
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// writable reports whether dir exists and we can create a file in it.
|
||||||
|
func writable(dir string) bool {
|
||||||
|
info, err := os.Stat(dir)
|
||||||
|
if err != nil || !info.IsDir() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
f, err := os.CreateTemp(dir, ".devbox-doctor-*")
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
name := f.Name()
|
||||||
|
f.Close()
|
||||||
|
_ = os.Remove(name)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func hookCmd() *cobra.Command {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "hook",
|
||||||
|
Short: "🪝 manage a mount's lifecycle hooks",
|
||||||
|
}
|
||||||
|
cmd.AddCommand(hookEditCmd(), hookListCmd())
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func hookEditCmd() *cobra.Command {
|
||||||
|
return &cobra.Command{
|
||||||
|
Use: "edit <share> <event>",
|
||||||
|
Short: "✏️ create/edit a hook script (events: " + strings.Join(hooks.AllEvents(), ", ") + ")",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
share, event := args[0], args[1]
|
||||||
|
if !hooks.IsEvent(event) {
|
||||||
|
return fmt.Errorf("unknown event %q (events: %s)", event, strings.Join(hooks.AllEvents(), ", "))
|
||||||
|
}
|
||||||
|
dir, err := config.Dir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d, err := config.LoadDaemon(dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m, ok := findMount(d.Mounts, share)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no mount for share %q — mount it first", share)
|
||||||
|
}
|
||||||
|
hookDir := hooks.Dir(m.Local)
|
||||||
|
if err := os.MkdirAll(hookDir, 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
path := filepath.Join(hookDir, event)
|
||||||
|
out := cmd.OutOrStdout()
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
if err := os.WriteFile(path, []byte(hooks.Sample(event)), 0o755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(out, "🆕 created %s\n", path)
|
||||||
|
}
|
||||||
|
_ = os.Chmod(path, 0o755) // ensure executable so the runner picks it up
|
||||||
|
|
||||||
|
editor := firstNonEmpty(os.Getenv("VISUAL"), os.Getenv("EDITOR"))
|
||||||
|
if editor == "" {
|
||||||
|
fmt.Fprintf(out, "✏️ edit it: %s (set $EDITOR to open automatically)\n", path)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ed := exec.Command(editor, path)
|
||||||
|
ed.Stdin, ed.Stdout, ed.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||||
|
return ed.Run()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hookListCmd() *cobra.Command {
|
||||||
|
return &cobra.Command{
|
||||||
|
Use: "list <share>",
|
||||||
|
Short: "📋 list installed + available hooks for a share's mount",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
share := args[0]
|
||||||
|
dir, err := config.Dir()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d, err := config.LoadDaemon(dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m, ok := findMount(d.Mounts, share)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no mount for share %q", share)
|
||||||
|
}
|
||||||
|
out := cmd.OutOrStdout()
|
||||||
|
hookDir := hooks.Dir(m.Local)
|
||||||
|
fmt.Fprintf(out, "hooks dir: %s\n", hookDir)
|
||||||
|
for _, e := range hooks.AllEvents() {
|
||||||
|
glyph := "·"
|
||||||
|
if isExecutable(filepath.Join(hookDir, e)) || isExecutable(filepath.Join(hookDir, e+".ps1")) {
|
||||||
|
glyph = "✅"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(out, " %s %s\n", glyph, e)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isExecutable(path string) bool {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
return err == nil && !info.IsDir() && info.Mode().Perm()&0o111 != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func firstNonEmpty(vals ...string) string {
|
||||||
|
for _, v := range vals {
|
||||||
|
if v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ func TestNextBackoffDoublesAndCaps(t *testing.T) {
|
||||||
func TestJitteredStaysWithinBand(t *testing.T) {
|
func TestJitteredStaysWithinBand(t *testing.T) {
|
||||||
const d = 8 * time.Second
|
const d = 8 * time.Second
|
||||||
lo, hi := d-d/4, d+d/4 // ±25%
|
lo, hi := d-d/4, d+d/4 // ±25%
|
||||||
for i := 0; i < 1000; i++ {
|
for range 1000 {
|
||||||
j := jittered(d)
|
j := jittered(d)
|
||||||
if j < lo || j >= hi {
|
if j < lo || j >= hi {
|
||||||
t.Fatalf("jittered(%s) = %s, want in [%s,%s)", d, j, lo, hi)
|
t.Fatalf("jittered(%s) = %s, want in [%s,%s)", d, j, lo, hi)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -27,6 +28,37 @@ const (
|
||||||
OnConflict = "on-conflict"
|
OnConflict = "on-conflict"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AllEvents lists the supported hook events (also the script filenames), in
|
||||||
|
// lifecycle order. pre-* events can veto their step by exiting non-zero.
|
||||||
|
func AllEvents() []string {
|
||||||
|
return []string{PrePush, PostPush, PrePull, PostPull, OnConflict}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEvent reports whether name is a supported hook event.
|
||||||
|
func IsEvent(name string) bool { return slices.Contains(AllEvents(), name) }
|
||||||
|
|
||||||
|
// Dir returns a mount's hooks directory: root/.devbox/hooks.
|
||||||
|
func Dir(root string) string { return filepath.Join(root, ".devbox", "hooks") }
|
||||||
|
|
||||||
|
// Sample returns a commented, ready-to-edit bash template for an event hook,
|
||||||
|
// documenting the env vars devbox injects.
|
||||||
|
func Sample(event string) string {
|
||||||
|
return "#!/usr/bin/env bash\n" +
|
||||||
|
"# devbox " + event + " hook — runs on every " + event + ".\n" +
|
||||||
|
"# Injected environment:\n" +
|
||||||
|
"# DEVBOX_EVENT the event name (" + event + ")\n" +
|
||||||
|
"# DEVBOX_MOUNT this mount's local root\n" +
|
||||||
|
"# DEVBOX_SHARE the share name\n" +
|
||||||
|
"# DEVBOX_HOST this device's hostname\n" +
|
||||||
|
"# DEVBOX_REMOTE the hub URL\n" +
|
||||||
|
"# DEVBOX_SNAPSHOT the snapshot id involved\n" +
|
||||||
|
"# DEVBOX_CHANGED_FILES path to a newline-delimited list of changed files\n" +
|
||||||
|
"#\n" +
|
||||||
|
"# A non-zero exit from a pre-* hook VETOES that sync step.\n" +
|
||||||
|
"set -euo pipefail\n\n" +
|
||||||
|
"echo \"devbox " + event + ": $DEVBOX_SHARE @ $DEVBOX_SNAPSHOT\"\n"
|
||||||
|
}
|
||||||
|
|
||||||
// Runner runs hooks for one mount.
|
// Runner runs hooks for one mount.
|
||||||
type Runner struct {
|
type Runner struct {
|
||||||
Root string // mount local root; hooks live at Root/.devbox/hooks/
|
Root string // mount local root; hooks live at Root/.devbox/hooks/
|
||||||
|
|
@ -104,7 +136,7 @@ func (r *Runner) Run(event string, changed []string, snapshot string) error {
|
||||||
// find locates the hook script + interpreter: an executable <event>.ps1 (pwsh)
|
// find locates the hook script + interpreter: an executable <event>.ps1 (pwsh)
|
||||||
// or an executable <event> (bash).
|
// or an executable <event> (bash).
|
||||||
func (r *Runner) find(event string) (script, interp string, ok bool) {
|
func (r *Runner) find(event string) (script, interp string, ok bool) {
|
||||||
dir := filepath.Join(r.Root, ".devbox", "hooks")
|
dir := Dir(r.Root)
|
||||||
if ps := filepath.Join(dir, event+".ps1"); executable(ps) {
|
if ps := filepath.Join(dir, event+".ps1"); executable(ps) {
|
||||||
return ps, "pwsh", true
|
return ps, "pwsh", true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue