|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "log" |
| 24 | + "os" |
| 25 | + "os/exec" |
| 26 | + "os/signal" |
| 27 | + "strings" |
| 28 | + "syscall" |
| 29 | + |
| 30 | + "github.com/pkg/errors" |
| 31 | +) |
| 32 | + |
| 33 | +var ( |
| 34 | + logFilePath = flag.String("log-file", "", "If non-empty, save stdout to this file") |
| 35 | + alsoToStdOut = flag.Bool("also-stdout", false, "useful with log-file, log to standard output as well as the log file") |
| 36 | + redirectStderr = flag.Bool("redirect-stderr", true, "treat stderr same as stdout") |
| 37 | +) |
| 38 | + |
| 39 | +func main() { |
| 40 | + flag.Parse() |
| 41 | + |
| 42 | + if err := configureAndRun(); err != nil { |
| 43 | + log.Fatal(err) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func configureAndRun() error { |
| 48 | + var ( |
| 49 | + outputStream io.Writer = os.Stdout |
| 50 | + errStream io.Writer = os.Stderr |
| 51 | + ) |
| 52 | + |
| 53 | + args := flag.Args() |
| 54 | + if len(args) == 0 { |
| 55 | + return errors.Errorf("not enough arguments to run") |
| 56 | + } |
| 57 | + |
| 58 | + if logFilePath != nil && *logFilePath != "" { |
| 59 | + logFile, err := os.OpenFile(*logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 60 | + if err != nil { |
| 61 | + return errors.Wrapf(err, "failed to create log file %v", *logFilePath) |
| 62 | + } |
| 63 | + if *alsoToStdOut { |
| 64 | + outputStream = io.MultiWriter(os.Stdout, logFile) |
| 65 | + } else { |
| 66 | + outputStream = logFile |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if *redirectStderr { |
| 71 | + errStream = outputStream |
| 72 | + } |
| 73 | + |
| 74 | + exe := args[0] |
| 75 | + var exeArgs []string |
| 76 | + if len(args) > 1 { |
| 77 | + exeArgs = args[1:] |
| 78 | + } |
| 79 | + cmd := exec.Command(exe, exeArgs...) |
| 80 | + cmd.Stdout = outputStream |
| 81 | + cmd.Stderr = errStream |
| 82 | + |
| 83 | + log.Printf("Running command:\n%v", cmdInfo(cmd)) |
| 84 | + err := cmd.Start() |
| 85 | + if err != nil { |
| 86 | + return errors.Wrap(err, "starting command") |
| 87 | + } |
| 88 | + |
| 89 | + // Handle signals and shutdown process gracefully. |
| 90 | + go setupSigHandler(cmd.Process) |
| 91 | + return errors.Wrap(cmd.Wait(), "running command") |
| 92 | +} |
| 93 | + |
| 94 | +// cmdInfo generates a useful look at what the command is for printing/debug. |
| 95 | +func cmdInfo(cmd *exec.Cmd) string { |
| 96 | + return fmt.Sprintf( |
| 97 | + `Command env: (log-file=%v, also-stdout=%v, redirect-stderr=%v) |
| 98 | +Run from directory: %v |
| 99 | +Executable path: %v |
| 100 | +Args (comma-delimited): %v`, *logFilePath, *alsoToStdOut, *redirectStderr, |
| 101 | + cmd.Dir, cmd.Path, strings.Join(cmd.Args, ","), |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +// setupSigHandler will forward any termination signals to the process |
| 106 | +func setupSigHandler(process *os.Process) { |
| 107 | + // terminationSignals are signals that cause the program to exit in the |
| 108 | + // supported platforms (linux, darwin, windows). |
| 109 | + terminationSignals := []os.Signal{syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT} |
| 110 | + |
| 111 | + c := make(chan os.Signal, 1) |
| 112 | + signal.Notify(c, terminationSignals...) |
| 113 | + |
| 114 | + // Block until a signal is received. |
| 115 | + log.Println("Now listening for interrupts") |
| 116 | + s := <-c |
| 117 | + log.Printf("Got signal: %v. Sending down to process (PID: %v)", s, process.Pid) |
| 118 | + if err := process.Signal(s); err != nil { |
| 119 | + log.Fatalf("Failed to signal process: %v", err) |
| 120 | + } |
| 121 | + log.Printf("Signalled process %v successfully.", process.Pid) |
| 122 | +} |
0 commit comments