From edf98f1889f9b8d87d1464fc981eef02dcd34555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sat, 12 Aug 2023 18:53:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86cluster.yaml=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BAapi=5Fcluster.yaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/configs/.gitignore | 1 + cmd/edge-node/main.go | 8 +++- internal/configs/cluster_config.go | 50 ++++++++++++++++++++++++- internal/configs/cluster_config_test.go | 23 ++++++++++++ internal/nodes/node.go | 14 ++----- 5 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 internal/configs/cluster_config_test.go diff --git a/build/configs/.gitignore b/build/configs/.gitignore index 1bdee8f..3326c8e 100644 --- a/build/configs/.gitignore +++ b/build/configs/.gitignore @@ -2,4 +2,5 @@ node.json api.yaml api_node.yaml cluster.yaml +api_cluster.yaml *.cache \ No newline at end of file diff --git a/cmd/edge-node/main.go b/cmd/edge-node/main.go index 1a632e5..0cd0563 100644 --- a/cmd/edge-node/main.go +++ b/cmd/edge-node/main.go @@ -37,8 +37,12 @@ func main() { // validate config _, err := configs.LoadAPIConfig() if err != nil { - fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile(configs.ConfigFileName) + "' failed: " + err.Error()) - os.Exit(0) + // validate cluster config + _, clusterErr := configs.LoadClusterConfig() + if clusterErr != nil { // fail again + fmt.Println("[ERROR]start failed: load api config from '" + Tea.ConfigFile(configs.ConfigFileName) + "' failed: " + err.Error()) + os.Exit(0) + } } }) app.On("uninstall", func() { diff --git a/internal/configs/cluster_config.go b/internal/configs/cluster_config.go index 8bdcb7c..fcef5ea 100644 --- a/internal/configs/cluster_config.go +++ b/internal/configs/cluster_config.go @@ -1,11 +1,57 @@ package configs +import ( + "github.com/iwind/TeaGo/Tea" + "gopkg.in/yaml.v3" + "os" +) + // ClusterConfig 集群配置 type ClusterConfig struct { - RPC struct { + OldRPC struct { Endpoints []string `yaml:"endpoints" json:"endpoints"` DisableUpdate bool `yaml:"disableUpdate" json:"disableUpdate"` - } `yaml:"rpc" json:"rpc"` + } `yaml:"rpc,omitempty" json:"rpc"` + + RPCEndpoints []string `yaml:"rpc.endpoints,flow" json:"rpc.endpoints"` + RPCDisableUpdate bool `yaml:"rpc.disableUpdate" json:"rpc.disableUpdate"` + ClusterId string `yaml:"clusterId" json:"clusterId"` Secret string `yaml:"secret" json:"secret"` } + +func (this *ClusterConfig) 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 + } + + return nil +} + +func LoadClusterConfig() (*ClusterConfig, error) { + for _, filename := range []string{"api_cluster.yaml", "cluster.yaml"} { + data, err := os.ReadFile(Tea.ConfigFile(filename)) + if err != nil { + if os.IsNotExist(err) { + continue + } + return nil, err + } + + var config = &ClusterConfig{} + err = yaml.Unmarshal(data, config) + if err != nil { + return config, err + } + + err = config.Init() + if err != nil { + return nil, err + } + + return config, nil + } + return nil, os.ErrNotExist +} diff --git a/internal/configs/cluster_config_test.go b/internal/configs/cluster_config_test.go new file mode 100644 index 0000000..0d44525 --- /dev/null +++ b/internal/configs/cluster_config_test.go @@ -0,0 +1,23 @@ +// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package configs_test + +import ( + "github.com/TeaOSLab/EdgeNode/internal/configs" + "gopkg.in/yaml.v3" + "testing" +) + +func TestLoadClusterConfig(t *testing.T) { + config, err := configs.LoadClusterConfig() + if err != nil { + t.Fatal(err) + } + t.Logf("%+v", config) + + configData, err := yaml.Marshal(config) + if err != nil { + t.Fatal(err) + } + t.Log(string(configData)) +} diff --git a/internal/nodes/node.go b/internal/nodes/node.go index bbdf0ca..0c911b5 100644 --- a/internal/nodes/node.go +++ b/internal/nodes/node.go @@ -36,7 +36,6 @@ import ( "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" "github.com/iwind/gosock/pkg/gosock" - "gopkg.in/yaml.v3" "log" "os" "os/exec" @@ -524,20 +523,13 @@ func (this *Node) startSyncTimer() { // 检查集群设置 func (this *Node) checkClusterConfig() error { - var configFile = Tea.ConfigFile("cluster.yaml") - data, err := os.ReadFile(configFile) + config, err := configs.LoadClusterConfig() if err != nil { return err } - config := &configs.ClusterConfig{} - err = yaml.Unmarshal(data, config) - if err != nil { - return err - } - rpcClient, err := rpc.NewRPCClient(&configs.APIConfig{ - RPCEndpoints: config.RPC.Endpoints, - RPCDisableUpdate: config.RPC.DisableUpdate, + RPCEndpoints: config.RPCEndpoints, + RPCDisableUpdate: config.RPCDisableUpdate, NodeId: config.ClusterId, Secret: config.Secret, })