Files
EdgeAdmin/internal/web/actions/default/clusters/cluster/createBatch.go

123 lines
2.7 KiB
Go
Raw Normal View History

package cluster
import (
2024-07-27 15:42:58 +08:00
"net"
"strconv"
"strings"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2023-06-28 16:18:52 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
)
type CreateBatchAction struct {
actionutils.ParentAction
}
func (this *CreateBatchAction) Init() {
this.Nav("", "node", "create")
this.SecondMenu("nodes")
}
func (this *CreateBatchAction) RunGet(params struct {
ClusterId int64
}) {
leftMenuItems := []maps.Map{
{
2023-06-30 18:08:30 +08:00
"name": this.Lang(codes.NodeMenu_CreateSingleNode),
"url": "/clusters/cluster/createNode?clusterId=" + strconv.FormatInt(params.ClusterId, 10),
"isActive": false,
},
{
2023-06-30 18:08:30 +08:00
"name": this.Lang(codes.NodeMenu_CreateMultipleNodes),
"url": "/clusters/cluster/createBatch?clusterId=" + strconv.FormatInt(params.ClusterId, 10),
"isActive": true,
},
}
this.Data["leftMenuItems"] = leftMenuItems
2023-05-27 15:33:08 +08:00
// 限额
maxNodes, leftNodes, err := this.findNodesQuota()
if err != nil {
this.ErrorPage(err)
return
}
this.Data["quota"] = maps.Map{
"maxNodes": maxNodes,
"leftNodes": leftNodes,
}
this.Show()
}
func (this *CreateBatchAction) RunPost(params struct {
ClusterId int64
GroupId int64
2020-12-10 16:11:41 +08:00
RegionId int64
IpList string
Must *actions.Must
2020-10-28 12:35:49 +08:00
CSRF *actionutils.CSRF
}) {
if params.ClusterId <= 0 {
this.Fail("请选择正确的集群")
}
// 校验
// TODO 支持IP范围比如192.168.1.[100-105]
realIPList := []string{}
for _, ip := range strings.Split(params.IpList, "\n") {
ip = strings.TrimSpace(ip)
if len(ip) == 0 {
continue
}
ip = strings.ReplaceAll(ip, " ", "")
if net.ParseIP(ip) == nil {
this.Fail("发现错误的IP地址" + ip)
}
if lists.ContainsString(realIPList, ip) {
continue
}
realIPList = append(realIPList, ip)
}
// 保存
for _, ip := range realIPList {
resp, err := this.RPC().NodeRPC().CreateNode(this.AdminContext(), &pb.CreateNodeRequest{
Name: ip,
NodeClusterId: params.ClusterId,
2021-05-25 17:48:51 +08:00
NodeGroupId: params.GroupId,
NodeRegionId: params.RegionId,
NodeLogin: nil,
})
if err != nil {
this.ErrorPage(err)
return
}
nodeId := resp.NodeId
_, err = this.RPC().NodeIPAddressRPC().CreateNodeIPAddress(this.AdminContext(), &pb.CreateNodeIPAddressRequest{
NodeId: nodeId,
Role: nodeconfigs.NodeRoleNode,
Name: "IP地址",
Ip: ip,
CanAccess: true,
IsUp: true,
})
if err != nil {
this.ErrorPage(err)
return
}
}
2020-11-10 21:37:48 +08:00
// 创建日志
2023-06-30 18:08:30 +08:00
defer this.CreateLogInfo(codes.Node_LogCreateNodeBatch)
2020-11-10 21:37:48 +08:00
this.Success()
}