5

I'm working on a chef cookbook to set up a vanilla ubuntu 12.04.2 with nginx postgresql and unicorn on my vagrant box, but I'm getting into troubles with nginx, there's an exception

undefined method `[]' for nil:NilClass

when installing it that seem to relate to ohai configuration.

I've posted all I have here:

https://github.com/DonGiulio/mychef

here's the output from chef-solo:

Starting Chef Client, version 11.4.0
Compiling Cookbooks...

================================================================================
Recipe Compile Error in /var/chef/cookbooks/nginx/recipes/source.rb
================================================================================


NoMethodError
-------------
undefined method `[]' for nil:NilClass


Cookbook Trace:
---------------
  /var/chef/cookbooks/nginx/recipes/ohai_plugin.rb:27:in `from_file'
  /var/chef/cookbooks/nginx/recipes/source.rb:42:in `from_file'


Relevant File Content:
----------------------
/var/chef/cookbooks/nginx/recipes/ohai_plugin.rb:

 20:  #
 21:  
 22:  ohai 'reload_nginx' do
 23:    plugin 'nginx'
 24:    action :nothing
 25:  end
 26:  
 27>> template "#{node['ohai']['plugin_path']}/nginx.rb" do
 28:    source 'plugins/nginx.rb.erb'
 29:    owner  'root'
 30:    group  'root'
 31:    mode   '0755'
 32:    notifies :reload, 'ohai[reload_nginx]', :immediately
 33:  end
 34:  
 35:  include_recipe 'ohai::default'
 36:  


[2013-11-19T14:22:23+00:00] ERROR: Running exception handlers
[2013-11-19T14:22:23+00:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-11-19T14:22:23+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2013-11-19T14:22:23+00:00] FATAL: NoMethodError: undefined method `[]' for nil:NilClass

and here's the stacktrace:

$ cat /var/chef/cache/chef-stacktrace.out
Generated at 2013-11-19 14:22:23 +0000
NoMethodError: undefined method `[]' for nil:NilClass
/var/chef/cookbooks/nginx/recipes/ohai_plugin.rb:27:in `from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/mixin/from_file.rb:30:in `instance_eval'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/mixin/from_file.rb:30:in `from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/cookbook_version.rb:346:in `load_recipe'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context.rb:151:in `load_recipe'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context.rb:132:in `block in include_recipe'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context.rb:131:in `each'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context.rb:131:in `include_recipe'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/dsl/include_recipe.rb:26:in `include_recipe'
/var/chef/cookbooks/nginx/recipes/source.rb:42:in `from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/mixin/from_file.rb:30:in `instance_eval'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/mixin/from_file.rb:30:in `from_file'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/cookbook_version.rb:346:in `load_recipe'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context.rb:151:in `load_recipe'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context/cookbook_compiler.rb:139:in `block in compile_recipes'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context/cookbook_compiler.rb:137:in `each'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context/cookbook_compiler.rb:137:in `compile_recipes'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context/cookbook_compiler.rb:74:in `compile'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/run_context.rb:86:in `load'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/client.rb:224:in `setup_run_context'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/client.rb:467:in `do_run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/client.rb:200:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/application.rb:190:in `run_chef_client'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/application/solo.rb:239:in `block in run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/application/solo.rb:231:in `loop'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/application/solo.rb:231:in `run_application'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/lib/chef/application.rb:73:in `run'
/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.4.0/bin/chef-solo:25:in `<top (required)>'
/usr/bin/chef-solo:23:in `load'
/usr/bin/chef-solo:23:in `<main>'

UPDATE 1 I'm provisioning the box with ruby-2.0.0-p247, is there a dependency on an earlier version of ruby maybe?

UPDATE 2 I've tried with ruby-1.9.3-p448, on a freshly provisioned box, same problem, no change.

Could you please help me?

thanks,

Don Giulio
  • 2,946
  • 3
  • 43
  • 82
  • It's a bit weird.. From what I can see the nginx cookbook depends on the ohai cookbook (defined in metadata.rb), which should load ohai's attributes. Can you just try to explicitly include the ohai cookbook explicitly in the run list before nginx? – StephenKing Nov 19 '13 at 15:48
  • I added `depends 'ohai'` before `depends 'nginx'` in my `metadata.rb` and this gives me exactly the same problem. – Don Giulio Nov 19 '13 at 15:53
  • That doesn't matter, as nginx already depends on ohai. But I meant explicitly adding it to the run lust. Still.. it's not clear to me, why this happens. Could you post/pastebin/gist more verbose log output (`chef-client -l debug`)? You're the most recent versions of all cookbooks? – StephenKing Nov 19 '13 at 19:29

4 Answers4

5

You haven't stated which version of the ohai and nginx cookbooks you're using.

While I can't answer your question I can provide some advice on managing your cookbooks by using a dependency manager (think bundler for cookbooks).

Install Berkshelf

gem install berkshelf

Create a Berkshelf file

$ cat Berksfile

site :opscode

cookbook "nginx"

And use this to download all the cookbooks associated with nginx:

$ berks install -p cookbooks
Using nginx (2.0.0)
Using apt (2.3.0)
Using bluepill (2.3.0)
Using rsyslog (1.9.0)
Using build-essential (1.4.2)
Using ohai (1.1.12)
Using runit (1.2.0)
Using yum (2.3.2)

The "-p" option places them in the "cookbooks" subdirectory:

$ ls -d cookbooks/*
cookbooks/apt       
cookbooks/build-essential  
cookbooks/ohai     
cookbooks/runit
cookbooks/bluepill  
cookbooks/nginx            
cookbooks/rsyslog  
cookbooks/yum
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • thanks, I cloned all the cookbooks directly from the git repository I'll try with berkshelf – Don Giulio Nov 19 '13 at 21:46
  • brilliant, berkshelf is what I needed, thanks a lot it's solved now. – Don Giulio Nov 19 '13 at 22:36
  • would you have a link to some proper documentation for berkshelf? I don't understand what's the structure of the template cookbook generated by berkshelf. and other things. – Don Giulio Nov 20 '13 at 11:44
  • 1
    http://misheska.com/blog/2013/06/16/getting-started-writing-chef-cookbooks-the-berkshelf-way/ found this. – Don Giulio Nov 20 '13 at 13:25
  • I had the same error, but found that I had to delete 'ohai' from my Berksfile - nginx was already calling it as a dependency. – JayCrossler Jan 26 '14 at 04:18
  • I was using vagrant plugin *vagrant-berkshelf* and got the same error, but when I did `gem install berkshelf` then I got an error `can't find gem berkshelf` – svassr Aug 26 '16 at 19:03
  • This is because Vagrant Berkshelf could not find the 'berks' executable in my PATH anymore. This is installed by Chefdk so I had to reinstall Chefdk – svassr Aug 26 '16 at 19:18
0

I have experienced the same error. You may find 3 other solutions to this problem, and an explanation why it's happening here on stackoverflow.

To mention one for those using Vagrant: Just include the recipe through Vagrantfile "chef.add_recipe()".

Using Berkshelf is obviously solving the issue, and I honestly like it. Just think that we should save the time of those who doesn't want to bother with installing new tools.

Community
  • 1
  • 1
Konzulic
  • 1,743
  • 14
  • 22
0

Ran into this trying to use community nexus cookbook. Solved it by specifying latest version of nginx cookbook in my metadata.rb: depends 'nginx', '>= 2.7.6'

The nexus cookbook metadata.rb specifies: depends "nginx", ">= 1.8.0"

So seems that chef will choose 1.8.0 when resolving deps unless you specify a newer version.

loafdog
  • 101
  • 7
-1

looks like ruby libraries were missing, I installed ruby by running following command on Ubuntu and it started working for me

apt-get install ruby
TECHISH
  • 1
  • 2