mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-22 21:06:35 +08:00
优化服务配置更新机制
This commit is contained in:
@@ -642,10 +642,6 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64) (*nodeconfigs.N
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, server := range servers {
|
for _, server := range servers {
|
||||||
if len(server.Config) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server)
|
serverConfig, err := SharedServerDAO.ComposeServerConfig(tx, server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models/dns"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
"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/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -310,49 +307,6 @@ func (this *ServerDAO) UpdateServerIsOn(tx *dbs.Tx, serverId int64, isOn bool) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateServerConfig 修改服务配置
|
|
||||||
func (this *ServerDAO) UpdateServerConfig(tx *dbs.Tx, serverId int64, configJSON []byte, updateMd5 bool) (isChanged bool, err error) {
|
|
||||||
if serverId <= 0 {
|
|
||||||
return false, errors.New("serverId should not be smaller than 0")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查询以前的md5
|
|
||||||
oldConfigMd5, err := this.Query(tx).
|
|
||||||
Pk(serverId).
|
|
||||||
Result("configMd5").
|
|
||||||
FindStringCol("")
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
globalConfig, err := SharedSysSettingDAO.ReadSetting(tx, systemconfigs.SettingCodeServerGlobalConfig)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
m := md5.New()
|
|
||||||
_, _ = m.Write(configJSON) // 当前服务配置
|
|
||||||
_, _ = m.Write(globalConfig) // 全局配置
|
|
||||||
h := m.Sum(nil)
|
|
||||||
newConfigMd5 := fmt.Sprintf("%x", h)
|
|
||||||
|
|
||||||
// 如果配置相同则不更改
|
|
||||||
if oldConfigMd5 == newConfigMd5 {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
op := NewServerOperator()
|
|
||||||
op.Id = serverId
|
|
||||||
op.Config = JSONBytes(configJSON)
|
|
||||||
op.Version = dbs.SQL("version+1")
|
|
||||||
|
|
||||||
if updateMd5 {
|
|
||||||
op.ConfigMd5 = newConfigMd5
|
|
||||||
}
|
|
||||||
err = this.Save(tx, op)
|
|
||||||
return true, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateServerHTTP 修改HTTP配置
|
// UpdateServerHTTP 修改HTTP配置
|
||||||
func (this *ServerDAO) UpdateServerHTTP(tx *dbs.Tx, serverId int64, config []byte) error {
|
func (this *ServerDAO) UpdateServerHTTP(tx *dbs.Tx, serverId int64, config []byte) error {
|
||||||
if serverId <= 0 {
|
if serverId <= 0 {
|
||||||
@@ -965,19 +919,6 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server) (*serverc
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenewServerConfig 更新服务的Config配置
|
|
||||||
func (this *ServerDAO) RenewServerConfig(tx *dbs.Tx, serverId int64, updateMd5 bool) (isChanged bool, err error) {
|
|
||||||
serverConfig, err := this.ComposeServerConfigWithServerId(tx, serverId)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
data, err := json.Marshal(serverConfig)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return this.UpdateServerConfig(tx, serverId, data, updateMd5)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindReverseProxyRef 根据条件获取反向代理配置
|
// FindReverseProxyRef 根据条件获取反向代理配置
|
||||||
func (this *ServerDAO) FindReverseProxyRef(tx *dbs.Tx, serverId int64) (*serverconfigs.ReverseProxyRef, error) {
|
func (this *ServerDAO) FindReverseProxyRef(tx *dbs.Tx, serverId int64) (*serverconfigs.ReverseProxyRef, error) {
|
||||||
reverseProxy, err := this.Query(tx).
|
reverseProxy, err := this.Query(tx).
|
||||||
@@ -1424,12 +1365,6 @@ func (this *ServerDAO) FindLatestServers(tx *dbs.Tx, size int64) (result []*Serv
|
|||||||
|
|
||||||
// NotifyUpdate 同步集群
|
// NotifyUpdate 同步集群
|
||||||
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
|
||||||
// 更新配置
|
|
||||||
_, err := this.RenewServerConfig(tx, serverId, true)
|
|
||||||
if err != nil && err != ErrNotFound {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建任务
|
// 创建任务
|
||||||
clusterId, err := this.FindServerClusterId(tx, serverId)
|
clusterId, err := this.FindServerClusterId(tx, serverId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -554,11 +554,21 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
|
|||||||
auditingResult.IsOk = true
|
auditingResult.IsOk = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 配置
|
||||||
|
config, err := models.SharedServerDAO.ComposeServerConfig(tx, server)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
configJSON, err := json.Marshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
result = append(result, &pb.Server{
|
result = append(result, &pb.Server{
|
||||||
Id: int64(server.Id),
|
Id: int64(server.Id),
|
||||||
IsOn: server.IsOn == 1,
|
IsOn: server.IsOn == 1,
|
||||||
Type: server.Type,
|
Type: server.Type,
|
||||||
Config: []byte(server.Config),
|
Config: configJSON,
|
||||||
Name: server.Name,
|
Name: server.Name,
|
||||||
Description: server.Description,
|
Description: server.Description,
|
||||||
HttpJSON: []byte(server.Http),
|
HttpJSON: []byte(server.Http),
|
||||||
@@ -685,6 +695,16 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 配置
|
||||||
|
config, err := models.SharedServerDAO.ComposeServerConfig(tx, server)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
configJSON, err := json.Marshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &pb.FindEnabledServerResponse{Server: &pb.Server{
|
return &pb.FindEnabledServerResponse{Server: &pb.Server{
|
||||||
Id: int64(server.Id),
|
Id: int64(server.Id),
|
||||||
IsOn: server.IsOn == 1,
|
IsOn: server.IsOn == 1,
|
||||||
@@ -692,7 +712,7 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn
|
|||||||
Name: server.Name,
|
Name: server.Name,
|
||||||
Description: server.Description,
|
Description: server.Description,
|
||||||
DnsName: server.DnsName,
|
DnsName: server.DnsName,
|
||||||
Config: []byte(server.Config),
|
Config: configJSON,
|
||||||
ServerNamesJSON: []byte(server.ServerNames),
|
ServerNamesJSON: []byte(server.ServerNames),
|
||||||
HttpJSON: []byte(server.Http),
|
HttpJSON: []byte(server.Http),
|
||||||
HttpsJSON: []byte(server.Https),
|
HttpsJSON: []byte(server.Https),
|
||||||
|
|||||||
Reference in New Issue
Block a user