2020-09-06 16:19:34 +08:00
|
|
|
package node
|
2020-07-30 22:41:35 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2021-09-13 16:47:34 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/cluster/node/nodeutils"
|
2020-10-17 21:15:22 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/ipAddresses/ipaddressutils"
|
2023-06-30 18:08:30 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
2021-05-26 14:43:29 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
2020-10-10 12:31:59 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-07-30 22:41:35 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2022-04-07 21:37:13 +08:00
|
|
|
"github.com/iwind/TeaGo/types"
|
2022-08-25 20:36:51 +08:00
|
|
|
"net"
|
2020-07-30 22:41:35 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UpdateAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *UpdateAction) Init() {
|
2020-09-06 16:19:34 +08:00
|
|
|
this.Nav("", "node", "update")
|
2021-09-13 16:47:34 +08:00
|
|
|
this.SecondMenu("basic")
|
2020-07-30 22:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *UpdateAction) RunGet(params struct {
|
|
|
|
|
NodeId int64
|
|
|
|
|
}) {
|
2021-09-13 16:47:34 +08:00
|
|
|
_, err := nodeutils.InitNodeInfo(this.Parent(), params.NodeId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 22:41:35 +08:00
|
|
|
this.Data["nodeId"] = params.NodeId
|
|
|
|
|
|
|
|
|
|
nodeResp, err := this.RPC().NodeRPC().FindEnabledNode(this.AdminContext(), &pb.FindEnabledNodeRequest{NodeId: params.NodeId})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-04-04 12:08:18 +08:00
|
|
|
var node = nodeResp.Node
|
2020-07-30 22:41:35 +08:00
|
|
|
if node == nil {
|
|
|
|
|
this.WriteString("找不到要操作的节点")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var clusterMap maps.Map = nil
|
2020-12-17 17:35:38 +08:00
|
|
|
if node.NodeCluster != nil {
|
2020-07-30 22:41:35 +08:00
|
|
|
clusterMap = maps.Map{
|
2020-12-17 17:35:38 +08:00
|
|
|
"id": node.NodeCluster.Id,
|
|
|
|
|
"name": node.NodeCluster.Name,
|
2020-07-30 22:41:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
// IP地址
|
2021-09-13 13:45:39 +08:00
|
|
|
ipAddressesResp, err := this.RPC().NodeIPAddressRPC().FindAllEnabledNodeIPAddressesWithNodeId(this.AdminContext(), &pb.FindAllEnabledNodeIPAddressesWithNodeIdRequest{
|
2021-05-26 14:43:29 +08:00
|
|
|
NodeId: params.NodeId,
|
|
|
|
|
Role: nodeconfigs.NodeRoleNode,
|
|
|
|
|
})
|
2020-08-21 21:09:42 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-08-25 20:36:51 +08:00
|
|
|
var ipAddressMaps = []maps.Map{}
|
2021-09-13 13:45:39 +08:00
|
|
|
for _, addr := range ipAddressesResp.NodeIPAddresses {
|
2023-03-01 11:38:21 +08:00
|
|
|
// 阈值
|
2021-09-12 20:21:32 +08:00
|
|
|
thresholds, err := ipaddressutils.InitNodeIPAddressThresholds(this.Parent(), addr.Id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
2021-08-18 16:19:07 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 11:38:21 +08:00
|
|
|
// 专属集群
|
|
|
|
|
var clusterMaps = []maps.Map{}
|
|
|
|
|
for _, addrCluster := range addr.NodeClusters {
|
|
|
|
|
clusterMaps = append(clusterMaps, maps.Map{
|
|
|
|
|
"id": addrCluster.Id,
|
|
|
|
|
"name": addrCluster.Name,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
ipAddressMaps = append(ipAddressMaps, maps.Map{
|
2021-08-18 16:19:07 +08:00
|
|
|
"id": addr.Id,
|
|
|
|
|
"name": addr.Name,
|
|
|
|
|
"ip": addr.Ip,
|
|
|
|
|
"canAccess": addr.CanAccess,
|
|
|
|
|
"isOn": addr.IsOn,
|
|
|
|
|
"isUp": addr.IsUp,
|
|
|
|
|
"thresholds": thresholds,
|
2023-03-01 11:38:21 +08:00
|
|
|
"clusters": clusterMaps,
|
2020-08-21 21:09:42 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 16:11:41 +08:00
|
|
|
// 分组
|
2020-10-28 20:00:27 +08:00
|
|
|
var groupMap maps.Map = nil
|
2021-05-25 17:48:51 +08:00
|
|
|
if node.NodeGroup != nil {
|
2020-10-28 20:00:27 +08:00
|
|
|
groupMap = maps.Map{
|
2021-05-25 17:48:51 +08:00
|
|
|
"id": node.NodeGroup.Id,
|
|
|
|
|
"name": node.NodeGroup.Name,
|
2020-10-28 20:00:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 16:11:41 +08:00
|
|
|
// 区域
|
|
|
|
|
var regionMap maps.Map = nil
|
2021-05-25 17:48:51 +08:00
|
|
|
if node.NodeRegion != nil {
|
2020-12-10 16:11:41 +08:00
|
|
|
regionMap = maps.Map{
|
2021-05-25 17:48:51 +08:00
|
|
|
"id": node.NodeRegion.Id,
|
|
|
|
|
"name": node.NodeRegion.Name,
|
2020-12-10 16:11:41 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 12:08:18 +08:00
|
|
|
var nodeMap = maps.Map{
|
2022-10-26 10:41:42 +08:00
|
|
|
"id": node.Id,
|
|
|
|
|
"name": node.Name,
|
|
|
|
|
"ipAddresses": ipAddressMaps,
|
|
|
|
|
"cluster": clusterMap,
|
|
|
|
|
"isOn": node.IsOn,
|
|
|
|
|
"group": groupMap,
|
|
|
|
|
"region": regionMap,
|
|
|
|
|
"level": node.Level,
|
|
|
|
|
"enableIPLists": node.EnableIPLists,
|
2020-07-30 22:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
2022-08-25 20:36:51 +08:00
|
|
|
if node.LnAddrs == nil {
|
|
|
|
|
nodeMap["lnAddrs"] = []string{}
|
|
|
|
|
} else {
|
|
|
|
|
nodeMap["lnAddrs"] = node.LnAddrs
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-31 22:23:07 +08:00
|
|
|
if node.NodeCluster != nil {
|
2022-04-04 12:08:18 +08:00
|
|
|
nodeMap["primaryCluster"] = maps.Map{
|
2021-07-31 22:23:07 +08:00
|
|
|
"id": node.NodeCluster.Id,
|
|
|
|
|
"name": node.NodeCluster.Name,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-04-04 12:08:18 +08:00
|
|
|
nodeMap["primaryCluster"] = nil
|
2020-07-30 22:41:35 +08:00
|
|
|
}
|
2021-07-31 22:23:07 +08:00
|
|
|
|
|
|
|
|
if len(node.SecondaryNodeClusters) > 0 {
|
|
|
|
|
var secondaryClusterMaps = []maps.Map{}
|
|
|
|
|
for _, cluster := range node.SecondaryNodeClusters {
|
|
|
|
|
secondaryClusterMaps = append(secondaryClusterMaps, maps.Map{
|
|
|
|
|
"id": cluster.Id,
|
|
|
|
|
"name": cluster.Name,
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-04-04 12:08:18 +08:00
|
|
|
nodeMap["secondaryClusters"] = secondaryClusterMaps
|
2021-07-31 22:23:07 +08:00
|
|
|
} else {
|
2022-04-04 12:08:18 +08:00
|
|
|
nodeMap["secondaryClusters"] = []interface{}{}
|
2020-07-30 22:41:35 +08:00
|
|
|
}
|
2021-07-31 22:23:07 +08:00
|
|
|
|
2022-04-04 12:08:18 +08:00
|
|
|
this.Data["node"] = nodeMap
|
2020-07-30 22:41:35 +08:00
|
|
|
|
2022-04-07 21:37:13 +08:00
|
|
|
this.Data["canUpdateLevel"] = this.CanUpdateLevel(2)
|
|
|
|
|
|
2020-07-30 22:41:35 +08:00
|
|
|
this.Show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *UpdateAction) RunPost(params struct {
|
2021-09-13 16:47:34 +08:00
|
|
|
LoginId int64
|
|
|
|
|
NodeId int64
|
|
|
|
|
GroupId int64
|
|
|
|
|
RegionId int64
|
|
|
|
|
Name string
|
|
|
|
|
IPAddressesJSON []byte `alias:"ipAddressesJSON"`
|
|
|
|
|
PrimaryClusterId int64
|
|
|
|
|
SecondaryClusterIds []byte
|
|
|
|
|
IsOn bool
|
2022-04-04 12:08:18 +08:00
|
|
|
Level int32
|
2022-08-25 20:36:51 +08:00
|
|
|
LnAddrs []string
|
2022-10-26 10:41:42 +08:00
|
|
|
EnableIPLists bool
|
2020-11-16 13:03:45 +08:00
|
|
|
|
2020-07-30 22:41:35 +08:00
|
|
|
Must *actions.Must
|
|
|
|
|
}) {
|
2020-11-10 21:37:48 +08:00
|
|
|
// 创建日志
|
2023-06-30 18:08:30 +08:00
|
|
|
defer this.CreateLogInfo(codes.Node_LogUpdateNode, params.NodeId)
|
2020-11-10 21:37:48 +08:00
|
|
|
|
2020-07-30 22:41:35 +08:00
|
|
|
if params.NodeId <= 0 {
|
|
|
|
|
this.Fail("要操作的节点不存在")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("name", params.Name).
|
|
|
|
|
Require("请输入节点名称")
|
|
|
|
|
|
|
|
|
|
// TODO 检查cluster
|
2021-07-31 22:23:07 +08:00
|
|
|
if params.PrimaryClusterId <= 0 {
|
|
|
|
|
this.Fail("请选择节点所在主集群")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var secondaryClusterIds = []int64{}
|
|
|
|
|
if len(params.SecondaryClusterIds) > 0 {
|
|
|
|
|
err := json.Unmarshal(params.SecondaryClusterIds, &secondaryClusterIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-07-30 22:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 19:44:10 +08:00
|
|
|
// IP地址
|
2022-08-25 20:36:51 +08:00
|
|
|
var ipAddresses = []maps.Map{}
|
2020-12-23 19:44:10 +08:00
|
|
|
if len(params.IPAddressesJSON) > 0 {
|
|
|
|
|
err := json.Unmarshal(params.IPAddressesJSON, &ipAddresses)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(ipAddresses) == 0 {
|
|
|
|
|
this.Fail("请至少输入一个IP地址")
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 22:41:35 +08:00
|
|
|
// 保存
|
2022-04-07 21:37:13 +08:00
|
|
|
if !this.CanUpdateLevel(params.Level) {
|
|
|
|
|
this.Fail("没有权限修改节点级别:" + types.String(params.Level))
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 20:36:51 +08:00
|
|
|
// 检查Ln节点地址
|
|
|
|
|
var lnAddrs = []string{}
|
|
|
|
|
if params.Level > 1 {
|
|
|
|
|
for _, lnAddr := range params.LnAddrs {
|
|
|
|
|
if len(lnAddr) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理 host:port
|
|
|
|
|
host, _, err := net.SplitHostPort(lnAddr)
|
|
|
|
|
if err == nil {
|
|
|
|
|
lnAddr = host
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if net.ParseIP(lnAddr) == nil {
|
|
|
|
|
this.Fail("L2级别访问地址 '" + lnAddr + "' 格式错误,请纠正后再提交")
|
|
|
|
|
}
|
|
|
|
|
lnAddrs = append(lnAddrs, lnAddr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 22:30:40 +08:00
|
|
|
_, err := this.RPC().NodeRPC().UpdateNode(this.AdminContext(), &pb.UpdateNodeRequest{
|
2021-07-31 22:23:07 +08:00
|
|
|
NodeId: params.NodeId,
|
|
|
|
|
NodeGroupId: params.GroupId,
|
|
|
|
|
NodeRegionId: params.RegionId,
|
|
|
|
|
Name: params.Name,
|
|
|
|
|
NodeClusterId: params.PrimaryClusterId,
|
|
|
|
|
SecondaryNodeClusterIds: secondaryClusterIds,
|
|
|
|
|
IsOn: params.IsOn,
|
2022-04-04 12:08:18 +08:00
|
|
|
Level: params.Level,
|
2022-08-25 20:36:51 +08:00
|
|
|
LnAddrs: lnAddrs,
|
2022-10-26 10:41:42 +08:00
|
|
|
EnableIPLists: params.EnableIPLists,
|
2020-07-30 22:41:35 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
// 禁用老的IP地址
|
2021-09-13 13:45:39 +08:00
|
|
|
_, err = this.RPC().NodeIPAddressRPC().DisableAllNodeIPAddressesWithNodeId(this.AdminContext(), &pb.DisableAllNodeIPAddressesWithNodeIdRequest{
|
2021-05-26 14:43:29 +08:00
|
|
|
NodeId: params.NodeId,
|
|
|
|
|
Role: nodeconfigs.NodeRoleNode,
|
|
|
|
|
})
|
2020-08-21 21:09:42 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加新的IP地址
|
2021-05-26 14:43:29 +08:00
|
|
|
err = ipaddressutils.UpdateNodeIPAddresses(this.Parent(), params.NodeId, nodeconfigs.NodeRoleNode, params.IPAddressesJSON)
|
2020-08-21 21:09:42 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 22:41:35 +08:00
|
|
|
this.Success()
|
|
|
|
|
}
|