The Joy of Hex

Drunken Monkey Coding Style with a hint of Carlin humor

Jan 8, 2013 - 2 minute read - Automation Linux Puppet Vagrant

Targeting MongoDB driver version with Puppet

As mentioned here for our current project we are using Vagrant and Puppet. In the link above I described how I solved the pear packages installation under Puppet.

We are using Symfony 2.0.15 with MongoDB, I need to use version 1.2.9 to avoid the whole setSlaveOK hubbub as described here.

The original puppet task we used looked like:

1
2
3
4
5
# install mongodb driver
exec { "/usr/bin/pecl install -f mongo-1.2.9":
    unless  => "/usr/bin/pecl info mongo",
    require => Exec["pear update-channels"]
}

Which would be great apart from one little detail, Puppet Pear setup executes the

1
pear upgrade

command which updates all the pear packages and updates the MongoDB driver to the latest version (1.3.9 at the time of writing), which then breaks the project and

1
sudo pecl install -f mongo-1.2.9

would have to be executed manually to bring order to chaos.

Luckily with a bit of shell scripting we can solve that by executing

1
test `pecl info mongo | grep 'Release Version' | tr -s '[:blank:]' | cut -d ' ' -f3` != '1.2.9'

which will return 0 if the condition is true (namely Release Version number is not 1.2.9).

By adding the one-liner above to the onlyif parameter of the exec task we can ensure that if the MongoDB driver version is not 1.2.9 the task will be executed.

The task now looks like this, note the use of full path:

1
2
3
4
5
# install mongodb driver
exec { "/usr/bin/pecl install -f mongo-1.2.9":
    require => Exec["pear update-channels"],
    onlyif => "/usr/bin/test `pecl info mongo | grep 'Release Version' | tr -s '[:blank:]' | cut -d ' ' -f3` != '1.2.9'"
}