0

I am using Chef 11.10.0.

I implement a new resource called MyCookbookFile which inherits Chef::Resource::CookbookFile.

And, I am not going to change the provider this moment (So Chef::Provider::CookbookFile will be used).

require 'chef/resource/cookbook_file'
require 'chef/mixin/securable'

class Chef
  class Resource
    class MyCookbookFile < Chef::Resource::CookbookFile
      include Chef::Mixin::Securable

      provides :my_cookbook_file, :on_platforms => :all

      # more codes here...

    end
  end
end

I met the following error:

================================================================================
Recipe Compile Error in /var/chef/cache/cookbooks/my-cookbook/resources/my_cookbook_file.rb
================================================================================


NameError
---------
uninitialized constant #<Class:0x8d78f18>::Chef::Resource::CookbookFile


Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/my-cookbook/resources/my_cookbook_file.rb:6:in `<class:Resource>'
  /var/chef/cache/cookbooks/my-cookbook/resources/my_cookbook_file.rb:5:in `<class:Chef>'
  /var/chef/cache/cookbooks/my-cookbook/resources/my_cookbook_file.rb:4:in `class_from_file'


Relevant File Content:
----------------------
/var/chef/cache/cookbooks/my-cookbook/resources/my_cookbook_file.rb:

  1:  require 'chef/resource/cookbook_file'
  2:  require 'chef/mixin/securable'
  3:
  4:  class Chef
  5:    class Resource
  6>>     class MyCookbookFile < Chef::Resource::CookbookFile


Running handlers:
[2014-02-17T08:16:26+00:00] ERROR: Running exception handlers
Running handlers complete

[2014-02-17T08:16:26+00:00] ERROR: Exception handlers complete
[2014-02-17T08:16:26+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 1.189243344 seconds
[2014-02-17T08:16:26+00:00] ERROR: uninitialized constant #<Class:0x8d78f18>::Chef::Resource::CookbookFile
[2014-02-17T08:16:26+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

I tried to print and check the LOAD_PATH in my cookbook. It includes:

/opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.10.0/bin/../lib

And the library should be in place:

[root@localhost /]$ ll /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.10.0/lib/chef/resource/cookbook_file.rb 
-rw-r--r-- 1 root root 1598 Feb  6 17:22 /opt/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.10.0/lib/chef/resource/cookbook_file.rb
Simon Ho
  • 35
  • 9

2 Answers2

2

You should add :: before Chef::Resource::CookbookFile.

 class MyCookbookFile < ::Chef::Resource::CookbookFile

And move your my_cookbook_file.rb to libraries folder of the cookbook, since you use pure Ruby and not LWRP syntax.

Draco Ater
  • 20,820
  • 8
  • 62
  • 86
  • Okay. I put inside the *libraries* folder. But when I use the resource in *ANOTHER* cookbook, it gives the following error: `NoMethodError ------------- No resource or method named `my_cookbook_my_cookbook' for `Chef::Recipe "test-recipe"'` My recipe is defined like this: ` my_cookbook_my_cookbook_file "/tmp/my_template" do source "my_template.erb" mode 0644 end` – Simon Ho Feb 18 '14 at 01:43
  • You must state `depends 'my-cookbook'` in your _other_ cookbook's _metadata.rb_. And it should be just `my_cookbook_file`. – Draco Ater Feb 18 '14 at 08:39
0

Change:

class MyCookbookFile < Chef::Resource::CookbookFile

to:

class MyCookbookFile < ::Chef::Resource::CookbookFile
Agis
  • 32,639
  • 3
  • 73
  • 81