// Run triggered commands first
for _, path := range s.triggers {
for _, x := range s.req.Files[path].TriggerCommands {
- err := s.syncCommand(x, path)
+ err := s.syncCommand(x, "", path)
if err != nil {
return err
}
// Regular commands afterwards so they can react on triggers if
// necessary
for _, x := range s.req.Commands {
- err := s.syncCommand(x, "")
+ err := s.syncCommand(x.Cmd, x.OrigGroup, "")
if err != nil {
return err
}
return nil
}
-func (s *Sync) syncCommand(command string, trigger string) error {
+func (s *Sync) syncCommand(command, group, trigger string) error {
s.resp.CommandChanges = append(s.resp.CommandChanges,
safcm.CommandChange{
Command: command,
}
change := &s.resp.CommandChanges[len(s.resp.CommandChanges)-1]
+ var info string
+ if trigger != "" {
+ info = fmt.Sprintf("%q", trigger)
+ } else if group != "" {
+ info = group
+ }
+
cmd := exec.Command("/bin/sh", "-c", command)
cmd.Env = safcmEnviroment(s.req.Groups)
// Cannot use cmd.CombinedOutputCmd() because we need another log
// level (here the command is the actual change and not a side effect)
// and different error handling.
- s.log.Verbosef("commands: running %s", run.QuoteForDebug(cmd))
+ s.log.Verbosef("commands: running %s (%s)",
+ run.QuoteForDebug(cmd), info)
out, err := s.cmd.Runner.CombinedOutput(cmd)
if len(out) > 0 {
s.log.Debug2f("commands: command output:\n%s", out)
}
func safcmEnviroment(groups []string) []string {
+ exe, err := os.Executable()
+ if err != nil {
+ panic(err) // should not happen on supported systems
+ }
+
env := os.Environ()
- // Provide additional environment variables so commands can check
- // group membership
+ // Provide additional environment variables
env = append(env,
+ fmt.Sprintf("SAFCM_HELPER=%s", exe),
fmt.Sprintf("SAFCM_GROUPS=%s", strings.Join(groups, " ")))
for _, x := range groups {
env = append(env, fmt.Sprintf("SAFCM_GROUP_%s=%s", x, x))