cookbook 'poise', '= 2.7.1'
poise
(25) Versions
2.7.1
-
Follow15
Helpers for writing extensible Chef cookbooks.
cookbook 'poise', '= 2.7.1', :supermarket
knife supermarket install poise
knife supermarket download poise
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
.
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.
Dependent cookbooks
This cookbook has no specified dependencies.
Contingent cookbooks
Changelog
v2.7.1
- Minor tweak for compatability with Chef master.
v2.7.0
- More compatibility improvements for Chef 12.9.
- 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 likeverify "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 foroptions
in template content properties to match thetemplate
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 normalshell_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 viasuper
.
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 thelazy { }
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.
- 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.7.1 passed this metric
2.7.1 passed this metric