2
votes

I am new to chef and started writing a simple recipe using the Users library. My Berksfile:

name@name:~/chef-repo$ cat Berksfile
site :opscode

cookbook "users"

and then I ran:

berks install

and my cookbooks:

name@name:~/chef-repo$ ls cookbooks/
users

finally my recipe:

name@name:~/chef-repo$ cat blah-users.rb 
include_recipe "users"

users_manage "blah-dev" do
  group_id 1001
  action [ :remove, :create ]
end

when I appply I get an error:

name@name:~/chef-repo$ chef-apply blah-users.rb 
[2015-05-04T09:05:48-04:00] FATAL: Stacktrace dumped to /home/name/.chef/cache/chef-stacktrace.out
[2015-05-04T09:05:48-04:00] FATAL: NoMethodError: undefined method `unreachable_cookbook?' for nil:NilClass

I am fairly certain that the include_recipe "users" is the problem (at least the first). I also find this line Stacktrace dumped to /home/name/.chef/cache/chef-stacktrace.out interesting as there isn't a file at that location.

I have tried with and without sudo. So I don't think it is a permissions issue.

I am running a local install of chef server 12.3 which looks to be functioning fine...

Thanks


~/chef-repo$ sudo chef-apply blah-users.rb -l debug

[2015-05-04T13:17:50-04:00] DEBUG: Building node object for tbrown
[2015-05-04T13:17:50-04:00] DEBUG: Extracting run list from JSON attributes provided on command line
[2015-05-04T13:17:50-04:00] DEBUG: Applying attributes from json file
[2015-05-04T13:17:50-04:00] DEBUG: Platform is ubuntu version 14.04
[2015-05-04T13:17:50-04:00] INFO: Run List is []
[2015-05-04T13:17:50-04:00] INFO: Run List expands to []
[2015-05-04T13:17:50-04:00] DEBUG: Loading Recipe users via include_recipe
[2015-05-04T13:17:50-04:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2015-05-04T13:17:50-04:00] DEBUG: NoMethodError: undefined method `unreachable_cookbook?' for nil:NilClass
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:257:in `unreachable_cookbook?'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:155:in `load_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:142:in `block in include_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:141:in `each'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:141:in `include_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/dsl/include_recipe.rb:26:in `include_recipe'
blah-users.rb:1:in `run_chef_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:168:in `instance_eval'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:168:in `run_chef_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:180:in `run_application'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:193:in `run'
/opt/chefdk/embedded/apps/chef/bin/chef-apply:25:in `<top (required)>'
/usr/bin/chef-apply:51:in `load'
/usr/bin/chef-apply:51:in `<main>'
[2015-05-04T13:17:50-04:00] FATAL: NoMethodError: undefined method `unreachable_cookbook?' for nil:NilClass
2
May you try with chef-apply blah-users.rb -l debug. At all I'm unsure chef-apply could be used with dependency (i.e require recipe from another cookbook) as it is intended to run small portion of internal resources only and there's no cookbook loading (and so no read of metadata.rb as there's none). Best workaround I can think of is to use chef-solo or chef-zero (See docs.chef.io for exented informations) with a cookbook having the correct dependency in it's metadata.rb file.Tensibai
the metadata.rb needed a "depends" on the "users" cookbookakaphenom
You may want to add that as an answer (and accept it) so that if someone stumbles upon this later, they can find it easily.Josh Edwards
I will - tensibai alluded to the answer in the comment. I want to give an opportunity for that answer to be formalized and if it is I will accept it, otherwsie I will add it myself.akaphenom
If you can wait untill tomorow I'll write a correct answer, I'm at home on phone and it's horrible to link to documentation :)Tensibai

2 Answers

1
votes

After comments:

When using include_recipe somewhere, the cookbook hosting this recipe has to be loaded, there's two way to get it loaded:

  1. Have it in the runlist
  2. Use depends "cookbook", "version limitation" in the metadata.rb file of the cookbook including the other (Documentation)

Here using chef-apply we run a specific recipe and not a runlist so the 1 does not apply and the 2 can't apply, there's no cookbook to load, only a recipe. Chef-apply aimed to use the internal chef resources and not LWRP.

The way to use multiples cookbooks is to create a cookbook and have a depend directive in its metadata.rb file.

After that you can run this cookbook with chef-solo or chef-zero specifying a runlist on command line with '-r or -o, this will allow to load all the needed cookbooks and makes the LWRP from depended cookbooks available.

More extended documentation about chef-client modes (solo and zero are just shortcuts) is available here

0
votes

I have found the source of this problem. Chef apply does not instantiate the cookbook_compiler variable in the run context object via the load method (https://github.com/chef/chef/blob/master/lib/chef/run_context.rb#L157). Therefore the compiler object on this line https://github.com/chef/chef/blob/master/lib/chef/run_context.rb#L469 is nil. It seems to me that the compiler will need to initialized.

Have opened a ticket here: https://github.com/chef/chef/issues/3914