mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	自动创建默认集群
This commit is contained in:
		@@ -465,6 +465,16 @@ func (this *NodeDAO) UpdateNodeConnectedAPINodes(nodeId int64, apiNodeIds []int6
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 根据UniqueId获取ID
 | 
			
		||||
// TODO 增加缓存
 | 
			
		||||
func (this *NodeDAO) FindEnabledNodeIdWithUniqueId(uniqueId string) (int64, error) {
 | 
			
		||||
	return this.Query().
 | 
			
		||||
		State(NodeStateEnabled).
 | 
			
		||||
		Attr("uniqueId", uniqueId).
 | 
			
		||||
		ResultPk().
 | 
			
		||||
		FindInt64Col(0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 生成唯一ID
 | 
			
		||||
func (this *NodeDAO) genUniqueId() (string, error) {
 | 
			
		||||
	for {
 | 
			
		||||
 
 | 
			
		||||
@@ -96,6 +96,18 @@ func ValidateRequest(ctx context.Context, userTypes ...UserType) (userType UserT
 | 
			
		||||
		return UserTypeNone, 0, errors.New("not supported user type: '" + userType + "'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch apiToken.Role {
 | 
			
		||||
	case UserTypeNode:
 | 
			
		||||
		nodeIntId, err := models.SharedNodeDAO.FindEnabledNodeIdWithUniqueId(nodeId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return UserTypeNode, 0, errors.New("context: " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
		if nodeIntId <= 0 {
 | 
			
		||||
			return UserTypeNode, 0, errors.New("context: not found node with id '" + nodeId + "'")
 | 
			
		||||
		}
 | 
			
		||||
		nodeUserId = nodeIntId
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if nodeUserId > 0 {
 | 
			
		||||
		return t, nodeUserId, nil
 | 
			
		||||
	} else {
 | 
			
		||||
 
 | 
			
		||||
@@ -134,6 +134,7 @@ func (this *Setup) Run() error {
 | 
			
		||||
			httpConfig.IsOn = true
 | 
			
		||||
			httpConfig.Listen = []*serverconfigs.NetworkAddressConfig{
 | 
			
		||||
				{
 | 
			
		||||
					Protocol:  "https",
 | 
			
		||||
					PortRange: strconv.Itoa(this.config.APINodePort),
 | 
			
		||||
				},
 | 
			
		||||
			}
 | 
			
		||||
@@ -148,6 +149,7 @@ func (this *Setup) Run() error {
 | 
			
		||||
			httpsConfig.IsOn = true
 | 
			
		||||
			httpsConfig.Listen = []*serverconfigs.NetworkAddressConfig{
 | 
			
		||||
				{
 | 
			
		||||
					Protocol:  "https",
 | 
			
		||||
					PortRange: strconv.Itoa(this.config.APINodePort),
 | 
			
		||||
				},
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,7 @@ import (
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 安装或升级SQL执行器
 | 
			
		||||
type SQLExecutor struct {
 | 
			
		||||
	dbConfig *dbs.DBConfig
 | 
			
		||||
}
 | 
			
		||||
@@ -190,6 +191,9 @@ func (this *SQLExecutor) checkData(db *dbs.DB) error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查集群配置
 | 
			
		||||
	err = this.checkCluster(db)
 | 
			
		||||
 | 
			
		||||
	// 更新版本号
 | 
			
		||||
	err = this.updateVersion(db, teaconst.Version)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -250,6 +254,35 @@ func (this *SQLExecutor) checkAdminNode(db *dbs.DB) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 检查集群配置
 | 
			
		||||
func (this *SQLExecutor) checkCluster(db *dbs.DB) error {
 | 
			
		||||
	/// 检查是否有集群数字
 | 
			
		||||
	stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeNodeClusters")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return errors.New("query clusters failed: " + err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		_ = stmt.Close()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	col, err := stmt.FindCol(0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return errors.New("query clusters failed: " + err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	count := types.Int(col)
 | 
			
		||||
	if count > 0 {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 创建默认集群
 | 
			
		||||
	_, err = db.Exec("INSERT INTO edgeNodeClusters (name, useAllAPINodes, state) VALUES (?, ?, ?)", "默认集群", 1, 1)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 更新版本号
 | 
			
		||||
func (this *SQLExecutor) updateVersion(db *dbs.DB, version string) error {
 | 
			
		||||
	stmt, err := db.Prepare("SELECT COUNT(*) FROM edgeVersions")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user