feat: run multiple init programs
- We log their outputs and status codes - Windows is supported as well, leveraging cmd.exe /c Co-authored-by: pancho horrillo <pancho.horrillo@bbva.com>
This commit is contained in:
committed by
pancho horrillo
parent
989bf2ed66
commit
0c16b5472f
@@ -0,0 +1,11 @@
|
||||
// +build !windows
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func BuildCmd(path string) *exec.Cmd {
|
||||
return exec.Command(path)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func BuildCmd(path string) *exec.Cmd {
|
||||
return exec.Command("cmd.exe", "/c", path)
|
||||
}
|
||||
+45
-18
@@ -17,9 +17,11 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -57,23 +59,8 @@ var ServerCmd = &cobra.Command{
|
||||
|
||||
server.StartServer(sConf)
|
||||
|
||||
if len(args) > 0 {
|
||||
powfile := args[0]
|
||||
_, err := os.Stat(powfile)
|
||||
if os.IsNotExist(err) {
|
||||
logger.L.Fatalf("%s does not exist", powfile)
|
||||
}
|
||||
logger.L.Printf("Running powfile: %q\n", powfile)
|
||||
kapowCMD := exec.Command("bash", powfile)
|
||||
kapowCMD.Stdout = os.Stdout
|
||||
kapowCMD.Stderr = os.Stderr
|
||||
kapowCMD.Env = os.Environ()
|
||||
|
||||
err = kapowCMD.Run()
|
||||
if err != nil {
|
||||
logger.L.Fatal(err)
|
||||
}
|
||||
logger.L.Printf("Done running powfile: %q\n", powfile)
|
||||
for _, path := range args {
|
||||
go Run(path)
|
||||
}
|
||||
|
||||
select {}
|
||||
@@ -112,3 +99,43 @@ func validateServerCommandArguments(cmd *cobra.Command, args []string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Run(path string) {
|
||||
logger.L.Printf("Running init program %+q", path)
|
||||
cmd := BuildCmd(path)
|
||||
cmd.Env = os.Environ()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
if stdout, err := cmd.StdoutPipe(); err == nil {
|
||||
wg.Add(1)
|
||||
go logPipe(path, "stdout", stdout, &wg)
|
||||
}
|
||||
if stderr, err := cmd.StderrPipe(); err == nil {
|
||||
wg.Add(1)
|
||||
go logPipe(path, "stderr", stderr, &wg)
|
||||
}
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
logger.L.Fatalf("Unable to run init program %+q: %s", path, err)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
logger.L.Printf("Init program exited with error: %s", err)
|
||||
} else {
|
||||
logger.L.Printf("Init program %+q finished OK", path)
|
||||
}
|
||||
}
|
||||
|
||||
func logPipe(path, name string, pipe io.ReadCloser, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
in := bufio.NewScanner(pipe)
|
||||
|
||||
for in.Scan() {
|
||||
logger.L.Printf("%+q (%s): %s", path, name, in.Text())
|
||||
}
|
||||
if err := in.Err(); err != nil {
|
||||
logger.L.Printf("Error reading from %+q’s %s: %s", path, name, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user