mirror of
https://github.com/labring/sealos.git
synced 2026-09-24 15:46:19 +08:00
implementation has to test, add some logs to shown what you have done add judge: kubernetes new version is bigger than current version, more than one major version is not allowed refactor upgrademaster0 method add upgrade docs add pr links in docs pre check for upgrade cmd , use /root/.sealos/admin.conf instead of /root/.kube/config when master0 upgrade apply failed should exit. add upgrade docs for checking cluster before upgrade fix Alert to Info delete unuse func ComSyncEtcd fix use duplicate load method Signed-off-by: oldthreefeng <louisehong4168@gmail.com>
104 lines
2.2 KiB
Go
104 lines
2.2 KiB
Go
package sshutil
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/wonderivan/logger"
|
|
)
|
|
|
|
//Cmd is in host exec cmd
|
|
func (ss *SSH) Cmd(host string, cmd string) []byte {
|
|
logger.Info("[ssh][%s] %s", host, cmd)
|
|
session, err := ss.Connect(host)
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
logger.Error("[ssh][%s]Error create ssh session failed,%s", host, err)
|
|
}
|
|
}()
|
|
if err != nil {
|
|
panic(1)
|
|
}
|
|
defer session.Close()
|
|
b, err := session.CombinedOutput(cmd)
|
|
logger.Debug("[ssh][%s]command result is: %s", host, string(b))
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
logger.Error("[ssh][%s]Error exec command failed: %s", host, err)
|
|
}
|
|
}()
|
|
if err != nil {
|
|
panic(1)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func readPipe(host string, pipe io.Reader, isErr bool) {
|
|
r := bufio.NewReader(pipe)
|
|
for {
|
|
line, _, err := r.ReadLine()
|
|
if line == nil {
|
|
return
|
|
} else if err != nil {
|
|
logger.Info("[%s] %s", host, line)
|
|
logger.Error("[ssh] [%s] %s", host, err)
|
|
return
|
|
} else {
|
|
if isErr {
|
|
logger.Error("[%s] %s", host, line)
|
|
} else {
|
|
logger.Info("[%s] %s", host, line)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (ss *SSH) CmdAsync(host string, cmd string) error {
|
|
logger.Info("[ssh][%s] %s", host, cmd)
|
|
session, err := ss.Connect(host)
|
|
if err != nil {
|
|
logger.Error("[ssh][%s]Error create ssh session failed,%s", host, err)
|
|
return err
|
|
}
|
|
defer session.Close()
|
|
stdout, err := session.StdoutPipe()
|
|
if err != nil {
|
|
logger.Error("[ssh][%s]Unable to request StdoutPipe(): %s", host, err)
|
|
return err
|
|
}
|
|
stderr, err := session.StderrPipe()
|
|
if err != nil {
|
|
logger.Error("[ssh][%s]Unable to request StderrPipe(): %s", host, err)
|
|
return err
|
|
}
|
|
if err := session.Start(cmd); err != nil {
|
|
logger.Error("[ssh][%s]Unable to execute command: %s", host, err)
|
|
return err
|
|
}
|
|
doneout := make(chan bool, 1)
|
|
doneerr := make(chan bool, 1)
|
|
go func() {
|
|
readPipe(host, stderr, true)
|
|
doneerr <- true
|
|
}()
|
|
go func() {
|
|
readPipe(host, stdout, false)
|
|
doneout <- true
|
|
}()
|
|
<-doneerr
|
|
<-doneout
|
|
return session.Wait()
|
|
}
|
|
|
|
//CmdToString is in host exec cmd and replace to spilt str
|
|
func (ss *SSH) CmdToString(host, cmd, spilt string) string {
|
|
data := ss.Cmd(host, cmd)
|
|
if data != nil {
|
|
str := string(data)
|
|
str = strings.ReplaceAll(str, "\r\n", spilt)
|
|
return str
|
|
}
|
|
return ""
|
|
}
|