From b3676406541f671b77c05a6ba79fe05260d2749e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=A1=E7=BE=8A=E7=BE=8A?= Date: Wed, 7 Aug 2019 12:15:01 +0800 Subject: [PATCH] use simple way write install node log and check login key path --- grctl/cmd/node.go | 19 +------ node/core/service/node_service.go | 50 ++-------------- util/node.go | 95 ++++++++----------------------- 3 files changed, 33 insertions(+), 131 deletions(-) diff --git a/grctl/cmd/node.go b/grctl/cmd/node.go index b82dab8ee..9b0a38eed 100644 --- a/grctl/cmd/node.go +++ b/grctl/cmd/node.go @@ -25,7 +25,6 @@ import ( "os" "strconv" "strings" - "sync" "github.com/fatih/color" @@ -637,20 +636,11 @@ func installNode(node *client.HostNode) { KeyPath: node.KeyPath, NodeID: node.ID, Stdin: os.Stdin, + Stdout: os.Stdout, + Stderr: os.Stderr, } - logChan := make(chan string, 10) - wg := sync.WaitGroup{} - wg.Add(1) - go func() { - // write log - for line := range logChan { - fmt.Fprint(os.Stdout, line) // write log to os.Stdout - } - wg.Done() - }() - - err := coreutil.RunNodeInstallCmd(option, logChan) + err := coreutil.RunNodeInstallCmd(option) if err != nil { logrus.Errorf("Error executing shell script %s", err.Error()) @@ -660,9 +650,6 @@ func installNode(node *client.HostNode) { return } - // wait log write finish - wg.Wait() - // node status success if _, err := clients.RegionClient.Nodes().UpdateNodeStatus(node.ID, client.InstallSuccess); err != nil { logrus.Errorf("update node %s status failure %s", node.ID, err.Error()) diff --git a/node/core/service/node_service.go b/node/core/service/node_service.go index 018dbdd29..389190d1d 100644 --- a/node/core/service/node_service.go +++ b/node/core/service/node_service.go @@ -24,12 +24,10 @@ import ( "os" "sort" "strconv" - "sync" "time" "github.com/goodrain/rainbond/event" - "github.com/goodrain/rainbond/util" ansibleUtil "github.com/goodrain/rainbond/util/ansible" "github.com/Sirupsen/logrus" @@ -187,20 +185,8 @@ func (n *NodeService) AsynchronousInstall(node *client.HostNode) { // start add node script logrus.Infof("Begin install node %s", node.ID) - if err := util.CheckAndCreateDir("/grdata/downloads/log/"); err != nil { - logrus.Errorf("check and create log dir failure %s", err.Error()) - } - - fileName := node.HostName + ".log" - f, err := os.OpenFile("/grdata/downloads/log/"+fileName, os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0755) - if err != nil { - logrus.Errorf("open log file %s failure %s", "/grdata/downloads/log/"+fileName, err.Error()) - node.Status = client.InstallFailed - node.NodeStatus.Status = client.InstallFailed - n.nodecluster.UpdateNode(node) - return - } - defer f.Close() + // write log to event log + logger := event.GetManager().GetLogger(node.ID + "-insatll") option := coreutil.NodeInstallOption{ HostRole: node.Role[0], @@ -211,44 +197,20 @@ func (n *NodeService) AsynchronousInstall(node *client.HostNode) { KeyPath: node.KeyPath, NodeID: node.ID, Stdin: nil, + Stdout: logger.GetWriter("node-install", "info"), + Stderr: logger.GetWriter("node-install", "err"), } - // write log to event log - logger := event.GetManager().GetLogger(node.ID + "-insatll") - - logChan := make(chan string, 10) - wg := sync.WaitGroup{} - wg.Add(1) - go func() { - // write log - for line := range logChan { - logger.Info(line, map[string]string{"step": "node-install", "status": "installing"}) // write log to eventLog - _, err = f.WriteString(line) // write log to file - if err != nil { - logrus.Error(err) - return - } - // fmt.Fprint(os.Stdout, line) //write os.Stdout - } - wg.Done() - }() - - err = coreutil.RunNodeInstallCmd(option, logChan) + err = coreutil.RunNodeInstallCmd(option) if err != nil { - if _, err := f.Write([]byte(err.Error())); err != nil { - logrus.Errorf("Error write file %s", err.Error()) - } - logrus.Errorf("Error executing shell script,View log fileļ¼š/grdata/downloads/log/" + fileName) + logrus.Error("Error executing shell script", err) node.Status = client.InstallFailed node.NodeStatus.Status = client.InstallFailed n.nodecluster.UpdateNode(node) return } - // wait log write finish - wg.Wait() - logrus.Infof("Install node %s successful", node.ID) node.Status = client.InstallSuccess diff --git a/util/node.go b/util/node.go index 63512f247..932eab653 100644 --- a/util/node.go +++ b/util/node.go @@ -19,13 +19,11 @@ package util import ( - "bufio" "fmt" "io" "os" "os/exec" "strings" - "sync" "github.com/Sirupsen/logrus" ) @@ -35,13 +33,16 @@ type NodeInstallOption struct { HostName string InternalIP string LinkModel string - RootPass string - KeyPath string + RootPass string // ssh login password + KeyPath string // ssh login key path NodeID string - Stdin *os.File + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer + loginValue string } -func RunNodeInstallCmd(option NodeInstallOption, logChan chan string) (err error) { +func RunNodeInstallCmd(option NodeInstallOption) (err error) { installNodeShellPath := os.Getenv("INSTALL_NODE_SHELL_PATH") if installNodeShellPath == "" { installNodeShellPath = "/opt/rainbond/rainbond-ansible/scripts/node.sh" @@ -59,60 +60,13 @@ func RunNodeInstallCmd(option NodeInstallOption, logChan chan string) (err error return } - line := fmt.Sprintf(installNodeShellPath+" %s %s %s %s %s %s %s", - option.HostRole, option.HostName, option.InternalIP, option.LinkModel, option.RootPass, option.KeyPath, option.NodeID) + line := fmt.Sprintf(installNodeShellPath+" %s %s %s %s %s %s", + option.HostRole, option.HostName, option.InternalIP, option.LinkModel, option.loginValue, option.NodeID) cmd := exec.Command("bash", "-c", line) cmd.Stdin = option.Stdin - - stderr, err := cmd.StderrPipe() - if err != nil { - logrus.Errorf("install node failed") - return err - } - stdout, err := cmd.StdoutPipe() - if err != nil { - logrus.Errorf("install node failed") - return err - } - - wg := sync.WaitGroup{} - - // for another log - reader := bufio.NewReader(stdout) - wg.Add(1) - go func() { - for { - line, err := reader.ReadString('\n') - if err == io.EOF { - break - } - if err != nil { - logrus.Errorf("install node failed") - return - } - - logChan <- line - } - wg.Done() - }() - - readerStderr := bufio.NewReader(stderr) - wg.Add(1) - go func() { - for { - line, err := readerStderr.ReadString('\n') - if err == io.EOF { - break - } - if err != nil { - logrus.Error("install node failed") - return - } - logChan <- line - } - wg.Done() - }() + cmd.Stdout = option.Stdout + cmd.Stderr = option.Stderr err = cmd.Start() if err != nil { @@ -124,14 +78,12 @@ func RunNodeInstallCmd(option NodeInstallOption, logChan chan string) (err error if err != nil { logrus.Errorf("install node finished with error : %v", err.Error()) } - // wait clse logChan - wg.Wait() - close(logChan) + return } +// check param func preCheckNodeInstall(option NodeInstallOption) (err error) { - // TODO check param if strings.TrimSpace(option.HostRole) == "" { err = fmt.Errorf("install node failed, install scripts needs param hostRole") logrus.Error(err) @@ -152,17 +104,18 @@ func preCheckNodeInstall(option NodeInstallOption) (err error) { logrus.Error(err) return } - if strings.TrimSpace(option.RootPass) == "" { - err = fmt.Errorf("install node failed, install scripts needs param rootPass") - logrus.Error(err) - return + + //login key path first, and then rootPass, so keyPath and RootPass can't all be empty + if strings.TrimSpace(option.KeyPath) == "" { + if strings.TrimSpace(option.RootPass) == "" { + err = fmt.Errorf("install node failed, install scripts needs login key path or login password") + logrus.Error(err) + return + } + option.loginValue = strings.TrimSpace(option.RootPass) } - // if rootPass is not empty then keyPath can be empty - // if strings.TrimSpace(option.KeyPath) == "" { - // err = fmt.Errorf("install node failed, install scripts needs param keyPath") - // logrus.Error(err) - // return - // } + option.loginValue = strings.TrimSpace(option.KeyPath) + if strings.TrimSpace(option.NodeID) == "" { err = fmt.Errorf("install node failed, install scripts needs param nodeID") logrus.Error(err)