Command or script execution

Execute a command, for instance to start a MySQL service. Note that simple shell commands like rm or mkdir cannot be managed by CFEngine, so none of the protections that CFEngine offers can be applied to the process. Moreover, this starts a new process, adding to the burden on the system.

code
body common control
{
      bundlesequence  => { "my_commands" };
      inputs => { "$(sys.libdir)/stdlib.cf" };
}

bundle agent my_commands
{
  commands:
    Sunday.Hr04.Min05_10.myhost::
      "/usr/bin/update_db";

    any::
      "/etc/mysql/start"
      contain => setuid("mysql");
}

Change directory for command

code
body common control
{
      bundlesequence  => { "example" };
}

body contain cd(dir)
{
      chdir => "${dir}";
      useshell => "true";
}

bundle agent example
{
  commands:
      "/bin/pwd"
      contain => cd("/tmp");
}

Commands example

code
body common control
{
      bundlesequence  => { "my_commands" };
      inputs => { "$(sys.libdir)/stdlib.cf" };
}

bundle agent my_commands
{
  commands:
    Sunday.Hr04.Min05_10.myhost::
      "/usr/bin/update_db";

    any::
      "/etc/mysql/start"
      contain => setuid("mysql");
}

Execresult example

code
body common control
{
      bundlesequence  => { "example" };
}

bundle agent example
{
  vars:
      "my_result" string => execresult("/bin/ls /tmp","noshell");

  reports:
      "Variable is $(my_result)";
}

Methods

code
body common control
{
      bundlesequence  => { "testbundle"  };
      version => "1.2.3";
}

bundle agent testbundle
{
  vars:
      "userlist" slist => { "mark", "jeang", "jonhenrik", "thomas", "eben" };
  methods:
      "any" usebundle => subtest("$(userlist)");
}


bundle agent subtest(user)
{
  commands:
      "/bin/echo Fix $(user)";
  reports:
      "Finished doing stuff for $(user)";
}

Method validation

code
body common control
{
      bundlesequence  => { "testbundle"  };
      version => "1.2.3";
}

body agent control
{
      abortbundleclasses => { "invalid" };
}

bundle agent testbundle
{
  vars:
      "userlist" slist => { "xyz", "mark", "jeang", "jonhenrik", "thomas", "eben" };

  methods:
      "any" usebundle => subtest("$(userlist)");
}

bundle agent subtest(user)
{
  classes:
      "invalid" not => regcmp("[a-z][a-z][a-z][a-z]","$(user)");

  reports:
    !invalid::
      "User name $(user) is valid at 4 letters";
    invalid::
      "User name $(user) is invalid";
}

Trigger classes

code
body common control
{
    any::
      bundlesequence  => { "insert" };
}


bundle agent insert
{
  vars:
      "v" string => "
                One potato
                Two potato
                Three potahto
                Four
                ";

  files:
      "/tmp/test_insert"
      edit_line => Insert("$(insert.v)"),
      edit_defaults => empty,
      classes => trigger("edited");

  commands:
    edited::
      "/bin/echo make bananas";

  reports:
    edited::
      "The potatoes are bananas";
}


bundle edit_line Insert(name)
{
  insert_lines:
      "Begin$(const.n) $(name)$(const.n)End";
}

body edit_defaults empty
{
      empty_file_before_editing => "true";
}

body classes trigger(x)
{
      promise_repaired => { $(x) };
}