1 // MsgSyncReq: run commands on the remote host
3 // Copyright (C) 2021-2023 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 "ruderich.org/simon/safcm"
27 "ruderich.org/simon/safcm/remote/run"
30 func (s *Sync) syncCommands() error {
31 // Run triggered commands first
32 for _, path := range s.triggers {
33 for _, x := range s.req.Files[path].TriggerCommands {
34 err := s.syncCommand(x, "", path)
40 // Regular commands afterwards so they can react on triggers if
42 for _, x := range s.req.Commands {
43 err := s.syncCommand(x.Cmd, x.OrigGroup, "")
51 func (s *Sync) syncCommand(command, group, trigger string) error {
52 s.resp.CommandChanges = append(s.resp.CommandChanges,
60 change := &s.resp.CommandChanges[len(s.resp.CommandChanges)-1]
64 info = fmt.Sprintf("%q", trigger)
65 } else if group != "" {
69 cmd := exec.Command("/bin/sh", "-c", command)
70 cmd.Env = safcmEnviroment(s.req.Groups)
71 // Cannot use cmd.CombinedOutputCmd() because we need another log
72 // level (here the command is the actual change and not a side effect)
73 // and different error handling.
74 s.log.Verbosef("commands: running %s (%s)",
75 run.QuoteForDebug(cmd), info)
76 out, err := s.cmd.Runner.CombinedOutput(cmd)
78 s.log.Debug2f("commands: command output:\n%s", out)
80 change.Output = string(out)
82 change.Error = err.Error()
83 return fmt.Errorf("%q failed: %v", command, err)
89 func safcmEnviroment(groups []string) []string {
90 exe, err := os.Executable()
92 panic(err) // should not happen on supported systems
96 // Provide additional environment variables
98 fmt.Sprintf("SAFCM_HELPER=%s", exe),
99 fmt.Sprintf("SAFCM_GROUPS=%s", strings.Join(groups, " ")))
100 for _, x := range groups {
101 env = append(env, fmt.Sprintf("SAFCM_GROUP_%s=%s", x, x))