Skip to end of metadata
Go to start of metadata

Overview

This page clarifies the syntax for cookbook versions and describes version constraints, which can be used in cookbook metadata and in environments feature to limit which versions of a cookbook should be used.

This departs from the currently documented, but not used, version constraint features found here.

Cookbook Version Syntax

A cookbook version has the form x.y.z, where x, y and z are decimal numbers representing major, minor and patch versions, respectively. A two-part version (e.g. x.y) is also allowed.

Note that alpha-numeric version numbers, such as 3.4.rc4, and version numbers with more than three parts (e.g. 1.2.3.4) are not allowed.

Version Constraints

A version constraint is a string of the form "OP VERSION", where OP is one of the operators listed below and VERSION is a cookbook version as described above.

Operators

  • = Equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • ~> Approximately greater than

Specifying ">= 2.6.5" is an optimistic version constraint. All versions greater than the one specified, including major releases (e.g. 3.0.0) are allowed.

Conversely, specifying "~> 2.6" is pessimistic about future major revisions and "~> 2.6.5" is pessimistic about future minor revisions.

"~> 2.6" matches cookbooks >= 2.6.0 AND < 3.0.0
"~> 2.6.5" matches cookbooks >= 2.6.5 AND < 2.7.0



Requires Chef 0.10
The Version Constraints functionality is only available in Chef 0.10.x and after.


Cookbook Metadata

The following DSL functions can be used in a cookbook's metadata.rb file. Each function accepts a name and an optional version constraint. If the version constraint is not provided, the default constraint of ">= 0.0.0" will be used.

Example version constraints in a cookbook/metadata.rb file:

Specifying a version constraint for these functions is optional. If no version constraint is provided, the default version constraint of ">= 0.0.0" will be used.

Environment Cookbook Versions

An environment can specify a list of allowed cookbook versions using version constraints. When defining a constraint, you must specify the cookbook name and the version constraint. Any cookbook not explicitly constrained in the environment file is assumed to have no version constrain and can be used at any version by every node in that environment.

Cookbook Versions in Run List Items

You can peg the cookbook version to use for recipe items in a node's run list. The syntax uses '@' to specify a version. Only the equality constraint is supported for specifying constraints in run lists.

Example run list JSON

Freezing Cookbooks

You can also freeze your cookbooks, to prevent anyone from uploading the frozen cookbook at the same version number. Once a cookbook is frozen, you can only upload a cookbook with the same name and version by using the --force option:

By combining frozen cookbook versions with environments you can make sure that production is safe from accidental updates when testing out changes in your development infrastructure.

There are two main strategies for implementing version control:

  • If you place supreme importance on always keeping every last bit of data in version control, you’ll want to use the maximum version control strategy and drive your use of environments by editing files only.
  • If you’re already managing your cookbooks for separate environments using git branches, and you already have your versioning policy information stored in your cookbooks’ metadata, you might opt for a lighter weight branch tracking strategy.

Below are examples of both of these strategies.

Branch Tracking Strategy

In the branch tracking strategy, you have a branch in your source control repo for each environment, and your cookbook versioning policy tracks whatever is in the tip of each branch. This strategy is simple and lightweight. For development environments that track the latest cookbooks, you just need to bump the version before you upload the cookbook for testing. For environments that need special protection, i.e., production, you always upload cookbooks using the -E ENVRIONMENT and --freeze flags.

In development:

1. Bump the version number as appropriate.
2. Make the changes needed to the cookbook.
3. Upload and test. Upload for dev is the same as always:

Repeat steps 2 and 3 as necessary.

When you’re ready to move your changes into production:

1. Upload for prod with automatic version constraints:

Adding the -E ENVIRONMENT option will cause knife to automatically set the version constraint on that environment to match the version you’re uploading.

Maximum Version Control Strategy

If you prefer to version control absolutely everything, then you need to use a file-editing based approach to environments. In your development environment, you work the same as the branch tracking strategy.

In development:

1. Bump the version number as appropriate.
2. Make the changes needed to the cookbook.
3. Upload and test. Upload for dev is the same as always:

Repeat 2 and 3 as necessary.

When you’re ready to move your cookbooks into production, you’ll do the following:

1. Upload and freeze your cookbooks:

2. Modify your environment to prefer the new version you uploaded:

3. Upload the updated environment:

4. Deploy!







Templates


Cookbook Site Help



Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.