提升ssh sudo安装的稳定性

This commit is contained in:
GoEdgeLab
2022-11-18 15:44:53 +08:00
parent 8b1a5e1fb7
commit 8151e30869
4 changed files with 62 additions and 18 deletions

View File

@@ -3,5 +3,5 @@ package installers
type Env struct { type Env struct {
OS string OS string
Arch string Arch string
HelperName string HelperPath string
} }

View File

@@ -147,13 +147,13 @@ func (this *BaseInstaller) LookupLatestInstaller(filePrefix string) (string, err
// InstallHelper 上传安装助手 // InstallHelper 上传安装助手
func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.NodeRole) (env *Env, err error) { func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.NodeRole) (env *Env, err error) {
uname, _, err := this.client.Exec("/usr/bin/uname -a") uname, stderr, err := this.client.Exec("/usr/bin/uname -a")
if err != nil { if err != nil {
return env, err return env, err
} }
if len(uname) == 0 { if len(uname) == 0 {
return nil, errors.New("unable to execute 'uname -a' on this system") return nil, errors.New("unable to execute 'uname -a' on this system: " + stderr)
} }
osName := "" osName := ""
@@ -181,22 +181,41 @@ func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.Node
archName = "386" archName = "386"
} }
exeName := "edge-installer-helper-" + osName + "-" + archName var exeName = "edge-installer-helper-" + osName + "-" + archName
switch role { switch role {
case nodeconfigs.NodeRoleDNS: case nodeconfigs.NodeRoleDNS:
exeName = "edge-installer-dns-helper-" + osName + "-" + archName exeName = "edge-installer-dns-helper-" + osName + "-" + archName
} }
exePath := Tea.Root + "/installers/" + exeName var exePath = Tea.Root + "/installers/" + exeName
err = this.client.Copy(exePath, targetDir+"/"+exeName, 0777) var realHelperPath = ""
if err != nil {
return env, errors.New("copy '" + exeName + "' to '" + targetDir + "' failed: " + err.Error()) var firstCopyErr error
for _, path := range []string{
targetDir + "/" + exeName,
this.client.UserHome() + "/" + exeName,
"/tmp/" + exeName,
} {
err = this.client.Copy(exePath, path, 0777)
if err != nil {
if firstCopyErr == nil {
firstCopyErr = err
}
} else {
err = nil
firstCopyErr = nil
realHelperPath = path
break
}
}
if firstCopyErr != nil {
return env, errors.New("copy '" + exeName + "' to '" + targetDir + "' failed: " + firstCopyErr.Error())
} }
env = &Env{ env = &Env{
OS: osName, OS: osName,
Arch: archName, Arch: archName,
HelperName: exeName, HelperPath: realHelperPath,
} }
return env, nil return env, nil
} }

View File

@@ -45,7 +45,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
} }
// 上传安装文件 // 上传安装文件
filePrefix := "edge-node-" + env.OS + "-" + env.Arch var filePrefix = "edge-node-" + env.OS + "-" + env.Arch
zipFile, err := this.LookupLatestInstaller(filePrefix) zipFile, err := this.LookupLatestInstaller(filePrefix)
if err != nil { if err != nil {
return err return err
@@ -53,16 +53,34 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
if len(zipFile) == 0 { if len(zipFile) == 0 {
return errors.New("can not find installer file for " + env.OS + "/" + env.Arch) return errors.New("can not find installer file for " + env.OS + "/" + env.Arch)
} }
targetZip := dir + "/" + filepath.Base(zipFile) var targetZip = ""
err = this.client.Copy(zipFile, targetZip, 0777) var firstCopyErr error
if err != nil { var zipName = filepath.Base(zipFile)
return err for _, candidateTargetZip := range []string{
dir + "/" + zipName,
this.client.UserHome() + "/" + zipName,
"/tmp/" + zipName,
} {
err = this.client.Copy(zipFile, candidateTargetZip, 0777)
if err != nil {
if firstCopyErr == nil {
firstCopyErr = err
}
} else {
err = nil
firstCopyErr = nil
targetZip = candidateTargetZip
break
}
}
if firstCopyErr != nil {
return errors.New("upload node file failed: " + firstCopyErr.Error())
} }
// 测试运行环境 // 测试运行环境
// 升级的节点暂时不列入测试 // 升级的节点暂时不列入测试
if !nodeParams.IsUpgrading { if !nodeParams.IsUpgrading {
_, stderr, err := this.client.Exec(dir + "/" + env.HelperName + " -cmd=test") _, stderr, err := this.client.Exec(env.HelperPath + " -cmd=test")
if err != nil { if err != nil {
return errors.New("test failed: " + err.Error()) return errors.New("test failed: " + err.Error())
} }
@@ -72,7 +90,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
} }
// 如果是升级则优雅停止先前的进程 // 如果是升级则优雅停止先前的进程
exePath := dir + "/edge-node/bin/edge-node" var exePath = dir + "/edge-node/bin/edge-node"
if nodeParams.IsUpgrading { if nodeParams.IsUpgrading {
_, err = this.client.Stat(exePath) _, err = this.client.Stat(exePath)
if err == nil { if err == nil {
@@ -87,7 +105,7 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
} }
// 解压 // 解压
_, stderr, err := this.client.Exec(dir + "/" + env.HelperName + " -cmd=unzip -zip=\"" + targetZip + "\" -target=\"" + dir + "\"") _, stderr, err := this.client.Exec(env.HelperPath + " -cmd=unzip -zip=\"" + targetZip + "\" -target=\"" + dir + "\"")
if err != nil { if err != nil {
return err return err
} }

View File

@@ -165,7 +165,14 @@ func (this *SSHClient) Mkdir(path string) error {
} }
func (this *SSHClient) MkdirAll(path string) error { func (this *SSHClient) MkdirAll(path string) error {
return this.sftp.MkdirAll(path) err := this.sftp.MkdirAll(path)
if err != nil && this.sudo {
_, _, err2 := this.execSudo("mkdir -p "+path, this.sudoPassword)
if err2 == nil {
return nil
}
}
return err
} }
func (this *SSHClient) Chmod(path string, mode os.FileMode) error { func (this *SSHClient) Chmod(path string, mode os.FileMode) error {