mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-15 00:40:26 +08:00
网站服务变更也能自动同步DNS
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -131,3 +132,12 @@ func (this *DNSProviderDAO) FindAllEnabledDNSProvidersWithType(providerType stri
|
|||||||
FindAll()
|
FindAll()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新数据更新时间
|
||||||
|
func (this *DNSProviderDAO) UpdateProviderDataUpdatedTime(providerId int64) error {
|
||||||
|
_, err := this.Query().
|
||||||
|
Pk(providerId).
|
||||||
|
Set("dataUpdatedAt", time.Now().Unix()).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -862,6 +862,14 @@ func (this *ServerDAO) createEvent() error {
|
|||||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询当前服务的ClusterId
|
||||||
|
func (this *ServerDAO) FindServerClusterId(serverId int64) (int64, error) {
|
||||||
|
return this.Query().
|
||||||
|
Pk(serverId).
|
||||||
|
Result("clusterId").
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
// 生成DNS Name
|
// 生成DNS Name
|
||||||
func (this *ServerDAO) genDNSName() (string, error) {
|
func (this *ServerDAO) genDNSName() (string, error) {
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/logs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerService struct {
|
type ServerService struct {
|
||||||
@@ -60,6 +61,17 @@ func (this *ServerService) UpdateServerBasic(ctx context.Context, req *pb.Update
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查服务变化
|
||||||
|
oldIsOn := server.IsOn == 1
|
||||||
|
if oldIsOn != req.IsOn {
|
||||||
|
go func() {
|
||||||
|
err := this.notifyServerDNSChanged(req.ServerId)
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("[DNS]notify server changed: " + err.Error())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// 更新老的节点版本
|
// 更新老的节点版本
|
||||||
if req.ClusterId != int64(server.ClusterId) {
|
if req.ClusterId != int64(server.ClusterId) {
|
||||||
err = models.SharedNodeDAO.UpdateAllNodesLatestVersionMatch(int64(server.ClusterId))
|
err = models.SharedNodeDAO.UpdateAllNodesLatestVersionMatch(int64(server.ClusterId))
|
||||||
@@ -890,3 +902,45 @@ func (this *ServerService) FindAllEnabledServersDNSWithClusterId(ctx context.Con
|
|||||||
|
|
||||||
return &pb.FindAllEnabledServersDNSWithClusterIdResponse{Servers: result}, nil
|
return &pb.FindAllEnabledServersDNSWithClusterIdResponse{Servers: result}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 自动同步DNS状态
|
||||||
|
func (this *ServerService) notifyServerDNSChanged(serverId int64) error {
|
||||||
|
clusterId, err := models.SharedServerDAO.FindServerClusterId(serverId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dnsInfo, err := models.SharedNodeClusterDAO.FindClusterDNSInfo(clusterId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dnsInfo == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if len(dnsInfo.DnsName) == 0 || dnsInfo.DnsDomainId == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
dnsConfig, err := dnsInfo.DecodeDNSConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !dnsConfig.ServersAutoSync {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行同步
|
||||||
|
domainService := &DNSDomainService{}
|
||||||
|
resp, err := domainService.syncClusterDNS(&pb.SyncDNSDomainDataRequest{
|
||||||
|
DnsDomainId: int64(dnsInfo.DnsDomainId),
|
||||||
|
NodeClusterId: clusterId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !resp.IsOk {
|
||||||
|
err = models.SharedMessageDAO.CreateClusterMessage(clusterId, models.MessageTypeClusterDNSSyncFailed, models.LevelError, "集群DNS同步失败:"+resp.Error, nil)
|
||||||
|
if err != nil {
|
||||||
|
logs.Println("[NODE_SERVICE]" + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user