From 965efed323f58c63887960cc2fa54485bac21e2f Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 14 Oct 2020 18:44:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E9=9B=86=E7=BE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/node_dao.go | 10 ++++++++++ internal/rpc/utils/utils.go | 12 ++++++++++++ internal/setup/setup.go | 2 ++ internal/setup/sql_executor.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/internal/db/models/node_dao.go b/internal/db/models/node_dao.go index bfa8b5a6..22ef0eb1 100644 --- a/internal/db/models/node_dao.go +++ b/internal/db/models/node_dao.go @@ -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 { diff --git a/internal/rpc/utils/utils.go b/internal/rpc/utils/utils.go index 4aa37135..cce49ae6 100644 --- a/internal/rpc/utils/utils.go +++ b/internal/rpc/utils/utils.go @@ -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 { diff --git a/internal/setup/setup.go b/internal/setup/setup.go index af96f9bd..496b3115 100644 --- a/internal/setup/setup.go +++ b/internal/setup/setup.go @@ -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), }, } diff --git a/internal/setup/sql_executor.go b/internal/setup/sql_executor.go index 6008c7a7..aa923eb2 100644 --- a/internal/setup/sql_executor.go +++ b/internal/setup/sql_executor.go @@ -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")