Adoptable Cookbooks List

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.

Poise

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.

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:

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:

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:

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.

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.

Changelog

v2.7.0

New helper: Poise::Helpers::Win32User to automatically convert 'root'
defaults for user and group properties to more platform-appropriate values.

Enhanced poise_shell_out to better cope with Windows command parsing. Use
Bash-style commands and it will automatically convert.

Overall compatibility fixes for Windows.

v2.6.1

Compatibility with Chef master to fix issues with defined_in! not ignoring
stack frames from Chef code.

Setting a provider in a inversion options resource now works as (probably)
expected.

v2.6.0

New backwards-compatibility helper: Poise::Backports::VERIFY_PATH. Use it
like verify "myapp -t #{Poise::Backports::VERIFY_PATH}" if defined?(verify)
for backwards-compatible usage of file verifications.

Fixed Poise's implementation of lazy defaults to more closely match Chef's
even when both are used in conjunction. Lazy defaults will no longer be
evaluated when setting a value or getting an existing non-default value.

v2.5.0

New property for inversion resources: provider_no_auto. Set one or more
provider names that will be ignored for automatic resolution for that instance.

Support variables as an alias for options in template content properties
to match the template resource.

Template content properties are no longer validated after creation for
non-default actions.

Formalize the extra-verbose logging mode for Poise and expose it via helpers.

Extra-verbose logging mode can now be enabled by creating a /poise_debug file.

New helper: poise_shell_out. Like normal shell_out but sets group and
environment variables automatically to better defaults.

v2.4.0

Added return value to Container#register_subresource to track if the resource
was already added.

Improve inspect output for subresources and containers.

Ensure notifications work with subresources.

Inversion providers process name equivalences.

v2.3.2

Improve handling of deeply nested subresources.

v2.3.1

Ensure a container with a parent link to its own type doesn't use self as the
default parent.

Improve handling of load_current_resource in providers that call it via
super.

v2.3.0

New helper: ResourceSubclass, a helper for subclassing a resource while
still using the providers as the base class.

New feature: Non-default containers. Use container_default: false to mark
a container class as ineligible for default lookup.

New feature: parent attribute defaults. You can set a parent_default to
provide a default value for the parent of a resource. This supports the
lazy { } helper as with normal default values.

New feature: use forced_keys: [:name] on an option collector property to
force keys that would otherwise be clobbered by resource methods.

Can enable verbose logging mode via a node attribute in addition to an
environment variable.

v2.2.3

Add ancestor_send utility method for use in other helpers.

Improve subresource support for use in mixins.

v2.2.2

Fix 2.2.1 for older versions of Chef.

v2.2.1

Fixed delayed notifications inside notifying_block.

Default actions as expected within LWRPs.

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.