Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

One way to achieve this would be to use the module protocol supported by CFEngine: https://cfengine.com/manuals/cf3-Reference#module-in-commands. 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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.