diff --git a/internal/db/models/node_dao.go b/internal/db/models/node_dao.go index d925bb40..ebb869f4 100644 --- a/internal/db/models/node_dao.go +++ b/internal/db/models/node_dao.go @@ -190,7 +190,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI } // UpdateNode 修改节点 -func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, maxCPU int32, isOn bool, maxCacheDiskCapacityJSON []byte, maxCacheMemoryCapacityJSON []byte) error { +func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId int64, secondaryClusterIds []int64, groupId int64, regionId int64, isOn bool) error { if nodeId <= 0 { return errors.New("invalid nodeId") } @@ -226,14 +226,7 @@ func (this *NodeDAO) UpdateNode(tx *dbs.Tx, nodeId int64, name string, clusterId op.GroupId = groupId op.RegionId = regionId op.LatestVersion = dbs.SQL("latestVersion+1") - op.MaxCPU = maxCPU op.IsOn = isOn - if len(maxCacheDiskCapacityJSON) > 0 { - op.MaxCacheDiskCapacity = maxCacheDiskCapacityJSON - } - if len(maxCacheMemoryCapacityJSON) > 0 { - op.MaxCacheMemoryCapacity = maxCacheMemoryCapacityJSON - } err = this.Save(tx, op) if err != nil { return err @@ -500,9 +493,9 @@ func (this *NodeDAO) FindAllInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int _, err = this.Query(tx). State(NodeStateEnabled). Attr("clusterId", clusterId). - Attr("isOn", true). // 只监控启用的节点 + Attr("isOn", true). // 只监控启用的节点 Attr("isInstalled", true). // 只监控已经安装的节点 - Attr("isActive", true). // 当前已经在线的 + Attr("isActive", true). // 当前已经在线的 Where("(status IS NULL OR (JSON_EXTRACT(status, '$.isActive')=false AND UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>10) OR UNIX_TIMESTAMP()-JSON_EXTRACT(status, '$.updatedAt')>120)"). Result("id", "name"). Slice(&result). @@ -1113,6 +1106,41 @@ func (this *NodeDAO) UpdateNodeDNS(tx *dbs.Tx, nodeId int64, routes map[int64][] return nil } +// UpdateNodeSystem 设置系统信息 +func (this *NodeDAO) UpdateNodeSystem(tx *dbs.Tx, nodeId int64, maxCPU int32) error { + if nodeId <= 0 { + return errors.New("invalid nodeId") + } + var op = NewNodeOperator() + op.Id = nodeId + op.MaxCPU = maxCPU + err := this.Save(tx, op) + if err != nil { + return err + } + return this.NotifyUpdate(tx, nodeId) +} + +// UpdateNodeCache 设置缓存相关 +func (this *NodeDAO) UpdateNodeCache(tx *dbs.Tx, nodeId int64, maxCacheDiskCapacityJSON []byte, maxCacheMemoryCapacityJSON []byte) error { + if nodeId <= 0 { + return errors.New("invalid nodeId") + } + var op = NewNodeOperator() + op.Id = nodeId + if len(maxCacheDiskCapacityJSON) > 0 { + op.MaxCacheDiskCapacity = maxCacheDiskCapacityJSON + } + if len(maxCacheMemoryCapacityJSON) > 0 { + op.MaxCacheMemoryCapacity = maxCacheMemoryCapacityJSON + } + err := this.Save(tx, op) + if err != nil { + return err + } + return this.NotifyUpdate(tx, nodeId) +} + // UpdateNodeUpCount 计算节点上线|下线状态 func (this *NodeDAO) UpdateNodeUpCount(tx *dbs.Tx, nodeId int64, isUp bool, maxUp int, maxDown int) (changed bool, err error) { if nodeId <= 0 { diff --git a/internal/db/models/node_model_ext.go b/internal/db/models/node_model_ext.go index e04512ba..d097e020 100644 --- a/internal/db/models/node_model_ext.go +++ b/internal/db/models/node_model_ext.go @@ -3,6 +3,7 @@ package models import ( "encoding/json" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" + "sort" "time" ) @@ -65,6 +66,11 @@ func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error) return nil, err } domainRoutes, _ := routes[dnsDomainId] + + if len(domainRoutes) > 0 { + sort.Strings(domainRoutes) + } + return domainRoutes, nil } diff --git a/internal/rpc/services/service_node.go b/internal/rpc/services/service_node.go index 9c5fa9d3..d12202cb 100644 --- a/internal/rpc/services/service_node.go +++ b/internal/rpc/services/service_node.go @@ -409,79 +409,11 @@ func (this *NodeService) UpdateNode(ctx context.Context, req *pb.UpdateNodeReque tx := this.NullTx() - var maxCacheDiskCapacityJSON []byte - if req.MaxCacheDiskCapacity != nil { - maxCacheDiskCapacityJSON, err = json.Marshal(&shared.SizeCapacity{ - Count: req.MaxCacheDiskCapacity.Count, - Unit: req.MaxCacheDiskCapacity.Unit, - }) - if err != nil { - return nil, err - } - } - - var maxCacheMemoryCapacityJSON []byte - if req.MaxCacheMemoryCapacity != nil { - maxCacheMemoryCapacityJSON, err = json.Marshal(&shared.SizeCapacity{ - Count: req.MaxCacheMemoryCapacity.Count, - Unit: req.MaxCacheMemoryCapacity.Unit, - }) - if err != nil { - return nil, err - } - } - - err = models.SharedNodeDAO.UpdateNode(tx, req.NodeId, req.Name, req.NodeClusterId, req.SecondaryNodeClusterIds, req.NodeGroupId, req.NodeRegionId, req.MaxCPU, req.IsOn, maxCacheDiskCapacityJSON, maxCacheMemoryCapacityJSON) + err = models.SharedNodeDAO.UpdateNode(tx, req.NodeId, req.Name, req.NodeClusterId, req.SecondaryNodeClusterIds, req.NodeGroupId, req.NodeRegionId, req.IsOn) if err != nil { return nil, err } - // 登录信息 - if req.NodeLogin == nil { - err = models.SharedNodeLoginDAO.DisableNodeLogins(tx, nodeconfigs.NodeRoleNode, req.NodeId) - if err != nil { - return nil, err - } - } else { - if req.NodeLogin.Id > 0 { - err = models.SharedNodeLoginDAO.UpdateNodeLogin(tx, req.NodeLogin.Id, req.NodeLogin.Name, req.NodeLogin.Type, req.NodeLogin.Params) - if err != nil { - return nil, err - } - } else { - _, err = models.SharedNodeLoginDAO.CreateNodeLogin(tx, nodeconfigs.NodeRoleNode, req.NodeId, req.NodeLogin.Name, req.NodeLogin.Type, req.NodeLogin.Params) - if err != nil { - return nil, err - } - } - } - - // 保存DNS相关 - nodeDNS, err := models.SharedNodeDAO.FindEnabledNodeDNS(tx, req.NodeId) - if err != nil { - return nil, err - } - if nodeDNS != nil { - var routesMap = nodeDNS.DNSRouteCodes() - var m = map[int64][]string{} // domainId => codes - for _, route := range req.DnsRoutes { - var pieces = strings.SplitN(route, "@", 2) - if len(pieces) != 2 { - continue - } - var code = pieces[0] - var domainId = types.Int64(pieces[1]) - m[domainId] = append(m[domainId], code) - } - for domainId, codes := range m { - routesMap[domainId] = codes - } - err = models.SharedNodeDAO.UpdateNodeDNS(tx, req.NodeId, routesMap) - if err != nil { - return nil, err - } - } - return this.Success() } @@ -1377,22 +1309,43 @@ func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDN } routeCodeMap := node.DNSRouteCodes() - if req.DnsDomainId > 0 && len(req.Routes) > 0 { - var m = map[int64][]string{} // domainId => codes - for _, route := range req.Routes { - var pieces = strings.SplitN(route, "@", 2) - if len(pieces) != 2 { - continue + if req.DnsDomainId > 0 { + if len(req.Routes) > 0 { + var m = map[int64][]string{} // domainId => codes + for _, route := range req.Routes { + var pieces = strings.SplitN(route, "@", 2) + if len(pieces) != 2 { + continue + } + var code = pieces[0] + var domainId = types.Int64(pieces[1]) + m[domainId] = append(m[domainId], code) } - var code = pieces[0] - var domainId = types.Int64(pieces[1]) - m[domainId] = append(m[domainId], code) - } - for domainId, codes := range m { - routeCodeMap[domainId] = codes + for domainId, codes := range m { + routeCodeMap[domainId] = codes + } + } else { + delete(routeCodeMap, req.DnsDomainId) } } else { - delete(routeCodeMap, req.DnsDomainId) + if len(req.Routes) > 0 { + var m = map[int64][]string{} // domainId => codes + for _, route := range req.Routes { + var pieces = strings.SplitN(route, "@", 2) + if len(pieces) != 2 { + continue + } + var code = pieces[0] + var domainId = types.Int64(pieces[1]) + m[domainId] = append(m[domainId], code) + } + for domainId, codes := range m { + routeCodeMap[domainId] = codes + } + } else { + // 清空 + routeCodeMap = map[int64][]string{} + } } err = models.SharedNodeDAO.UpdateNodeDNS(tx, req.NodeId, routeCodeMap) @@ -1532,3 +1485,57 @@ func (this *NodeService) DownloadNodeInstallationFile(ctx context.Context, req * Filename: filepath.Base(file.Path), }, nil } + +// UpdateNodeSystem 修改节点系统信息 +func (this *NodeService) UpdateNodeSystem(ctx context.Context, req *pb.UpdateNodeSystemRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + err = models.SharedNodeDAO.UpdateNodeSystem(tx, req.NodeId, req.MaxCPU) + if err != nil { + return nil, err + } + return this.Success() +} + +// UpdateNodeCache 修改节点缓存设置 +func (this *NodeService) UpdateNodeCache(ctx context.Context, req *pb.UpdateNodeCacheRequest) (*pb.RPCSuccess, error) { + _, err := this.ValidateAdmin(ctx, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + + var maxCacheDiskCapacityJSON []byte + if req.MaxCacheDiskCapacity != nil { + maxCacheDiskCapacityJSON, err = json.Marshal(&shared.SizeCapacity{ + Count: req.MaxCacheDiskCapacity.Count, + Unit: req.MaxCacheDiskCapacity.Unit, + }) + if err != nil { + return nil, err + } + } + + var maxCacheMemoryCapacityJSON []byte + if req.MaxCacheMemoryCapacity != nil { + maxCacheMemoryCapacityJSON, err = json.Marshal(&shared.SizeCapacity{ + Count: req.MaxCacheMemoryCapacity.Count, + Unit: req.MaxCacheMemoryCapacity.Unit, + }) + if err != nil { + return nil, err + } + } + + err = models.SharedNodeDAO.UpdateNodeCache(tx, req.NodeId, maxCacheDiskCapacityJSON, maxCacheMemoryCapacityJSON) + if err != nil { + return nil, err + } + + return this.Success() +}