package launcher import ( "context" "fmt" "os" "os/exec" "runtime" "runtime/debug" "strings" "syscall" "time" v1 "github.com/google/go-containerregistry/pkg/v1" ) type process struct { cmd *exec.Cmd done chan struct{} err error } // StartProcess runs path with the launcher's environment and output. func StartProcess(path string) (Process, error) { cmd := exec.Command(path) cmd.Stdout, cmd.Stderr, cmd.Env = os.Stdout, os.Stderr, os.Environ() if err := cmd.Start(); err != nil { return nil, err } p := &process{cmd: cmd, done: make(chan struct{})} go func() { p.err = cmd.Wait() close(p.done) }() return p, nil } // Done is closed when the agent has exited. func (p *process) Done() <-chan struct{} { return p.done } // Err is the agent's exit error. func (p *process) Err() error { return p.err } // Stop sends SIGTERM and kills the agent after timeout. func (p *process) Stop(timeout time.Duration) { _ = p.cmd.Process.Signal(syscall.SIGTERM) select { case <-p.done: case <-time.After(timeout): _ = p.cmd.Process.Kill() <-p.done } } // BinaryVersion asks an agent binary which release it is. func BinaryVersion(path string) (string, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() out, err := exec.CommandContext(ctx, path, "version").Output() if err != nil { return "", err } v := strings.TrimSpace(string(out)) if v == "" || strings.ContainsAny(v, " \n") { return "", fmt.Errorf("launcher: %s reports no release", path) } return v, nil } // Platform is the platform this launcher was built for, and so the agent // binaries it runs. func Platform() v1.Platform { p := v1.Platform{OS: runtime.GOOS, Architecture: runtime.GOARCH} if runtime.GOARCH != "arm" { return p } p.Variant = "v7" // Go's default for linux/arm if info, ok := debug.ReadBuildInfo(); ok { for _, s := range info.Settings { if s.Key == "GOARM" && s.Value != "" { p.Variant = "v" + s.Value[:1] } } } return p }