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:
- Less typing
- Consistency
- Smaller file sizes
- And more…
Exploring the cookbook
First, let’s create a new cookbook.
Change to the directory inside your new cookbook, and list the files
The folder we need doesn’t exist yet, so let’s create it:
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:
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
Then put in your recipe the following line attached to a code block:
Like this: