mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-02 22:10:26 +08:00
提升ssh sudo安装的稳定性
This commit is contained in:
@@ -3,5 +3,5 @@ package installers
|
||||
type Env struct {
|
||||
OS string
|
||||
Arch string
|
||||
HelperName string
|
||||
HelperPath string
|
||||
}
|
||||
|
||||
@@ -147,13 +147,13 @@ func (this *BaseInstaller) LookupLatestInstaller(filePrefix string) (string, err
|
||||
|
||||
// InstallHelper 上传安装助手
|
||||
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 {
|
||||
return env, err
|
||||
}
|
||||
|
||||
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 := ""
|
||||
@@ -181,22 +181,41 @@ func (this *BaseInstaller) InstallHelper(targetDir string, role nodeconfigs.Node
|
||||
archName = "386"
|
||||
}
|
||||
|
||||
exeName := "edge-installer-helper-" + osName + "-" + archName
|
||||
var exeName = "edge-installer-helper-" + osName + "-" + archName
|
||||
switch role {
|
||||
case nodeconfigs.NodeRoleDNS:
|
||||
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)
|
||||
if err != nil {
|
||||
return env, errors.New("copy '" + exeName + "' to '" + targetDir + "' failed: " + err.Error())
|
||||
var realHelperPath = ""
|
||||
|
||||
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{
|
||||
OS: osName,
|
||||
Arch: archName,
|
||||
HelperName: exeName,
|
||||
HelperPath: realHelperPath,
|
||||
}
|
||||
return env, nil
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -53,16 +53,34 @@ func (this *NodeInstaller) Install(dir string, params interface{}, installStatus
|
||||
if len(zipFile) == 0 {
|
||||
return errors.New("can not find installer file for " + env.OS + "/" + env.Arch)
|
||||
}
|
||||
targetZip := dir + "/" + filepath.Base(zipFile)
|
||||
err = this.client.Copy(zipFile, targetZip, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
var targetZip = ""
|
||||
var firstCopyErr error
|
||||
var zipName = filepath.Base(zipFile)
|
||||
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 {
|
||||
_, stderr, err := this.client.Exec(dir + "/" + env.HelperName + " -cmd=test")
|
||||
_, stderr, err := this.client.Exec(env.HelperPath + " -cmd=test")
|
||||
if err != nil {
|
||||
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 {
|
||||
_, err = this.client.Stat(exePath)
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -165,7 +165,14 @@ func (this *SSHClient) Mkdir(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 {
|
||||
|
||||
Reference in New Issue
Block a user