2020-07-22 22:17:53 +08:00
package models
import (
2020-10-17 11:14:53 +08:00
"encoding/json"
2020-08-30 16:12:00 +08:00
"errors"
2021-01-25 16:40:03 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
2021-11-11 14:16:42 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2020-11-15 21:17:42 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
2020-12-02 14:26:03 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-10-17 21:15:31 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2020-07-22 22:17:53 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
2021-01-11 18:16:04 +08:00
"github.com/iwind/TeaGo/maps"
2020-10-17 11:14:53 +08:00
"github.com/iwind/TeaGo/rands"
2020-08-30 16:12:00 +08:00
"github.com/iwind/TeaGo/types"
2020-10-17 21:15:31 +08:00
"strconv"
2020-07-22 22:17:53 +08:00
)
const (
NodeClusterStateEnabled = 1 // 已启用
NodeClusterStateDisabled = 0 // 已禁用
)
type NodeClusterDAO dbs . DAO
func NewNodeClusterDAO ( ) * NodeClusterDAO {
return dbs . NewDAO ( & NodeClusterDAO {
DAOObject : dbs . DAOObject {
DB : Tea . Env ,
Table : "edgeNodeClusters" ,
Model : new ( NodeCluster ) ,
PkName : "id" ,
} ,
} ) . ( * NodeClusterDAO )
}
2020-10-13 20:05:13 +08:00
var SharedNodeClusterDAO * NodeClusterDAO
func init ( ) {
dbs . OnReady ( func ( ) {
SharedNodeClusterDAO = NewNodeClusterDAO ( )
} )
}
2020-07-22 22:17:53 +08:00
2021-05-03 11:32:43 +08:00
// EnableNodeCluster 启用条目
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) EnableNodeCluster ( tx * dbs . Tx , id int64 ) error {
_ , err := this . Query ( tx ) .
2020-07-22 22:17:53 +08:00
Pk ( id ) .
Set ( "state" , NodeClusterStateEnabled ) .
Update ( )
2020-08-30 16:12:00 +08:00
return err
2020-07-22 22:17:53 +08:00
}
2021-05-03 11:32:43 +08:00
// DisableNodeCluster 禁用条目
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) DisableNodeCluster ( tx * dbs . Tx , id int64 ) error {
_ , err := this . Query ( tx ) .
2020-07-22 22:17:53 +08:00
Pk ( id ) .
Set ( "state" , NodeClusterStateDisabled ) .
Update ( )
2020-08-30 16:12:00 +08:00
return err
2020-07-22 22:17:53 +08:00
}
2021-05-03 11:32:43 +08:00
// FindEnabledNodeCluster 查找集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindEnabledNodeCluster ( tx * dbs . Tx , id int64 ) ( * NodeCluster , error ) {
result , err := this . Query ( tx ) .
2020-07-22 22:17:53 +08:00
Pk ( id ) .
Attr ( "state" , NodeClusterStateEnabled ) .
Find ( )
if result == nil {
return nil , err
}
return result . ( * NodeCluster ) , err
}
2021-05-03 11:32:43 +08:00
// FindEnabledClusterIdWithUniqueId 根据UniqueId获取ID
2020-10-17 11:14:53 +08:00
// TODO 增加缓存
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindEnabledClusterIdWithUniqueId ( tx * dbs . Tx , uniqueId string ) ( int64 , error ) {
return this . Query ( tx ) .
2020-10-17 11:14:53 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "uniqueId" , uniqueId ) .
ResultPk ( ) .
FindInt64Col ( 0 )
}
2021-05-03 11:32:43 +08:00
// FindNodeClusterName 根据主键查找名称
2021-06-30 19:59:49 +08:00
func ( this * NodeClusterDAO ) FindNodeClusterName ( tx * dbs . Tx , clusterId int64 ) ( string , error ) {
2021-01-01 23:31:30 +08:00
return this . Query ( tx ) .
2021-06-30 19:59:49 +08:00
Pk ( clusterId ) .
2020-07-22 22:17:53 +08:00
Result ( "name" ) .
FindStringCol ( "" )
}
2021-05-03 11:32:43 +08:00
// FindAllEnableClusters 查找所有可用的集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnableClusters ( tx * dbs . Tx ) ( result [ ] * NodeCluster , err error ) {
_ , err = this . Query ( tx ) .
2020-07-22 22:17:53 +08:00
State ( NodeClusterStateEnabled ) .
Slice ( & result ) .
Desc ( "order" ) .
DescPk ( ) .
FindAll ( )
return
}
2020-08-30 16:12:00 +08:00
2021-05-03 11:32:43 +08:00
// FindAllEnableClusterIds 查找所有可用的集群Ids
2021-01-17 16:48:00 +08:00
func ( this * NodeClusterDAO ) FindAllEnableClusterIds ( tx * dbs . Tx ) ( result [ ] int64 , err error ) {
ones , err := this . Query ( tx ) .
State ( NodeClusterStateEnabled ) .
ResultPk ( ) .
FindAll ( )
if err != nil {
return nil , err
}
for _ , one := range ones {
result = append ( result , int64 ( one . ( * NodeCluster ) . Id ) )
}
return
}
2021-05-03 11:32:43 +08:00
// CreateCluster 创建集群
2021-01-11 18:16:04 +08:00
func ( this * NodeClusterDAO ) CreateCluster ( tx * dbs . Tx , adminId int64 , name string , grantId int64 , installDir string , dnsDomainId int64 , dnsName string , cachePolicyId int64 , httpFirewallPolicyId int64 , systemServices map [ string ] maps . Map ) ( clusterId int64 , err error ) {
2021-01-27 23:00:02 +08:00
uniqueId , err := this . GenUniqueId ( tx )
2020-10-17 11:14:53 +08:00
if err != nil {
return 0 , err
}
secret := rands . String ( 32 )
2021-05-26 14:40:05 +08:00
err = SharedApiTokenDAO . CreateAPIToken ( tx , uniqueId , secret , nodeconfigs . NodeRoleCluster )
2020-10-17 11:14:53 +08:00
if err != nil {
return 0 , err
}
2020-08-30 16:12:00 +08:00
op := NewNodeClusterOperator ( )
2020-11-27 09:57:21 +08:00
op . AdminId = adminId
2020-08-30 16:12:00 +08:00
op . Name = name
op . GrantId = grantId
op . InstallDir = installDir
2020-11-15 21:17:42 +08:00
// DNS设置
2020-11-15 16:28:29 +08:00
op . DnsDomainId = dnsDomainId
op . DnsName = dnsName
2020-11-15 21:17:42 +08:00
dnsConfig := & dnsconfigs . ClusterDNSConfig {
NodesAutoSync : true ,
ServersAutoSync : true ,
2021-09-20 20:01:21 +08:00
CNameRecords : [ ] string { } ,
TTL : 0 ,
2020-11-15 21:17:42 +08:00
}
dnsJSON , err := json . Marshal ( dnsConfig )
if err != nil {
return 0 , err
}
op . Dns = dnsJSON
2020-12-17 15:51:02 +08:00
// 缓存策略
op . CachePolicyId = cachePolicyId
// WAF策略
op . HttpFirewallPolicyId = httpFirewallPolicyId
2021-01-11 18:16:04 +08:00
// 系统服务
systemServicesJSON , err := json . Marshal ( systemServices )
if err != nil {
return 0 , err
}
op . SystemServices = systemServicesJSON
2020-09-06 16:19:54 +08:00
op . UseAllAPINodes = 1
op . ApiNodes = "[]"
2020-10-17 11:14:53 +08:00
op . UniqueId = uniqueId
op . Secret = secret
2020-08-30 16:12:00 +08:00
op . State = NodeClusterStateEnabled
2021-01-01 23:31:30 +08:00
err = this . Save ( tx , op )
2020-08-30 16:12:00 +08:00
if err != nil {
return 0 , err
}
return types . Int64 ( op . Id ) , nil
}
2021-05-03 11:32:43 +08:00
// UpdateCluster 修改集群
2022-01-03 16:27:50 +08:00
func ( this * NodeClusterDAO ) UpdateCluster ( tx * dbs . Tx , clusterId int64 , name string , grantId int64 , installDir string , timezone string , nodeMaxThreads int32 , nodeTCPMaxConnections int32 , autoOpenPorts bool ) error {
2020-08-30 16:12:00 +08:00
if clusterId <= 0 {
return errors . New ( "invalid clusterId" )
}
op := NewNodeClusterOperator ( )
op . Id = clusterId
op . Name = name
op . GrantId = grantId
op . InstallDir = installDir
2021-10-12 11:44:24 +08:00
op . TimeZone = timezone
2021-12-09 18:49:51 +08:00
if nodeMaxThreads < 0 {
nodeMaxThreads = 0
}
op . NodeMaxThreads = nodeMaxThreads
if nodeTCPMaxConnections < 0 {
nodeTCPMaxConnections = 0
}
op . NodeTCPMaxConnections = nodeTCPMaxConnections
2022-01-03 16:27:50 +08:00
op . AutoOpenPorts = autoOpenPorts
2021-12-09 18:49:51 +08:00
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2021-10-12 11:44:24 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , clusterId )
2020-08-30 16:12:00 +08:00
}
2021-05-03 11:32:43 +08:00
// CountAllEnabledClusters 计算所有集群数量
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) CountAllEnabledClusters ( tx * dbs . Tx , keyword string ) ( int64 , error ) {
query := this . Query ( tx ) .
2020-12-24 19:15:17 +08:00
State ( NodeClusterStateEnabled )
if len ( keyword ) > 0 {
2021-08-25 18:39:17 +08:00
query . Where ( "(name LIKE :keyword OR dnsName like :keyword OR (dnsDomainId > 0 AND dnsDomainId IN (SELECT id FROM " + dns . SharedDNSDomainDAO . Table + " WHERE name LIKE :keyword AND state=1)))" ) .
2020-12-24 19:15:17 +08:00
Param ( "keyword" , "%" + keyword + "%" )
}
return query . Count ( )
2020-08-30 16:12:00 +08:00
}
2021-05-03 11:32:43 +08:00
// ListEnabledClusters 列出单页集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) ListEnabledClusters ( tx * dbs . Tx , keyword string , offset , size int64 ) ( result [ ] * NodeCluster , err error ) {
query := this . Query ( tx ) .
2020-12-24 19:15:17 +08:00
State ( NodeClusterStateEnabled )
if len ( keyword ) > 0 {
2021-08-25 18:39:17 +08:00
query . Where ( "(name LIKE :keyword OR dnsName like :keyword OR (dnsDomainId > 0 AND dnsDomainId IN (SELECT id FROM " + dns . SharedDNSDomainDAO . Table + " WHERE name LIKE :keyword AND state=1)))" ) .
2020-12-24 19:15:17 +08:00
Param ( "keyword" , "%" + keyword + "%" )
}
_ , err = query .
2020-08-30 16:12:00 +08:00
Offset ( offset ) .
Limit ( size ) .
Slice ( & result ) .
2020-10-17 11:14:53 +08:00
DescPk ( ) .
2020-08-30 16:12:00 +08:00
FindAll ( )
return
}
2020-10-17 11:14:53 +08:00
2021-05-03 11:32:43 +08:00
// FindAllAPINodeAddrsWithCluster 查找所有API节点地址
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllAPINodeAddrsWithCluster ( tx * dbs . Tx , clusterId int64 ) ( result [ ] string , err error ) {
one , err := this . Query ( tx ) .
2020-10-17 11:14:53 +08:00
Pk ( clusterId ) .
Result ( "useAllAPINodes" , "apiNodes" ) .
Find ( )
if err != nil {
return nil , err
}
if one == nil {
return nil , nil
}
cluster := one . ( * NodeCluster )
if cluster . UseAllAPINodes == 1 {
2021-01-01 23:31:30 +08:00
apiNodes , err := SharedAPINodeDAO . FindAllEnabledAPINodes ( tx )
2020-10-17 11:14:53 +08:00
if err != nil {
return nil , err
}
for _ , apiNode := range apiNodes {
if apiNode . IsOn != 1 {
continue
}
addrs , err := apiNode . DecodeAccessAddrStrings ( )
if err != nil {
return nil , err
}
result = append ( result , addrs ... )
}
return result , nil
}
apiNodeIds := [ ] int64 { }
if ! IsNotNull ( cluster . ApiNodes ) {
return
}
err = json . Unmarshal ( [ ] byte ( cluster . ApiNodes ) , & apiNodeIds )
if err != nil {
return nil , err
}
for _ , apiNodeId := range apiNodeIds {
2022-01-19 16:53:52 +08:00
apiNode , err := SharedAPINodeDAO . FindEnabledAPINode ( tx , apiNodeId , nil )
2020-10-17 11:14:53 +08:00
if err != nil {
return nil , err
}
if apiNode == nil || apiNode . IsOn != 1 {
continue
}
addrs , err := apiNode . DecodeAccessAddrStrings ( )
if err != nil {
return nil , err
}
result = append ( result , addrs ... )
}
return result , nil
}
2021-05-03 11:32:43 +08:00
// FindClusterHealthCheckConfig 查找健康检查设置
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindClusterHealthCheckConfig ( tx * dbs . Tx , clusterId int64 ) ( * serverconfigs . HealthCheckConfig , error ) {
col , err := this . Query ( tx ) .
2020-10-17 21:15:31 +08:00
Pk ( clusterId ) .
Result ( "healthCheck" ) .
FindStringCol ( "" )
if err != nil {
return nil , err
}
if len ( col ) == 0 || col == "null" {
return nil , nil
}
config := & serverconfigs . HealthCheckConfig { }
err = json . Unmarshal ( [ ] byte ( col ) , config )
if err != nil {
return nil , err
}
return config , nil
}
2021-05-03 11:32:43 +08:00
// UpdateClusterHealthCheck 修改健康检查设置
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) UpdateClusterHealthCheck ( tx * dbs . Tx , clusterId int64 , healthCheckJSON [ ] byte ) error {
2020-10-17 21:15:31 +08:00
if clusterId <= 0 {
return errors . New ( "invalid clusterId '" + strconv . FormatInt ( clusterId , 10 ) + "'" )
}
op := NewNodeClusterOperator ( )
op . Id = clusterId
op . HealthCheck = healthCheckJSON
2021-08-29 16:01:31 +08:00
// 不需要通知更新
return this . Save ( tx , op )
2020-10-17 21:15:31 +08:00
}
2021-05-03 11:32:43 +08:00
// CountAllEnabledClustersWithGrantId 计算使用某个认证的集群数量
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) CountAllEnabledClustersWithGrantId ( tx * dbs . Tx , grantId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-10-25 21:27:46 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "grantId" , grantId ) .
Count ( )
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledClustersWithGrantId 获取使用某个认证的所有集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledClustersWithGrantId ( tx * dbs . Tx , grantId int64 ) ( result [ ] * NodeCluster , err error ) {
_ , err = this . Query ( tx ) .
2020-10-25 21:27:46 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "grantId" , grantId ) .
Slice ( & result ) .
DescPk ( ) .
FindAll ( )
return
}
2021-05-03 11:32:43 +08:00
// CountAllEnabledClustersWithDNSProviderId 计算使用某个DNS服务商的集群数量
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) CountAllEnabledClustersWithDNSProviderId ( tx * dbs . Tx , dnsProviderId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-11-11 21:32:25 +08:00
State ( NodeClusterStateEnabled ) .
2021-01-25 16:40:03 +08:00
Where ( "dnsDomainId IN (SELECT id FROM " + dns . SharedDNSDomainDAO . Table + " WHERE state=1 AND providerId=:providerId)" ) .
2020-11-12 14:41:28 +08:00
Param ( "providerId" , dnsProviderId ) .
2020-11-11 21:32:25 +08:00
Count ( )
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledClustersWithDNSProviderId 获取所有使用某个DNS服务商的集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledClustersWithDNSProviderId ( tx * dbs . Tx , dnsProviderId int64 ) ( result [ ] * NodeCluster , err error ) {
_ , err = this . Query ( tx ) .
2020-11-11 21:32:25 +08:00
State ( NodeClusterStateEnabled ) .
2021-01-25 16:40:03 +08:00
Where ( "dnsDomainId IN (SELECT id FROM " + dns . SharedDNSDomainDAO . Table + " WHERE state=1 AND providerId=:providerId)" ) .
2020-11-12 14:41:28 +08:00
Param ( "providerId" , dnsProviderId ) .
2020-11-11 21:32:25 +08:00
Slice ( & result ) .
DescPk ( ) .
FindAll ( )
return
}
2021-05-03 11:32:43 +08:00
// CountAllEnabledClustersWithDNSDomainId 计算使用某个DNS域名的集群数量
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) CountAllEnabledClustersWithDNSDomainId ( tx * dbs . Tx , dnsDomainId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-11-12 14:41:28 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "dnsDomainId" , dnsDomainId ) .
Count ( )
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledClusterIdsWithDNSDomainId 查询使用某个DNS域名的集群ID列表
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledClusterIdsWithDNSDomainId ( tx * dbs . Tx , dnsDomainId int64 ) ( [ ] int64 , error ) {
ones , err := this . Query ( tx ) .
2020-11-13 18:22:22 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "dnsDomainId" , dnsDomainId ) .
ResultPk ( ) .
FindAll ( )
if err != nil {
return nil , err
}
result := [ ] int64 { }
for _ , one := range ones {
result = append ( result , int64 ( one . ( * NodeCluster ) . Id ) )
}
return result , nil
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledClustersWithDNSDomainId 查询使用某个DNS域名的所有集群域名
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledClustersWithDNSDomainId ( tx * dbs . Tx , dnsDomainId int64 ) ( result [ ] * NodeCluster , err error ) {
_ , err = this . Query ( tx ) .
2020-11-13 18:22:22 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "dnsDomainId" , dnsDomainId ) .
2021-07-31 22:23:11 +08:00
Result ( "id" , "name" , "dnsName" , "dnsDomainId" , "isOn" ) .
2020-11-14 21:28:07 +08:00
Slice ( & result ) .
FindAll ( )
return
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledClustersHaveDNSDomain 查询已经设置了域名的集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledClustersHaveDNSDomain ( tx * dbs . Tx ) ( result [ ] * NodeCluster , err error ) {
_ , err = this . Query ( tx ) .
2020-11-14 21:28:07 +08:00
State ( NodeClusterStateEnabled ) .
Gt ( "dnsDomainId" , 0 ) .
2021-07-31 22:23:11 +08:00
Result ( "id" , "name" , "dnsName" , "dnsDomainId" , "isOn" ) .
2020-11-13 18:22:22 +08:00
Slice ( & result ) .
FindAll ( )
return
}
2021-05-03 11:32:43 +08:00
// FindClusterGrantId 查找集群的认证ID
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindClusterGrantId ( tx * dbs . Tx , clusterId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-10-26 21:14:56 +08:00
Pk ( clusterId ) .
Result ( "grantId" ) .
FindInt64Col ( 0 )
}
2021-05-03 11:32:43 +08:00
// FindClusterDNSInfo 查找DNS信息
2021-11-11 14:16:42 +08:00
func ( this * NodeClusterDAO ) FindClusterDNSInfo ( tx * dbs . Tx , clusterId int64 , cacheMap * utils . CacheMap ) ( * NodeCluster , error ) {
2021-10-12 11:44:24 +08:00
var cacheKey = this . Table + ":FindClusterDNSInfo:" + types . String ( clusterId )
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cache , ok := cacheMap . Get ( cacheKey )
if ok {
return cache . ( * NodeCluster ) , nil
}
2021-08-22 11:35:33 +08:00
}
2021-01-01 23:31:30 +08:00
one , err := this . Query ( tx ) .
2020-11-11 21:32:25 +08:00
Pk ( clusterId ) .
2021-07-31 22:23:11 +08:00
Result ( "id" , "name" , "dnsName" , "dnsDomainId" , "dns" , "isOn" ) .
2020-11-11 21:32:25 +08:00
Find ( )
if err != nil {
return nil , err
}
if one == nil {
return nil , nil
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap . Put ( cacheKey , one )
}
2020-11-11 21:32:25 +08:00
return one . ( * NodeCluster ) , nil
}
2021-05-03 11:32:43 +08:00
// ExistClusterDNSName 检查某个子域名是否可用
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) ExistClusterDNSName ( tx * dbs . Tx , dnsName string , excludeClusterId int64 ) ( bool , error ) {
return this . Query ( tx ) .
2020-11-13 18:22:22 +08:00
Attr ( "dnsName" , dnsName ) .
State ( NodeClusterStateEnabled ) .
Where ( "id!=:clusterId" ) .
Param ( "clusterId" , excludeClusterId ) .
Exist ( )
}
2021-05-03 11:32:43 +08:00
// UpdateClusterDNS 修改集群DNS相关信息
2021-09-20 20:01:21 +08:00
func ( this * NodeClusterDAO ) UpdateClusterDNS ( tx * dbs . Tx , clusterId int64 , dnsName string , dnsDomainId int64 , nodesAutoSync bool , serversAutoSync bool , cnameRecords [ ] string , ttl int32 ) error {
2020-11-13 18:22:22 +08:00
if clusterId <= 0 {
return errors . New ( "invalid clusterId" )
}
op := NewNodeClusterOperator ( )
op . Id = clusterId
op . DnsName = dnsName
op . DnsDomainId = dnsDomainId
2020-11-15 21:17:42 +08:00
2021-09-20 16:37:48 +08:00
if len ( cnameRecords ) == 0 {
cnameRecords = [ ] string { }
}
2020-11-15 21:17:42 +08:00
dnsConfig := & dnsconfigs . ClusterDNSConfig {
NodesAutoSync : nodesAutoSync ,
ServersAutoSync : serversAutoSync ,
2021-09-20 16:37:48 +08:00
CNameRecords : cnameRecords ,
2021-09-20 20:01:21 +08:00
TTL : ttl ,
2020-11-15 21:17:42 +08:00
}
dnsJSON , err := json . Marshal ( dnsConfig )
if err != nil {
return err
}
op . Dns = dnsJSON
2021-01-01 23:31:30 +08:00
err = this . Save ( tx , op )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
2021-01-27 23:00:02 +08:00
err = this . NotifyUpdate ( tx , clusterId )
if err != nil {
return err
}
return this . NotifyDNSUpdate ( tx , clusterId )
2020-11-13 18:22:22 +08:00
}
2021-05-03 11:32:43 +08:00
// FindClusterAdminId 查找集群所属管理员
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindClusterAdminId ( tx * dbs . Tx , clusterId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-11-27 09:57:21 +08:00
Pk ( clusterId ) .
Result ( "adminId" ) .
FindInt64Col ( 0 )
}
2021-05-03 11:32:43 +08:00
// FindClusterTOAConfig 查找集群的TOA设置
2021-11-11 14:16:42 +08:00
func ( this * NodeClusterDAO ) FindClusterTOAConfig ( tx * dbs . Tx , clusterId int64 , cacheMap * utils . CacheMap ) ( * nodeconfigs . TOAConfig , error ) {
var cacheKey = this . Table + ":FindClusterTOAConfig:" + types . String ( clusterId )
if cacheMap != nil {
cache , ok := cacheMap . Get ( cacheKey )
if ok {
return cache . ( * nodeconfigs . TOAConfig ) , nil
}
}
2021-01-01 23:31:30 +08:00
toa , err := this . Query ( tx ) .
2020-12-02 14:26:03 +08:00
Pk ( clusterId ) .
Result ( "toa" ) .
FindStringCol ( "" )
if err != nil {
return nil , err
}
if ! IsNotNull ( toa ) {
return nodeconfigs . DefaultTOAConfig ( ) , nil
}
config := & nodeconfigs . TOAConfig { }
err = json . Unmarshal ( [ ] byte ( toa ) , config )
if err != nil {
return nil , err
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap . Put ( cacheKey , config )
}
2020-12-02 14:26:03 +08:00
return config , nil
}
2021-05-03 11:32:43 +08:00
// UpdateClusterTOA 修改集群的TOA设置
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) UpdateClusterTOA ( tx * dbs . Tx , clusterId int64 , toaJSON [ ] byte ) error {
2020-12-02 14:26:03 +08:00
if clusterId <= 0 {
return errors . New ( "invalid clusterId" )
}
op := NewNodeClusterOperator ( )
op . Id = clusterId
op . Toa = toaJSON
2021-01-01 23:31:30 +08:00
err := this . Save ( tx , op )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , clusterId )
2020-12-02 14:26:03 +08:00
}
2021-05-03 11:32:43 +08:00
// CountAllEnabledNodeClustersWithHTTPCachePolicyId 计算使用某个缓存策略的集群数量
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) CountAllEnabledNodeClustersWithHTTPCachePolicyId ( tx * dbs . Tx , httpCachePolicyId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-12-17 15:51:02 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "cachePolicyId" , httpCachePolicyId ) .
Count ( )
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledNodeClustersWithHTTPCachePolicyId 查找使用缓存策略的所有集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledNodeClustersWithHTTPCachePolicyId ( tx * dbs . Tx , httpCachePolicyId int64 ) ( result [ ] * NodeCluster , err error ) {
_ , err = this . Query ( tx ) .
2020-12-17 15:51:02 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "cachePolicyId" , httpCachePolicyId ) .
DescPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-05-03 11:32:43 +08:00
// CountAllEnabledNodeClustersWithHTTPFirewallPolicyId 计算使用某个WAF策略的集群数量
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) CountAllEnabledNodeClustersWithHTTPFirewallPolicyId ( tx * dbs . Tx , httpFirewallPolicyId int64 ) ( int64 , error ) {
return this . Query ( tx ) .
2020-12-17 15:51:02 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "httpFirewallPolicyId" , httpFirewallPolicyId ) .
Count ( )
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledNodeClustersWithHTTPFirewallPolicyId 查找使用WAF策略的所有集群
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledNodeClustersWithHTTPFirewallPolicyId ( tx * dbs . Tx , httpFirewallPolicyId int64 ) ( result [ ] * NodeCluster , err error ) {
_ , err = this . Query ( tx ) .
2020-12-17 15:51:02 +08:00
State ( NodeClusterStateEnabled ) .
Attr ( "httpFirewallPolicyId" , httpFirewallPolicyId ) .
DescPk ( ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledNodeClusterIdsWithHTTPFirewallPolicyId 查找使用WAF策略的所有集群Ids
2021-01-17 16:48:00 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledNodeClusterIdsWithHTTPFirewallPolicyId ( tx * dbs . Tx , httpFirewallPolicyId int64 ) ( result [ ] int64 , err error ) {
ones , err := this . Query ( tx ) .
State ( NodeClusterStateEnabled ) .
Attr ( "httpFirewallPolicyId" , httpFirewallPolicyId ) .
ResultPk ( ) .
FindAll ( )
for _ , one := range ones {
result = append ( result , int64 ( one . ( * NodeCluster ) . Id ) )
}
return
}
2021-11-26 10:39:50 +08:00
// FindAllEnabledNodeClusterIds 查找所有可用的集群
func ( this * NodeClusterDAO ) FindAllEnabledNodeClusterIds ( tx * dbs . Tx ) ( [ ] int64 , error ) {
ones , err := this . Query ( tx ) .
State ( NodeClusterStateEnabled ) .
ResultPk ( ) .
FindAll ( )
if err != nil {
return nil , err
}
var result = [ ] int64 { }
for _ , one := range ones {
result = append ( result , int64 ( one . ( * NodeCluster ) . Id ) )
}
return result , nil
}
2021-05-03 11:32:43 +08:00
// FindAllEnabledNodeClusterIdsWithCachePolicyId 查找使用缓存策略的所有集群Ids
2021-01-17 16:48:00 +08:00
func ( this * NodeClusterDAO ) FindAllEnabledNodeClusterIdsWithCachePolicyId ( tx * dbs . Tx , cachePolicyId int64 ) ( result [ ] int64 , err error ) {
ones , err := this . Query ( tx ) .
State ( NodeClusterStateEnabled ) .
Attr ( "cachePolicyId" , cachePolicyId ) .
ResultPk ( ) .
FindAll ( )
for _ , one := range ones {
result = append ( result , int64 ( one . ( * NodeCluster ) . Id ) )
}
return
}
2021-05-03 11:32:43 +08:00
// FindClusterHTTPFirewallPolicyId 获取集群的WAF策略ID
2021-11-11 14:16:42 +08:00
func ( this * NodeClusterDAO ) FindClusterHTTPFirewallPolicyId ( tx * dbs . Tx , clusterId int64 , cacheMap * utils . CacheMap ) ( int64 , error ) {
2021-08-22 11:35:33 +08:00
if cacheMap == nil {
2021-11-11 14:16:42 +08:00
cacheMap = utils . NewCacheMap ( )
2021-08-22 11:35:33 +08:00
}
var cacheKey = this . Table + ":FindClusterHTTPFirewallPolicyId:" + types . String ( clusterId )
2021-11-11 14:16:42 +08:00
var cache , _ = cacheMap . Get ( cacheKey )
2021-08-22 11:35:33 +08:00
if cache != nil {
return cache . ( int64 ) , nil
}
firewallPolicyId , err := this . Query ( tx ) .
2020-12-17 17:36:20 +08:00
Pk ( clusterId ) .
Result ( "httpFirewallPolicyId" ) .
FindInt64Col ( 0 )
2021-08-22 11:35:33 +08:00
if err != nil {
return 0 , err
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap . Put ( cacheKey , firewallPolicyId )
}
2021-08-22 11:35:33 +08:00
return firewallPolicyId , nil
2020-12-17 17:36:20 +08:00
}
2021-05-03 11:32:43 +08:00
// UpdateNodeClusterHTTPCachePolicyId 设置集群的缓存策略
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) UpdateNodeClusterHTTPCachePolicyId ( tx * dbs . Tx , clusterId int64 , httpCachePolicyId int64 ) error {
_ , err := this . Query ( tx ) .
2020-12-17 15:51:02 +08:00
Pk ( clusterId ) .
Set ( "cachePolicyId" , httpCachePolicyId ) .
Update ( )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , clusterId )
2020-12-17 15:51:02 +08:00
}
2021-05-03 11:32:43 +08:00
// FindClusterHTTPCachePolicyId 获取集群的缓存策略ID
2021-11-11 14:16:42 +08:00
func ( this * NodeClusterDAO ) FindClusterHTTPCachePolicyId ( tx * dbs . Tx , clusterId int64 , cacheMap * utils . CacheMap ) ( int64 , error ) {
2021-08-22 11:35:33 +08:00
if cacheMap == nil {
2021-11-11 14:16:42 +08:00
cacheMap = utils . NewCacheMap ( )
2021-08-22 11:35:33 +08:00
}
var cacheKey = this . Table + ":FindClusterHTTPCachePolicyId:" + types . String ( clusterId )
2021-11-11 14:16:42 +08:00
var cache , _ = cacheMap . Get ( cacheKey )
2021-08-22 11:35:33 +08:00
if cache != nil {
return cache . ( int64 ) , nil
}
cachePolicyId , err := this . Query ( tx ) .
2020-12-17 17:36:20 +08:00
Pk ( clusterId ) .
Result ( "cachePolicyId" ) .
FindInt64Col ( 0 )
2021-08-22 11:35:33 +08:00
if err != nil {
return 0 , err
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap . Put ( cacheKey , cachePolicyId )
}
2021-08-22 11:35:33 +08:00
return cachePolicyId , nil
2020-12-17 17:36:20 +08:00
}
2021-05-03 11:32:43 +08:00
// UpdateNodeClusterHTTPFirewallPolicyId 设置集群的WAF策略
2021-01-01 23:31:30 +08:00
func ( this * NodeClusterDAO ) UpdateNodeClusterHTTPFirewallPolicyId ( tx * dbs . Tx , clusterId int64 , httpFirewallPolicyId int64 ) error {
_ , err := this . Query ( tx ) .
2020-12-17 15:51:02 +08:00
Pk ( clusterId ) .
Set ( "httpFirewallPolicyId" , httpFirewallPolicyId ) .
Update ( )
2021-01-17 16:48:00 +08:00
if err != nil {
return err
}
return this . NotifyUpdate ( tx , clusterId )
2020-12-17 15:51:02 +08:00
}
2021-05-03 11:32:43 +08:00
// UpdateNodeClusterSystemService 修改集群的系统服务设置
2021-01-11 18:16:04 +08:00
func ( this * NodeClusterDAO ) UpdateNodeClusterSystemService ( tx * dbs . Tx , clusterId int64 , serviceType nodeconfigs . SystemServiceType , params maps . Map ) error {
if clusterId <= 0 {
return errors . New ( "invalid clusterId" )
}
service , err := this . Query ( tx ) .
Pk ( clusterId ) .
Result ( "systemServices" ) .
FindStringCol ( "" )
if err != nil {
return err
}
servicesMap := map [ string ] maps . Map { }
if IsNotNull ( service ) {
err = json . Unmarshal ( [ ] byte ( service ) , & servicesMap )
if err != nil {
return err
}
}
if params == nil {
params = maps . Map { }
}
servicesMap [ serviceType ] = params
servicesJSON , err := json . Marshal ( servicesMap )
if err != nil {
return err
}
_ , err = this . Query ( tx ) .
Pk ( clusterId ) .
Set ( "systemServices" , servicesJSON ) .
Update ( )
if err != nil {
return err
}
2021-01-17 16:48:00 +08:00
return this . NotifyUpdate ( tx , clusterId )
2021-01-11 18:16:04 +08:00
}
2021-05-03 11:32:43 +08:00
// FindNodeClusterSystemServiceParams 查找集群的系统服务设置
2021-01-11 18:16:04 +08:00
func ( this * NodeClusterDAO ) FindNodeClusterSystemServiceParams ( tx * dbs . Tx , clusterId int64 , serviceType nodeconfigs . SystemServiceType ) ( params maps . Map , err error ) {
if clusterId <= 0 {
return nil , errors . New ( "invalid clusterId" )
}
service , err := this . Query ( tx ) .
Pk ( clusterId ) .
Result ( "systemServices" ) .
FindStringCol ( "" )
if err != nil {
return nil , err
}
servicesMap := map [ string ] maps . Map { }
if IsNotNull ( service ) {
err = json . Unmarshal ( [ ] byte ( service ) , & servicesMap )
if err != nil {
return nil , err
}
}
return servicesMap [ serviceType ] , nil
}
2021-05-03 11:32:43 +08:00
// FindNodeClusterSystemServices 查找集群的所有服务设置
2021-11-11 14:16:42 +08:00
func ( this * NodeClusterDAO ) FindNodeClusterSystemServices ( tx * dbs . Tx , clusterId int64 , cacheMap * utils . CacheMap ) ( services map [ string ] maps . Map , err error ) {
2021-01-11 18:16:04 +08:00
if clusterId <= 0 {
return nil , errors . New ( "invalid clusterId" )
}
2021-11-11 14:16:42 +08:00
var cacheKey = this . Table + ":FindNodeClusterSystemServices:" + types . String ( clusterId )
if cacheMap != nil {
cache , ok := cacheMap . Get ( cacheKey )
if ok {
return cache . ( map [ string ] maps . Map ) , nil
}
}
2021-01-11 18:16:04 +08:00
service , err := this . Query ( tx ) .
Pk ( clusterId ) .
Result ( "systemServices" ) .
FindStringCol ( "" )
if err != nil {
return nil , err
}
servicesMap := map [ string ] maps . Map { }
if IsNotNull ( service ) {
err = json . Unmarshal ( [ ] byte ( service ) , & servicesMap )
if err != nil {
return nil , err
}
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap . Put ( cacheKey , servicesMap )
}
2021-01-11 18:16:04 +08:00
return servicesMap , nil
}
2021-05-03 11:32:43 +08:00
// GenUniqueId 生成唯一ID
2021-01-27 23:00:02 +08:00
func ( this * NodeClusterDAO ) GenUniqueId ( tx * dbs . Tx ) ( string , error ) {
2020-10-17 11:14:53 +08:00
for {
uniqueId := rands . HexString ( 32 )
2021-01-01 23:31:30 +08:00
ok , err := this . Query ( tx ) .
2020-10-17 11:14:53 +08:00
Attr ( "uniqueId" , uniqueId ) .
Exist ( )
if err != nil {
return "" , err
}
if ok {
continue
}
return uniqueId , nil
}
}
2021-01-17 16:48:00 +08:00
2021-05-03 11:32:43 +08:00
// FindLatestNodeClusters 查询最近访问的集群
func ( this * NodeClusterDAO ) FindLatestNodeClusters ( tx * dbs . Tx , size int64 ) ( result [ ] * NodeCluster , err error ) {
2021-05-03 15:16:02 +08:00
itemTable := SharedLatestItemDAO . Table
itemType := LatestItemTypeCluster
2021-05-03 11:32:43 +08:00
_ , err = this . Query ( tx ) .
Result ( this . Table + ".id" , this . Table + ".name" ) .
2021-05-03 15:16:02 +08:00
Join ( SharedLatestItemDAO , dbs . QueryJoinRight , this . Table + ".id=" + itemTable + ".itemId AND " + itemTable + ".itemType='" + itemType + "'" ) .
Asc ( "CEIL((UNIX_TIMESTAMP() - " + itemTable + ".updatedAt) / (7 * 86400))" ) . // 优先一个星期以内的
Desc ( itemTable + ".count" ) .
2021-05-03 11:32:43 +08:00
State ( NodeClusterStateEnabled ) .
Limit ( size ) .
Slice ( & result ) .
FindAll ( )
return
}
2021-07-31 22:23:11 +08:00
// CheckNodeClusterIsOn 获取集群是否正在启用状态
func ( this * NodeClusterDAO ) CheckNodeClusterIsOn ( tx * dbs . Tx , clusterId int64 ) ( bool , error ) {
return this . Query ( tx ) .
Pk ( clusterId ) .
State ( NodeClusterStateEnabled ) .
Attr ( "isOn" , true ) .
Exist ( )
}
// FindEnabledNodeClustersWithIds 查找一组集群
func ( this * NodeClusterDAO ) FindEnabledNodeClustersWithIds ( tx * dbs . Tx , clusterIds [ ] int64 ) ( result [ ] * NodeCluster , err error ) {
if len ( clusterIds ) == 0 {
return
}
for _ , clusterId := range clusterIds {
cluster , err := this . Query ( tx ) .
Pk ( clusterId ) .
State ( NodeClusterStateEnabled ) .
Find ( )
if err != nil {
return nil , err
}
if cluster == nil {
continue
}
result = append ( result , cluster . ( * NodeCluster ) )
}
return
}
2021-09-13 10:51:05 +08:00
// ExistsEnabledCluster 检查集群是否存在
func ( this * NodeClusterDAO ) ExistsEnabledCluster ( tx * dbs . Tx , clusterId int64 ) ( bool , error ) {
if clusterId <= 0 {
return false , nil
}
return this . Query ( tx ) .
Pk ( clusterId ) .
State ( NodeClusterStateEnabled ) .
Exist ( )
}
2021-12-09 18:49:51 +08:00
// FindClusterBasicInfo 查找集群基础信息
func ( this * NodeClusterDAO ) FindClusterBasicInfo ( tx * dbs . Tx , clusterId int64 , cacheMap * utils . CacheMap ) ( * NodeCluster , error ) {
var cacheKey = this . Table + ":FindClusterBasicInfo:" + types . String ( clusterId )
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cache , ok := cacheMap . Get ( cacheKey )
if ok {
2021-12-09 18:49:51 +08:00
return cache . ( * NodeCluster ) , nil
2021-11-11 14:16:42 +08:00
}
2021-10-12 11:44:24 +08:00
}
2021-12-09 18:49:51 +08:00
cluster , err := this . Query ( tx ) .
2021-10-12 11:44:24 +08:00
Pk ( clusterId ) .
2022-01-03 16:27:50 +08:00
Result ( "timeZone" , "nodeMaxThreads" , "nodeTCPMaxConnections" , "cachePolicyId" , "httpFirewallPolicyId" , "autoOpenPorts" ) .
2021-12-09 18:49:51 +08:00
Find ( )
if err != nil || cluster == nil {
return nil , err
2021-10-12 11:44:24 +08:00
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
2021-12-09 18:49:51 +08:00
cacheMap . Put ( cacheKey , cluster )
2021-11-11 14:16:42 +08:00
}
2021-12-09 18:49:51 +08:00
return cluster . ( * NodeCluster ) , nil
2021-10-12 11:44:24 +08:00
}
2021-05-03 11:32:43 +08:00
// NotifyUpdate 通知更新
2021-01-17 16:48:00 +08:00
func ( this * NodeClusterDAO ) NotifyUpdate ( tx * dbs . Tx , clusterId int64 ) error {
2021-08-08 15:47:48 +08:00
return SharedNodeTaskDAO . CreateClusterTask ( tx , nodeconfigs . NodeRoleNode , clusterId , NodeTaskTypeConfigChanged )
2021-01-17 16:48:00 +08:00
}
2021-01-27 23:00:02 +08:00
2021-05-03 11:32:43 +08:00
// NotifyDNSUpdate 通知DNS更新
2021-01-27 23:00:02 +08:00
// TODO 更新新的DNS解析记录的同时, 需要删除老的DNS解析记录
func ( this * NodeClusterDAO ) NotifyDNSUpdate ( tx * dbs . Tx , clusterId int64 ) error {
err := dns . SharedDNSTaskDAO . CreateClusterTask ( tx , clusterId , dns . DNSTaskTypeClusterChange )
if err != nil {
return err
}
return nil
}