Hi,

Sorry to reply to my own message. Combing through the cf3 reference
guide, I have found a way to get rid of the flag file, using
returnszero to check whether an ls of the file is successful.

This is better, but I still would like to find a way to achieve this
without resorting to external commands (plus I have to use "useshell"
in returnszero, for it to understand the shell metacharacters).

Thanks again,
--Diego 

body common control
{
  bundlesequence => { "run" };
}

##################################################

bundle agent run
{
vars:
  "filepattern" string => "/tmp/runfile.*";
  "cmd"      string => "/bin/mktemp /tmp/runfile.XXXXXXXXXXXX";

classes:
  "fileexists" expression => returnszero("/bin/ls ${filepattern}
>/dev/null 2>&1", "useshell");

commands:
  !fileexists::
    "${cmd}"
    classes => if_ok("cmdran");

reports:
  cmdran::
    "Command ${cmd} ran successfully";

  fileexists::
    "A file matching ${filepattern} already exists, command not running";

}

# if_ok copied from cfengine_stdlib.cf, to make example self-contained
body classes if_ok(x) {
  promise_repaired => { "$(x)" };
  promise_kept => { "$(x)" };
}




On Thu, Jan 21, 2010 at 10:30 AM, Diego Zamboni <di...@zzamboni.org> wrote:
> Hi,
>
> I have a script that produces an output file with a known file pattern, but
> the precise output filename is not predictable. I would like to ensure the
> output file is there, and if not run the script to produce it.
> Unfortunately, it seems that neither fileexists() nor file promises allow
> metacharacters to specify patterns. What I would like to do, ideally, would
> be something like this (which does not work):
>
> classes:
>    "fileexists" expression => fileexists("/tmp/runfile.*");
>
> commands:
>   !fileexists:
>       "${cmd}
>            classes => if_ok("cmdran");
>
> What I have come up with so far involves using a flag file with a fixed
> filename, that gets created after the command is run, then I can test for
> existence of the flag file. This works, but seems inelegant and breaks
> easily due to the dependance on a second, unrelated file. Below is my
> example file, which works (I have replaced my script with mktemp to make it
> self-contained).
>
> I am new to cfengine, using community 3.0.3. Thanks for any ideas.
>
> --Diego
>
>
> body common control
> {
>  bundlesequence => { "run" };
> }
>
> ##################################################
>
> bundle agent run
> {
> vars:
>  "flagfile" string => "/tmp/.done_run";
>  "cmd"      string => "/bin/mktemp /tmp/runfile.XXXXXXXXXXXX";
>
> classes:
>  "flagexists" expression => fileexists(${flagfile});
>
> commands:
>  !flagexists::
>    "${cmd}"
>    classes => if_ok("cmdran");
>
> files:
>  cmdran::
>    "${flagfile}"
>    create => true;
>
> reports:
>  cmdran::
>    "Command ${cmd} ran successfully";
>
>  flagexists::
>    "Flag file ${flagfile} already exists, command not running";
>
> }
>
> # if_ok copied from cfengine_stdlib.cf, to make example self-contained
> body classes if_ok(x)
> {
>  promise_repaired => { "$(x)" };
>  promise_kept => { "$(x)" };
> }
>
> ----------------
> # ls /tmp/runfile.* /tmp/.done_run
> /bin/ls: /tmp/runfile.*: No such file or directory
> /bin/ls: /tmp/.done_run: No such file or directory
>
> # cf-agent -f
> /var/cf-masterfiles/config/branches/cf3/inputs/tests/test_filepattern.cf -K
> Q: "...bin/mktemp /tmp": /tmp/runfile.LdxffjH28353
> I: Last 1 QUOTEed lines were generated by promiser "/bin/mktemp
> /tmp/runfile.XXXXXXXXXXXX"
> I: Made in version 'not specified' of
> '/var/cf-masterfiles/config/branches/cf3/inputs/tests/test_filepattern.cf'
> near line 24
> R: Command /bin/mktemp /tmp/runfile.XXXXXXXXXXXX ran successfully
> R: Flag file /tmp/.done_run already exists, command not running
>
> # ls /tmp/runfile.* /tmp/.done_run
> /tmp/.done_run  /tmp/runfile.LdxffjH28353
>
> # cf-agent -f
> /var/cf-masterfiles/config/branches/cf3/inputs/tests/test_filepattern.cf -K
> R: Flag file /tmp/.done_run already exists, command not running
>
_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to