Adoptable Cookbooks List

Looking for a cookbook to adopt? You can now see a list of cookbooks available for adoption!
List of Adoptable Cookbooks

Supermarket Belongs to the Community

Supermarket belongs to the community. While Chef has the responsibility to keep it running and be stewards of its functionality, what it does and how it works is driven by the community. The chef/supermarket repository will continue to be where development of the Supermarket application takes place. Come be part of shaping the direction of Supermarket by opening issues and pull requests or by joining us on the Chef Mailing List.

Select Badges

Select Supported Platforms

Select Status

RSS

poise (25) Versions 2.2.0

Helpers for writing extensible Chef cookbooks.

Policyfile
Berkshelf
Knife
cookbook 'poise', '= 2.2.0', :supermarket
cookbook 'poise', '= 2.2.0'
knife supermarket install poise
knife supermarket download poise
README
Dependencies
Changelog
Quality 0%

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
providers 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.

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-2015, 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.

Dependent cookbooks

This cookbook has no specified dependencies.

Contingent cookbooks

acd Applicable Versions
apache_tomcat Applicable Versions
application Applicable Versions
application_git Applicable Versions
application_javascript Applicable Versions
application_python Applicable Versions
application_ruby Applicable Versions
automatic_updates Applicable Versions
berkshelf-api Applicable Versions
ceph-chef Applicable Versions
cerner_tomcat Applicable Versions
chef-updater Applicable Versions
chef_server_omnibus Applicable Versions
clickhouse Applicable Versions
cloudformation-magic Applicable Versions
cloudformation-test-cookbook Applicable Versions
collectd Applicable Versions
collectdwin Applicable Versions
confd Applicable Versions
consul Applicable Versions
consul-replicate Applicable Versions
consul_template Applicable Versions
cyclesafe_chef Applicable Versions
elasticsearch Applicable Versions
eldus-s3 Applicable Versions
eldus-tomcat Applicable Versions
firefox_package Applicable Versions
firewall Applicable Versions
gitlab_omnibus Applicable Versions
grub Applicable Versions
hashicorp-vault Applicable Versions
hosts_allow Applicable Versions
java-service Applicable Versions
jmeter Applicable Versions
kafka-cluster Applicable Versions
kibana_lwrp Applicable Versions
libartifact Applicable Versions
lockrun Applicable Versions
mod_security2 Applicable Versions
monit Applicable Versions
netrc Applicable Versions
nrpe-ng Applicable Versions
ohai_public_info Applicable Versions
openbazaar Applicable Versions
opentsdb Applicable Versions
orchestrator Applicable Versions
pkcs12 Applicable Versions
poise-archive Applicable Versions
poise-build-essential Applicable Versions
poise-file Applicable Versions
poise-git Applicable Versions
poise-github Applicable Versions
poise-javascript Applicable Versions
poise-languages Applicable Versions
poise-monit Applicable Versions
poise-python Applicable Versions
poise-ruby Applicable Versions
poise-ruby-build Applicable Versions
poise-service Applicable Versions
poise-service-aix Applicable Versions
poise-service-monit Applicable Versions
poise-service-runit Applicable Versions
poise-service-solaris Applicable Versions
proxysql Applicable Versions
radiator Applicable Versions
rc Applicable Versions
resource_from_hash Applicable Versions
ros Applicable Versions
rubyzip Applicable Versions
s3-cookbook Applicable Versions
search_helpers Applicable Versions
securetty Applicable Versions
snap Applicable Versions
sonarqube_server Applicable Versions
ssh_keygen Applicable Versions
sysconfig Applicable Versions
vitess Applicable Versions
xmledit Applicable Versions
zookeeper-cluster Applicable Versions

Changelog

v2.2.0

  • Compatibility with Chef 12.4.1 and Chefspec 4.3.0.
  • New helper ResourceCloning: Disables resource cloning between Poise-based resources. This is enabled by default.
  • Subresource parent references can be set to nil.

v2.1.0

  • Compatibility with Chef 12.4.
  • Add #property as an alias for #attribute in resources. This provides forward compatibility with future versions of Chef.
  • Freeze default resource attribute values. This may break your code, however this is not a major release because any code broken by this change was itself already a bug.

v2.0.1

  • Make the ChefspecHelpers helper a no-op if chefspec is not already loaded.
  • Fix for finding the correct cookbook for a file when using vendored gems.
  • New flag for the OptionCollector helper, parser:
class Resource < Chef::Resource
  include Poise
  attribute(:options, option_collector: true, parser: proc {|val| parse(val) })

  def parse(val)
    {name: val}
  end
end
  • Fix for a possible infinite loop when using ResourceProviderMixin in a nested module structure.

v2.0.0

Major overhaul! Poise is now a Halite gem/cookbook. New helpers:

  • ChefspecMatchers – Automatically create Chefspec matchers for Poise resources.
  • DefinedIn – Track which file (and cookbook) a resource or provider is defined in.
  • Fused – Experimental support for defining provider actions in the resource class.
  • Inversion – Support for end-user dependency inversion with providers.

All helpers are compatible with Chef >= 12.0. Chef 11 is now deprecated, if you
need to support Chef 11 please continue to use Poise 1.

v1.0.12

  • Correctly propagate errors from inside notifying_block.

v1.0.10

  • Fixes an issue with the LWRPPolyfill helper and false values.

v1.0.8

  • Delayed notifications from nested converges will still only run at the end of the main converge.

v1.0.6

  • The include_recipe helper now works correctly when used at compile time.

v1.0.4

  • Redeclaring a template attribute with the same name as a parent class will inherit its options.

v1.0.2

  • New template attribute pattern.
attribute(:config, template: true)

...

resource 'name' do
  config_source 'template.erb'
end

...

new_resource.config_content

v1.0.0

  • Initial release!

Foodcritic Metric
            

2.2.0 failed this metric

FC031: Cookbook without metadata file: /tmp/cook/f3401d932645669260eb6b0e/poise/metadata.rb:1
FC045: Consider setting cookbook name in metadata: /tmp/cook/f3401d932645669260eb6b0e/poise/metadata.rb:1