]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm-remote/sync/commands.go
84a318d79668b8e43118d9981e5d7736db3f766c
[safcm/safcm.git] / cmd / safcm-remote / sync / commands.go
1 // MsgSyncReq: run commands on the remote host
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
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.
9 //
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.
14 //
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/>.
17
18 package sync
19
20 import (
21         "fmt"
22         "os"
23         "os/exec"
24         "strings"
25
26         "ruderich.org/simon/safcm"
27         "ruderich.org/simon/safcm/cmd/safcm-remote/run"
28 )
29
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)
35                         if err != nil {
36                                 return err
37                         }
38                 }
39         }
40         // Regular commands afterwards so they can react on triggers if
41         // necessary
42         for _, x := range s.req.Commands {
43                 err := s.syncCommand(x.Cmd, "")
44                 if err != nil {
45                         return err
46                 }
47         }
48         return nil
49 }
50
51 func (s *Sync) syncCommand(command string, trigger string) error {
52         s.resp.CommandChanges = append(s.resp.CommandChanges,
53                 safcm.CommandChange{
54                         Command: command,
55                         Trigger: trigger,
56                 })
57         if s.req.DryRun {
58                 return nil
59         }
60         change := &s.resp.CommandChanges[len(s.resp.CommandChanges)-1]
61
62         cmd := exec.Command("/bin/sh", "-c", command)
63         cmd.Env = safcmEnviroment(s.req.Groups)
64         // Cannot use cmd.CombinedOutputCmd() because we need another log
65         // level (here the command is the actual change and not a side effect)
66         // and different error handling.
67         s.log.Verbosef("commands: running %s", run.QuoteForDebug(cmd))
68         out, err := s.cmd.Runner.CombinedOutput(cmd)
69         if len(out) > 0 {
70                 s.log.Debug2f("commands: command output:\n%s", out)
71         }
72         change.Output = string(out)
73         if err != nil {
74                 change.Error = err.Error()
75                 return fmt.Errorf("%q failed: %v", command, err)
76         }
77
78         return nil
79 }
80
81 func safcmEnviroment(groups []string) []string {
82         exe, err := os.Executable()
83         if err != nil {
84                 panic(err) // should not happen on supported systems
85         }
86
87         env := os.Environ()
88         // Provide additional environment variables
89         env = append(env,
90                 fmt.Sprintf("SAFCM_HELPER=%s", exe),
91                 fmt.Sprintf("SAFCM_GROUPS=%s", strings.Join(groups, " ")))
92         for _, x := range groups {
93                 env = append(env, fmt.Sprintf("SAFCM_GROUP_%s=%s", x, x))
94         }
95         return env
96 }