集群增加是否远程启动选项

This commit is contained in:
GoEdgeLab
2022-09-17 15:11:34 +08:00
parent ae0bfd368a
commit 26c32e0621
7 changed files with 77 additions and 53 deletions

View File

@@ -9,3 +9,7 @@ dbs:
prefix: "edge" prefix: "edge"
models: models:
package: internal/web/models package: internal/web/models
fields:
bool: [ "uamIsOn", "followPort", "requestHostExcludingPort", "autoRemoteStart" ]

View File

@@ -197,7 +197,7 @@ func (this *NodeClusterDAO) CreateCluster(tx *dbs.Tx, adminId int64, name string
} }
// UpdateCluster 修改集群 // UpdateCluster 修改集群
func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool, clockConfig *nodeconfigs.ClockConfig) error { func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name string, grantId int64, installDir string, timezone string, nodeMaxThreads int32, autoOpenPorts bool, clockConfig *nodeconfigs.ClockConfig, autoRemoteStart bool) error {
if clusterId <= 0 { if clusterId <= 0 {
return errors.New("invalid clusterId") return errors.New("invalid clusterId")
} }
@@ -222,6 +222,8 @@ func (this *NodeClusterDAO) UpdateCluster(tx *dbs.Tx, clusterId int64, name stri
op.Clock = clockJSON op.Clock = clockJSON
} }
op.AutoRemoteStart = autoRemoteStart
err := this.Save(tx, op) err := this.Save(tx, op)
if err != nil { if err != nil {
return err return err

View File

@@ -37,6 +37,7 @@ type NodeCluster struct {
Uam dbs.JSON `field:"uam"` // UAM设置 Uam dbs.JSON `field:"uam"` // UAM设置
Clock dbs.JSON `field:"clock"` // 时钟配置 Clock dbs.JSON `field:"clock"` // 时钟配置
GlobalServerConfig dbs.JSON `field:"globalServerConfig"` // 全局服务配置 GlobalServerConfig dbs.JSON `field:"globalServerConfig"` // 全局服务配置
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
} }
type NodeClusterOperator struct { type NodeClusterOperator struct {
@@ -73,6 +74,7 @@ type NodeClusterOperator struct {
Uam any // UAM设置 Uam any // UAM设置
Clock any // 时钟配置 Clock any // 时钟配置
GlobalServerConfig any // 全局服务配置 GlobalServerConfig any // 全局服务配置
AutoRemoteStart any // 自动远程启动
} }
func NewNodeClusterOperator() *NodeClusterOperator { func NewNodeClusterOperator() *NodeClusterOperator {

View File

@@ -4,35 +4,37 @@ import "github.com/iwind/TeaGo/dbs"
// NSCluster 域名服务器集群 // NSCluster 域名服务器集群
type NSCluster struct { type NSCluster struct {
Id uint32 `field:"id"` // ID Id uint32 `field:"id"` // ID
IsOn bool `field:"isOn"` // 是否启用 IsOn bool `field:"isOn"` // 是否启用
Name string `field:"name"` // 集群名 Name string `field:"name"` // 集群名
InstallDir string `field:"installDir"` // 安装目录 InstallDir string `field:"installDir"` // 安装目录
State uint8 `field:"state"` // 状态 State uint8 `field:"state"` // 状态
AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置 AccessLog dbs.JSON `field:"accessLog"` // 访问日志配置
GrantId uint32 `field:"grantId"` // 授权ID GrantId uint32 `field:"grantId"` // 授权ID
Recursion dbs.JSON `field:"recursion"` // 递归DNS设置 Recursion dbs.JSON `field:"recursion"` // 递归DNS设置
Tcp dbs.JSON `field:"tcp"` // TCP设置 Tcp dbs.JSON `field:"tcp"` // TCP设置
Tls dbs.JSON `field:"tls"` // TLS设置 Tls dbs.JSON `field:"tls"` // TLS设置
Udp dbs.JSON `field:"udp"` // UDP设置 Udp dbs.JSON `field:"udp"` // UDP设置
DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置 DdosProtection dbs.JSON `field:"ddosProtection"` // DDoS防护设置
Hosts dbs.JSON `field:"hosts"` // DNS主机地址 Hosts dbs.JSON `field:"hosts"` // DNS主机地址
AutoRemoteStart bool `field:"autoRemoteStart"` // 自动远程启动
} }
type NSClusterOperator struct { type NSClusterOperator struct {
Id any // ID Id any // ID
IsOn any // 是否启用 IsOn any // 是否启用
Name any // 集群名 Name any // 集群名
InstallDir any // 安装目录 InstallDir any // 安装目录
State any // 状态 State any // 状态
AccessLog any // 访问日志配置 AccessLog any // 访问日志配置
GrantId any // 授权ID GrantId any // 授权ID
Recursion any // 递归DNS设置 Recursion any // 递归DNS设置
Tcp any // TCP设置 Tcp any // TCP设置
Tls any // TLS设置 Tls any // TLS设置
Udp any // UDP设置 Udp any // UDP设置
DdosProtection any // DDoS防护设置 DdosProtection any // DDoS防护设置
Hosts any // DNS主机地址 Hosts any // DNS主机地址
AutoRemoteStart any // 自动远程启动
} }
func NewNSClusterOperator() *NSClusterOperator { func NewNSClusterOperator() *NSClusterOperator {

View File

@@ -116,7 +116,7 @@ func (this *NodeClusterService) UpdateNodeCluster(ctx context.Context, req *pb.U
} }
} }
err = models.SharedNodeClusterDAO.UpdateCluster(tx, req.NodeClusterId, req.Name, req.NodeGrantId, req.InstallDir, req.TimeZone, req.NodeMaxThreads, req.AutoOpenPorts, clockConfig) err = models.SharedNodeClusterDAO.UpdateCluster(tx, req.NodeClusterId, req.Name, req.NodeGrantId, req.InstallDir, req.TimeZone, req.NodeMaxThreads, req.AutoOpenPorts, clockConfig, req.AutoRemoteStart)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -167,6 +167,9 @@ func (this *NodeClusterService) FindEnabledNodeCluster(ctx context.Context, req
if userId > 0 { if userId > 0 {
// TODO 检查用户是否有权限 // TODO 检查用户是否有权限
// 禁止通过REST访问
// TODO
} }
var tx = this.NullTx() var tx = this.NullTx()
@@ -197,6 +200,7 @@ func (this *NodeClusterService) FindEnabledNodeCluster(ctx context.Context, req
NodeMaxThreads: int32(cluster.NodeMaxThreads), NodeMaxThreads: int32(cluster.NodeMaxThreads),
AutoOpenPorts: cluster.AutoOpenPorts == 1, AutoOpenPorts: cluster.AutoOpenPorts == 1,
ClockJSON: cluster.Clock, ClockJSON: cluster.Clock,
AutoRemoteStart: cluster.AutoRemoteStart,
}}, nil }}, nil
} }

View File

@@ -5,6 +5,14 @@ import (
"time" "time"
) )
func IsRest(ctx context.Context) bool {
if ctx == nil {
return false
}
_, ok := ctx.(*PlainContext)
return ok
}
type PlainContext struct { type PlainContext struct {
UserType string UserType string
UserId int64 UserId int64

View File

@@ -85,35 +85,37 @@ func (this *NodeMonitorTask) MonitorCluster(cluster *models.NodeCluster) error {
} }
// 尝试自动远程启动 // 尝试自动远程启动
var nodeQueue = installers.NewNodeQueue() if cluster.AutoRemoteStart {
for _, node := range inactiveNodes { var nodeQueue = installers.NewNodeQueue()
var nodeId = int64(node.Id) for _, node := range inactiveNodes {
tryInfo, ok := this.recoverMap[nodeId] var nodeId = int64(node.Id)
if !ok { tryInfo, ok := this.recoverMap[nodeId]
tryInfo = &nodeStartingTry{ if !ok {
count: 1, tryInfo = &nodeStartingTry{
timestamp: time.Now().Unix(), count: 1,
} timestamp: time.Now().Unix(),
this.recoverMap[nodeId] = tryInfo
} else {
if tryInfo.count >= 3 /** 3次 **/ { // N 秒内超过 M 次就暂时不再重新尝试,防止阻塞当前任务
if tryInfo.timestamp+10*60 /** 10 分钟 **/ > time.Now().Unix() {
continue
} }
tryInfo.timestamp = time.Now().Unix() this.recoverMap[nodeId] = tryInfo
tryInfo.count = 0 } else {
if tryInfo.count >= 3 /** 3次 **/ { // N 秒内超过 M 次就暂时不再重新尝试,防止阻塞当前任务
if tryInfo.timestamp+10*60 /** 10 分钟 **/ > time.Now().Unix() {
continue
}
tryInfo.timestamp = time.Now().Unix()
tryInfo.count = 0
}
tryInfo.count++
} }
tryInfo.count++
}
// TODO 如果用户手工安装的位置不在标准位置,需要节点自身记住最近启动的位置 // TODO 如果用户手工安装的位置不在标准位置,需要节点自身记住最近启动的位置
err = nodeQueue.StartNode(nodeId) err = nodeQueue.StartNode(nodeId)
if err != nil { if err != nil {
if !installers.IsGrantError(err) { if !installers.IsGrantError(err) {
_ = models.SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleNode, nodeId, 0, 0, models.LevelError, "NODE", "start node from remote API failed: "+err.Error(), time.Now().Unix(), "", nil) _ = models.SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleNode, nodeId, 0, 0, models.LevelError, "NODE", "start node from remote API failed: "+err.Error(), time.Now().Unix(), "", nil)
}
} else {
_ = models.SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleNode, nodeId, 0, 0, models.LevelSuccess, "NODE", "start node from remote API successfully", time.Now().Unix(), "", nil)
} }
} else {
_ = models.SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleNode, nodeId, 0, 0, models.LevelSuccess, "NODE", "start node from remote API successfully", time.Now().Unix(), "", nil)
} }
} }