将api.yaml修改为api_admin.yaml

This commit is contained in:
GoEdgeLab
2023-08-12 17:58:00 +08:00
parent 769c1b79d6
commit b06c44bebf
19 changed files with 113 additions and 114 deletions

View File

@@ -1,4 +1,5 @@
api.yaml
api_admin.yaml
server.yaml
api_db.yaml
*.pem

View File

@@ -1,4 +0,0 @@
rpc:
endpoints: [ "http://127.0.0.1:8003" ]
nodeId: ""
secret: ""

View File

@@ -0,0 +1,3 @@
rpc.endpoints: [ "http://127.0.0.1:8003" ]
nodeId: ""
secret: ""

View File

@@ -1,6 +1,7 @@
package configs
import (
"errors"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"github.com/iwind/TeaGo/Tea"
"gopkg.in/yaml.v3"
@@ -8,12 +9,19 @@ import (
"path/filepath"
)
const ConfigFileName = "api_admin.yaml"
const oldConfigFileName = "api.yaml"
// APIConfig API配置
type APIConfig struct {
RPC struct {
OldRPC struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
} `yaml:"rpc"`
RPCEndpoints []string `yaml:"rpc.endpoints" json:"rpc.endpoints"`
RPCDisableUpdate bool `yaml:"rpc.disableUpdate" json:"rpc.disableUpdate"`
NodeId string `yaml:"nodeId"`
Secret string `yaml:"secret"`
}
@@ -21,21 +29,22 @@ type APIConfig struct {
// LoadAPIConfig 加载API配置
func LoadAPIConfig() (*APIConfig, error) {
// 候选文件
var localFile = Tea.ConfigFile("api.yaml")
var realFile = Tea.ConfigFile(ConfigFileName)
var oldRealFile = Tea.ConfigFile(oldConfigFileName)
var isFromLocal = false
var paths = []string{localFile}
var paths = []string{realFile, oldRealFile}
homeDir, homeErr := os.UserHomeDir()
if homeErr == nil {
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/api.yaml")
paths = append(paths, homeDir+"/."+teaconst.ProcessName+"/"+ConfigFileName)
}
paths = append(paths, "/etc/"+teaconst.ProcessName+"/api.yaml")
paths = append(paths, "/etc/"+teaconst.ProcessName+"/"+ConfigFileName)
var data []byte
var err error
for _, path := range paths {
data, err = os.ReadFile(path)
if err == nil {
if path == localFile {
if path == realFile || path == oldRealFile {
isFromLocal = true
}
break
@@ -51,9 +60,14 @@ func LoadAPIConfig() (*APIConfig, error) {
return nil, err
}
err = config.Init()
if err != nil {
return nil, errors.New("init error: " + err.Error())
}
if !isFromLocal {
// 恢复文件
_ = os.WriteFile(localFile, data, 0666)
_ = os.WriteFile(realFile, data, 0666)
}
return config, nil
@@ -61,9 +75,9 @@ func LoadAPIConfig() (*APIConfig, error) {
// ResetAPIConfig 重置配置
func ResetAPIConfig() error {
var filename = "api.yaml"
var filename = ConfigFileName
// 重置 configs/api.yaml
// 重置 configs/api_admin.yaml
{
var configFile = Tea.ConfigFile(filename)
stat, err := os.Stat(configFile)
@@ -75,7 +89,7 @@ func ResetAPIConfig() error {
}
}
// 重置 ~/.edge-admin/api.yaml
// 重置 ~/.edge-admin/api_admin.yaml
homeDir, homeErr := os.UserHomeDir()
if homeErr == nil {
var configFile = homeDir + "/." + teaconst.ProcessName + "/" + filename
@@ -88,7 +102,7 @@ func ResetAPIConfig() error {
}
}
// 重置 /etc/edge-admin/api.yaml
// 重置 /etc/edge-admin/api_admin.yaml
{
var configFile = "/etc/" + teaconst.ProcessName + "/" + filename
stat, err := os.Stat(configFile)
@@ -103,6 +117,22 @@ func ResetAPIConfig() error {
return nil
}
func IsNewInstalled() bool {
homeDir, err := os.UserHomeDir()
if err != nil {
return false
}
for _, filename := range []string{ConfigFileName, oldConfigFileName} {
_, err = os.Stat(homeDir + "/." + teaconst.ProcessName + "/" + filename)
if err == nil {
return false
}
}
return true
}
// WriteFile 写入API配置
func (this *APIConfig) WriteFile(path string) error {
data, err := yaml.Marshal(this)
@@ -159,11 +189,27 @@ func (this *APIConfig) WriteFile(path string) error {
// Clone 克隆当前配置
func (this *APIConfig) Clone() *APIConfig {
return &APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{},
NodeId: this.NodeId,
Secret: this.Secret,
}
}
func (this *APIConfig) Init() error {
// compatible with old
if len(this.RPCEndpoints) == 0 && len(this.OldRPC.Endpoints) > 0 {
this.RPCEndpoints = this.OldRPC.Endpoints
this.RPCDisableUpdate = this.OldRPC.DisableUpdate
}
if len(this.RPCEndpoints) == 0 {
return errors.New("no valid 'rpc.endpoints'")
}
if len(this.NodeId) == 0 {
return errors.New("'nodeId' required")
}
if len(this.Secret) == 0 {
return errors.New("'secret' required")
}
return nil
}

View File

@@ -255,7 +255,7 @@ func (this *AdminNode) addPortsToFirewall() {
// 启动API节点
func (this *AdminNode) startAPINode() {
configPath := Tea.Root + "/edge-api/configs/api.yaml"
var configPath = Tea.Root + "/edge-api/configs/api.yaml"
_, err := os.Stat(configPath)
canStart := false
if err == nil {

View File

@@ -523,7 +523,7 @@ func (this *RPCClient) init() error {
// 重新连接
var conns = []*grpc.ClientConn{}
for _, endpoint := range this.apiConfig.RPC.Endpoints {
for _, endpoint := range this.apiConfig.RPCEndpoints {
u, err := url.Parse(endpoint)
if err != nil {
return fmt.Errorf("parse endpoint failed: %w", err)

View File

@@ -2,8 +2,6 @@ package setup
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
"os"
)
var isConfigured bool
@@ -21,10 +19,5 @@ func IsConfigured() bool {
// IsNewInstalled IsNew 检查是否新安装
func IsNewInstalled() bool {
homeDir, err := os.UserHomeDir()
if err != nil {
return false
}
_, err = os.Stat(homeDir + "/." + teaconst.ProcessName + "/api.yaml")
return err != nil
return configs.IsNewInstalled()
}

View File

@@ -65,7 +65,7 @@ func (this *SyncAPINodesTask) Loop() error {
}
// 是否禁止自动升级
if config.RPC.DisableUpdate {
if config.RPCDisableUpdate {
return nil
}
@@ -88,7 +88,7 @@ func (this *SyncAPINodesTask) Loop() error {
}
// 和现有的对比
if this.isSame(newEndpoints, config.RPC.Endpoints) {
if this.isSame(newEndpoints, config.RPCEndpoints) {
return nil
}
@@ -99,14 +99,14 @@ func (this *SyncAPINodesTask) Loop() error {
}
// 修改RPC对象配置
config.RPC.Endpoints = newEndpoints
config.RPCEndpoints = newEndpoints
err = rpcClient.UpdateConfig(config)
if err != nil {
return err
}
// 保存到文件
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
err = config.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
if err != nil {
return err
}

View File

@@ -57,7 +57,7 @@ func (this *Upgrader) Upgrade() error {
return err
}
var newAPIConfig = apiConfig.Clone()
newAPIConfig.RPC.Endpoints = apiNode.AccessAddrs
newAPIConfig.RPCEndpoints = apiNode.AccessAddrs
rpcClient, err := rpc.NewRPCClient(newAPIConfig, false)
if err != nil {

View File

@@ -155,11 +155,11 @@ func parseAPIErr(action actions.ActionWrapper, err error) (apiNodeIsStarting boo
var apiEndpoints = []string{}
apiConfig, apiConfigErr := configs.LoadAPIConfig()
if apiConfigErr == nil && apiConfig != nil {
apiEndpoints = append(apiEndpoints, apiConfig.RPC.Endpoints...)
apiEndpoints = append(apiEndpoints, apiConfig.RPCEndpoints...)
}
var isRPCConnError bool
_, isRPCConnError = rpcerrors.HumanError(err, apiEndpoints, Tea.ConfigFile("api.yaml"))
_, isRPCConnError = rpcerrors.HumanError(err, apiEndpoints, Tea.ConfigFile(configs.ConfigFileName))
if isRPCConnError {
// API节点是否正在启动
var sock = gosock.NewTmpSock("edge-api")

View File

@@ -101,14 +101,9 @@ func SendMessageToCluster(ctx context.Context, clusterId int64, code string, msg
apiNode := apiNodeResp.ApiNode
apiRPCClient, err := rpc.NewRPCClient(&configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: apiNode.AccessAddrs,
},
NodeId: apiNode.UniqueId,
Secret: apiNode.Secret,
RPCEndpoints: apiNode.AccessAddrs,
NodeId: apiNode.UniqueId,
Secret: apiNode.Secret,
}, false)
if err != nil {
locker.Lock()
@@ -282,14 +277,9 @@ func SendMessageToNodeIds(ctx context.Context, nodeIds []int64, code string, msg
apiNode := apiNodeResp.ApiNode
apiRPCClient, err := rpc.NewRPCClient(&configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: apiNode.AccessAddrs,
},
NodeId: apiNode.UniqueId,
Secret: apiNode.Secret,
RPCEndpoints: apiNode.AccessAddrs,
NodeId: apiNode.UniqueId,
Secret: apiNode.Secret,
}, false)
if err != nil {
locker.Lock()

View File

@@ -35,14 +35,9 @@ func (this *UpdateHostsAction) RunPost(params struct {
}
client, err := rpc.NewRPCClient(&configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port},
},
NodeId: params.NodeId,
Secret: params.NodeSecret,
RPCEndpoints: []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port},
NodeId: params.NodeId,
Secret: params.NodeSecret,
}, false)
if err != nil {
this.FailField("host", "测试API节点时出错请检查配置错误信息"+err.Error())
@@ -165,23 +160,18 @@ func (this *UpdateHostsAction) RunPost(params struct {
}
}
// 修改api.yaml
// 修改api_admin.yaml
var apiConfig = &configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: endpoints,
},
NodeId: adminAPIToken.NodeId,
Secret: adminAPIToken.Secret,
RPCEndpoints: endpoints,
NodeId: adminAPIToken.NodeId,
Secret: adminAPIToken.Secret,
}
err = apiConfig.WriteFile(Tea.Root + "/configs/api.yaml")
err = apiConfig.WriteFile(Tea.Root + "/configs/" + configs.ConfigFileName)
if err != nil {
this.Fail("保存configs/api.yaml失败:" + err.Error())
this.Fail("保存configs/" + configs.ConfigFileName + "失败:" + err.Error())
}
// 加载api.yaml
// 加载api_admin.yaml
rpcClient, err := rpc.SharedRPC()
if err != nil {
this.Fail("初始化RPC失败" + err.Error())

View File

@@ -42,14 +42,9 @@ func (this *ValidateApiAction) RunPost(params struct {
Field("nodeSecret", params.NodeSecret).
Require("请输入节点secret")
client, err := rpc.NewRPCClient(&configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port},
},
NodeId: params.NodeId,
Secret: params.NodeSecret,
RPCEndpoints: []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port},
NodeId: params.NodeId,
Secret: params.NodeSecret,
}, false)
if err != nil {
this.FailField("host", "测试API节点时出错请检查配置错误信息"+err.Error())

View File

@@ -43,7 +43,7 @@ func (this *UpgradeCheckAction) RunPost(params struct {
}
var newAPIConfig = apiConfig.Clone()
newAPIConfig.RPC.Endpoints = node.AccessAddrs
newAPIConfig.RPCEndpoints = node.AccessAddrs
rpcClient, err := rpc.NewRPCClient(newAPIConfig, false)
if err != nil {
this.Success()

View File

@@ -36,7 +36,7 @@ func (this *UpdateHostsAction) RunPost(params struct {
this.Fail("加载当前平台的API配置失败" + err.Error())
}
var apiURL = params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port
config.RPC.Endpoints = []string{apiURL}
config.RPCEndpoints = []string{apiURL}
client, err := rpc.NewRPCClient(config, false)
if err != nil {
this.Fail("检查API节点地址出错" + err.Error())

View File

@@ -43,7 +43,7 @@ func (this *ValidateAPIAction) RunPost(params struct {
if err != nil {
this.Fail("加载当前平台的API配置失败" + err.Error())
}
config.RPC.Endpoints = []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port}
config.RPCEndpoints = []string{params.Protocol + "://" + configutils.QuoteIP(params.Host) + ":" + params.Port}
client, err := rpc.NewRPCClient(config, false)
if err != nil {
this.Fail("检查API节点地址出错" + err.Error())

View File

@@ -26,7 +26,7 @@ func (this *IndexAction) RunGet(params struct{}) {
config, err := configs.LoadAPIConfig()
if err == nil {
endpoints = config.RPC.Endpoints
endpoints = config.RPCEndpoints
this.Data["nodeId"] = config.NodeId
this.Data["secret"] = config.Secret
this.Data["canInstall"] = false
@@ -78,7 +78,7 @@ func (this *IndexAction) RunPost(params struct {
var config = &configs.APIConfig{}
config.NodeId = params.NodeId
config.Secret = params.Secret
config.RPC.Endpoints = []string{endpoint}
config.RPCEndpoints = []string{endpoint}
client, err := rpc.NewRPCClient(config, false)
if err != nil {
this.Fail("尝试配置RPC发生错误" + err.Error())
@@ -124,9 +124,9 @@ func (this *IndexAction) RunPost(params struct {
}
config.NodeId = params.NodeId
config.Secret = params.Secret
config.RPC.Endpoints = endpoints
config.RPC.DisableUpdate = true
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
config.RPCEndpoints = endpoints
config.RPCDisableUpdate = true
err = config.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
if err != nil {
this.Fail("配置保存失败:" + err.Error())
}

View File

@@ -231,14 +231,9 @@ func (this *InstallAction) RunPost(params struct {
// 写入API节点配置完成安装
var apiConfig = &configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: []string{"http://" + configutils.QuoteIP(apiNodeMap.GetString("newHost")) + ":" + apiNodeMap.GetString("newPort")},
},
NodeId: resultMap.GetString("adminNodeId"),
Secret: resultMap.GetString("adminNodeSecret"),
RPCEndpoints: []string{"http://" + configutils.QuoteIP(apiNodeMap.GetString("newHost")) + ":" + apiNodeMap.GetString("newPort")},
NodeId: resultMap.GetString("adminNodeId"),
Secret: resultMap.GetString("adminNodeSecret"),
}
// 设置管理员
@@ -285,7 +280,7 @@ func (this *InstallAction) RunPost(params struct {
}
}
err = apiConfig.WriteFile(Tea.ConfigFile("api.yaml"))
err = apiConfig.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
if err != nil {
this.Fail("保存配置失败,原因:" + err.Error())
}
@@ -294,14 +289,9 @@ func (this *InstallAction) RunPost(params struct {
} else if mode == "old" {
// 构造RPC
var apiConfig = &configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: []string{apiNodeMap.GetString("oldProtocol") + "://" + configutils.QuoteIP(apiNodeMap.GetString("oldHost")) + ":" + apiNodeMap.GetString("oldPort")},
},
NodeId: apiNodeMap.GetString("oldNodeId"),
Secret: apiNodeMap.GetString("oldNodeSecret"),
RPCEndpoints: []string{apiNodeMap.GetString("oldProtocol") + "://" + configutils.QuoteIP(apiNodeMap.GetString("oldHost")) + ":" + apiNodeMap.GetString("oldPort")},
NodeId: apiNodeMap.GetString("oldNodeId"),
Secret: apiNodeMap.GetString("oldNodeSecret"),
}
client, err := rpc.NewRPCClient(apiConfig, false)
if err != nil {
@@ -343,7 +333,7 @@ func (this *InstallAction) RunPost(params struct {
}
// 写入API节点配置完成安装
err = apiConfig.WriteFile(Tea.ConfigFile("api.yaml"))
err = apiConfig.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
if err != nil {
this.Fail("保存配置失败,原因:" + err.Error())
}

View File

@@ -102,14 +102,9 @@ func (this *ValidateApiAction) RunPost(params struct {
Field("oldNodeSecret", params.OldNodeSecret).
Require("请输入节点secret")
client, err := rpc.NewRPCClient(&configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate"`
}{
Endpoints: []string{params.OldProtocol + "://" + configutils.QuoteIP(params.OldHost) + ":" + params.OldPort},
},
NodeId: params.OldNodeId,
Secret: params.OldNodeSecret,
RPCEndpoints: []string{params.OldProtocol + "://" + configutils.QuoteIP(params.OldHost) + ":" + params.OldPort},
NodeId: params.OldNodeId,
Secret: params.OldNodeSecret,
}, false)
if err != nil {
this.FailField("oldHost", "测试API节点时出错请检查配置错误信息"+err.Error())