Files
EdgeAdmin/internal/web/actions/default/clusters/cluster/node/update.go

358 lines
9.1 KiB
Go
Raw Normal View History

2020-09-06 16:19:34 +08:00
package node
2020-07-30 22:41:35 +08:00
import (
"encoding/json"
2020-11-10 21:37:48 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
2020-07-30 22:41:35 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2020-10-25 21:27:28 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/grants/grantutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/ipAddresses/ipaddressutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
2020-07-30 22:41:35 +08:00
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
2020-09-06 16:19:34 +08:00
this.Nav("", "node", "update")
this.SecondMenu("nodes")
2020-07-30 22:41:35 +08:00
}
func (this *UpdateAction) RunGet(params struct {
NodeId int64
}) {
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
}
node := nodeResp.Node
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地址
ipAddressesResp, err := this.RPC().NodeIPAddressRPC().FindAllEnabledIPAddressesWithNodeId(this.AdminContext(), &pb.FindAllEnabledIPAddressesWithNodeIdRequest{NodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
ipAddressMaps := []maps.Map{}
for _, addr := range ipAddressesResp.Addresses {
ipAddressMaps = append(ipAddressMaps, maps.Map{
"id": addr.Id,
"name": addr.Name,
"ip": addr.Ip,
"canAccess": addr.CanAccess,
2020-08-21 21:09:42 +08:00
})
}
2020-11-16 13:03:45 +08:00
// DNS相关
dnsInfoResp, err := this.RPC().NodeRPC().FindEnabledNodeDNS(this.AdminContext(), &pb.FindEnabledNodeDNSRequest{NodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
nodeDNS := dnsInfoResp.Node
2020-11-16 13:03:45 +08:00
dnsRouteMaps := []maps.Map{}
if nodeDNS != nil {
for _, dnsInfo := range nodeDNS.Routes {
dnsRouteMaps = append(dnsRouteMaps, maps.Map{
"name": dnsInfo.Name,
"code": dnsInfo.Code,
})
}
2020-11-16 13:03:45 +08:00
}
this.Data["dnsRoutes"] = dnsRouteMaps
this.Data["allDNSRoutes"] = []maps.Map{}
if nodeDNS != nil {
this.Data["dnsDomainId"] = nodeDNS.DnsDomainId
} else {
this.Data["dnsDomainId"] = 0
}
if nodeDNS != nil && nodeDNS.DnsDomainId > 0 {
2020-11-16 13:03:45 +08:00
routesMaps := []maps.Map{}
routesResp, err := this.RPC().DNSDomainRPC().FindAllDNSDomainRoutes(this.AdminContext(), &pb.FindAllDNSDomainRoutesRequest{DnsDomainId: dnsInfoResp.Node.DnsDomainId})
if err != nil {
this.ErrorPage(err)
return
}
for _, route := range routesResp.Routes {
routesMaps = append(routesMaps, maps.Map{
"name": route.Name,
"code": route.Code,
})
}
this.Data["allDNSRoutes"] = routesMaps
}
2020-08-21 21:09:42 +08:00
// 登录信息
2020-07-30 22:41:35 +08:00
var loginMap maps.Map = nil
if node.Login != nil {
loginParams := maps.Map{}
if len(node.Login.Params) > 0 {
err = json.Unmarshal(node.Login.Params, &loginParams)
if err != nil {
this.ErrorPage(err)
return
}
}
grantMap := maps.Map{}
grantId := loginParams.GetInt64("grantId")
if grantId > 0 {
2021-04-18 21:19:50 +08:00
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledNodeGrant(this.AdminContext(), &pb.FindEnabledNodeGrantRequest{NodeGrantId: grantId})
2020-07-30 22:41:35 +08:00
if err != nil {
this.ErrorPage(err)
return
}
2021-04-18 21:19:50 +08:00
if grantResp.NodeGrant != nil {
2020-07-30 22:41:35 +08:00
grantMap = maps.Map{
2021-04-18 21:19:50 +08:00
"id": grantResp.NodeGrant.Id,
"name": grantResp.NodeGrant.Name,
"method": grantResp.NodeGrant.Method,
"methodName": grantutils.FindGrantMethodName(grantResp.NodeGrant.Method),
2020-07-30 22:41:35 +08:00
}
}
}
loginMap = maps.Map{
"id": node.Login.Id,
"name": node.Login.Name,
"type": node.Login.Type,
"params": loginParams,
"grant": grantMap,
}
}
2020-12-10 16:11:41 +08:00
// 分组
var groupMap maps.Map = nil
2021-05-25 17:48:51 +08:00
if node.NodeGroup != nil {
groupMap = maps.Map{
2021-05-25 17:48:51 +08:00
"id": node.NodeGroup.Id,
"name": node.NodeGroup.Name,
}
}
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
}
}
// 缓存硬盘 & 内存容量
var maxCacheDiskCapacity maps.Map = nil
if node.MaxCacheDiskCapacity != nil {
maxCacheDiskCapacity = maps.Map{
"count": node.MaxCacheDiskCapacity.Count,
"unit": node.MaxCacheDiskCapacity.Unit,
}
} else {
maxCacheDiskCapacity = maps.Map{
"count": 0,
"unit": "gb",
}
}
var maxCacheMemoryCapacity maps.Map = nil
if node.MaxCacheMemoryCapacity != nil {
maxCacheMemoryCapacity = maps.Map{
"count": node.MaxCacheMemoryCapacity.Count,
"unit": node.MaxCacheMemoryCapacity.Unit,
}
} else {
maxCacheMemoryCapacity = maps.Map{
"count": 0,
"unit": "gb",
}
}
2020-07-30 22:41:35 +08:00
this.Data["node"] = maps.Map{
"id": node.Id,
"name": node.Name,
"ipAddresses": ipAddressMaps,
"cluster": clusterMap,
"login": loginMap,
"maxCPU": node.MaxCPU,
"isOn": node.IsOn,
"group": groupMap,
"region": regionMap,
"maxCacheDiskCapacity": maxCacheDiskCapacity,
"maxCacheMemoryCapacity": maxCacheMemoryCapacity,
2020-07-30 22:41:35 +08:00
}
// 所有集群
2020-09-06 16:19:34 +08:00
resp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{})
2020-07-30 22:41:35 +08:00
if err != nil {
this.ErrorPage(err)
}
if err != nil {
this.ErrorPage(err)
return
}
clusterMaps := []maps.Map{}
2020-12-17 17:35:38 +08:00
for _, cluster := range resp.NodeClusters {
2020-07-30 22:41:35 +08:00
clusterMaps = append(clusterMaps, maps.Map{
"id": cluster.Id,
"name": cluster.Name,
})
}
this.Data["clusters"] = clusterMaps
this.Show()
}
func (this *UpdateAction) RunPost(params struct {
LoginId int64
NodeId int64
GroupId int64
RegionId int64
Name string
IPAddressesJSON []byte `alias:"ipAddressesJSON"`
ClusterId int64
GrantId int64
SshHost string
SshPort int
MaxCPU int32
IsOn bool
MaxCacheDiskCapacityJSON []byte
MaxCacheMemoryCapacityJSON []byte
2020-07-30 22:41:35 +08:00
2020-11-16 13:03:45 +08:00
DnsDomainId int64
DnsRoutesJSON []byte
2020-07-30 22:41:35 +08:00
Must *actions.Must
}) {
2020-11-10 21:37:48 +08:00
// 创建日志
2020-11-20 15:32:42 +08:00
defer this.CreateLog(oplogs.LevelInfo, "修改节点 %d", 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
if params.ClusterId <= 0 {
this.Fail("请选择所在集群")
}
2020-12-23 19:44:10 +08:00
// IP地址
ipAddresses := []maps.Map{}
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-11-16 13:03:45 +08:00
dnsRouteCodes := []string{}
if len(params.DnsRoutesJSON) > 0 {
err := json.Unmarshal(params.DnsRoutesJSON, &dnsRouteCodes)
if err != nil {
this.ErrorPage(err)
return
}
2020-11-16 13:03:45 +08:00
}
2020-07-30 22:41:35 +08:00
// TODO 检查登录授权
loginInfo := &pb.NodeLogin{
Id: params.LoginId,
Name: "SSH",
Type: "ssh",
Params: maps.Map{
"grantId": params.GrantId,
"host": params.SshHost,
"port": params.SshPort,
}.AsJSON(),
}
// 缓存硬盘 & 内存容量
var pbMaxCacheDiskCapacity *pb.SizeCapacity
if len(params.MaxCacheDiskCapacityJSON) > 0 {
var sizeCapacity = &shared.SizeCapacity{}
err := json.Unmarshal(params.MaxCacheDiskCapacityJSON, sizeCapacity)
if err != nil {
this.ErrorPage(err)
return
}
pbMaxCacheDiskCapacity = &pb.SizeCapacity{
Count: sizeCapacity.Count,
Unit: sizeCapacity.Unit,
}
}
var pbMaxCacheMemoryCapacity *pb.SizeCapacity
if len(params.MaxCacheMemoryCapacityJSON) > 0 {
var sizeCapacity = &shared.SizeCapacity{}
err := json.Unmarshal(params.MaxCacheMemoryCapacityJSON, sizeCapacity)
if err != nil {
this.ErrorPage(err)
return
}
pbMaxCacheMemoryCapacity = &pb.SizeCapacity{
Count: sizeCapacity.Count,
Unit: sizeCapacity.Unit,
}
}
2020-07-30 22:41:35 +08:00
// 保存
_, err := this.RPC().NodeRPC().UpdateNode(this.AdminContext(), &pb.UpdateNodeRequest{
NodeId: params.NodeId,
2021-05-25 17:48:51 +08:00
NodeGroupId: params.GroupId,
NodeRegionId: params.RegionId,
Name: params.Name,
NodeClusterId: params.ClusterId,
2021-05-25 17:48:51 +08:00
NodeLogin: loginInfo,
MaxCPU: params.MaxCPU,
IsOn: params.IsOn,
DnsDomainId: params.DnsDomainId,
DnsRoutes: dnsRouteCodes,
MaxCacheDiskCapacity: pbMaxCacheDiskCapacity,
MaxCacheMemoryCapacity: pbMaxCacheMemoryCapacity,
2020-07-30 22:41:35 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
2020-08-21 21:09:42 +08:00
// 禁用老的IP地址
_, err = this.RPC().NodeIPAddressRPC().DisableAllIPAddressesWithNodeId(this.AdminContext(), &pb.DisableAllIPAddressesWithNodeIdRequest{NodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
// 添加新的IP地址
err = ipaddressutils.UpdateNodeIPAddresses(this.Parent(), params.NodeId, 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()
}