diff --git a/go.mod b/go.mod index 22e45213..448a47e7 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/go-sql-driver/mysql v1.5.0 github.com/go-yaml/yaml v2.1.0+incompatible github.com/golang/protobuf v1.4.2 - github.com/iwind/TeaGo v0.0.0-20210103021650-62acfa30bcea // indirect + github.com/iwind/TeaGo v0.0.0-20210106152225-413a5aba30aa // indirect github.com/lionsoul2014/ip2region v2.2.0-release+incompatible github.com/mozillazg/go-pinyin v0.18.0 github.com/pkg/sftp v1.12.0 diff --git a/go.sum b/go.sum index 4561b00d..5fddb411 100644 --- a/go.sum +++ b/go.sum @@ -176,6 +176,8 @@ github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhK github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc= github.com/iwind/TeaGo v0.0.0-20210103021650-62acfa30bcea h1:ACgcrVeyHpKt8K6k92RrmPndxVq7Qx+zNXZBzRKv3+0= github.com/iwind/TeaGo v0.0.0-20210103021650-62acfa30bcea/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc= +github.com/iwind/TeaGo v0.0.0-20210106152225-413a5aba30aa h1:kGf94NRhZik+SPBbY+NgQhwIjJjqv1E1nqgLQ0BtOzw= +github.com/iwind/TeaGo v0.0.0-20210106152225-413a5aba30aa/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc= github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= diff --git a/internal/const/const.go b/internal/const/const.go index 46cce597..2fe752d9 100644 --- a/internal/const/const.go +++ b/internal/const/const.go @@ -1,7 +1,7 @@ package teaconst const ( - Version = "0.0.7.1" + Version = "0.0.8" ProductName = "Edge API" ProcessName = "edge-api" diff --git a/internal/db/models/node_cluster_dao.go b/internal/db/models/node_cluster_dao.go index dea77e45..20bd3acc 100644 --- a/internal/db/models/node_cluster_dao.go +++ b/internal/db/models/node_cluster_dao.go @@ -11,6 +11,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" + "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/types" "strconv" @@ -102,7 +103,7 @@ func (this *NodeClusterDAO) FindAllEnableClusters(tx *dbs.Tx) (result []*NodeClu } // 创建集群 -func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string, grantId int64, installDir string, dnsDomainId int64, dnsName string, cachePolicyId int64, httpFirewallPolicyId int64) (clusterId int64, err error) { +func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string, grantId int64, installDir string, dnsDomainId int64, dnsName string, cachePolicyId int64, httpFirewallPolicyId int64, systemServices map[string]maps.Map) (clusterId int64, err error) { uniqueId, err := this.genUniqueId(tx) if err != nil { return 0, err @@ -139,6 +140,13 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string // WAF策略 op.HttpFirewallPolicyId = httpFirewallPolicyId + // 系统服务 + systemServicesJSON, err := json.Marshal(systemServices) + if err != nil { + return 0, err + } + op.SystemServices = systemServicesJSON + op.UseAllAPINodes = 1 op.ApiNodes = "[]" op.UniqueId = uniqueId @@ -652,6 +660,89 @@ func (this *NodeClusterDAO) UpdateNodeClusterHTTPFirewallPolicyId(tx *dbs.Tx, cl return err } +// 修改集群的系统服务设置 +func (this *NodeClusterDAO) UpdateNodeClusterSystemService(tx *dbs.Tx, clusterId int64, serviceType nodeconfigs.SystemServiceType, params maps.Map) error { + if clusterId <= 0 { + return errors.New("invalid clusterId") + } + service, err := this.Query(tx). + Pk(clusterId). + Result("systemServices"). + FindStringCol("") + if err != nil { + return err + } + servicesMap := map[string]maps.Map{} + if IsNotNull(service) { + err = json.Unmarshal([]byte(service), &servicesMap) + if err != nil { + return err + } + } + + if params == nil { + params = maps.Map{} + } + servicesMap[serviceType] = params + servicesJSON, err := json.Marshal(servicesMap) + if err != nil { + return err + } + + _, err = this.Query(tx). + Pk(clusterId). + Set("systemServices", servicesJSON). + Update() + if err != nil { + return err + } + return nil +} + +// 查找集群的系统服务设置 +func (this *NodeClusterDAO) FindNodeClusterSystemServiceParams(tx *dbs.Tx, clusterId int64, serviceType nodeconfigs.SystemServiceType) (params maps.Map, err error) { + if clusterId <= 0 { + return nil, errors.New("invalid clusterId") + } + service, err := this.Query(tx). + Pk(clusterId). + Result("systemServices"). + FindStringCol("") + if err != nil { + return nil, err + } + servicesMap := map[string]maps.Map{} + if IsNotNull(service) { + err = json.Unmarshal([]byte(service), &servicesMap) + if err != nil { + return nil, err + } + } + return servicesMap[serviceType], nil +} + +// 查找集群的所有服务设置 +func (this *NodeClusterDAO) FindNodeClusterSystemServices(tx *dbs.Tx, clusterId int64) (services map[string]maps.Map, err error) { + if clusterId <= 0 { + return nil, errors.New("invalid clusterId") + } + service, err := this.Query(tx). + Pk(clusterId). + Result("systemServices"). + FindStringCol("") + if err != nil { + return nil, err + } + servicesMap := map[string]maps.Map{} + if IsNotNull(service) { + err = json.Unmarshal([]byte(service), &servicesMap) + if err != nil { + return nil, err + } + } + return servicesMap, nil +} + // 生成唯一ID func (this *NodeClusterDAO) genUniqueId(tx *dbs.Tx) (string, error) { for { diff --git a/internal/db/models/node_cluster_model.go b/internal/db/models/node_cluster_model.go index 5fcc24bb..5b29ac86 100644 --- a/internal/db/models/node_cluster_model.go +++ b/internal/db/models/node_cluster_model.go @@ -24,6 +24,7 @@ type NodeCluster struct { CachePolicyId uint32 `field:"cachePolicyId"` // 缓存策略ID HttpFirewallPolicyId uint32 `field:"httpFirewallPolicyId"` // WAF策略ID AccessLog string `field:"accessLog"` // 访问日志设置 + SystemServices string `field:"systemServices"` // 系统服务设置 } type NodeClusterOperator struct { @@ -49,6 +50,7 @@ type NodeClusterOperator struct { CachePolicyId interface{} // 缓存策略ID HttpFirewallPolicyId interface{} // WAF策略ID AccessLog interface{} // 访问日志设置 + SystemServices interface{} // 系统服务设置 } func NewNodeClusterOperator() *NodeClusterOperator { diff --git a/internal/db/models/node_dao.go b/internal/db/models/node_dao.go index 8d03854f..0bf84861 100644 --- a/internal/db/models/node_dao.go +++ b/internal/db/models/node_dao.go @@ -553,6 +553,15 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N } config.TOA = toaConfig + // 系统服务 + services, err := SharedNodeClusterDAO.FindNodeClusterSystemServices(tx, clusterId) + if err != nil { + return nil, err + } + if len(services) > 0 { + config.SystemServices = services + } + return config, nil } diff --git a/internal/rpc/services/service_base.go b/internal/rpc/services/service_base.go index e8e5a74b..3d3f7887 100644 --- a/internal/rpc/services/service_base.go +++ b/internal/rpc/services/service_base.go @@ -91,3 +91,12 @@ func (this *BaseService) PermissionError() error { func (this *BaseService) NullTx() *dbs.Tx { return nil } + +// 获取当前的数据库 +func (this *BaseService) RunTx(callback func(tx *dbs.Tx) error) error { + db, err := dbs.Default() + if err != nil { + return err + } + return db.RunTx(callback) +} diff --git a/internal/rpc/services/service_node_cluster.go b/internal/rpc/services/service_node_cluster.go index e43b49f0..24f50c85 100644 --- a/internal/rpc/services/service_node_cluster.go +++ b/internal/rpc/services/service_node_cluster.go @@ -9,6 +9,8 @@ import ( rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" "github.com/TeaOSLab/EdgeAPI/internal/tasks" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/dbs" + "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" "strconv" ) @@ -24,9 +26,23 @@ func (this *NodeClusterService) CreateNodeCluster(ctx context.Context, req *pb.C return nil, err } - tx := this.NullTx() + systemServices := map[string]maps.Map{} + if len(req.SystemServicesJSON) > 0 { + err = json.Unmarshal(req.SystemServicesJSON, &systemServices) + if err != nil { + return nil, err + } + } + + var clusterId int64 + err = this.RunTx(func(tx *dbs.Tx) error { + clusterId, err = models.SharedNodeClusterDAO.CreateCluster(tx, adminId, req.Name, req.GrantId, req.InstallDir, req.DnsDomainId, req.DnsName, req.HttpCachePolicyId, req.HttpFirewallPolicyId, systemServices) + if err != nil { + return err + } + return nil + }) - clusterId, err := models.SharedNodeClusterDAO.CreateCluster(tx, adminId, req.Name, req.GrantId, req.InstallDir, req.DnsDomainId, req.DnsName, req.HttpCachePolicyId, req.HttpFirewallPolicyId) if err != nil { return nil, err } @@ -777,3 +793,55 @@ func (this *NodeClusterService) UpdateNodeClusterHTTPFirewallPolicyId(ctx contex return this.Success() } + +// 修改集群的系统服务设置 +func (this *NodeClusterService) UpdateNodeClusterSystemService(ctx context.Context, req *pb.UpdateNodeClusterSystemServiceRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + params := maps.Map{} + if len(req.ParamsJSON) > 0 { + err = json.Unmarshal(req.ParamsJSON, ¶ms) + if err != nil { + return nil, err + } + } + + tx := this.NullTx() + err = models.SharedNodeClusterDAO.UpdateNodeClusterSystemService(tx, req.NodeClusterId, req.Type, params) + if err != nil { + return nil, err + } + + // 增加节点版本号 + err = models.SharedNodeDAO.IncreaseAllNodesLatestVersionMatch(tx, req.NodeClusterId) + if err != nil { + return nil, err + } + + return this.Success() +} + +// 查找集群的系统服务设置 +func (this *NodeClusterService) FindNodeClusterSystemService(ctx context.Context, req *pb.FindNodeClusterSystemServiceRequest) (*pb.FindNodeClusterSystemServiceResponse, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + tx := this.NullTx() + params, err := models.SharedNodeClusterDAO.FindNodeClusterSystemServiceParams(tx, req.NodeClusterId, req.Type) + if err != nil { + return nil, err + } + if params == nil { + params = maps.Map{} + } + paramsJSON, err := json.Marshal(params) + if err != nil { + return nil, err + } + return &pb.FindNodeClusterSystemServiceResponse{ParamsJSON: paramsJSON}, nil +}