0
votes

I am trying to write my first chef custom resource. However I am unable to access properties in some other embedded resources.

I have created the following resource

property :template_shortname, String

action :setup do
  node.set['apache']['version'] = '2.4'
  node.set['apache']['package'] = 'apache2'

  include_recipe "apache2::default"

  print "the shortname is " + template_shortname

  web_app "sample_app" do
    server_name "server.com"
    server_aliases []
    docroot "/www-data"
    template "prefix " + template_shortname
  end
end

However when executing this I get:

From debug log

The shortname is: server

But the parameter for template_shortname contains an empty array. So it quits with:

       TypeError
       ---------
       no implicit conversion of Array into String

       Cookbook Trace:
       ---------------
       /tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:21:in `+'
       /tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:21:in `block (2 levels) in class_from_file'
       /tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:17:in `block in class_from_file'

I am using chef-dk Chef Development Kit Version: 1.1.16 chef-client version: 12.17.44 delivery version: master (83358fb62c0f711c70ad5a81030a6cae4017f103) berks version: 5.2.0 kitchen version: 1.14.2

2
template "prefix #{template_shortname}" use string interpolation, not addition - Tensibai
And probably new_resource.template_shortname from the doc. All in all, setting attributes like that and calling include_recipe withing an action sounds brittle and likely to bite you soon. Thos should be in your phpapp cookbook attributes file and recipe, all in all I don't get what you'r trying to do here. - Tensibai
Sry I should have mentioned new_resource is empty as well. As I understand this blog post blog.backslasher.net/chef-custom-resources.html it is no longer needed. If I am reading the chef doc correctly it should only be required in case of name clashes. - crasu
Indeed, but I'd argue it's better to be consistent. all in all your problem comes from the fact the templateproperty of web_app is an array, so the + operator act as for an array and not for string. Use string interpolation and it should work, I still think attributes and include_recipe should absolutely not be defined in a custom resource (makes no sense to try to install apache twice for two apps). - Tensibai

2 Answers

1
votes

The issue is that web_app is not a resource itself, it's a definition. So the magic scope stuff doesn't work there. Overall it's recommended to use new_resource.whatever anyway because the fused-mode scope magic is very footgun-prone.

0
votes

Turns out that the poise resource is doing something similar:

https://github.com/poise/application_php/blob/master/providers/mod_php_apache2.rb

This works:

  new_resource = @new_resource

  web_app "sample_app" do
    server_name "server.com"
    server_aliases []
    docroot "/www-data"
    template "prefix " + new_resource.template_shortname
  end

I would still be delighted to know why this works ....