3

This question is very similar to How can I use the output of a command in cfengine3 but the answer does not apply in my case I believe.

I want to update a git repository via "git pull" and based on whether that lead to changes trigger some follow up action.

Simplified, if there was something like "match output and set class" via some body if_output_matches I would want to use something like this:

bundle agent updateRepo {

commands:
  "/usr/bin/git pull"
    contain => setuidgiddir_sh("$(globals.user)","$(globals.group)","$(target)"),
    classes => if_output_matches("Already up-to-date.","no_update");

reports:
  no_update::
    "nothing updated";
}

body contain setuidgiddir_sh(owner,group,folder) {
  exec_owner => "$(owner)";
  exec_group => "$(group)";
  useshell => "true";
  chdir => "$(folder)";

}

So, is it possible to use the output of a - possibly expensive command - and base some decision on that?

The execresult function is no good choice for me as a) the pull may become expensive at times (not recommended following the cfengine3 reference) and b) does not allow to specify user, group, working dir - which is important in my case. The repository is in user space and not owned by root.

gnomie
  • 133
  • 3

1 Answers1

6

One way to achieve this would be to use the module protocol supported by CFEngine. With this, you can set arbitrary classes and variables from the script itself. For example, a script like this (untested):

#!/bin/bash
if git pull | grep -q 'Already up-to-date.'; then
  echo "+no_update"
fi

Store it in /var/cfengine/modules/update_git, And then you could do something like this:

commands:
  "update_git"
    contain => setuidgiddir_sh("$(globals.user)","$(globals.group)","$(target)"),
    module => "true";

And then act on no_update as you had before.

You could probably achieve something like this with a classes body using the kept/repaired/failed_returncodes attributes, but a module seems cleaner and more expressive.

Wildcard
  • 153
  • 2
  • 14
Diego Zamboni
  • 284
  • 1
  • 4
  • Thanks, that's a great suggestion. I think I can make something up with sed. Would have upvoted but guess I am too new on serverfault :-( – gnomie Oct 12 '12 at 20:46