use simple way write install node log and check login key path

This commit is contained in:
凡羊羊 2019-08-07 12:15:01 +08:00
parent 7d83add0f0
commit b367640654
3 changed files with 33 additions and 131 deletions

View File

@ -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())

View File

@ -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

View File

@ -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)