Tuesday, April 8, 2014

puppet create directory recursively

The whole VERY OLD rant is here  http://projects.puppetlabs.com/issues/86
Why on earth in 8 years the Puppet Gods have kept ignoring this very basic and common need, they only know.

In a nutshell, I have a variable
$mypath='/opt/oracle/fmw11_1_1_6'
file { "$mypath":
   ensure => directory,
   owner => 'soa'
}

This will of course fail if /opt and /opt/oracle have not been already defined.

Of course since $mypath is a configuration value there is  no way I can hardcode all the parents directories.

Hence I am screwed, and Puppet doesn't offer me any way around.

I can think of only one elegant way, that is writing a Ruby function (I am not good in Puppet stinky DSL) "pathExpand" to transform a full path into an array of his components:

assert [ '/opt', '/opt/oracle', '/opt/oracle/fmw11_1_1_6' ] = pathExpand('/opt/oracle/fmw11_1_1_6')

and a define "conditionalDirectory($mypath ):

if (! defined($mypath)) {
  file { "$mypath":
    ensure => directory,
  }
}



All this sucks, but not as much as Puppet DSL anyway.


This is a Puppet solution (hack) to the problem:

define common::mkdirp(
  $owner     = 'root',
  $group     = 'root',
  $exec_path = '/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin') {

  exec { "create ${name}":
    command     => "mkdir -p ${name}",
    creates     => $name,
    path        => $exec_path,
  }

  common::ownership { $name:
    user        => $owner,
    group       => $group,
    exec_path   => $exec_path,
    require     => Exec["create ${name}"]
  }

}

define common::ownership(
  $user,
  $group     = undef,
  $dir       = $name,
  $exec_path = '/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin'
) {

  $real_group = $group ? {
    undef   => $user,
    default => $group
  }

  exec { "full '${dir}' directory ownership for ${user}:${real_group}":
    path    => $exec_path,
    command => "chown -R ${user}:${real_group} ${dir}",
    unless  => "test `find ${dir} -user ${user} -group ${real_group} | wc -l` -eq `find ${dir} | wc -l`",
  }

}


Saturday, April 5, 2014

Book: Puppet Types and Providers

http://shop.oreilly.com/product/0636920026860.do



This is a rather specialist book, but worth reading for all those who use Puppet in a less-than-basic way.

It shows the structure of a Puppet type (resource) with simple and detailed example, so you get to understand all the possibilities of the language and what is going on under the hood.




Friday, April 4, 2014

Object Oriented Puppet

One of the most shocking aspects of working with Puppet is that it goes to a great length at providing a High Level Interface to Resources, but it provides literally no facility to abstract access to configuration values.

All is a hiera() and Hash/Array manipulation, from which to painfully extract subset of data with totally garbled and complex manipulations of the Hashes, Keys and Values, interspersed with some create_resources() to be able to cycle over a collection of homogeneous resources.

People who come from ANY Object Oriented, functional and fluent-interface programming languages, like Groovy or Java 8,  can only scream in horror in front of the total inelegance, un-refactorability, un-maintainability and lack of encapsulation of the Puppet DSL.

To this, add that YAML is not generally associated to a schema - hence you can't validate that you haven't broken your model somewhere in your hundred of YAML files - on per each host - and you have the mother of all Spaghetti Western Code.

For all these reasons, please Puppet Gods, heed my call, dump that pathetic DSL and adopt some OO language.