restart service after rebuild, fix #1

This commit is contained in:
Darko Luketic 2024-01-08 17:44:56 +01:00
parent 58c4ed602e
commit 7432d3423d
1 changed files with 24 additions and 3 deletions

View File

@ -48,7 +48,7 @@ func (r *Runner) Run() error {
SetContent(l).
Save(context.Background())
if e != nil {
return fmt.Errorf("error creating log entry: %s", e)
return fmt.Errorf("error creating go build log entry: %s", e)
}
if m.MoveToTarget {
if fl, e := copyFile(
@ -62,7 +62,7 @@ func (r *Runner) Run() error {
SetContent(fl).
Save(context.Background())
if e != nil {
return fmt.Errorf("error saving log entry: %s", e)
return fmt.Errorf("error creating move to target log entry: %s", e)
}
}
} else {
@ -70,7 +70,17 @@ func (r *Runner) Run() error {
return fmt.Errorf("error chowning binary: %s", e)
}
}
if l, e := restartSystemdService(m.ServiceName); e != nil {
return fmt.Errorf("error restarting systemd service %s: %s", m.ServiceName, e)
} else {
_, e = r.client.Logentry.
Create().
SetContent(l).
Save(context.Background())
if e != nil {
return fmt.Errorf("error creating service restart log entry: %s", e)
}
}
}
}
return nil
@ -146,3 +156,14 @@ func copyFile(src, dst string) (string, error) {
return fmt.Sprintf("copied %d bytes from %s to %s", size, src, dst), nil
}
}
// restartSystemdService restarts the named systemd service via systemctl restart shell command
func restartSystemdService(name string) (string, error) {
cmd := exec.Command("systemctl", "restart", name)
var out strings.Builder
cmd.Stdout = &out
if e := cmd.Run(); e != nil {
return "", e
}
return out.String(), nil
}