运行chef-client </class时遇到“未初始化的常量#<class:0x8d78f18> :: Chef :: Resource :: CookbookFile”:0x8d78f18>

时间:2014-02-17 08:32:16

标签: ruby chef cookbook

我正在使用Chef 11.10.0。

我实现了一个名为MyCookbookFile的新资源,它继承了Chef :: Resource :: CookbookFile。

而且,我此刻不会更改提供者(因此将使用Chef :: Provider :: CookbookFile)。

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

我遇到了以下错误:

================================================================================
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)

我试着在我的食谱中打印并检查LOAD_PATH。它包括:

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

图书馆应该到位:

[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

2 个答案:

答案 0 :(得分:2)

您应该在 Chef :: Resource :: CookbookFile 之前添加 ::

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

my_cookbook_file.rb 移动到cookbook的 libraries 文件夹,因为你使用纯Ruby而不是LWRP语法。

答案 1 :(得分:0)

变化:

class MyCookbookFile < Chef::Resource::CookbookFile

为:

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