2020-09-06 16:19:34 +08:00
|
|
|
package cluster
|
2020-07-22 22:19:39 +08:00
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
import (
|
2020-08-21 12:32:16 +08:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
|
2020-07-29 19:34:54 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2020-09-26 08:07:18 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2020-08-21 12:32:16 +08:00
|
|
|
"github.com/iwind/TeaGo/logs"
|
2020-07-29 19:34:54 +08:00
|
|
|
"github.com/iwind/TeaGo/maps"
|
2020-09-13 20:37:07 +08:00
|
|
|
"github.com/iwind/TeaGo/types"
|
2020-08-21 12:32:16 +08:00
|
|
|
"time"
|
2020-07-29 19:34:54 +08:00
|
|
|
)
|
2020-07-22 22:19:39 +08:00
|
|
|
|
|
|
|
|
type IndexAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) Init() {
|
2020-09-13 20:37:07 +08:00
|
|
|
this.Nav("", "node", "index")
|
2020-09-06 16:19:34 +08:00
|
|
|
this.SecondMenu("nodes")
|
2020-07-22 22:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
2020-09-06 16:19:34 +08:00
|
|
|
func (this *IndexAction) RunGet(params struct {
|
2020-09-13 20:37:07 +08:00
|
|
|
ClusterId int64
|
|
|
|
|
InstalledState int
|
2020-09-26 08:07:18 +08:00
|
|
|
ActiveState int
|
2020-09-06 16:19:34 +08:00
|
|
|
}) {
|
2020-09-13 20:37:07 +08:00
|
|
|
this.Data["installState"] = params.InstalledState
|
2020-09-26 08:07:18 +08:00
|
|
|
this.Data["activeState"] = params.ActiveState
|
2020-09-13 20:37:07 +08:00
|
|
|
|
2020-09-06 16:19:34 +08:00
|
|
|
countResp, err := this.RPC().NodeRPC().CountAllEnabledNodesMatch(this.AdminContext(), &pb.CountAllEnabledNodesMatchRequest{
|
2020-09-13 20:37:07 +08:00
|
|
|
ClusterId: params.ClusterId,
|
|
|
|
|
InstallState: types.Int32(params.InstalledState),
|
2020-09-26 08:07:18 +08:00
|
|
|
ActiveState: types.Int32(params.ActiveState),
|
2020-09-06 16:19:34 +08:00
|
|
|
})
|
2020-07-29 19:34:54 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page := this.NewPage(countResp.Count)
|
|
|
|
|
this.Data["page"] = page.AsHTML()
|
|
|
|
|
|
2020-09-06 16:19:34 +08:00
|
|
|
nodesResp, err := this.RPC().NodeRPC().ListEnabledNodesMatch(this.AdminContext(), &pb.ListEnabledNodesMatchRequest{
|
2020-09-13 20:37:07 +08:00
|
|
|
Offset: page.Offset,
|
|
|
|
|
Size: page.Size,
|
|
|
|
|
ClusterId: params.ClusterId,
|
|
|
|
|
InstallState: types.Int32(params.InstalledState),
|
2020-09-26 08:07:18 +08:00
|
|
|
ActiveState: types.Int32(params.ActiveState),
|
2020-07-29 19:34:54 +08:00
|
|
|
})
|
|
|
|
|
nodeMaps := []maps.Map{}
|
|
|
|
|
for _, node := range nodesResp.Nodes {
|
2020-08-21 12:32:16 +08:00
|
|
|
// 状态
|
2020-09-26 08:07:18 +08:00
|
|
|
isSynced := false
|
2020-08-21 12:32:16 +08:00
|
|
|
status := &nodes.NodeStatus{}
|
|
|
|
|
if len(node.Status) > 0 && node.Status != "null" {
|
|
|
|
|
err = json.Unmarshal([]byte(node.Status), &status)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error(err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-09-26 08:07:18 +08:00
|
|
|
status.IsActive = time.Now().Unix()-status.UpdatedAt <= 60 // N秒之内认为活跃
|
|
|
|
|
isSynced = status.ConfigVersion == node.Version
|
2020-08-21 12:32:16 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-21 21:09:42 +08:00
|
|
|
// IP
|
|
|
|
|
ipAddressesResp, err := this.RPC().NodeIPAddressRPC().FindAllEnabledIPAddressesWithNodeId(this.AdminContext(), &pb.FindAllEnabledIPAddressesWithNodeIdRequest{NodeId: node.Id})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ipAddresses := []maps.Map{}
|
|
|
|
|
for _, addr := range ipAddressesResp.Addresses {
|
|
|
|
|
ipAddresses = append(ipAddresses, maps.Map{
|
2020-10-17 21:15:22 +08:00
|
|
|
"id": addr.Id,
|
|
|
|
|
"name": addr.Name,
|
|
|
|
|
"ip": addr.Ip,
|
|
|
|
|
"canAccess": addr.CanAccess,
|
2020-08-21 21:09:42 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 19:34:54 +08:00
|
|
|
nodeMaps = append(nodeMaps, maps.Map{
|
2020-09-06 16:19:34 +08:00
|
|
|
"id": node.Id,
|
|
|
|
|
"name": node.Name,
|
|
|
|
|
"isInstalled": node.IsInstalled,
|
2020-10-10 12:31:59 +08:00
|
|
|
"isOn": node.IsOn,
|
2020-09-13 20:37:07 +08:00
|
|
|
"installStatus": maps.Map{
|
|
|
|
|
"isRunning": node.InstallStatus.IsRunning,
|
|
|
|
|
"isFinished": node.InstallStatus.IsFinished,
|
|
|
|
|
"isOk": node.InstallStatus.IsOk,
|
|
|
|
|
"error": node.InstallStatus.Error,
|
|
|
|
|
},
|
2020-08-21 12:32:16 +08:00
|
|
|
"status": maps.Map{
|
|
|
|
|
"isActive": status.IsActive,
|
|
|
|
|
"updatedAt": status.UpdatedAt,
|
|
|
|
|
"hostname": status.Hostname,
|
|
|
|
|
"cpuUsage": status.CPUUsage,
|
|
|
|
|
"cpuUsageText": fmt.Sprintf("%.2f%%", status.CPUUsage*100),
|
|
|
|
|
"memUsage": status.MemoryUsage,
|
|
|
|
|
"memUsageText": fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
|
|
|
|
|
},
|
2020-07-29 19:34:54 +08:00
|
|
|
"cluster": maps.Map{
|
|
|
|
|
"id": node.Cluster.Id,
|
|
|
|
|
"name": node.Cluster.Name,
|
|
|
|
|
},
|
2020-09-26 08:07:18 +08:00
|
|
|
"isSynced": isSynced,
|
2020-08-21 21:09:42 +08:00
|
|
|
"ipAddresses": ipAddresses,
|
2020-07-29 19:34:54 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
this.Data["nodes"] = nodeMaps
|
|
|
|
|
|
2020-07-22 22:19:39 +08:00
|
|
|
this.Show()
|
|
|
|
|
}
|