优化节点设置交互

This commit is contained in:
刘祥超
2021-09-13 16:47:40 +08:00
parent a96c1434b6
commit c2f559d48e
3 changed files with 133 additions and 92 deletions

View File

@@ -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
@@ -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 {

View File

@@ -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
}

View File

@@ -409,78 +409,10 @@ 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,
})
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
}
}
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 {
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,7 +1309,8 @@ func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDN
}
routeCodeMap := node.DNSRouteCodes()
if req.DnsDomainId > 0 && len(req.Routes) > 0 {
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)
@@ -1394,6 +1327,26 @@ func (this *NodeService) UpdateNodeDNS(ctx context.Context, req *pb.UpdateNodeDN
} else {
delete(routeCodeMap, req.DnsDomainId)
}
} else {
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)
if err != nil {
@@ -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()
}