Files
EdgeAPI/internal/installers/deploy_manager.go

153 lines
3.3 KiB
Go
Raw Permalink Normal View History

2020-10-28 12:35:36 +08:00
package installers
import (
2024-07-27 14:15:25 +08:00
"regexp"
"sync"
2020-10-28 12:35:36 +08:00
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
stringutil "github.com/iwind/TeaGo/utils/string"
)
var SharedDeployManager = NewDeployManager()
2022-01-01 17:36:01 +08:00
// DeployManager 节点部署文件管理器
// 如果节点部署文件有变化需要重启API节点以便于生效
2020-10-28 12:35:36 +08:00
type DeployManager struct {
dir string
2022-01-01 17:36:01 +08:00
nodeFiles []*DeployFile
nsNodeFiles []*DeployFile
locker sync.Mutex
2020-10-28 12:35:36 +08:00
}
2022-01-01 17:36:01 +08:00
// NewDeployManager 获取新节点部署文件管理器
2020-10-28 12:35:36 +08:00
func NewDeployManager() *DeployManager {
2022-01-01 17:36:01 +08:00
var manager = &DeployManager{
2020-10-28 12:35:36 +08:00
dir: Tea.Root + "/deploy",
}
2022-01-01 17:36:01 +08:00
manager.LoadNodeFiles()
manager.LoadNSNodeFiles()
return manager
2020-10-28 12:35:36 +08:00
}
// LoadNodeFiles 加载所有边缘节点文件
func (this *DeployManager) LoadNodeFiles() []*DeployFile {
this.locker.Lock()
defer this.locker.Unlock()
2022-01-01 17:36:01 +08:00
if len(this.nodeFiles) > 0 {
return this.nodeFiles
}
var keyMap = map[string]*DeployFile{} // key => File
2020-10-28 12:35:36 +08:00
var reg = regexp.MustCompile(`^edge-node-(\w+)-(\w+)-v([0-9.]+)\.zip$`)
2020-10-28 12:35:36 +08:00
for _, file := range files.NewFile(this.dir).List() {
var name = file.Name()
2020-10-28 12:35:36 +08:00
if !reg.MatchString(name) {
continue
}
var matches = reg.FindStringSubmatch(name)
var osName = matches[1]
var arch = matches[2]
var version = matches[3]
2020-10-28 12:35:36 +08:00
var key = osName + "_" + arch
2020-10-28 12:35:36 +08:00
oldFile, ok := keyMap[key]
if ok && stringutil.VersionCompare(oldFile.Version, version) > 0 {
continue
}
keyMap[key] = &DeployFile{
OS: osName,
Arch: arch,
Version: version,
Path: file.Path(),
}
}
var result = []*DeployFile{}
2020-10-28 12:35:36 +08:00
for _, v := range keyMap {
result = append(result, v)
}
2022-01-01 17:36:01 +08:00
this.nodeFiles = result
2020-10-28 12:35:36 +08:00
return result
}
2021-06-10 19:21:45 +08:00
// FindNodeFile 查找特别平台的节点文件
func (this *DeployManager) FindNodeFile(os string, arch string) *DeployFile {
for _, file := range this.LoadNodeFiles() {
if file.OS == os && file.Arch == arch {
return file
}
}
return nil
}
2021-07-22 18:42:57 +08:00
// LoadNSNodeFiles 加载所有NS节点安装文件
func (this *DeployManager) LoadNSNodeFiles() []*DeployFile {
this.locker.Lock()
defer this.locker.Unlock()
2022-01-01 17:36:01 +08:00
if len(this.nsNodeFiles) > 0 {
return this.nsNodeFiles
}
var keyMap = map[string]*DeployFile{} // key => File
var reg = regexp.MustCompile(`^edge-dns-(\w+)-(\w+)-v([0-9.]+)\.zip$`)
for _, file := range files.NewFile(this.dir).List() {
var name = file.Name()
if !reg.MatchString(name) {
continue
}
var matches = reg.FindStringSubmatch(name)
var osName = matches[1]
var arch = matches[2]
var version = matches[3]
var key = osName + "_" + arch
oldFile, ok := keyMap[key]
if ok && stringutil.VersionCompare(oldFile.Version, version) > 0 {
continue
}
keyMap[key] = &DeployFile{
OS: osName,
Arch: arch,
Version: version,
Path: file.Path(),
}
}
var result = []*DeployFile{}
for _, v := range keyMap {
result = append(result, v)
}
2022-01-01 17:36:01 +08:00
this.nsNodeFiles = result
return result
2021-06-10 19:21:45 +08:00
}
2021-07-22 18:42:57 +08:00
// FindNSNodeFile 查找特别平台的NS节点安装文件
func (this *DeployManager) FindNSNodeFile(os string, arch string) *DeployFile {
for _, file := range this.LoadNSNodeFiles() {
if file.OS == os && file.Arch == arch {
return file
}
}
return nil
}
// Reload 重置缓存
func (this *DeployManager) Reload() {
this.locker.Lock()
defer this.locker.Unlock()
this.nodeFiles = nil
this.nsNodeFiles = nil
}