@@ -18,7 +18,7 @@ import (
1818 "bytes"
1919 "context"
2020 "fmt"
21- "io/ioutil "
21+ "io"
2222 "os"
2323 "os/exec"
2424 "path/filepath"
@@ -65,9 +65,6 @@ func NewHaproxyHelper(opts *Options, lbagentId string) (*HaproxyHelper, error) {
6565 return nil , fmt .Errorf ("sysctl: %s" , err )
6666 }
6767 }
68-
69- system_service .Init ()
70-
7168 return helper , nil
7269}
7370
@@ -199,7 +196,7 @@ func (h *HaproxyHelper) handleUseCorpusCmd(ctx context.Context, cmd *LbagentCmd)
199196 if err == nil {
200197 d := buf .Bytes ()
201198 p := filepath .Join (dir , "telegraf.conf" )
202- err := ioutil .WriteFile (p , d , agentutils .FileModeFile )
199+ err := os .WriteFile (p , d , agentutils .FileModeFile )
203200 if err == nil {
204201 err := h .reloadTelegraf (ctx , agentParams )
205202 if err != nil {
@@ -590,16 +587,21 @@ func (h *HaproxyHelper) reloadKeepalived(ctx context.Context) error {
590587 "--vrrp_pid" , vrrpPidFile .Path ,
591588 "--checkers_pid" , checkersPidFile .Path ,
592589 "--use-file" , h .keepalivedConf (),
593- "-D" ,
594- "-d" ,
595- "-S" ,
596- "0" ,
590+ "--log-detail" ,
591+ "--no-syslog" ,
592+ "--dump-conf" ,
593+ "--log-console" ,
594+ "--dont-fork" ,
595+ }
596+ err := h .runService (args )
597+ if err != nil {
598+ return errors .Wrapf (err , "run service %s" , strings .Join (args , " " ))
597599 }
598- return h . runCmd ( args )
600+ return nil
599601}
600602
601603func (h * HaproxyHelper ) runCmd (args []string ) error {
602- log .Debugf ("run command %s" , args )
604+ log .Infof ("run command %s" , strings . Join ( args , " " ) )
603605
604606 name := args [0 ]
605607 args = args [1 :]
@@ -619,6 +621,8 @@ func (h *HaproxyHelper) runCmd(args []string) error {
619621}
620622
621623func (h * HaproxyHelper ) startCmd (args []string ) (* exec.Cmd , error ) {
624+ log .Infof ("start command %s" , strings .Join (args , " " ))
625+
622626 name := args [0 ]
623627 args = args [1 :]
624628 cmd := exec .Command (name , args ... )
@@ -629,3 +633,51 @@ func (h *HaproxyHelper) startCmd(args []string) (*exec.Cmd, error) {
629633 }
630634 return cmd , nil
631635}
636+
637+ func (h * HaproxyHelper ) runService (args []string ) error {
638+ log .Infof ("run service %s" , strings .Join (args , " " ))
639+
640+ name := args [0 ]
641+ args = args [1 :]
642+ cmd := exec .Command (name , args ... )
643+
644+ stdout , err := cmd .StdoutPipe ()
645+ if err != nil {
646+ log .Errorf ("service %s stdout pipe error: %s" , cmd .String (), err )
647+ return errors .Wrapf (err , "service %s stdout pipe error" , cmd .String ())
648+ }
649+ stderr , err := cmd .StderrPipe ()
650+ if err != nil {
651+ log .Errorf ("service %s stderr pipe error: %s" , cmd .String (), err )
652+ return errors .Wrapf (err , "service %s stderr pipe error" , cmd .String ())
653+ }
654+ drain := func (out io.ReadCloser , isErr bool ) {
655+ defer out .Close ()
656+ buf := make ([]byte , 1024 )
657+ for {
658+ n , err := stdout .Read (buf )
659+ if err != nil {
660+ log .Errorf ("read pipe error: %s" , err )
661+ return
662+ }
663+ if isErr {
664+ log .Errorln (string (buf [:n ]))
665+ } else {
666+ log .Infoln (string (buf [:n ]))
667+ }
668+ }
669+ }
670+ err = cmd .Start ()
671+ if err != nil {
672+ return errors .Wrapf (err , "start service %s" , cmd .String ())
673+ }
674+ go func (cmd * exec.Cmd ) {
675+ err := cmd .Wait ()
676+ if err != nil {
677+ log .Errorf ("service %s exited with error: %s" , cmd .String (), err )
678+ }
679+ }(cmd )
680+ go drain (stdout , false )
681+ go drain (stderr , true )
682+ return nil
683+ }
0 commit comments