69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package component
|
|
|
|
import (
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/boot"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/config/bin_path"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/pkg/log"
|
|
process_manager "git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/pkg/process-manager"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/pkg/utils"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
telegrafConfFile = filepath.Join(bin_path.BINDIR, "/data-collector/telegraf/conf/", "telegraf.conf")
|
|
bakFile = filepath.Join(bin_path.BINDIR, "/data-collector/telegraf/conf/", "telegraf.conf.bak")
|
|
)
|
|
|
|
func UpdateTelegrafConfig(configText []byte) error {
|
|
// 1.先备份
|
|
oldConfig, err := os.ReadFile(telegrafConfFile)
|
|
err = os.WriteFile(bakFile, oldConfig, fs.ModePerm)
|
|
if err != nil {
|
|
panic("备份文件失败:" + err.Error())
|
|
return err
|
|
}
|
|
|
|
// 2.后替换
|
|
err = os.WriteFile(telegrafConfFile, configText, fs.ModePerm)
|
|
|
|
if err != nil {
|
|
panic("替换文件失败:" + err.Error())
|
|
go rollback(false, err)
|
|
return err
|
|
}
|
|
|
|
// 3.再启动,让minit重启
|
|
utils.StopCh <- os.Interrupt
|
|
time.Sleep(time.Second * 5)
|
|
go process_manager.Start(boot.ProcessUnitDir)
|
|
|
|
return err
|
|
}
|
|
|
|
func RecoverConfig() {
|
|
config, _ := os.ReadFile(bakFile)
|
|
os.WriteFile(telegrafConfFile, config, fs.ModePerm)
|
|
|
|
utils.StopCh <- os.Interrupt
|
|
time.Sleep(time.Second * 5)
|
|
|
|
go process_manager.Start(boot.ProcessUnitDir)
|
|
}
|
|
|
|
func rollback(needRestart bool, err error) {
|
|
|
|
log.Infof("Telegraf配置更新失败: %s", err)
|
|
config, _ := os.ReadFile(bakFile)
|
|
os.WriteFile(telegrafConfFile, config, fs.ModePerm)
|
|
if needRestart {
|
|
utils.StopCh <- os.Interrupt
|
|
time.Sleep(time.Second * 5)
|
|
|
|
go process_manager.Start(boot.ProcessUnitDir)
|
|
}
|
|
|
|
}
|