1 // MsgSyncReq: run commands on the remote host
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024 Simon Ruderich
14 "ruderich.org/simon/safcm"
15 "ruderich.org/simon/safcm/remote/run"
18 func (s *Sync) syncCommands() error {
19 // Run triggered commands first
20 for _, path := range s.triggers {
21 for _, x := range s.req.Files[path].TriggerCommands {
22 err := s.syncCommand(x, "", path)
28 // Regular commands afterwards so they can react on triggers if
30 for _, x := range s.req.Commands {
31 err := s.syncCommand(x.Cmd, x.OrigGroup, "")
39 func (s *Sync) syncCommand(command, group, trigger string) error {
40 s.resp.CommandChanges = append(s.resp.CommandChanges,
48 change := &s.resp.CommandChanges[len(s.resp.CommandChanges)-1]
52 info = fmt.Sprintf("%q", trigger)
53 } else if group != "" {
57 cmd := exec.Command("/bin/sh", "-c", command)
58 cmd.Env = safcmEnviroment(s.req.Groups)
59 // Cannot use cmd.CombinedOutputCmd() because we need another log
60 // level (here the command is the actual change and not a side effect)
61 // and different error handling.
62 s.log.Verbosef("commands: running %s (%s)",
63 run.QuoteForDebug(cmd), info)
64 out, err := s.cmd.Runner.CombinedOutput(cmd)
66 s.log.Debug2f("commands: command output:\n%s", out)
68 change.Output = string(out)
70 change.Error = err.Error()
71 return fmt.Errorf("%q failed: %v", command, err)
77 func safcmEnviroment(groups []string) []string {
78 exe, err := os.Executable()
80 panic(err) // should not happen on supported systems
84 // Provide additional environment variables
86 fmt.Sprintf("SAFCM_HELPER=%s", exe),
87 fmt.Sprintf("SAFCM_GROUPS=%s", strings.Join(groups, " ")))
88 for _, x := range groups {
89 env = append(env, fmt.Sprintf("SAFCM_GROUP_%s=%s", x, x))