阶段性提交

This commit is contained in:
GoEdgeLab
2020-07-29 19:34:54 +08:00
parent b984b68089
commit 16d2dff57a
56 changed files with 5799 additions and 959 deletions

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/admin"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
@@ -77,13 +77,13 @@ func (this *IndexAction) RunPost(params struct {
if err != nil {
this.Fail("服务器出了点小问题:" + err.Error())
}
resp, err := rpcClient.AdminRPC().Login(rpcClient.Context(0), &admin.LoginRequest{
resp, err := rpcClient.AdminRPC().LoginAdmin(rpcClient.Context(0), &pb.LoginAdminRequest{
Username: params.Username,
Password: params.Password,
})
if err != nil {
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
Level: oplogs.LevelError,
Description: "登录时发生系统错误:" + err.Error(),
Action: this.Request.URL.Path,
@@ -97,7 +97,7 @@ func (this *IndexAction) RunPost(params struct {
}
if !resp.IsOk {
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
Level: oplogs.LevelWarn,
Description: "登录失败,用户名:" + params.Username,
Action: this.Request.URL.Path,
@@ -114,7 +114,7 @@ func (this *IndexAction) RunPost(params struct {
params.Auth.StoreAdmin(adminId, params.Remember)
// 记录日志
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
Level: oplogs.LevelInfo,
Description: "成功登录系统,用户名:" + params.Username,
Action: this.Request.URL.Path,

View File

@@ -1,8 +1,10 @@
package nodes
import (
"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 {
@@ -10,31 +12,34 @@ type CreateAction struct {
}
func (this *CreateAction) Init() {
this.Nav("", "", "create")
this.Nav("", "node", "create")
}
func (this *CreateAction) RunGet(params struct{}) {
// 所有集群
/**clusters, err := models.SharedNodeClusterDAO.FindAllEnableClusters()
resp, err := this.RPC().NodeClusterRPC().FindAllEnabledClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{})
if err != nil {
this.ErrorPage(err)
}
if err != nil {
this.ErrorPage(err)
return
}
clusterMaps := []maps.Map{}
for _, cluster := range clusters {
for _, cluster := range resp.Clusters {
clusterMaps = append(clusterMaps, maps.Map{
"id": cluster.Id,
"name": cluster.Name,
})
}
this.Data["clusters"] = clusterMaps**/
this.Data["clusters"] = clusterMaps
this.Show()
}
func (this *CreateAction) RunPost(params struct {
Name string
ClusterId int
ClusterId int64
Must *actions.Must
}) {
@@ -48,4 +53,16 @@ func (this *CreateAction) RunPost(params struct {
}
// TODO 检查SSH授权
// 保存
_, err := this.RPC().NodeRPC().CreateNode(this.AdminContext(), &pb.CreateNodeRequest{
Name: params.Name,
ClusterId: params.ClusterId,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,66 @@
package grants
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"
)
type CreateAction struct {
actionutils.ParentAction
}
func (this *CreateAction) Init() {
this.Nav("", "grant", "create")
}
func (this *CreateAction) RunGet(params struct{}) {
this.Data["methods"] = grantutils.AllGrantMethods()
this.Show()
}
func (this *CreateAction) RunPost(params struct {
Name string
Method string
Username string
Password string
PrivateKey string
Description string
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入名称")
switch params.Method {
case "user":
if len(params.Username) == 0 {
this.FailField("username", "请输入SSH登录用户名")
}
case "privateKey":
if len(params.PrivateKey) == 0 {
this.FailField("privateKey", "请输入RSA私钥")
}
default:
this.Fail("请选择正确的认证方式")
}
_, err := this.RPC().NodeGrantRPC().CreateNodeGrant(this.AdminContext(), &pb.CreateNodeGrantRequest{
Name: params.Name,
Method: params.Method,
Username: params.Username,
Password: params.Password,
PrivateKey: params.PrivateKey,
Description: params.Description,
NodeId: 0,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,22 @@
package grants
import (
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
GrantId int64
}) {
_, err := this.RPC().NodeGrantRPC().DisableNodeGrant(this.AdminContext(), &pb.DisableNodeGrantRequest{GrantId: params.GrantId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,47 @@
package grants
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/maps"
)
type GrantAction struct {
actionutils.ParentAction
}
func (this *GrantAction) Init() {
this.Nav("", "grant", "index")
}
func (this *GrantAction) RunGet(params struct {
GrantId int64
}) {
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
if err != nil {
this.ErrorPage(err)
return
}
if grantResp.Grant == nil {
this.WriteString("can not find the grant")
return
}
// TODO 处理节点专用的认证
grant := grantResp.Grant
this.Data["grant"] = maps.Map{
"id": grant.Id,
"name": grant.Name,
"method": grant.Method,
"methodName": grantutils.FindGrantMethodName(grant.Method),
"username": grant.Username,
"password": grant.Password,
"privateKey": grant.PrivateKey,
"description": grant.Description,
"su": grant.Su,
}
this.Show()
}

View File

@@ -0,0 +1,27 @@
package grantutils
import "github.com/iwind/TeaGo/maps"
// 所有的认证类型
func AllGrantMethods() []maps.Map {
return []maps.Map{
{
"name": "用户名+密码",
"value": "user",
},
{
"name": "私钥",
"value": "privateKey",
},
}
}
// 获得对应的认证类型名称
func FindGrantMethodName(method string) string {
for _, m := range AllGrantMethods() {
if m.GetString("value") == method {
return m.GetString("name")
}
}
return ""
}

View File

@@ -0,0 +1,49 @@
package grants
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/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "grant", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
countResp, err := this.RPC().NodeGrantRPC().CountAllEnabledNodeGrants(this.AdminContext(), &pb.CountAllEnabledNodeGrantsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
page := this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
grantsResp, err := this.RPC().NodeGrantRPC().ListEnabledNodeGrants(this.AdminContext(), &pb.ListEnabledNodeGrantsRequest{
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
grantMaps := []maps.Map{}
for _, grant := range grantsResp.Grants {
grantMaps = append(grantMaps, maps.Map{
"id": grant.Id,
"name": grant.Name,
"method": maps.Map{
"type": grant.Method,
"name": grantutils.FindGrantMethodName(grant.Method),
},
})
}
this.Data["grants"] = grantMaps
this.Show()
}

View File

@@ -0,0 +1,98 @@
package grants
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 UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
this.Nav("", "", "")
}
func (this *UpdateAction) RunGet(params struct {
GrantId int64
}) {
this.Data["methods"] = grantutils.AllGrantMethods()
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
if err != nil {
this.ErrorPage(err)
return
}
if grantResp.Grant == nil {
this.WriteString("can not find the grant")
return
}
// TODO 处理节点专用的认证
grant := grantResp.Grant
this.Data["grant"] = maps.Map{
"id": grant.Id,
"name": grant.Name,
"method": grant.Method,
"methodName": grantutils.FindGrantMethodName(grant.Method),
"username": grant.Username,
"password": grant.Password,
"privateKey": grant.PrivateKey,
"description": grant.Description,
"su": grant.Su,
}
this.Show()
}
func (this *UpdateAction) RunPost(params struct {
GrantId int64
Name string
Method string
Username string
Password string
PrivateKey string
Description string
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入名称")
switch params.Method {
case "user":
if len(params.Username) == 0 {
this.FailField("username", "请输入SSH登录用户名")
}
case "privateKey":
if len(params.PrivateKey) == 0 {
this.FailField("privateKey", "请输入RSA私钥")
}
default:
this.Fail("请选择正确的认证方式")
}
// TODO 检查grantId是否存在
_, err := this.RPC().NodeGrantRPC().UpdateNodeGrant(this.AdminContext(), &pb.UpdateNodeGrantRequest{
GrantId: params.GrantId,
Name: params.Name,
Method: params.Method,
Username: params.Username,
Password: params.Password,
PrivateKey: params.PrivateKey,
Description: params.Description,
NodeId: 0,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -1,10 +1,20 @@
package nodes
import "github.com/iwind/TeaGo/actions"
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
type Helper struct {
}
func (this *Helper) BeforeAction(action *actions.ActionObject) {
action.Data["teaMenu"] = "nodes"
selectedTabbar, _ := action.Data["mainTab"]
tabbar := actionutils.NewTabbar()
tabbar.Add("节点管理", "", "/nodes", "", selectedTabbar == "node")
tabbar.Add("认证管理", "", "/nodes/grants", "", selectedTabbar == "grant")
actionutils.SetTabbar(action, tabbar)
}

View File

@@ -1,15 +1,45 @@
package nodes
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
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("", "", "index")
this.Nav("", "node", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
countResp, err := this.RPC().NodeRPC().CountAllEnabledNodes(this.AdminContext(), &pb.CountAllEnabledNodesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
page := this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
nodesResp, err := this.RPC().NodeRPC().ListEnabledNodes(this.AdminContext(), &pb.ListEnabledNodesRequest{
Offset: page.Offset,
Size: page.Size,
})
nodeMaps := []maps.Map{}
for _, node := range nodesResp.Nodes {
nodeMaps = append(nodeMaps, maps.Map{
"id": node.Id,
"name": node.Name,
"cluster": maps.Map{
"id": node.Cluster.Id,
"name": node.Cluster.Name,
},
})
}
this.Data["nodes"] = nodeMaps
this.Show()
}

View File

@@ -1,6 +1,7 @@
package nodes
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
@@ -13,6 +14,13 @@ func init() {
Prefix("/nodes").
Get("", new(IndexAction)).
GetPost("/create", new(CreateAction)).
// 授权管理
Get("/grants", new(grants.IndexAction)).
GetPost("/grants/create", new(grants.CreateAction)).
GetPost("/grants/update", new(grants.UpdateAction)).
Post("/grants/delete", new(grants.DeleteAction)).
Get("/grants/grant", new(grants.GrantAction)).
EndAll()
})
}

View File

@@ -0,0 +1,98 @@
package servers
import (
"encoding/json"
"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/actions"
"github.com/iwind/TeaGo/maps"
)
type CreateAction struct {
actionutils.ParentAction
}
func (this *CreateAction) Init() {
this.Nav("", "", "create")
}
func (this *CreateAction) RunGet(params struct{}) {
// 所有集群
resp, err := this.RPC().NodeClusterRPC().FindAllEnabledClusters(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 *CreateAction) RunPost(params struct {
Name string
ClusterId int64
Must *actions.Must
}) {
params.Must.
Field("name", params.Name).
Require("请输入服务名称")
if params.ClusterId <= 0 {
this.Fail("请选择部署的集群")
}
// TODO 验证集群ID
// 配置
serverConfig := &nodes.ServerConfig{}
serverConfig.IsOn = true
serverConfig.Name = params.Name
serverConfigJSON, err := serverConfig.AsJSON()
if err != nil {
this.ErrorPage(err)
return
}
// 包含条件
includeNodes := []maps.Map{}
includeNodesJSON, err := json.Marshal(includeNodes)
if err != nil {
this.ErrorPage(err)
return
}
// 排除条件
excludeNodes := []maps.Map{}
excludeNodesJSON, err := json.Marshal(excludeNodes)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().ServerRPC().CreateServer(this.AdminContext(), &pb.CreateServerRequest{
UserId: 0,
AdminId: this.AdminId(),
ClusterId: params.ClusterId,
Config: serverConfigJSON,
IncludeNodesJSON: includeNodesJSON,
ExcludeNodesJSON: excludeNodesJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -1,6 +1,8 @@
package servers
import "github.com/iwind/TeaGo/actions"
import (
"github.com/iwind/TeaGo/actions"
)
type Helper struct {
}

View File

@@ -1,15 +1,60 @@
package servers
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
import (
"encoding/json"
"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/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
countResp, err := this.RPC().ServerRPC().CountAllEnabledServers(this.AdminContext(), &pb.CountAllEnabledServersRequest{})
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count)
this.Data["page"] = page.AsHTML()
// 服务列表
serversResp, err := this.RPC().ServerRPC().ListEnabledServers(this.AdminContext(), &pb.ListEnabledServersRequest{
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
serverMaps := []maps.Map{}
for _, server := range serversResp.Servers {
// 服务名
serverConfig := &nodes.ServerConfig{}
err = json.Unmarshal(server.Config, &serverConfig)
if err != nil {
this.ErrorPage(err)
return
}
serverMaps = append(serverMaps, maps.Map{
"id": server.Id,
"name": serverConfig.Name,
"cluster": maps.Map{
"id": server.Cluster.Id,
"name": server.Cluster.Name,
},
})
}
this.Data["servers"] = serverMaps
this.Show()
}

View File

@@ -12,6 +12,7 @@ func init() {
Helper(NewHelper()).
Prefix("/servers").
Get("", new(IndexAction)).
GetPost("/create", new(CreateAction)).
EndAll()
})
}