mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-08 03:00:26 +08:00
优化节点设置交互
This commit is contained in:
@@ -190,7 +190,7 @@ func (this *NodeDAO) CreateNode(tx *dbs.Tx, adminId int64, name string, clusterI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNode 修改节点
|
// 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 {
|
if nodeId <= 0 {
|
||||||
return errors.New("invalid nodeId")
|
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.GroupId = groupId
|
||||||
op.RegionId = regionId
|
op.RegionId = regionId
|
||||||
op.LatestVersion = dbs.SQL("latestVersion+1")
|
op.LatestVersion = dbs.SQL("latestVersion+1")
|
||||||
op.MaxCPU = maxCPU
|
|
||||||
op.IsOn = isOn
|
op.IsOn = isOn
|
||||||
if len(maxCacheDiskCapacityJSON) > 0 {
|
|
||||||
op.MaxCacheDiskCapacity = maxCacheDiskCapacityJSON
|
|
||||||
}
|
|
||||||
if len(maxCacheMemoryCapacityJSON) > 0 {
|
|
||||||
op.MaxCacheMemoryCapacity = maxCacheMemoryCapacityJSON
|
|
||||||
}
|
|
||||||
err = this.Save(tx, op)
|
err = this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -500,9 +493,9 @@ func (this *NodeDAO) FindAllInactiveNodesWithClusterId(tx *dbs.Tx, clusterId int
|
|||||||
_, err = this.Query(tx).
|
_, err = this.Query(tx).
|
||||||
State(NodeStateEnabled).
|
State(NodeStateEnabled).
|
||||||
Attr("clusterId", clusterId).
|
Attr("clusterId", clusterId).
|
||||||
Attr("isOn", true). // 只监控启用的节点
|
Attr("isOn", true). // 只监控启用的节点
|
||||||
Attr("isInstalled", 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)").
|
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").
|
Result("id", "name").
|
||||||
Slice(&result).
|
Slice(&result).
|
||||||
@@ -1113,6 +1106,41 @@ func (this *NodeDAO) UpdateNodeDNS(tx *dbs.Tx, nodeId int64, routes map[int64][]
|
|||||||
return nil
|
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 计算节点上线|下线状态
|
// UpdateNodeUpCount 计算节点上线|下线状态
|
||||||
func (this *NodeDAO) UpdateNodeUpCount(tx *dbs.Tx, nodeId int64, isUp bool, maxUp int, maxDown int) (changed bool, err error) {
|
func (this *NodeDAO) UpdateNodeUpCount(tx *dbs.Tx, nodeId int64, isUp bool, maxUp int, maxDown int) (changed bool, err error) {
|
||||||
if nodeId <= 0 {
|
if nodeId <= 0 {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -65,6 +66,11 @@ func (this *Node) DNSRouteCodesForDomainId(dnsDomainId int64) ([]string, error)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
domainRoutes, _ := routes[dnsDomainId]
|
domainRoutes, _ := routes[dnsDomainId]
|
||||||
|
|
||||||
|
if len(domainRoutes) > 0 {
|
||||||
|
sort.Strings(domainRoutes)
|
||||||
|
}
|
||||||
|
|
||||||
return domainRoutes, nil
|
return domainRoutes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -409,79 +409,11 @@ func (this *NodeService) UpdateNode(ctx context.Context, req *pb.UpdateNodeReque
|
|||||||
|
|
||||||
tx := this.NullTx()
|
tx := this.NullTx()
|
||||||
|
|
||||||
var maxCacheDiskCapacityJSON []byte
|
err = models.SharedNodeDAO.UpdateNode(tx, req.NodeId, req.Name, req.NodeClusterId, req.SecondaryNodeClusterIds, req.NodeGroupId, req.NodeRegionId, req.IsOn)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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()
|
return this.Success()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1377,22 +1309,43 @@ func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDN
|
|||||||
}
|
}
|
||||||
|
|
||||||
routeCodeMap := node.DNSRouteCodes()
|
routeCodeMap := node.DNSRouteCodes()
|
||||||
if req.DnsDomainId > 0 && len(req.Routes) > 0 {
|
if req.DnsDomainId > 0 {
|
||||||
var m = map[int64][]string{} // domainId => codes
|
if len(req.Routes) > 0 {
|
||||||
for _, route := range req.Routes {
|
var m = map[int64][]string{} // domainId => codes
|
||||||
var pieces = strings.SplitN(route, "@", 2)
|
for _, route := range req.Routes {
|
||||||
if len(pieces) != 2 {
|
var pieces = strings.SplitN(route, "@", 2)
|
||||||
continue
|
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]
|
for domainId, codes := range m {
|
||||||
var domainId = types.Int64(pieces[1])
|
routeCodeMap[domainId] = codes
|
||||||
m[domainId] = append(m[domainId], code)
|
}
|
||||||
}
|
} else {
|
||||||
for domainId, codes := range m {
|
delete(routeCodeMap, req.DnsDomainId)
|
||||||
routeCodeMap[domainId] = codes
|
|
||||||
}
|
}
|
||||||
} else {
|
} 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)
|
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),
|
Filename: filepath.Base(file.Path),
|
||||||
}, nil
|
}, 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()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user