mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 15:00:27 +08:00
增加服务分组管理,增加多个CPU架构支持
This commit is contained in:
@@ -29,7 +29,7 @@ function build() {
|
||||
fi
|
||||
cd $ROOT"/../../EdgeNode/build"
|
||||
echo "=============================="
|
||||
architects=("amd64" "386")
|
||||
architects=("amd64" "386" "arm64" "arm64be" "mips64" "mips64le")
|
||||
for arch in "${architects[@]}"; do
|
||||
./build.sh linux $arch
|
||||
done
|
||||
|
||||
@@ -737,6 +737,15 @@ func (this *ServerDAO) CountAllEnabledServersWithNodeClusterId(clusterId int64)
|
||||
Count()
|
||||
}
|
||||
|
||||
// 计算使用某个分组的服务数量
|
||||
func (this *ServerDAO) CountAllEnabledServersWithGroupId(groupId int64) (int64, error) {
|
||||
return this.Query().
|
||||
State(ServerStateEnabled).
|
||||
Where("JSON_CONTAINS(groupIds, :groupId)").
|
||||
Param("groupId", groupId).
|
||||
Count()
|
||||
}
|
||||
|
||||
// 创建事件
|
||||
func (this *ServerDAO) createEvent() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -33,7 +35,7 @@ func init() {
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *ServerGroupDAO) EnableServerGroup(id uint32) error {
|
||||
func (this *ServerGroupDAO) EnableServerGroup(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", ServerGroupStateEnabled).
|
||||
@@ -42,7 +44,7 @@ func (this *ServerGroupDAO) EnableServerGroup(id uint32) error {
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *ServerGroupDAO) DisableServerGroup(id uint32) error {
|
||||
func (this *ServerGroupDAO) DisableServerGroup(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", ServerGroupStateDisabled).
|
||||
@@ -51,7 +53,7 @@ func (this *ServerGroupDAO) DisableServerGroup(id uint32) error {
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *ServerGroupDAO) FindEnabledServerGroup(id uint32) (*ServerGroup, error) {
|
||||
func (this *ServerGroupDAO) FindEnabledServerGroup(id int64) (*ServerGroup, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", ServerGroupStateEnabled).
|
||||
@@ -63,9 +65,58 @@ func (this *ServerGroupDAO) FindEnabledServerGroup(id uint32) (*ServerGroup, err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *ServerGroupDAO) FindServerGroupName(id uint32) (string, error) {
|
||||
func (this *ServerGroupDAO) FindServerGroupName(id int64) (string, error) {
|
||||
return this.Query().
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 创建分组
|
||||
func (this *ServerGroupDAO) CreateGroup(name string) (groupId int64, err error) {
|
||||
op := NewServerGroupOperator()
|
||||
op.State = ServerGroupStateEnabled
|
||||
op.Name = name
|
||||
_, err = this.Save(op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改分组
|
||||
func (this *ServerGroupDAO) UpdateGroup(groupId int64, name string) error {
|
||||
if groupId <= 0 {
|
||||
return errors.New("invalid groupId")
|
||||
}
|
||||
op := NewServerGroupOperator()
|
||||
op.Id = groupId
|
||||
op.Name = name
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找所有分组
|
||||
func (this *ServerGroupDAO) FindAllEnabledGroups() (result []*ServerGroup, err error) {
|
||||
_, err = this.Query().
|
||||
State(ServerGroupStateEnabled).
|
||||
Desc("order").
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 修改分组排序
|
||||
func (this *ServerGroupDAO) UpdateGroupOrders(groupIds []int64) error {
|
||||
for index, groupId := range groupIds {
|
||||
_, err := this.Query().
|
||||
Pk(groupId).
|
||||
Set("order", len(groupIds)-index).
|
||||
Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -135,19 +135,26 @@ func (this *BaseInstaller) InstallHelper(targetDir string) (env *Env, err error)
|
||||
|
||||
osName := ""
|
||||
archName := ""
|
||||
if strings.Index(uname, "Darwin") > 0 {
|
||||
if strings.Contains(uname, "Darwin") {
|
||||
osName = "darwin"
|
||||
} else if strings.Index(uname, "Linux") >= 0 {
|
||||
} else if strings.Contains(uname, "Linux") {
|
||||
osName = "linux"
|
||||
} else {
|
||||
// TODO 支持freebsd, aix ...
|
||||
return env, errors.New("installer not supported os '" + uname + "'")
|
||||
}
|
||||
|
||||
if strings.Index(uname, "x86_64") > 0 {
|
||||
if strings.Contains(uname, "aarch64") || strings.Contains(uname, "armv8") {
|
||||
archName = "arm64"
|
||||
} else if strings.Contains(uname, "aarch64_be") {
|
||||
archName = "arm64be"
|
||||
} else if strings.Contains(uname, "mips64el") {
|
||||
archName = "mips64le"
|
||||
} else if strings.Contains(uname, "mips64") {
|
||||
archName = "mips64"
|
||||
} else if strings.Contains(uname, "x86_64") {
|
||||
archName = "amd64"
|
||||
} else {
|
||||
// TODO 支持ARM和MIPS等架构
|
||||
archName = "386"
|
||||
}
|
||||
|
||||
|
||||
@@ -172,6 +172,7 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err
|
||||
pb.RegisterHTTPAccessLogServiceServer(rpcServer, &services.HTTPAccessLogService{})
|
||||
pb.RegisterMessageServiceServer(rpcServer, &services.MessageService{})
|
||||
pb.RegisterNodeGroupServiceServer(rpcServer, &services.NodeGroupService{})
|
||||
pb.RegisterServerGroupServiceServer(rpcServer, &services.ServerGroupService{})
|
||||
err := rpcServer.Serve(listener)
|
||||
if err != nil {
|
||||
return errors.New("[API]start rpc failed: " + err.Error())
|
||||
|
||||
@@ -733,3 +733,20 @@ func (this *ServerService) CountAllEnabledServersWithNodeClusterId(ctx context.C
|
||||
}
|
||||
return &pb.CountAllEnabledServersWithNodeClusterIdResponse{Count: count}, nil
|
||||
}
|
||||
|
||||
// 计算使用某个分组的服务数量
|
||||
func (this *ServerService) CountAllEnabledServersWithGroupId(ctx context.Context, req *pb.CountAllEnabledServersWithGroupIdRequest) (*pb.CountAllEnabledServersWithGroupIdResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, err := models.SharedServerDAO.CountAllEnabledServersWithGroupId(req.GroupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CountAllEnabledServersWithGroupIdResponse{
|
||||
Count: count,
|
||||
}, nil
|
||||
}
|
||||
|
||||
122
internal/rpc/services/service_server_group.go
Normal file
122
internal/rpc/services/service_server_group.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// 服务分组相关服务
|
||||
type ServerGroupService struct {
|
||||
}
|
||||
|
||||
// 创建分组
|
||||
func (this *ServerGroupService) CreateServerGroup(ctx context.Context, req *pb.CreateServerGroupRequest) (*pb.CreateServerGroupResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
groupId, err := models.SharedServerGroupDAO.CreateGroup(req.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateServerGroupResponse{GroupId: groupId}, nil
|
||||
}
|
||||
|
||||
// 修改分组
|
||||
func (this *ServerGroupService) UpdateServerGroup(ctx context.Context, req *pb.UpdateServerGroupRequest) (*pb.RPCUpdateSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedServerGroupDAO.UpdateGroup(req.GroupId, req.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rpcutils.RPCUpdateSuccess()
|
||||
}
|
||||
|
||||
// 删除分组
|
||||
func (this *ServerGroupService) DeleteServerGroup(ctx context.Context, req *pb.DeleteServerGroupRequest) (*pb.RPCDeleteSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedServerGroupDAO.DisableServerGroup(req.GroupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rpcutils.RPCDeleteSuccess()
|
||||
}
|
||||
|
||||
// 查询所有分组
|
||||
func (this *ServerGroupService) FindAllEnabledServerGroups(ctx context.Context, req *pb.FindAllEnabledServerGroupsRequest) (*pb.FindAllEnabledServerGroupsResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
groups, err := models.SharedServerGroupDAO.FindAllEnabledGroups()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []*pb.ServerGroup{}
|
||||
for _, group := range groups {
|
||||
result = append(result, &pb.ServerGroup{
|
||||
Id: int64(group.Id),
|
||||
Name: group.Name,
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledServerGroupsResponse{Groups: result}, nil
|
||||
}
|
||||
|
||||
// 修改分组排序
|
||||
func (this *ServerGroupService) UpdateServerGroupOrders(ctx context.Context, req *pb.UpdateServerGroupOrdersRequest) (*pb.RPCUpdateSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedServerGroupDAO.UpdateGroupOrders(req.GroupIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rpcutils.RPCUpdateSuccess()
|
||||
}
|
||||
|
||||
// 查找单个分组信息
|
||||
func (this *ServerGroupService) FindEnabledServerGroup(ctx context.Context, req *pb.FindEnabledServerGroupRequest) (*pb.FindEnabledServerGroupResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
group, err := models.SharedServerGroupDAO.FindEnabledServerGroup(req.GroupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if group == nil {
|
||||
return &pb.FindEnabledServerGroupResponse{
|
||||
Group: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &pb.FindEnabledServerGroupResponse{
|
||||
Group: &pb.ServerGroup{
|
||||
Id: int64(group.Id),
|
||||
Name: group.Name,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user