fix(host-deployer): deploy telegraf systemd service type simple (#20458)

This commit is contained in:
wanyaoqi
2024-06-03 19:51:21 +08:00
committed by GitHub
parent 64a7e7c268
commit 4e3feec902
3 changed files with 15 additions and 7 deletions
+4 -1
View File
@@ -793,7 +793,10 @@ func (d *sLinuxRootFs) DeployTelegraf(config string) (bool, error) {
return false, errors.Wrap(err, "chmod supervise run script")
}
initCmd := fmt.Sprintf("%s/supervise %s", cloudMonitorPath, telegrafPath)
err = d.installInitScript("telegraf", initCmd)
if d.isSupportSystemd() {
initCmd = path.Join(telegrafPath, "run")
}
err = d.installInitScript("telegraf", initCmd, false)
if err != nil {
return false, errors.Wrap(err, "installInitScript")
}
+10 -5
View File
@@ -31,9 +31,9 @@ func (d *sLinuxRootFs) isSupportSystemd() bool {
return d.rootFs.Exists(unitDirPath, false) && d.rootFs.Exists(enableDirPath, false)
}
func (d *sLinuxRootFs) installInitScript(name, cmd string) error {
func (d *sLinuxRootFs) installInitScript(name, cmd string, oneshot bool) error {
if d.isSupportSystemd() {
return d.installSystemd(name, cmd)
return d.installSystemd(name, cmd, oneshot)
} else {
return d.installCrond(cmd)
}
@@ -54,10 +54,15 @@ func (d *sLinuxRootFs) installCrond(cmd string) error {
return nil
}
func (d *sLinuxRootFs) installSystemd(name, cmd string) error {
func (d *sLinuxRootFs) installSystemd(name, cmd string, oneshot bool) error {
var serviceName = fmt.Sprintf("%s.service", name)
var unitPath = fmt.Sprintf("%s/%s", unitDirPath, serviceName)
var enablePath = fmt.Sprintf("%s/%s", enableDirPath, serviceName)
var serviceType = "simple"
if oneshot {
serviceType = "oneshot"
}
var unitContent = fmt.Sprintf(`[Unit]
Description=Run once
After=local-fs.target
@@ -66,11 +71,11 @@ After=network.target
[Service]
ExecStart=%s
RemainAfterExit=true
Type=oneshot
Type=%s
[Install]
WantedBy=multi-user.target
`, cmd)
`, cmd, serviceType)
err := d.rootFs.FilePutContents(unitPath, unitContent, false, false)
if err != nil {
return errors.Wrap(err, "save user_data unit fail")
+1 -1
View File
@@ -93,7 +93,7 @@ func (d *sLinuxRootFs) deployUserDataBySystemd(userData string) error {
}
}
{
err := d.installInitScript(serviceName, "/bin/sh "+scriptPath)
err := d.installInitScript(serviceName, "/bin/sh "+scriptPath, true)
if err != nil {
return errors.Wrap(err, "installInitScript")
}