阶段性提交

This commit is contained in:
GoEdgeLab
2020-09-06 16:19:34 +08:00
parent 2ba46b9162
commit 124cbd3302
185 changed files with 5306 additions and 1832 deletions

View File

@@ -0,0 +1,93 @@
package cluster
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"time"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "node", "")
this.SecondMenu("nodes")
}
func (this *IndexAction) RunGet(params struct {
ClusterId int64
}) {
countResp, err := this.RPC().NodeRPC().CountAllEnabledNodesMatch(this.AdminContext(), &pb.CountAllEnabledNodesMatchRequest{
ClusterId: params.ClusterId,
})
if err != nil {
this.ErrorPage(err)
return
}
page := this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
nodesResp, err := this.RPC().NodeRPC().ListEnabledNodesMatch(this.AdminContext(), &pb.ListEnabledNodesMatchRequest{
Offset: page.Offset,
Size: page.Size,
ClusterId: params.ClusterId,
})
nodeMaps := []maps.Map{}
for _, node := range nodesResp.Nodes {
// 状态
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
}
status.IsActive = time.Now().Unix()-status.UpdatedAt < 120 // 2分钟之内认为活跃
}
// 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{
"id": addr.Id,
"name": addr.Name,
"ip": addr.Ip,
})
}
nodeMaps = append(nodeMaps, maps.Map{
"id": node.Id,
"name": node.Name,
"isInstalled": node.IsInstalled,
"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),
},
"cluster": maps.Map{
"id": node.Cluster.Id,
"name": node.Cluster.Name,
},
"ipAddresses": ipAddresses,
})
}
this.Data["nodes"] = nodeMaps
this.Show()
}

View File

@@ -0,0 +1,26 @@
package cluster
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/cluster/node"
clusters "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/clusterutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(clusters.NewClusterHelper()).
Prefix("/clusters/cluster").
Get("", new(IndexAction)).
// 节点相关
Get("/node", new(node.NodeAction)).
GetPost("/node/create", new(node.CreateAction)).
GetPost("/node/update", new(node.UpdateAction)).
GetPost("/node/install", new(node.InstallAction)).
Post("/node/updateInstallStatus", new(node.UpdateInstallStatusAction)).
EndAll()
})
}

View File

@@ -0,0 +1,87 @@
package node
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type CreateAction struct {
actionutils.ParentAction
}
func (this *CreateAction) Init() {
this.Nav("", "node", "")
this.SecondMenu("nodes")
}
func (this *CreateAction) RunGet(params struct{}) {
this.Show()
}
func (this *CreateAction) RunPost(params struct {
Name string
IPAddresses string `alias:"ipAddresses"`
ClusterId int64
GrantId int64
SshHost string
SshPort int
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入节点名称")
// TODO 检查cluster
if params.ClusterId <= 0 {
this.Fail("请选择所在集群")
}
// TODO 检查登录授权
loginInfo := &pb.NodeLogin{
Id: 0,
Name: "SSH",
Type: "ssh",
Params: maps.Map{
"grantId": params.GrantId,
"host": params.SshHost,
"port": params.SshPort,
}.AsJSON(),
}
// 保存
createResp, err := this.RPC().NodeRPC().CreateNode(this.AdminContext(), &pb.CreateNodeRequest{
Name: params.Name,
ClusterId: params.ClusterId,
Login: loginInfo,
})
if err != nil {
this.ErrorPage(err)
return
}
nodeId := createResp.NodeId
// IP地址
ipAddresses := []maps.Map{}
err = json.Unmarshal([]byte(params.IPAddresses), &ipAddresses)
if err != nil {
this.ErrorPage(err)
return
}
for _, address := range ipAddresses {
addressId := address.GetInt64("id")
_, err = this.RPC().NodeIPAddressRPC().UpdateNodeIPAddressNodeId(this.AdminContext(), &pb.UpdateNodeIPAddressNodeIdRequest{
AddressId: addressId,
NodeId: nodeId,
})
if err != nil {
this.ErrorPage(err)
return
}
}
this.Success()
}

View File

@@ -0,0 +1,65 @@
package node
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
)
type InstallAction struct {
actionutils.ParentAction
}
func (this *InstallAction) Init() {
this.Nav("", "node", "install")
this.SecondMenu("nodes")
}
func (this *InstallAction) 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
if node.Cluster != nil {
clusterId := node.Cluster.Id
clusterResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeCluster(this.AdminContext(), &pb.FindEnabledNodeClusterRequest{ClusterId: clusterId})
if err != nil {
this.ErrorPage(err)
return
}
cluster := clusterResp.Cluster
if cluster != nil {
clusterMap = maps.Map{
"id": cluster.Id,
"name": cluster.Name,
"installDir": cluster.InstallDir,
}
}
}
this.Data["node"] = maps.Map{
"id": node.Id,
"name": node.Name,
"installDir": node.InstallDir,
"isInstalled": node.IsInstalled,
"uniqueId": node.UniqueId,
"secret": node.Secret,
"cluster": clusterMap,
}
this.Show()
}

View File

@@ -0,0 +1,121 @@
package node
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
"github.com/iwind/TeaGo/maps"
)
type NodeAction struct {
actionutils.ParentAction
}
func (this *NodeAction) Init() {
this.Nav("", "node", "node")
this.SecondMenu("nodes")
}
func (this *NodeAction) 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
if node.Cluster != nil {
clusterId := node.Cluster.Id
clusterResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeCluster(this.AdminContext(), &pb.FindEnabledNodeClusterRequest{ClusterId: clusterId})
if err != nil {
this.ErrorPage(err)
return
}
cluster := clusterResp.Cluster
if cluster != nil {
clusterMap = maps.Map{
"id": cluster.Id,
"name": cluster.Name,
"installDir": cluster.InstallDir,
}
}
}
// 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,
})
}
// 登录信息
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 {
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: grantId})
if err != nil {
this.ErrorPage(err)
return
}
if grantResp.Grant != nil {
grantMap = maps.Map{
"id": grantResp.Grant.Id,
"name": grantResp.Grant.Name,
"method": grantResp.Grant.Method,
"methodName": grantutils.FindGrantMethodName(grantResp.Grant.Method),
}
}
}
loginMap = maps.Map{
"id": node.Login.Id,
"name": node.Login.Name,
"type": node.Login.Type,
"params": loginParams,
"grant": grantMap,
}
}
this.Data["node"] = maps.Map{
"id": node.Id,
"name": node.Name,
"ipAddresses": ipAddressMaps,
"cluster": clusterMap,
"login": loginMap,
"installDir": node.InstallDir,
"isInstalled": node.IsInstalled,
"uniqueId": node.UniqueId,
"secret": node.Secret,
}
this.Show()
}

View File

@@ -0,0 +1,204 @@
package node
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
this.Nav("", "node", "update")
this.SecondMenu("nodes")
}
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
if node.Cluster != nil {
clusterMap = maps.Map{
"id": node.Cluster.Id,
"name": node.Cluster.Name,
}
}
// 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,
})
}
// 登录信息
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 {
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: grantId})
if err != nil {
this.ErrorPage(err)
return
}
if grantResp.Grant != nil {
grantMap = maps.Map{
"id": grantResp.Grant.Id,
"name": grantResp.Grant.Name,
"method": grantResp.Grant.Method,
"methodName": grantutils.FindGrantMethodName(grantResp.Grant.Method),
}
}
}
loginMap = maps.Map{
"id": node.Login.Id,
"name": node.Login.Name,
"type": node.Login.Type,
"params": loginParams,
"grant": grantMap,
}
}
this.Data["node"] = maps.Map{
"id": node.Id,
"name": node.Name,
"ipAddresses": ipAddressMaps,
"cluster": clusterMap,
"login": loginMap,
}
// 所有集群
resp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{})
if err != nil {
this.ErrorPage(err)
}
if err != nil {
this.ErrorPage(err)
return
}
clusterMaps := []maps.Map{}
for _, cluster := range resp.Clusters {
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
Name string
IPAddresses string `alias:"ipAddresses"`
ClusterId int64
GrantId int64
SshHost string
SshPort int
Must *actions.Must
}) {
if params.NodeId <= 0 {
this.Fail("要操作的节点不存在")
}
params.Must.
Field("name", params.Name).
Require("请输入节点名称")
// TODO 检查cluster
if params.ClusterId <= 0 {
this.Fail("请选择所在集群")
}
// TODO 检查登录授权
loginInfo := &pb.NodeLogin{
Id: params.LoginId,
Name: "SSH",
Type: "ssh",
Params: maps.Map{
"grantId": params.GrantId,
"host": params.SshHost,
"port": params.SshPort,
}.AsJSON(),
}
// 保存
_, err := this.RPC().NodeRPC().UpdateNode(this.AdminContext(), &pb.UpdateNodeRequest{
NodeId: params.NodeId,
Name: params.Name,
ClusterId: params.ClusterId,
Login: loginInfo,
})
if err != nil {
this.ErrorPage(err)
return
}
// 禁用老的IP地址
_, err = this.RPC().NodeIPAddressRPC().DisableAllIPAddressesWithNodeId(this.AdminContext(), &pb.DisableAllIPAddressesWithNodeIdRequest{NodeId: params.NodeId})
if err != nil {
this.ErrorPage(err)
return
}
// 添加新的IP地址
ipAddresses := []maps.Map{}
err = json.Unmarshal([]byte(params.IPAddresses), &ipAddresses)
if err != nil {
this.ErrorPage(err)
return
}
for _, address := range ipAddresses {
addressId := address.GetInt64("id")
_, err = this.RPC().NodeIPAddressRPC().UpdateNodeIPAddressNodeId(this.AdminContext(), &pb.UpdateNodeIPAddressNodeIdRequest{
AddressId: addressId,
NodeId: params.NodeId,
})
if err != nil {
this.ErrorPage(err)
return
}
}
this.Success()
}

View File

@@ -0,0 +1,26 @@
package node
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
)
type UpdateInstallStatusAction struct {
actionutils.ParentAction
}
func (this *UpdateInstallStatusAction) RunPost(params struct {
NodeId int64
IsInstalled bool
}) {
_, err := this.RPC().NodeRPC().UpdateNodeIsInstalled(this.AdminContext(), &pb.UpdateNodeIsInstalledRequest{
NodeId: params.NodeId,
IsInstalled: params.IsInstalled,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,89 @@
package settings
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "")
this.SecondMenu("basic")
}
func (this *IndexAction) RunGet(params struct {
ClusterId int64
}) {
clusterResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeCluster(this.AdminContext(), &pb.FindEnabledNodeClusterRequest{ClusterId: params.ClusterId})
if err != nil {
this.ErrorPage(err)
return
}
cluster := clusterResp.Cluster
if cluster == nil {
this.WriteString("not found cluster")
return
}
// 认证
var grantMap interface{} = nil
if cluster.GrantId > 0 {
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: cluster.GrantId})
if err != nil {
this.ErrorPage(err)
return
}
grant := grantResp.Grant
if grant != nil {
grantMap = maps.Map{
"id": grant.Id,
"name": grant.Name,
"method": grant.Method,
"methodName": grantutils.FindGrantMethodName(grant.Method),
}
}
}
this.Data["grant"] = grantMap
this.Data["cluster"] = maps.Map{
"id": cluster.Id,
"name": cluster.Name,
"installDir": cluster.InstallDir,
}
this.Show()
}
// 保存设置
func (this *IndexAction) RunPost(params struct {
ClusterId int64
Name string
GrantId int64
InstallDir string
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入集群名称")
_, err := this.RPC().NodeClusterRPC().UpdateNodeCluster(this.AdminContext(), &pb.UpdateNodeClusterRequest{
ClusterId: params.ClusterId,
Name: params.Name,
GrantId: params.GrantId,
InstallDir: params.InstallDir,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,18 @@
package settings
import (
clusters "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/clusters/clusterutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(clusters.NewClusterHelper()).
Prefix("/clusters/cluster/settings").
GetPost("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,84 @@
package clusters
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"net/http"
"strconv"
)
type ClusterHelper struct {
}
func NewClusterHelper() *ClusterHelper {
return &ClusterHelper{}
}
func (this *ClusterHelper) BeforeAction(action *actions.ActionObject) {
if action.Request.Method != http.MethodGet {
return
}
action.Data["teaMenu"] = "clusters"
selectedTabbar := action.Data.GetString("mainTab")
clusterId := action.ParamInt64("clusterId")
clusterIdString := strconv.FormatInt(clusterId, 10)
action.Data["clusterId"] = clusterId
rpcClient, err := rpc.SharedRPC()
if err != nil {
logs.Error(err)
return
}
clusterResp, err := rpcClient.NodeClusterRPC().FindEnabledNodeCluster(rpcClient.Context(action.Context.GetInt64("adminId")), &pb.FindEnabledNodeClusterRequest{ClusterId: clusterId})
if err != nil {
logs.Error(err)
return
}
cluster := clusterResp.Cluster
if cluster == nil {
action.WriteString("can not find cluster")
return
}
tabbar := actionutils.NewTabbar()
tabbar.Add("当前集群:"+cluster.Name, "", "/clusters", "left long alternate arrow", false)
tabbar.Add("节点", "", "/clusters/cluster?clusterId="+clusterIdString, "server", selectedTabbar == "node")
tabbar.Add("设置", "", "/clusters/cluster/settings?clusterId="+clusterIdString, "setting", selectedTabbar == "setting")
actionutils.SetTabbar(action, tabbar)
// 左侧菜单
secondMenuItem := action.Data.GetString("secondMenuItem")
switch selectedTabbar {
case "setting":
action.Data["leftMenuItems"] = this.createSettingMenu(clusterIdString, secondMenuItem)
case "node":
action.Data["leftMenuItems"] = this.createNodeMenu(clusterIdString, secondMenuItem)
}
}
// 节点菜单
func (this *ClusterHelper) createNodeMenu(clusterId string, selectedItem string) (items []maps.Map) {
items = append(items, maps.Map{
"name": "节点列表",
"url": "/clusters/cluster?clusterId=" + clusterId,
"isActive": selectedItem == "nodes",
})
return
}
// 设置菜单
func (this *ClusterHelper) createSettingMenu(clusterId string, selectedItem string) (items []maps.Map) {
items = append(items, maps.Map{
"name": "基础设置",
"url": "/clusters/cluster/settings?clusterId=" + clusterId,
"isActive": selectedItem == "basic",
})
return
}

View File

@@ -0,0 +1,43 @@
package clusters
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
type CreateAction struct {
actionutils.ParentAction
}
func (this *CreateAction) Init() {
this.Nav("", "cluster", "create")
}
func (this *CreateAction) RunGet(params struct{}) {
this.Show()
}
func (this *CreateAction) RunPost(params struct {
Name string
GrantId int64
InstallDir string
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入集群名称")
_, err := this.RPC().NodeClusterRPC().CreateNodeCluster(this.AdminContext(), &pb.CreateNodeClusterRequest{
Name: params.Name,
GrantId: params.GrantId,
InstallDir: params.InstallDir,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,23 @@
package clusters
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
type Helper struct {
}
func NewHelper() *Helper {
return &Helper{}
}
func (this *Helper) BeforeAction(action *actions.ActionObject) {
action.Data["teaMenu"] = "clusters"
selectedTabbar, _ := action.Data["mainTab"]
tabbar := actionutils.NewTabbar()
tabbar.Add("集群", "", "/clusters", "", selectedTabbar == "cluster")
actionutils.SetTabbar(action, tabbar)
}

View File

@@ -0,0 +1,57 @@
package clusters
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "cluster", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
countResp, err := this.RPC().NodeClusterRPC().CountAllEnabledNodeClusters(this.AdminContext(), &pb.CountAllEnabledNodeClustersRequest{})
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count)
this.Data["page"] = page.AsHTML()
clusterMaps := []maps.Map{}
if count > 0 {
clustersResp, err := this.RPC().NodeClusterRPC().ListEnabledNodeClusters(this.AdminContext(), &pb.ListEnabledNodeClustersRequest{
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
for _, cluster := range clustersResp.Clusters {
// 节点数量
countNodesResp, err := this.RPC().NodeRPC().CountAllEnabledNodesMatch(this.AdminContext(), &pb.CountAllEnabledNodesMatchRequest{ClusterId: cluster.Id})
if err != nil {
this.ErrorPage(err)
return
}
clusterMaps = append(clusterMaps, maps.Map{
"id": cluster.Id,
"name": cluster.Name,
"installDir": cluster.InstallDir,
"hasGrant": cluster.GrantId > 0,
"countNodes": countNodesResp.Count,
})
}
}
this.Data["clusters"] = clusterMaps
this.Show()
}

View File

@@ -0,0 +1,18 @@
package clusters
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(NewHelper()).
Prefix("/clusters").
Get("", new(IndexAction)).
GetPost("/create", new(CreateAction)).
EndAll()
})
}