将api.yaml修改为api_node.yaml

This commit is contained in:
刘祥超
2023-08-12 15:08:22 +08:00
parent 94b9c23323
commit d416a2186c
11 changed files with 70 additions and 54 deletions

View File

@@ -49,7 +49,7 @@ function build() {
fi
fi
cp "$ROOT"/configs/api.template.yaml "$DIST"/configs
cp "$ROOT"/configs/api_node.template.yaml "$DIST"/configs
cp "$ROOT"/configs/cluster.template.yaml "$DIST"/configs
cp -R "$ROOT"/www "$DIST"/
cp -R "$ROOT"/pages "$DIST"/

View File

@@ -1,4 +1,5 @@
node.json
api.yaml
api_node.yaml
cluster.yaml
*.cache

View File

@@ -1,2 +1,2 @@
* `api.template.yaml` - API相关配置模板
* `api_node.template.yaml` - API相关配置模板
* `cluster.template.yaml` - 通过集群自动接入节点模板

View File

@@ -37,7 +37,7 @@ func main() {
// validate config
_, err := configs.LoadAPIConfig()
if err != nil {
fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile("api.yaml") + "' failed: " + err.Error())
fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile(configs.ConfigFileName) + "' failed: " + err.Error())
os.Exit(0)
}
})
@@ -64,7 +64,7 @@ func main() {
// verify dir
{
fmt.Println("Checking '" + dir + "' ...")
for _, subDir := range []string{"bin", "configs/api.yaml", "logs"} {
for _, subDir := range []string{"bin/" + filepath.Base(exe), "configs", "logs"} {
_, err := os.Stat(dir + "/" + subDir)
if err != nil {
fmt.Println("[ERROR]program directory structure has been broken, please remove it manually.")

View File

@@ -7,12 +7,17 @@ import (
"os"
)
// APIConfig 节点API配置
const ConfigFileName = "api_node.yaml"
const oldConfigFileName = "api.yaml"
type APIConfig struct {
RPC struct {
OldRPC struct {
Endpoints []string `yaml:"endpoints" json:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"`
} `yaml:"rpc" json:"rpc"`
RPCEndpoints []string `yaml:"rpc.endpoints" json:"rpc.endpoints"`
RPCDisableUpdate bool `yaml:"rpc.disableUpdate" json:"rpc.disableUpdate"`
NodeId string `yaml:"nodeId" json:"nodeId"`
Secret string `yaml:"secret" json:"secret"`
}
@@ -22,9 +27,16 @@ func NewAPIConfig() *APIConfig {
}
func (this *APIConfig) Init() error {
if len(this.RPC.Endpoints) == 0 {
// 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")
}
@@ -35,8 +47,12 @@ func (this *APIConfig) Init() error {
}
func LoadAPIConfig() (*APIConfig, error) {
data, err := os.ReadFile(Tea.ConfigFile("api.yaml"))
for _, filename := range []string{ConfigFileName, oldConfigFileName} {
data, err := os.ReadFile(Tea.ConfigFile(filename))
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
@@ -52,6 +68,8 @@ func LoadAPIConfig() (*APIConfig, error) {
}
return config, nil
}
return nil, errors.New("no config file '" + ConfigFileName + "' found")
}
// WriteFile 保存到文件

View File

@@ -438,10 +438,10 @@ func (this *APIStream) handleChangeAPINode(message *pb.NodeStreamMessage) error
return nil
}
config.RPC.Endpoints = []string{messageData.Addr}
config.RPCEndpoints = []string{messageData.Addr}
// 保存到文件
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
err = config.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
if err != nil {
this.replyFail(message.RequestId, "save config file failed: "+err.Error())
return nil

View File

@@ -314,15 +314,15 @@ func (this *Node) syncConfig(taskVersion int64) error {
this.locker.Lock()
defer this.locker.Unlock()
// 检查api.yaml是否存在
apiConfigFile := Tea.ConfigFile("api.yaml")
// 检查api_node.yaml是否存在
var apiConfigFile = Tea.ConfigFile(configs.ConfigFileName)
_, err := os.Stat(apiConfigFile)
if err != nil {
if os.IsNotExist(err) {
clusterErr := this.checkClusterConfig()
if clusterErr != nil {
if os.IsNotExist(clusterErr) {
return errors.New("can not find config file 'configs/api.yaml'")
return fmt.Errorf("can not find config file 'configs/%s'", configs.ConfigFileName)
}
return fmt.Errorf("check cluster config failed: %w", clusterErr)
}
@@ -524,7 +524,7 @@ func (this *Node) startSyncTimer() {
// 检查集群设置
func (this *Node) checkClusterConfig() error {
configFile := Tea.ConfigFile("cluster.yaml")
var configFile = Tea.ConfigFile("cluster.yaml")
data, err := os.ReadFile(configFile)
if err != nil {
return err
@@ -536,7 +536,8 @@ func (this *Node) checkClusterConfig() error {
}
rpcClient, err := rpc.NewRPCClient(&configs.APIConfig{
RPC: config.RPC,
RPCEndpoints: config.RPC.Endpoints,
RPCDisableUpdate: config.RPC.DisableUpdate,
NodeId: config.ClusterId,
Secret: config.Secret,
})
@@ -556,22 +557,17 @@ func (this *Node) checkClusterConfig() error {
resp.Endpoints = []string{}
}
var apiConfig = &configs.APIConfig{
RPC: struct {
Endpoints []string `yaml:"endpoints" json:"endpoints"`
DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"`
}{
Endpoints: resp.Endpoints,
DisableUpdate: false,
},
RPCEndpoints: resp.Endpoints,
RPCDisableUpdate: false,
NodeId: resp.UniqueId,
Secret: resp.Secret,
}
remotelogs.Debug("NODE", "writing 'configs/api.yaml' ...")
err = apiConfig.WriteFile(Tea.ConfigFile("api.yaml"))
remotelogs.Debug("NODE", "writing 'configs/"+configs.ConfigFileName+"' ...")
err = apiConfig.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
if err != nil {
return err
}
remotelogs.Debug("NODE", "wrote 'configs/api.yaml' successfully")
remotelogs.Debug("NODE", "wrote 'configs/"+configs.ConfigFileName+"' successfully")
return nil
}
@@ -1136,7 +1132,7 @@ func (this *Node) changeAPINodeAddrs(apiNodeAddrs []*serverconfigs.NetworkAddres
if config == nil {
return
}
var oldEndpoints = config.RPC.Endpoints
var oldEndpoints = config.RPCEndpoints
rpcClient, err := rpc.SharedRPC()
if err != nil {
@@ -1150,9 +1146,9 @@ func (this *Node) changeAPINodeAddrs(apiNodeAddrs []*serverconfigs.NetworkAddres
go func(v int64) {
// 测试新的API节点地址
if rpcClient.TestEndpoints(addrs) {
config.RPC.Endpoints = addrs
config.RPCEndpoints = addrs
} else {
config.RPC.Endpoints = oldEndpoints
config.RPCEndpoints = oldEndpoints
this.lastAPINodeAddrs = nil // 恢复为空,以便于下次更新重试
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
"github.com/TeaOSLab/EdgeNode/internal/configs"
"github.com/TeaOSLab/EdgeNode/internal/firewalls"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
@@ -26,8 +27,8 @@ func (this *Node) loopTasks() error {
var tr = trackers.Begin("CHECK_NODE_CONFIG_CHANGES")
defer tr.End()
// 检查api.yaml是否存在
var apiConfigFile = Tea.ConfigFile("api.yaml")
// 检查api_node.yaml是否存在
var apiConfigFile = Tea.ConfigFile(configs.ConfigFileName)
_, err := os.Stat(apiConfigFile)
if err != nil {
return nil

View File

@@ -70,7 +70,7 @@ func (this *SyncAPINodesTask) Loop() error {
}
// 是否禁止自动升级
if config.RPC.DisableUpdate {
if config.RPCDisableUpdate {
return nil
}
@@ -96,7 +96,7 @@ func (this *SyncAPINodesTask) Loop() error {
}
// 和现有的对比
if utils.EqualStrings(newEndpoints, config.RPC.Endpoints) {
if utils.EqualStrings(newEndpoints, config.RPCEndpoints) {
return nil
}
@@ -107,7 +107,7 @@ func (this *SyncAPINodesTask) Loop() error {
}
// 修改RPC对象配置
config.RPC.Endpoints = newEndpoints
config.RPCEndpoints = newEndpoints
// 更新当前RPC
if !hasCustomizedAPINodeAddrs {
@@ -118,7 +118,7 @@ func (this *SyncAPINodesTask) Loop() error {
}
// 保存到文件
err = config.WriteFile(Tea.ConfigFile("api.yaml"))
err = config.WriteFile(Tea.ConfigFile(configs.ConfigFileName))
if err != nil {
return err
}

View File

@@ -229,7 +229,7 @@ func (this *RPCClient) TestEndpoints(endpoints []string) bool {
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)