Giter VIP home page Giter VIP logo

poise-derived's Introduction

Poise

Build Status Gem Version Cookbook Version Coverage Gemnasium License

What is Poise?

The poise cookbook is a set of libraries for writing reusable cookbooks. It provides helpers for common patterns and a standard structure to make it easier to create flexible cookbooks.

Writing your first resource

Rather than LWRPs, Poise promotes the idea of using normal, or "heavy weight" resources, while including helpers to reduce much of boilerplate needed for this. Each resource goes in its own file under libraries/ named to match the resource, which is in turn based on the class name. This means that the file libraries/my_app.rb would contain Chef::Resource::MyApp which maps to the resource my_app.

An example of a simple shell to start from:

require 'poise'
require 'chef/resource'
require 'chef/provider'

module MyApp
  class Resource < Chef::Resource
    include Poise
    provides(:my_app)
    actions(:enable)

    attribute(:path, kind_of: String)
    # Other attribute definitions.
  end

  class Provider < Chef::Provider
    include Poise
    provides(:my_app)

    def action_enable
      notifying_block do
        ... # Normal Chef recipe code goes here
      end
    end
  end
end

Starting from the top, first we require the libraries we will be using. Then we create a module to hold our resource and provider. If your cookbook declares multiple resources and/or providers, you might want additional nesting here. Then we declare the resource class, which inherits from Chef::Resource. This is similar to the resources/ file in an LWRP, and a similar DSL can be used. We then include the Poise mixin to load our helpers, and then call provides(:my_app) to tell Chef this class will implement the my_app resource. Then we use the familiar DSL, though with a few additions we'll cover later.

Then we declare the provider class, again similar to the providers/ file in an LWRP. We include the Poise mixin again to get access to all the helpers and call provides() to tell Chef what provider this is. Rather than use the action :enable do ... end DSL from LWRPs, we just define the action method directly. The implementation of action comes from a block of recipe code wrapped with notifying_block to capture changes in much the same way as use_inline_resources, see below for more information about all the features of notifying_block.

We can then use this resource like any other Chef resource:

my_app 'one' do
  path '/tmp'
end

Helpers

While not exposed as a specific method, Poise will automatically set the resource_name based on the class name.

Notifying Block

As mentioned above, notifying_block is similar to use_inline_resources in LWRPs. Any Chef resource created inside the block will be converged in a sub-context and if any have updated it will trigger notifications on the current resource. Unlike use_inline_resources, resources inside the sub-context can still see resources outside of it, with lookups propagating up sub-contexts until a match is found. Also any delayed notifications are scheduled to run at the end of the main converge cycle, instead of the end of this inner converge.

This can be used to write action methods using the normal Chef recipe DSL, while still offering more flexibility through subclassing and other forms of code reuse.

Include Recipe

In keeping with notifying_block to implement action methods using the Chef DSL, Poise adds an include_recipe helper to match the method of the same name in recipes. This will load and converge the requested recipe.

Resource DSL

To make writing resource classes easier, Poise exposes a DSL similar to LWRPs for defining actions and attributes. Both actions and default_action are just like in LWRPs, though default_action is rarely needed as the first action becomes the default. attribute is also available just like in LWRPs, but with some enhancements noted below.

One notable difference over the standard DSL method is that Poise attributes can take a block argument.

Template Content

A common pattern with resources is to allow passing either a template filename or raw file content to be used in a configuration file. Poise exposes a new attribute flag to help with this behavior:

attribute(:name, template: true)

This creates four methods on the class, name_source, name_cookbook, name_content, and name_options. If the name is set to '', no prefix is applied to the function names. The content method can be set directly, but if not set and source is set, then it will render the template and return it as a string. Default values can also be set for any of these:

attribute(:name, template: true, default_source: 'app.cfg.erb',
          default_options: {host: 'localhost'})

As an example, you can replace this:

if new_resource.source
  template new_resource.path do
    source new_resource.source
    owner 'app'
    group 'app'
    variables new_resource.options
  end
else
  file new_resource.path do
    content new_resource.content
    owner 'app'
    group 'app'
  end
end

with simply:

file new_resource.path do
  content new_resource.content
  owner 'app'
  group 'app'
end

As the content method returns the rendered template as a string, this can also be useful within other templates to build from partials.

Lazy Initializers

One issue with Poise-style resources is that when the class definition is executed, Chef hasn't loaded very far so things like the node object are not yet available. This means setting defaults based on node attributes does not work directly:

attribute(:path, default: node['myapp']['path'])
...
NameError: undefined local variable or method 'node'

To work around this, Poise extends the idea of lazy initializers from Chef recipes to work with resource definitions as well:

attribute(:path, default: lazy { node['myapp']['path'] })

These initializers are run in the context of the resource object, allowing complex default logic to be moved to a method if desired:

attribute(:path, default: lazy { my_default_path })

def my_default_path
  ...
end

Option Collector

Another common pattern with resources is to need a set of key/value pairs for configuration data or options. This can done with a simple Hash, but an option collector attribute can offer a nicer syntax:

attribute(:mydata, option_collector: true)
...

my_app 'name' do
  mydata do
    key1 'value1'
    key2 'value2'
  end
end

This will be converted to {key1: 'value1', key2: 'value2'}. You can also pass a Hash to an option collector attribute just as you would with a normal attribute.

Debugging Poise

Poise has its own extra-verbose level of debug logging that can be enabled in three different ways. You can either set the environment variable $POISE_DEBUG, set a node attribute node['POISE_DEBUG'], or touch the file /POISE_DEBUG. You will see a log message Extra verbose logging enabled at the start of the run to confirm Poise debugging has been enabled. Make sure you also set Chef's log level to debug, usually via -l debug on the command line.

Upgrading from Poise 1.x

The biggest change when upgrading from Poise 1.0 is that the mixin is no longer loaded automatically. You must add require 'poise' to your code is you want to load it, as you would with normal Ruby code outside of Chef. It is also highly recommended to add provides(:name) calls to your resources and providers, this will be required in Chef 13 and will display a deprecation warning if you do not. This also means you can move your code out of the Chef module namespace and instead declare it in your own namespace. An example of this is shown above.

Sponsors

The Poise test server infrastructure is generously sponsored by Rackspace. Thanks Rackspace!

License

Copyright 2013-2016, Noah Kantrowitz

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

poise-derived's People

Contributors

coderanger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

poise-derived's Issues

Lazy evaluation not working inside hash or array

attributes/default.rb

node.default['a']['b'] = 'aaaa'
node.default['a']['c'] = lazy [{
  'a' => '%{a.b}/test',
  'b' => 'sasdasd'
}

recipes/default.rb

log 'message' do
  message node['a']['c'].to_s
  level :info
end

ERROR Message:

Recipe Compile Error in /tmp/kitchen/cache/cookbooks/.../recipes/default.rb

   NoMethodError
   -------------
   undefined method `scan' for #<Array:0x000000018e19e8>

   Cookbook Trace:
   ---------------
     /tmp/kitchen/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/lazy_attribute.rb:116:in `_evaluate'
     /tmp/kitchen/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/lazy_attribute.rb:77:in `to_s'
     /tmp/kitchen/cache/cookbooks/.../recipes/default.rb:18:in `block in from_file'
     /tmp/kitchen/cache/cookbooks/.../recipes/default.rb:17:in `from_file'
     /tmp/kitchen/cache/cookbooks/.../recipes/default.rb:8:in `from_file'

   Relevant File Content:
   ----------------------
   /tmp/kitchen/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/lazy_attribute.rb:

   109:      def _evaluate
   110:        return @evaluate_cache if defined?(@evaluate_cache)
   111:        @evaluate_cache = if @value.is_a?(Proc)
   112:          # Block mode, just run the block.
   113:          @node.instance_eval(&@value).to_s
   114:        else
   115:          # String mode, parse the template string and fill it in.
   116>>         format_keys = @value.scan(FORMAT_STRING_REGEXP).inject({}) do |memo, (key)|
   117:            # Keys must be symbols because Ruby.
   118:            memo[key.to_sym] = key.split(/\./).inject(@node) {|n, k| n.nil? ? n : n[k] }
   119:            memo
   120:          end
   121:          @value % format_keys
   122:        end
   123:      end
   124:    end
   125:  end

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

chef-shell in client mode does not work

Background

We are using poise-derived cookbook for lazy attributes. It works fine in our ecosystem.

Problem

The problem was when we had to use the chef-shell command in a client mode to make some debugging.

It does not work and it raises the following error:

 $ chef-shell -z -l debug

[...]

[2017-01-02T15:13:45+00:00] DEBUG: [poise-derived] Installing event handler
[2017-01-02T15:13:45+00:00] DEBUG: Filtered backtrace of compile error: /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:41:in `<class:Handler>',/var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:28:in `<module:PoiseDerived>',/var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:23:in `<top (required)>',/var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/cheftie.rb:18:in `<top (required)>',/var/chef/cache/cookbooks/poise-derived/libraries/default.rb:19:in `<top (required)>'
[2017-01-02T15:13:45+00:00] DEBUG: Backtrace entry for compile error: '/var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:41:in `<class:Handler>''
[2017-01-02T15:13:45+00:00] DEBUG: Line number of compile error: '41'

================================================================================
Recipe Compile Error in /var/chef/cache/cookbooks/poise-derived/libraries/default.rb
================================================================================

NoMethodError
-------------
undefined method `events' for nil:NilClass

Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:41:in `<class:Handler>'
  /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:28:in `<module:PoiseDerived>'
  /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:23:in `<top (required)>'
  /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/cheftie.rb:18:in `<top (required)>'
  /var/chef/cache/cookbooks/poise-derived/libraries/default.rb:19:in `<top (required)>'

Relevant File Content:
----------------------
/var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:

 34:
 35:      def attribute_load_complete
 36:        PoiseDerived::DSL.uninstall
 37:      end
 38:
 39:      # Install event handler.
 40:      Chef::Log.debug('[poise-derived] Installing event handler')
 41>>     Chef.run_context.events.register(self.instance)
 42:    end
 43:  end
 44:

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

epic fail!

/var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:41:in `<class:Handler>': undefined method `events' for nil:NilClass (NoMethodError)
	from /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:28:in `<module:PoiseDerived>'
	from /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/handler.rb:23:in `<top (required)>'
	from /opt/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /opt/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /var/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/cheftie.rb:18:in `<top (required)>'
	from /opt/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /opt/chef/embedded/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /var/chef/cache/cookbooks/poise-derived/libraries/default.rb:19:in `<top (required)>'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:193:in `load'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:193:in `block in load_libraries_from_cookbook'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:189:in `each'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:189:in `load_libraries_from_cookbook'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:99:in `block in compile_libraries'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:98:in `each'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:98:in `compile_libraries'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context/cookbook_compiler.rb:71:in `compile'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/run_context.rb:187:in `load'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/shell/shell_session.rb:208:in `rebuild_context'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/shell/shell_session.rb:59:in `block in reset!'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/shell/shell_session.rb:101:in `loading'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/shell/shell_session.rb:54:in `reset!'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/shell.rb:127:in `session'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/shell.rb:136:in `init'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/lib/chef/shell.rb:65:in `start'
	from /opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.17.44/bin/chef-shell:34:in `<top (required)>'
	from /usr/bin/chef-shell:57:in `load'
	from /usr/bin/chef-shell:57:in `<main>'

It looks like a bug in the handler definition. Chef.run_context is nil in that mode while in the normal chef-client it is a Chef::RunContext object.

Software versions

Chef: 12.17.44

NoMethodError on "prepend", possibly a Windows-specific bug.

================================================================================
Recipe Compile Error in C:/chef/cache/cookbooks/poise-derived/libraries/default.rb
================================================================================

NoMethodError
-------------
private method `prepend' called for Chef::Mixin::DeepMerge:Module

chef-stacktrace.out shows:

Generated at 2016-10-10 17:07:13 -0400
NoMethodError: private method `prepend' called for Chef::Mixin::DeepMerge:Module
C:/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/core_ext/deep_merge.rb:51:in `<module:DeepMerge>'
C:/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/core_ext/deep_merge.rb:32:in `<module:CoreExt>'
C:/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/core_ext/deep_merge.rb:23:in `<module:PoiseDerived>'
C:/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/core_ext/deep_merge.rb:22:in `<top (required)>'
C:/opscode/chef/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
C:/opscode/chef/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
C:/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/core_ext.rb:17:in `<top (required)>'
C:/opscode/chef/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
C:/opscode/chef/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
C:/chef/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/cheftie.rb:17:in `<top (required)>'
C:/opscode/chef/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
C:/opscode/chef/embedded/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
C:/chef/cache/cookbooks/poise-derived/libraries/default.rb:19:in `<top (required)>'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:191:in `load'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:191:in `block in load_libraries_from_cookbook'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:188:in `each'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:188:in `load_libraries_from_cookbook'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:99:in `block in compile_libraries'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:98:in `each'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:98:in `compile_libraries'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context/cookbook_compiler.rb:71:in `compile'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/run_context.rb:167:in `load'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/policy_builder/policyfile.rb:162:in `setup_run_context'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/client.rb:509:in `setup_run_context'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/client.rb:277:in `run'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/application.rb:252:in `run_with_graceful_exit_option'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/application.rb:228:in `block in run_chef_client'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/local_mode.rb:44:in `with_server_connectivity'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/application.rb:211:in `run_chef_client'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/application/client.rb:417:in `run_application'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/lib/chef/application.rb:58:in `run'
C:/opscode/chef/embedded/lib/ruby/gems/2.0.0/gems/chef-12.8.1-universal-mingw32/bin/chef-client:26:in `<top (required)>'
C:/opscode/chef/bin/chef-client:60:in `load'
C:/opscode/chef/bin/chef-client:60:in `<main>'

Alias lazy method to something else

I'm using chef 12.13.37, chefspec 4.7.0.

It's just an idea, today I've encountered strange issue, when I'm testing (using chefspec) cookbook which depends on cookbook where I'm using lazy in attributes, I'm getting ArgumentError exception. Looks like upstream lazy method takes precedence over poise-derived. I wasn't able to isolate it to separate code yet, but maybe it would be good to use different method name - derived?

     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/mixin/params_validate.rb:116:in `lazy'
     # ./cookbooks/generic_server/attributes/openssl.rb:2:in `from_file'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/mixin/from_file.rb:30:in `instance_eval'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/mixin/from_file.rb:30:in `from_file'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/dsl/include_attribute.rb:39:in `block in include_attribute'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/dsl/include_attribute.rb:31:in `each'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/dsl/include_attribute.rb:31:in `include_attribute'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:181:in `load_attribute_file'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:174:in `block in load_attributes_from_cookbook'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:173:in `each'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:173:in `load_attributes_from_cookbook'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:110:in `block in compile_attributes'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:109:in `each'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:109:in `compile_attributes'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context/cookbook_compiler.rb:72:in `compile'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/run_context.rb:176:in `load'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/policy_builder/expand_node_object.rb:97:in `setup_run_context'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chef-12.13.37/lib/chef/client.rb:510:in `setup_run_context'
     # /home/szymon/.rvm/gems/ruby-2.2.5/gems/chefspec-4.7.0/lib/chefspec/solo_runner.rb:118:in `converge'

Chef 13 recipe compile error.

Using lazy attribute with Chef 13.2.20 causes recipe compilation errors, e.g.

================================================================================
       Recipe Compile Error in /tmp/kitchen/cache/cookbooks/radian_chef_automate/recipes/default.rb
       ================================================================================

       RuntimeError
       ------------
       can't modify frozen PoiseDerived::LazyAttribute

       Cookbook Trace:
       ---------------
         /tmp/kitchen/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/lazy_attribute.rb:111:in `_evaluate'
         /tmp/kitchen/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/lazy_attribute.rb:65:in `method_missing'
         /tmp/kitchen/cache/cookbooks/radian_chef_automate/recipes/default.rb:9:in `from_file'

       Relevant File Content:
       ----------------------
       /tmp/kitchen/cache/cookbooks/poise-derived/files/halite_gem/poise_derived/lazy_attribute.rb:

       104:      private
       105:
       106:      # Evaluate the lazy attribute.
       107:      #
       108:      # @return [Object]
       109:      def _evaluate
       110:        return @evaluate_cache if defined?(@evaluate_cache)
       111>>       @evaluate_cache = if @value.is_a?(Proc)
       112:          # Block mode, just run the block.
       113:          @node.instance_eval(&@value).to_s
       114:        else
       115:          # String mode, parse the template string and fill it in.
       116:          format_keys = @value.scan(FORMAT_STRING_REGEXP).inject({}) do |memo, (key)|
       117:            # Keys must be symbols because Ruby.
       118:            memo[key.to_sym] = key.split(/\./).inject(@node) {|n, k| n.nil? ? n : n[k] }
       119:            memo
       120:          end

Attribute code for repro:

base_pkg_url = lazy 'https://packages.chef.io/files/stable/automate/%{radian_chef_automate.version}'

case node['platform_family']
when 'rhel'
  if node['platform_version'] =~ /7\..*/
    default['radian_chef_automate']['pkg_url'] = lazy {
      "#{base_pkg_url}/el/7/automate-#{node['radian_chef_automate']['version']}-1.el7.x86_64.rpm"
    }
  elsif node['platform_version'] =~ /6\..*/
    default['radian_chef_automate']['pkg_url'] = lazy {
      "#{base_pkg_url}/el/6/automate-#{node['radian_chef_automate']['version']}-1.el6.x86_64.rpm"
    }
  else
  end
when 'amazon'
  default['radian_chef_automate']['pkg_url'] = lazy {
      "#{base_pkg_url}/el/6/automate-#{node['radian_chef_automate']['version']}-1.el6.x86_64.rpm"
  }
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.