在声明包资源时提供名称 - Chef

时间:2016-09-13 02:45:06

标签: ruby-on-rails ruby chef chef-recipe cookbook

我有以下Chef食谱(web.rb):

# Install Apache and start the service.
httpd_service 'customers' do
  mpm 'prefork'
  action [:create, :start]
end

# Add the site configuration.
httpd_config 'customers' do
  instance 'customers'
  source 'customers.conf.erb'
  notifies :restart, 'httpd_service[customers]'
end

# Create the document root directory.
directory node['awesome_customers_ubuntu']['document_root'] do
  recursive true
end

# Write the home page.
file "#{node['awesome_customers_ubuntu']['document_root']}/index.html" do
  content '<html>This is a placeholder</html>'
  mode '0644'
  owner node['awesome_customers_ubuntu']['user']
  group node['awesome_customers_ubuntu']['group']
end

我的default.rb食谱如下:

include_recipe 'apt::default'
include_recipe 'awesome_customers_ubuntu::firewall'
include_recipe 'awesome_customers_ubuntu::web_user'
include_recipe 'awesome_customers_ubuntu::web'

attributes/default.rb

default['firewall']['allow_ssh'] = true
default['awesome_customers_ubuntu']['open_ports'] = 80

default['awesome_customers_ubuntu']['user'] = 'web_admin'
default['awesome_customers_ubuntu']['group'] = 'web_admin'
default['awesome_customers_ubuntu']['document_root'] = '/var/www/customers/public_html'

当我执行kitchen converge时,我收到以下错误:

Recipe: awesome_customers_ubuntu::web
         * httpd_service_debian_sysvinit[customers] action create[2016-09-13T02:40:18+00:00] WARN: Default value nil is invalid for property version of resource . Possible fixes: 1. Remove 'default: nil' if nil means 'undefined'. 2. Set a valid default value if there is a reasonable one. 3. Allow nil as a valid value of your property (for example, 'property :version, [ String, nil ], default: nil'). Error: Property version must be one of: String!  You passed nil. at /tmp/kitchen/cache/cookbooks/httpd/libraries/helpers.rb:101:in `default_package_name'
       [2016-09-13T02:40:18+00:00] WARN: Default value nil is invalid for property package_name of resource . Possible fixes: 1. Remove 'default: nil' if nil means 'undefined'. 2. Set a valid default value if there is a reasonable one. 3. Allow nil as a valid value of your property (for example, 'property :package_name, [ String, nil ], default: nil'). Error: Property package_name must be one of: String!  You passed nil. at /tmp/kitchen/cache/cookbooks/httpd/libraries/httpd_service_debian.rb:8:in `block in <class:HttpdServiceDebian>'


           ================================================================================
           Error executing action `create` on resource 'httpd_service_debian_sysvinit[customers]'
           ================================================================================

           ArgumentError
           -------------
           You must supply a name when declaring a package resource

           Cookbook Trace:
           ---------------
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/copied_from_chef/chef/dsl/declare_resource.rb:302:in `build_resource'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/copied_from_chef/chef/dsl/declare_resource.rb:259:in `declare_resource'
           /tmp/kitchen/cache/cookbooks/httpd/libraries/httpd_service_debian.rb:8:in `block in <class:HttpdServiceDebian>'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/copied_from_chef/chef/provider.rb:132:in `instance_eval'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/copied_from_chef/chef/provider.rb:132:in `compile_and_converge_action'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/runner.rb:78:in `run_action'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/runner.rb:106:in `block (2 levels) in converge'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/runner.rb:106:in `each'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/runner.rb:106:in `block in converge'
           /tmp/kitchen/cache/cookbooks/compat_resource/files/lib/chef_compat/monkeypatches/chef/runner.rb:105:in `converge'

           Resource Declaration:
           ---------------------
           # In /tmp/kitchen/cache/cookbooks/awesome_customers_ubuntu/recipes/web.rb

             8: httpd_service "customers" do
             9:     mpm "prefork"
            10:     action [:create, :start]
            11: end
            12: 

           Compiled Resource:
           ------------------
           # Declared in /tmp/kitchen/cache/cookbooks/awesome_customers_ubuntu/recipes/web.rb:8:in `from_file'

           httpd_service_debian_sysvinit("customers") do
             action [:create, :start]
             retries 0
             retry_delay 2
             default_guard_interpreter :default
             declared_type :httpd_service
             cookbook_name "awesome_customers_ubuntu"
             recipe_name "web"
             mpm "prefork"
           end

           Platform:
           ---------
           x86_64-linux

       Recipe: firewall::default
         * firewall[default] action restart
            (skipped due to only_if)
          (skipped due to only_if)
        (skipped due to only_if)
       * file[/etc/default/ufw-chef.rules] action create (up to date)
        (up to date)

       Running handlers:
       [2016-09-13T02:40:18+00:00] ERROR: Running exception handlers
       Running handlers complete
       [2016-09-13T02:40:18+00:00] ERROR: Exception handlers complete
       Chef Client failed. 3 resources updated in 06 seconds
       [2016-09-13T02:40:18+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
       [2016-09-13T02:40:18+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
       [2016-09-13T02:40:18+00:00] ERROR: httpd_service_debian_sysvinit[customers] (awesome_customers_ubuntu::web line 8) had an error: ArgumentError: You must supply a name when declaring a package resource
       [2016-09-13T02:40:19+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

似乎在https://github.com/chef-cookbooks/httpd/blob/master/libraries/httpd_service_debian.rb#L8,package_name为nil。

跟踪此内容会导致https://github.com/chef-cookbooks/httpd/blob/master/libraries/httpd_service.rb#L23依赖于从https://github.com/chef-cookbooks/httpd/blob/master/libraries/info_service_packages.rb#L37延迟加载包名称

您可以通过明确声明您的资源来解决此问题:

httpd_service 'customers' do 
  mpm 'prefork'
  package_name 'apache2'
  action [:create, :start]
end

虽然,你真的不需要!也许提出错误?