Files
waf-platform/EdgeAdmin/internal/web/actions/default/httpdns/clusters/updateNodeSSH.go
2026-02-27 10:35:22 +08:00

141 lines
3.4 KiB
Go

package clusters
import (
"encoding/json"
"net"
"regexp"
"strings"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
type UpdateNodeSSHAction struct {
actionutils.ParentAction
}
func (this *UpdateNodeSSHAction) RunGet(params struct {
NodeId int64
}) {
resp, err := this.RPC().HTTPDNSNodeRPC().FindHTTPDNSNode(this.AdminContext(), &pb.FindHTTPDNSNodeRequest{
NodeId: params.NodeId,
})
if err != nil {
this.ErrorPage(err)
return
}
clusterId := int64(0)
nodeName := ""
if resp.GetNode() != nil {
clusterId = resp.GetNode().GetClusterId()
nodeName = resp.GetNode().GetName()
}
this.Data["nodeId"] = params.NodeId
this.Data["clusterId"] = clusterId
this.Data["node"] = maps.Map{
"id": params.NodeId,
"name": nodeName,
}
loginParams := maps.Map{
"host": "",
"port": 22,
"grantId": 0,
}
this.Data["loginId"] = 0
if resp.GetNode() != nil && len(resp.GetNode().GetInstallStatusJSON()) > 0 {
installStatus := maps.Map{}
_ = json.Unmarshal(resp.GetNode().GetInstallStatusJSON(), &installStatus)
sshInfo := installStatus.GetMap("ssh")
if sshInfo != nil {
if host := strings.TrimSpace(sshInfo.GetString("host")); len(host) > 0 {
loginParams["host"] = host
}
if port := sshInfo.GetInt("port"); port > 0 {
loginParams["port"] = port
}
if grantID := sshInfo.GetInt64("grantId"); grantID > 0 {
loginParams["grantId"] = grantID
}
}
}
this.Data["params"] = loginParams
this.Data["grant"] = nil
this.Show()
}
func (this *UpdateNodeSSHAction) RunPost(params struct {
NodeId int64
LoginId int64
SshHost string
SshPort int
GrantId int64
Must *actions.Must
}) {
params.SshHost = strings.TrimSpace(params.SshHost)
params.Must.
Field("sshHost", params.SshHost).
Require("请输入 SSH 主机地址").
Field("sshPort", params.SshPort).
Gt(0, "SSH 端口必须大于 0").
Lt(65535, "SSH 端口必须小于 65535")
if params.GrantId <= 0 {
this.Fail("请选择节点登录认证信息")
}
if regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+$`).MatchString(params.SshHost) && net.ParseIP(params.SshHost) == nil {
this.Fail("SSH 主机地址 IP 格式错误")
}
resp, err := this.RPC().HTTPDNSNodeRPC().FindHTTPDNSNode(this.AdminContext(), &pb.FindHTTPDNSNodeRequest{
NodeId: params.NodeId,
})
if err != nil {
this.ErrorPage(err)
return
}
node := resp.GetNode()
if node == nil {
this.Fail("节点不存在")
return
}
installStatus := maps.Map{
"isRunning": false,
"isFinished": true,
"isOk": node.GetIsInstalled(),
"error": "",
"errorCode": "",
}
if len(node.GetInstallStatusJSON()) > 0 {
_ = json.Unmarshal(node.GetInstallStatusJSON(), &installStatus)
}
installStatus["ssh"] = maps.Map{
"host": params.SshHost,
"port": params.SshPort,
"grantId": params.GrantId,
}
installStatusJSON, _ := json.Marshal(installStatus)
_, err = this.RPC().HTTPDNSNodeRPC().UpdateHTTPDNSNodeStatus(this.AdminContext(), &pb.UpdateHTTPDNSNodeStatusRequest{
NodeId: params.NodeId,
IsUp: node.GetIsUp(),
IsInstalled: node.GetIsInstalled(),
IsActive: node.GetIsActive(),
StatusJSON: node.GetStatusJSON(),
InstallStatusJSON: installStatusJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}