]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/commands.go
Use SPDX license identifiers
[safcm/safcm.git] / remote / sync / commands.go
1 // MsgSyncReq: run commands on the remote host
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package sync
7
8 import (
9         "fmt"
10         "os"
11         "os/exec"
12         "strings"
13
14         "ruderich.org/simon/safcm"
15         "ruderich.org/simon/safcm/remote/run"
16 )
17
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)
23                         if err != nil {
24                                 return err
25                         }
26                 }
27         }
28         // Regular commands afterwards so they can react on triggers if
29         // necessary
30         for _, x := range s.req.Commands {
31                 err := s.syncCommand(x.Cmd, x.OrigGroup, "")
32                 if err != nil {
33                         return err
34                 }
35         }
36         return nil
37 }
38
39 func (s *Sync) syncCommand(command, group, trigger string) error {
40         s.resp.CommandChanges = append(s.resp.CommandChanges,
41                 safcm.CommandChange{
42                         Command: command,
43                         Trigger: trigger,
44                 })
45         if s.req.DryRun {
46                 return nil
47         }
48         change := &s.resp.CommandChanges[len(s.resp.CommandChanges)-1]
49
50         var info string
51         if trigger != "" {
52                 info = fmt.Sprintf("%q", trigger)
53         } else if group != "" {
54                 info = group
55         }
56
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)
65         if len(out) > 0 {
66                 s.log.Debug2f("commands: command output:\n%s", out)
67         }
68         change.Output = string(out)
69         if err != nil {
70                 change.Error = err.Error()
71                 return fmt.Errorf("%q failed: %v", command, err)
72         }
73
74         return nil
75 }
76
77 func safcmEnviroment(groups []string) []string {
78         exe, err := os.Executable()
79         if err != nil {
80                 panic(err) // should not happen on supported systems
81         }
82
83         env := os.Environ()
84         // Provide additional environment variables
85         env = append(env,
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))
90         }
91         return env
92 }