Creating your own Cookbook - nshenry03/chef-repo GitHub Wiki

On Debian based systems, we want to update the apt cache so that we can be sure that we are always installing the latest software packages. Similarly, on RHEL systems, we want to install the EPEL repository so that we have access to extra packages that we can install.

We want to do this on all of our systems, so we want it in our 'base' role; however, roles do not have any logic (to determine if this is a RHEL based system or a Debian based system) so we will need to create a wrapper cookbook called 'base-logic'.

First, let's create a cookbook called 'base-logic' using knife (which automatically generates files and folders based on your knife configuration files):

knife cookbook create 'base-logic' --cookbook-path ../cookbooks

In metadata.rb, edit the 'description' to provide a short description about the cookbook.

Edit README.md (See https://raw.github.com/nshenry03/base-logic/master/README.md for an example)

Create the default recipe recipes/default.rb:

#
# Cookbook Name:: base-logic
# Recipe:: default
#
# Copyright 2013, Standing Cloud
#
# 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.
#
case node["platform_family"]
when "debian"
  include_recipe "apt"
when "rhel", "fedora"
  include_recipe "yum::epel"
end

Okay, this should be ready to test... Simply add this near the top of Cheffile (change to match your environment)

# Variable that starts the path of the current directory
current_dir = File.dirname(__FILE__)
cookbook_dir = "#{current_dir}/../cookbooks"

Then simply add this at the bottom:

cookbook 'base-logic',
   :path => "#{cookbook_dir}/base-logic"

You should then be able to run librarian-chef install to download the cookbook and knife cookbook upload 'base-logic' to upload the cookbook to the chef server.

Then, just add the recipe, recipe[base-logic] to your base role: roles/base.rb.

Finally you should be able to test this cookbook by starting up a server (vagrant up <<VM NAME>>) and bootstrapping it:

knife bootstrap localhost --ssh-user vagrant --ssh-password vagrant --ssh-port 2222 --run-list 'role[base]' --sudo --environment development --secret-file .chef/encrypted_data_bag_secret

If all goes well, you can modify metadata.rb to change the 'version' to 1.0.0 and also update CHANGELOG.md to explain that 1.0.0 is the first stable release.

Go ahead and commit all of that to subversion and then create a tag:

git tag "1.0.0" -m "Tagging 1.0.0"
git push origin 1.0.0

Change the source in the Cheffile to point to your repo:

cookbook 'base-logic',
#   :path => "#{cookbook_dir}/base-logic"
   :git => 'https://github.com/nshenry03/base-logic',
   :ref => '1.0.0'

You should then be able to run librarian-chef install to download the cookbook and knife cookbook upload 'base-logic' to upload the cookbook to the chef server.

You can also let all of your environments know about the new cookbook and version:

for env in $(ls environments/*.rb); do
  echo 'cookbook "base-logic", "1.0.0"' >> ${env}
  knife environment from file ${env}
done
⚠️ **GitHub.com Fallback** ⚠️