Although it is a very bad idea to directly run shell commands with Cfengine 3 (see here why), there might be a few situations where this is still the best attempt, e.g. when performing custom maintenance work which really can not be done by Cfengine 3. Writing the Cfengine 3 code body common control { […]

Author:

Although it is a very bad idea to directly run shell commands with Cfengine 3 (see here why), there might be a few situations where this is still the best attempt, e.g. when performing custom maintenance work which really can not be done by Cfengine 3.

Writing the Cfengine 3 code

body common control {
        version         => "1.0";
        inputs          => { "cfengine_stdlib.cf" };
        bundlesequence  => { "run_shell_command" };
}

bundle agent run_shell_command {
  commands:
        "/etc/init.d/mysql start"
        comment => "Doing it this way is a really bad idea!";
}

Save the file (I used the file name “example6.cf”) and make sure that it is located in the right directory (e.g. /var/lib/cfengine3/inputs or /etc/cfengine3).
Let’s see if the syntax is correct:

cf-promises -f /var/lib/cfengine/inputs/example6.cf

No output or error message? Great, let’s run it then:

cf-agent -f /var/lib/cfengine3/inputs/example6.cf
Q: "...it.d/mysql star": Rather than invoking init scripts through /etc/init.d, use the service(8)
Q: "...it.d/mysql star": utility, e.g. service mysql start
Q: "...it.d/mysql star": Since the script you are attempting to invoke has been converted to an
Q: "...it.d/mysql star": Upstart job, you may also use the start(8) utility, e.g. start mysql
Q: "...it.d/mysql star": mysql start/running, process 4521
I: Last 5 quoted lines were generated by promiser "/etc/init.d/mysql start"

As you can see, the cf-agent is able to run our simple shell command and also shows you its output. As stated above, it is a really bad idea to start services in this way. Cfengine 3 is able to do that for you, but we just needed a simple example here.

Analyzing the Cfengine 3 code snippet
We jump directly to the interesting part:

bundle agent run_shell_command {
  commands:
        "/etc/init.d/mysql start"
        comment => "Doing it this way is a really bad idea!";
}

Inside our bundle agent “run_shell_command”, a promise of the type “command” is created. This promise simply contains the command to be performed and a small comment. That’s it!

As usual, you can download todays’s Cfengine 3 code snippet here.

Comments on this entry (3 comments)

Did you like this post? You can share your opinion with us! Simply click here.

Nice writeup. And your code listing is beautiful… How did you do that, please?

Reply

Hey Aleksey,
nice reading from you. I read your blog from time to time and stumbled across your Cfengine 3 videos.

I use WordPress as a blog system and “Qoate Simple Code Snippets” for the syntax highlighting. Hope that helps!

Reply

Add Your Comment

Powered by sweet Captcha


two − = 1