Thursday, 30 May 2013

Creating Directories With Chef

Creating directories using the automation tool Chef is fairly easy to do, unfortunately when creating a directory the correct permissions are only applied to the last directory. E.g. If you created /tmp/foo/bar only bar would have the correct permissions. This to me seems to be a bug and has been logged with Chef, but at the time of writing this; the version I am using which is 10 still has the bug. The bug is logged here:-

https://tickets.opscode.com/browse/CHEF-1327

This could be worked around in multiple ways, a possible solution is to create a method and call this for each level of the directory. For example:-
 def make_dir(dir_path)  
  directory dir_path do  
   owner username  
   group usergroup  
   mode "0755"  
   recursive true  
   action :create  
  end  
 end  

This saves us from calling the chef directory resource for each level of the directory. This could be called like so:-
 make_dir("/tmp/foo")  
 make_dir("/tmp/foo/bar")  

Hopefully this will be fixed in future versions but having the directory resource within a ruby method similar to the above will save lines of code