Files
EdgeAPI/internal/installers/installer_node.go

167 lines
4.2 KiB
Go
Raw Normal View History

2020-09-06 16:19:54 +08:00
package installers
import (
"bytes"
"errors"
2023-08-11 16:13:33 +08:00
"fmt"
"os"
2020-09-06 16:19:54 +08:00
"path/filepath"
"regexp"
2024-07-27 14:15:25 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-09-06 16:19:54 +08:00
)
type NodeInstaller struct {
BaseInstaller
}
func (this *NodeInstaller) Install(dir string, params interface{}, installStatus *models.NodeInstallStatus) error {
2020-09-06 16:19:54 +08:00
if params == nil {
return errors.New("'params' required for node installation")
}
nodeParams, ok := params.(*NodeParams)
if !ok {
return errors.New("'params' should be *NodeParams")
}
err := nodeParams.Validate()
if err != nil {
2023-08-11 16:13:33 +08:00
return fmt.Errorf("params validation: %w", err)
2020-09-06 16:19:54 +08:00
}
// 检查目标目录是否存在
_, err = this.client.Stat(dir)
if err != nil {
err = this.client.MkdirAll(dir)
if err != nil {
installStatus.ErrorCode = "CREATE_ROOT_DIRECTORY_FAILED"
2023-08-11 16:13:33 +08:00
return fmt.Errorf("create directory '%s' failed: %w", dir, err)
}
}
2020-09-06 16:19:54 +08:00
// 安装助手
env, err := this.InstallHelper(dir, nodeconfigs.NodeRoleNode)
2020-09-06 16:19:54 +08:00
if err != nil {
installStatus.ErrorCode = "INSTALL_HELPER_FAILED"
2020-09-06 16:19:54 +08:00
return err
}
// 上传安装文件
2022-11-18 15:44:53 +08:00
var filePrefix = "edge-node-" + env.OS + "-" + env.Arch
2020-09-06 16:19:54 +08:00
zipFile, err := this.LookupLatestInstaller(filePrefix)
if err != nil {
return err
}
if len(zipFile) == 0 {
return errors.New("can not find installer file for " + env.OS + "/" + env.Arch)
}
2022-11-18 15:44:53 +08:00
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 {
2023-08-11 16:13:33 +08:00
return fmt.Errorf("upload node file failed: %w", firstCopyErr)
2020-09-06 16:19:54 +08:00
}
2020-10-28 12:35:36 +08:00
// 测试运行环境
// 升级的节点暂时不列入测试
if !nodeParams.IsUpgrading {
2022-11-18 15:44:53 +08:00
_, stderr, err := this.client.Exec(env.HelperPath + " -cmd=test")
2020-10-28 12:35:36 +08:00
if err != nil {
2023-08-11 16:13:33 +08:00
return fmt.Errorf("test failed: %w", err)
2020-10-28 12:35:36 +08:00
}
if len(stderr) > 0 {
return errors.New("test failed: " + stderr)
}
}
// 如果是升级则优雅停止先前的进程
2022-11-18 15:44:53 +08:00
var exePath = dir + "/edge-node/bin/edge-node"
2020-10-28 12:35:36 +08:00
if nodeParams.IsUpgrading {
_, err = this.client.Stat(exePath)
if err == nil {
_, _, _ = this.client.Exec(exePath + " quit")
2022-07-27 08:35:15 +08:00
// 删除可执行文件防止冲突
err = this.client.Remove(exePath)
if err != nil && err != os.ErrNotExist {
2023-08-11 16:13:33 +08:00
return fmt.Errorf("remove old file failed: %w", err)
2022-07-27 08:35:15 +08:00
}
2020-10-28 12:35:36 +08:00
}
}
2020-09-06 16:19:54 +08:00
// 解压
2022-11-18 15:44:53 +08:00
_, stderr, err := this.client.Exec(env.HelperPath + " -cmd=unzip -zip=\"" + targetZip + "\" -target=\"" + dir + "\"")
2020-09-06 16:19:54 +08:00
if err != nil {
return err
}
if len(stderr) > 0 {
return errors.New("unzip installer failed: " + stderr)
}
// 修改配置文件
{
var configFile = dir + "/edge-node/configs/api_node.yaml"
2021-12-06 19:27:11 +08:00
// sudo之后我们需要修改配置目录才能写入文件
if this.client.sudo {
_, _, _ = this.client.Exec("chown " + this.client.User() + " " + filepath.Dir(configFile))
}
var data = []byte(`rpc.endpoints: [ ${endpoints} ]
2021-08-11 21:00:29 +08:00
nodeId: "${nodeId}"
secret: "${nodeSecret}"`)
2020-09-06 16:19:54 +08:00
2020-09-13 20:37:28 +08:00
data = bytes.ReplaceAll(data, []byte("${endpoints}"), []byte(nodeParams.QuoteEndpoints()))
2020-09-06 16:19:54 +08:00
data = bytes.ReplaceAll(data, []byte("${nodeId}"), []byte(nodeParams.NodeId))
data = bytes.ReplaceAll(data, []byte("${nodeSecret}"), []byte(nodeParams.Secret))
_, err = this.client.WriteFile(configFile, data)
if err != nil {
2023-08-11 16:13:33 +08:00
return fmt.Errorf("write '%s': %w", configFile, err)
2020-09-06 16:19:54 +08:00
}
}
// 测试
_, stderr, err = this.client.Exec(dir + "/edge-node/bin/edge-node test")
if err != nil {
installStatus.ErrorCode = "TEST_FAILED"
2023-08-11 16:13:33 +08:00
return fmt.Errorf("test edge node failed: %w, stderr: %s", err, stderr)
}
if len(stderr) > 0 {
if regexp.MustCompile(`(?i)rpc`).MatchString(stderr) {
installStatus.ErrorCode = "RPC_TEST_FAILED"
}
return errors.New("test edge node failed: " + stderr)
}
2020-09-06 16:19:54 +08:00
// 启动
_, stderr, err = this.client.Exec(dir + "/edge-node/bin/edge-node start")
if err != nil {
2023-08-11 16:13:33 +08:00
return fmt.Errorf("start edge node failed: %w", err)
2020-09-06 16:19:54 +08:00
}
if len(stderr) > 0 {
return errors.New("start edge node failed: " + stderr)
}
return nil
}