mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-01-04 22:35:48 +08:00
阶段性提交
This commit is contained in:
@@ -43,7 +43,7 @@ func (this *NodeClusterDAO) DisableNodeCluster(id uint32) (rowsAffected int64, e
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *NodeClusterDAO) FindEnabledNodeCluster(id uint32) (*NodeCluster, error) {
|
||||
func (this *NodeClusterDAO) FindEnabledNodeCluster(id int64) (*NodeCluster, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", NodeClusterStateEnabled).
|
||||
|
||||
@@ -69,9 +69,14 @@ func (this *NodeDAO) FindNodeName(id uint32) (string, error) {
|
||||
|
||||
// 创建节点
|
||||
func (this *NodeDAO) CreateNode(name string, clusterId int64) (nodeId int64, err error) {
|
||||
uniqueId, err := this.genUniqueId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
op := NewNodeOperator()
|
||||
op.Name = name
|
||||
op.NodeId = rands.HexString(32)
|
||||
op.UniqueId = uniqueId
|
||||
op.Secret = rands.String(32)
|
||||
op.ClusterId = clusterId
|
||||
op.IsOn = 1
|
||||
@@ -93,10 +98,69 @@ func (this *NodeDAO) UpdateNode(nodeId int64, name string, clusterId int64) erro
|
||||
op.Id = nodeId
|
||||
op.Name = name
|
||||
op.ClusterId = clusterId
|
||||
op.LatestVersion = dbs.SQL("latestVersion+1")
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新节点版本
|
||||
func (this *NodeDAO) UpdateNodeLatestVersion(nodeId int64) error {
|
||||
if nodeId <= 0 {
|
||||
return errors.New("invalid nodeId")
|
||||
}
|
||||
op := NewNodeOperator()
|
||||
op.Id = nodeId
|
||||
op.LatestVersion = dbs.SQL("latestVersion+1")
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 批量更新节点版本
|
||||
func (this *NodeDAO) UpdateAllNodesLatestVersionMatch(clusterId int64) error {
|
||||
nodeIds, err := this.FindAllNodeIdsMatch(clusterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(nodeIds) == 0 {
|
||||
return nil
|
||||
}
|
||||
_, err = this.Query().
|
||||
Pk(nodeIds).
|
||||
Set("latestVersion", dbs.SQL("latestVersion+1")).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 同步集群中的节点版本
|
||||
func (this *NodeDAO) SyncNodeVersionsWithCluster(clusterId int64) error {
|
||||
if clusterId <= 0 {
|
||||
return errors.New("invalid cluster")
|
||||
}
|
||||
_, err := this.Query().
|
||||
Attr("clusterId", clusterId).
|
||||
Set("version", dbs.SQL("latestVersion")).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 取得有变更的集群
|
||||
func (this *NodeDAO) FindChangedClusterIds() ([]int64, error) {
|
||||
ones, _, err := this.Query().
|
||||
State(NodeStateEnabled).
|
||||
Gt("latestVersion", 0).
|
||||
Where("version!=latestVersion").
|
||||
Result("DISTINCT(clusterId) AS clusterId").
|
||||
FindOnes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []int64{}
|
||||
for _, one := range ones {
|
||||
result = append(result, one.GetInt64("clusterId"))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 计算所有节点数量
|
||||
func (this *NodeDAO) CountAllEnabledNodes() (int64, error) {
|
||||
return this.Query().
|
||||
@@ -115,3 +179,85 @@ func (this *NodeDAO) ListEnabledNodes(offset int64, size int64) (result []*Node,
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 根据节点ID和密钥查询节点
|
||||
func (this *NodeDAO) FindEnabledNodeWithUniqueIdAndSecret(uniqueId string, secret string) (*Node, error) {
|
||||
one, err := this.Query().
|
||||
Attr("uniqueId", uniqueId).
|
||||
Attr("secret", secret).
|
||||
State(NodeStateEnabled).
|
||||
Find()
|
||||
|
||||
if one != nil {
|
||||
return one.(*Node), err
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 根据节点ID获取节点
|
||||
func (this *NodeDAO) FindEnabledNodeWithUniqueId(uniqueId string) (*Node, error) {
|
||||
one, err := this.Query().
|
||||
Attr("uniqueId", uniqueId).
|
||||
State(NodeStateEnabled).
|
||||
Find()
|
||||
|
||||
if one != nil {
|
||||
return one.(*Node), err
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取节点集群ID
|
||||
func (this *NodeDAO) FindNodeClusterId(nodeId int64) (int64, error) {
|
||||
col, err := this.Query().
|
||||
Pk(nodeId).
|
||||
Result("clusterId").
|
||||
FindCol(0)
|
||||
return types.Int64(col), err
|
||||
}
|
||||
|
||||
// 匹配节点并返回节点ID
|
||||
func (this *NodeDAO) FindAllNodeIdsMatch(clusterId int64) (result []int64, err error) {
|
||||
query := this.Query()
|
||||
query.State(NodeStateEnabled)
|
||||
if clusterId > 0 {
|
||||
query.Attr("clusterId", clusterId)
|
||||
}
|
||||
query.Result("id")
|
||||
ones, _, err := query.FindOnes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, one := range ones {
|
||||
result = append(result, one.GetInt64("id"))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 更改节点状态
|
||||
func (this *NodeDAO) UpdateNodeStatus(nodeId int64, statusJSON []byte) error {
|
||||
_, err := this.Query().
|
||||
Pk(nodeId).
|
||||
Set("status", string(statusJSON)).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
func (this *NodeDAO) genUniqueId() (string, error) {
|
||||
for {
|
||||
uniqueId := rands.HexString(32)
|
||||
ok, err := this.Query().
|
||||
Attr("uniqueId", uniqueId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
return uniqueId, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,21 @@ package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNodeDAO_FindAllNodeIdsMatch(t *testing.T) {
|
||||
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(nodeIds)
|
||||
}
|
||||
|
||||
func TestNodeDAO_FindChangedClusterIds(t *testing.T) {
|
||||
clusterIds, err := SharedNodeDAO.FindChangedClusterIds()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(clusterIds)
|
||||
}
|
||||
|
||||
@@ -2,33 +2,37 @@ package models
|
||||
|
||||
// 节点
|
||||
type Node struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
NodeId string `field:"nodeId"` // 节点ID
|
||||
Secret string `field:"secret"` // 密钥
|
||||
Name string `field:"name"` // 节点名
|
||||
Code string `field:"code"` // 代号
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
RegionId uint32 `field:"regionId"` // 区域ID
|
||||
GroupId uint32 `field:"groupId"` // 分组ID
|
||||
CreatedAt uint32 `field:"createdAt"` // 创建时间
|
||||
Status string `field:"status"` // 最新的状态
|
||||
State uint8 `field:"state"` // 状态
|
||||
Id uint32 `field:"id"` // ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
UniqueId string `field:"uniqueId"` // 节点ID
|
||||
Secret string `field:"secret"` // 密钥
|
||||
Name string `field:"name"` // 节点名
|
||||
Code string `field:"code"` // 代号
|
||||
ClusterId uint32 `field:"clusterId"` // 集群ID
|
||||
RegionId uint32 `field:"regionId"` // 区域ID
|
||||
GroupId uint32 `field:"groupId"` // 分组ID
|
||||
CreatedAt uint32 `field:"createdAt"` // 创建时间
|
||||
Status string `field:"status"` // 最新的状态
|
||||
Version uint32 `field:"version"` // 当前版本号
|
||||
LatestVersion uint32 `field:"latestVersion"` // 最后版本号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type NodeOperator struct {
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
NodeId interface{} // 节点ID
|
||||
Secret interface{} // 密钥
|
||||
Name interface{} // 节点名
|
||||
Code interface{} // 代号
|
||||
ClusterId interface{} // 集群ID
|
||||
RegionId interface{} // 区域ID
|
||||
GroupId interface{} // 分组ID
|
||||
CreatedAt interface{} // 创建时间
|
||||
Status interface{} // 最新的状态
|
||||
State interface{} // 状态
|
||||
Id interface{} // ID
|
||||
IsOn interface{} // 是否启用
|
||||
UniqueId interface{} // 节点ID
|
||||
Secret interface{} // 密钥
|
||||
Name interface{} // 节点名
|
||||
Code interface{} // 代号
|
||||
ClusterId interface{} // 集群ID
|
||||
RegionId interface{} // 区域ID
|
||||
GroupId interface{} // 分组ID
|
||||
CreatedAt interface{} // 创建时间
|
||||
Status interface{} // 最新的状态
|
||||
Version interface{} // 当前版本号
|
||||
LatestVersion interface{} // 最后版本号
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
func NewNodeOperator() *NodeOperator {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
@@ -37,15 +38,16 @@ func (this *ServerDAO) EnableServer(id uint32) (rowsAffected int64, err error) {
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *ServerDAO) DisableServer(id uint32) (rowsAffected int64, err error) {
|
||||
return this.Query().
|
||||
func (this *ServerDAO) DisableServer(id int64) (err error) {
|
||||
_, err = this.Query().
|
||||
Pk(id).
|
||||
Set("state", ServerStateDisabled).
|
||||
Update()
|
||||
return
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *ServerDAO) FindEnabledServer(id uint32) (*Server, error) {
|
||||
func (this *ServerDAO) FindEnabledServer(id int64) (*Server, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", ServerStateEnabled).
|
||||
@@ -58,7 +60,13 @@ func (this *ServerDAO) FindEnabledServer(id uint32) (*Server, error) {
|
||||
|
||||
// 创建服务
|
||||
func (this *ServerDAO) CreateServer(adminId int64, userId int64, clusterId int64, configJSON string, includeNodesJSON string, excludeNodesJSON string) (serverId int64, err error) {
|
||||
uniqueId, err := this.genUniqueId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
op := NewServerOperator()
|
||||
op.UniqueId = uniqueId
|
||||
op.UserId = userId
|
||||
op.AdminId = adminId
|
||||
op.ClusterId = clusterId
|
||||
@@ -119,3 +127,40 @@ func (this *ServerDAO) ListEnabledServers(offset int64, size int64) (result []*S
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取节点中的所有服务
|
||||
func (this *ServerDAO) FindAllEnabledServersWithNode(nodeId int64) (result []*Server, err error) {
|
||||
// 节点所在集群
|
||||
clusterId, err := SharedNodeDAO.FindNodeClusterId(nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if clusterId <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
_, err = this.Query().
|
||||
Attr("clusterId", clusterId).
|
||||
State(ServerStateEnabled).
|
||||
AscPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
func (this *ServerDAO) genUniqueId() (string, error) {
|
||||
for {
|
||||
uniqueId := rands.HexString(32)
|
||||
ok, err := this.Query().
|
||||
Attr("uniqueId", uniqueId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
return uniqueId, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package models
|
||||
// 服务
|
||||
type Server struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
UniqueId string `field:"uniqueId"` // 唯一ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
@@ -18,6 +19,7 @@ type Server struct {
|
||||
|
||||
type ServerOperator struct {
|
||||
Id interface{} // ID
|
||||
UniqueId interface{} // 唯一ID
|
||||
IsOn interface{} // 是否启用
|
||||
UserId interface{} // 用户ID
|
||||
AdminId interface{} // 管理员ID
|
||||
|
||||
Reference in New Issue
Block a user