forked from tilt-dev/tilt-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilt-restart-wrapper.go
94 lines (84 loc) · 3.6 KB
/
tilt-restart-wrapper.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
// `tilt-restart-wrapper` wraps `entr` (http://eradman.com/entrproject/) to easily
// rerun a user-specified command when a given file changes.
//
// This is Tilt's recommended way of restarting a process as part of a live_update:
// if your container invokes your app via the restart wrapper (e.g. `tilt-restart-wrapper /bin/my-app`),
// you can trigger re-execution of your app with a live_update `run` step that makes
// a trivial change to the file watched by `entr` (e.g. `run('date > /.restart-proc')`)
//
// This script exists (i.e. we're wrapping `entr` in a binary instead of invoking
// it directly) because in its canonical invocation, `entr` requires that the
// file(s) to watch be piped via stdin, i.e. it is invoked like:
// echo "/.restart-proc" | entr -rz /bin/my-app
//
// When specified as a `command` in Kubernetes or Docker Compose YAML (this is how
// Tilt overrides entrypoints), the above would therefore need to be executed as shell:
// /bin/sh -c 'echo "/.restart-proc" | entr -rz /bin/my-app'
// Any args specified in Kubernetes or Docker Compose YAML are attached to the end
// of this call, and therefore in this case apply TO THE `/bin/sh -c` CALL, rather
// than to the actual command run by `entr`; that is, any `args` specified by the
// user would be effectively ignored.
//
// In order to make `entr` executable as ARGV rather than as shell, we have wrapped it
// in a binary that can be called directly and takes care of the piping under the hood.
//
// Note: ideally `entr` could accept files-to-watch via flag instead of stdin,
// but (for a number of good reasons) this feature isn't likely to be added any
// time soon (see https://github.com/eradman/entr/issues/33).
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
)
var watchFile = flag.String("watch_file", "/.restart-proc", "File that entr will watch for changes; changes to this file trigger `entr` to rerun the command(s) passed")
var entrPath = flag.String("entr_path", "/entr", "Path to `entr` executable")
var entrFlags = flag.String("entr_flags", "-rz", "Command line flags to pass to `entr` executable")
var verboseSignals = flag.Bool("verbose_signals", false, "Print signals received by `tilt-restart-wrapper`")
func main() {
flag.Parse()
cmd := exec.Command(*entrPath, *entrFlags)
cmd.Stdin = strings.NewReader(fmt.Sprintf("%s\n", *watchFile))
cmd.Args = append(cmd.Args, flag.Args()...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
log.Fatalf("error starting command: %v", err)
}
// Set up a signal forwarding handler
sigs := make(chan os.Signal, 10)
signal.Notify(sigs)
go func() {
for sig := range sigs {
if *verboseSignals {
log.Printf("Received signal: %s (%d), forwarding to pid %d", sig, sig.(syscall.Signal), cmd.Process.Pid)
}
if err := cmd.Process.Signal(sig); err != nil {
log.Println("Error forwarding signal:", err)
}
}
}()
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
if len(flag.Args()) == 0 {
log.Println("`tilt-restart-wrapper` requires at least one positional arg " +
"(a command or set of args to be executed / rerun whenever `watch_file` changes)")
}
os.Exit(status.ExitStatus())
}
} else {
log.Fatalf("error running command: %v", err)
}
}
if len(flag.Args()) == 0 {
log.Fatal("`tilt-restart-wrapper` requires at least one positional arg " +
"(will be passed to `entr` and executed / rerun whenever `watch_file` changes)")
}
}