Ruby Library for Chef

One of the things that we can do to eliminate waste in code is to use libraries or functions so that we are not writing the same code over and over. This practice has many benefits, including:

  1. Less typing
  2. Consistency
  3. Smaller file sizes
  4. And more…

Exploring the cookbook

First, let’s create a new cookbook.

$ chef generate cookbook test

Change to the directory inside your new cookbook, and list the files

$ ls
> Berksfile  CHANGELOG.md  chefignore  LICENSE  metadata.rb  README.md  recipes  spec  test

The folder we need doesn’t exist yet, so let’s create it:

$ mkdir libraries

Create a Library

For this post, we’ll create a very simple library that will check the mtime (last time the file was modified, or touched)

Create a new file called mtime.rb in the libraries directory and add this to it:

def file_age(name)
  (Time.now - File.mtime(name))/(24*3600)
end

This code simply accepts a file name, then calculates it’s age in days.

Use Your Library

Let’s say we want Pulp to sync a repo every seven days… Create an attribute for the cadence by putting the following into attributes/default.rb

default['pulp-mirror']['sync']['cadence']       = 7       #In Days

Then put in your recipe the following line attached to a code block:

  action :sync if file_age("#{Chef::Config['file_cache_path']}/#{bag['id']}.sync") > node['pulp-mirror']['sync']['cadence']

Like this:

pulp_rpm_repo bag['id'] do
  display_name bag['display_name']
  description bag['description']
  feed bag['feed']
  http bag['serve_http']
  https bag['serve_https']
  pulp_cert_verify false
  relative_url bag['relative_url']
  action :sync if file_age("#{Chef::Config['file_cache_path']}/#{bag['id']}.sync") > node['pulp-mirror']['sync']['cadence']
end