]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
remote: show group/trigger in verbose log for commands
authorSimon Ruderich <simon@ruderich.org>
Tue, 20 Apr 2021 09:43:02 +0000 (11:43 +0200)
committerSimon Ruderich <simon@ruderich.org>
Tue, 20 Apr 2021 09:43:02 +0000 (11:43 +0200)
This helps the user to figure out why the command was executed.

cmd/safcm-remote/sync/commands.go
cmd/safcm-remote/sync/commands_test.go
cmd/safcm/config/commands.go
cmd/safcm/sync_sync_test.go
types.go

index 84a318d79668b8e43118d9981e5d7736db3f766c..5a3c59c1d1d4c156396992733c1ed27131fe2364 100644 (file)
@@ -31,7 +31,7 @@ func (s *Sync) syncCommands() error {
        // 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
                        }
@@ -40,7 +40,7 @@ func (s *Sync) syncCommands() error {
        // Regular commands afterwards so they can react on triggers if
        // necessary
        for _, x := range s.req.Commands {
-               err := s.syncCommand(x.Cmd, "")
+               err := s.syncCommand(x.Cmd, x.OrigGroup, "")
                if err != nil {
                        return err
                }
@@ -48,7 +48,7 @@ func (s *Sync) syncCommands() error {
        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,
@@ -59,12 +59,20 @@ func (s *Sync) syncCommand(command string, trigger string) error {
        }
        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)
index ba7f4a2e89ec47e16fec11f88f41aad4577a2617..1c534188eae0439e6c3eda15acd816aa11376e30 100644 (file)
@@ -67,6 +67,7 @@ func TestSyncCommands(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo; env | grep SAFCM_",
                                        },
                                },
@@ -84,7 +85,7 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr",
                        },
                        safcm.MsgSyncResp{
@@ -109,6 +110,7 @@ func TestSyncCommands(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo; env | grep SAFCM_",
                                        },
                                },
@@ -140,6 +142,7 @@ func TestSyncCommands(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo hi; false",
                                        },
                                },
@@ -157,7 +160,7 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo hi; false"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo hi; false" (group)`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr",
                        },
                        safcm.MsgSyncResp{
@@ -183,6 +186,7 @@ func TestSyncCommands(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo hi; false",
                                        },
                                },
@@ -214,12 +218,16 @@ func TestSyncCommands(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group1",
                                                Cmd: "echo first",
                                        }, {
+                                               OrigGroup: "group2",
                                                Cmd: "echo second",
                                        }, {
+                                               OrigGroup: "group3",
                                                Cmd: "false",
                                        }, {
+                                               OrigGroup: "group4",
                                                Cmd: "echo third",
                                        },
                                },
@@ -263,11 +271,11 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo first"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo first" (group1)`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr first",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo second"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo second" (group2)`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr second",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "false"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "false" (group3)`,
                        },
                        safcm.MsgSyncResp{
                                CommandChanges: []safcm.CommandChange{
@@ -333,6 +341,7 @@ func TestSyncCommands(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo; env | grep SAFCM_",
                                        },
                                },
@@ -379,11 +388,11 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ."`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ." (".")`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr .",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger dir"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger dir" ("dir")`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr dir",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo; env | grep SAFCM_" (group)`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr",
                        },
                        safcm.MsgSyncResp{
@@ -451,6 +460,7 @@ func TestSyncCommands(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo; env | grep SAFCM_",
                                        },
                                },
@@ -487,9 +497,9 @@ func TestSyncCommands(t *testing.T) {
                                Env: env,
                        }},
                        []string{
-                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ."`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "echo trigger ." (".")`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr .",
-                               `3: sync remote: commands: running "/bin/sh" "-c" "false"`,
+                               `3: sync remote: commands: running "/bin/sh" "-c" "false" ("dir")`,
                                "5: sync remote: commands: command output:\nfake stdout/stderr dir",
                        },
                        safcm.MsgSyncResp{
index 1ba2b18a4f530b1ba2deb43035b597e3032da87b..72a28fd860772b863127b8da12bfac4b4cf21091 100644 (file)
@@ -46,6 +46,7 @@ func LoadCommands(group string) ([]*safcm.Command, error) {
        var res []*safcm.Command
        for _, x := range cmds {
                res = append(res, &safcm.Command{
+                       OrigGroup: group,
                        Cmd: x,
                })
        }
index 1294a452dc65bbff13d74852789efdf356cc75e6..f100f35125e125fa64884086698f4aac78448d3d 100644 (file)
@@ -138,9 +138,11 @@ func TestHostSyncReq(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo command one",
                                        },
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo -n command two",
                                        },
                                },
@@ -241,9 +243,11 @@ func TestHostSyncReq(t *testing.T) {
                                },
                                Commands: []*safcm.Command{
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo command one",
                                        },
                                        {
+                                               OrigGroup: "group",
                                                Cmd: "echo -n command two",
                                        },
                                },
index d8868868cd299ab9f59c437c757870fee9819992..37c0d4a21c6c5f26672747edd6f42e7ee8ca7323 100644 (file)
--- a/types.go
+++ b/types.go
@@ -107,6 +107,7 @@ type File struct {
 }
 
 type Command struct {
+       OrigGroup string // group which provided this command
        Cmd string
 }