Skip to content

Instantly share code, notes, and snippets.

@abitdodgy
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abitdodgy/9956177 to your computer and use it in GitHub Desktop.
Save abitdodgy/9956177 to your computer and use it in GitHub Desktop.
Creating a chef recipe.

Creating a New Chef Kitchen

A kitchen is a collection of recipes associated with a deployment. For example, a kitchen can contain cookbooks for installing Ruby, MySQL, and nginx.

knife init solo kitchen_name

This will create the necessary stucture for the kitchen.

TODO write this section.

Creating a New Cookbook

Create a folder to hold your recipes.

mkdir chef-cookbooks
cd chef-cookbooks

Create a new cookbook with the following command.

knife cookbook create [cookbook_name] -o path_to_location

Ommitting the -o flag raises a Permission denied error. This issue may be on my development environment only.

** Creating cookbook my_cookbook
ERROR: Errno::EACCES: Permission denied - /var/chef

We must supply a path using the -o flag to avoid this error.

knife cookbook create brilliant_cookbook -o ~/documents/code/chef-cookbooks

This will create a cookbook directory brilliant_cookbook inside ~/documents/code/chef-cookbooks. This directory has the default cookbook structure ready. It includes a README.md file that contains useful annotations.

Folder Structure

TODO: Expound on this.

+ brilliant_cookbook
  + attributes
  + recipes
    - default.rb
  + templates
    + default
  - CHANGELOG.md
  - metadata.rb
  - README.md

The recipes Folder

The recipes folder contains your recipe files. You will notice the presence of a default.rb recipe file. All chef cookbooks must have a default recipe. Chef will install this recipe if the cookbook is specified in a role or a node definition without specifying a particular recipe.

Wait... what are role and node definitions? More on that later. But for now suffice it to say you can specify particular recipes for chef to install. And if you don't, Chef will install the default recipe inside default.rb.

A Chef cookbook can have many recipies. You may want to write a recipe for a debian linux distro, for example; and this recipe may require different packages or commands. In this case you would create a debian.rb file. If you were writing a recipe for a particular version of debian, you would specify the version in the recipe file name: debian-7.4.rb. You can then specify those recipes inside node and role definitions. If you do not, Chef will install the default recipe contained in default.rb.

Nodes

TODO. Write a section about nodes.

Roles

TODO. Write a section about roles.

The first step is to create a folder for your recipes.

mkdir chef
cd chef

Then you should add a Gemfile with the following contents.

source 'https://rubygems.org'

gem 'berkshelf', '~> 2.0.14'
gem 'chef', '11.10.0'
gem 'chef-zero', '1.7.2'
gem 'knife-solo', '0.3.0'

Run bundle install.

bundle install

Initialize a new repo through knife in the chef directory.

knife solo init my_cookbook

This will create the cookbook structure and setup Berkshelf (or librarian, depending on which you have installed. If you both are installed it will default to Berkshelf).

Next edit your Berksfile and add the recipies you need, then install them.

cd my_cookbook
bundle exec berkshelf install

Next, setup a user for your deployment. Create a users directory inside data_bags and add a deploy.json file to it.

cd data_bags
mkdir users

The contents of deploy.json should look something like this.

{
  "id": "deploy",
  "password": "",
  "ssh_keys": [
  ],
  "groups": [ "sysadmin"],
  "shell": "\/bin\/bash"
}

The value of id should match the name of the file. You can generate a password using openssl.

openssl passwd -1 'sometextstringhere'

Copy the hash and use it as a password.

Grab your ssh_key and add it to the array this file.

cat ~/.ssh/id_rsa.pub

Start a new server on your VPS and copy your SSH key.

ssh-copy-id root@server-ip-or-host

Finally, install chef, and the cookbooks on remote.

bundle exec knife solo prepare root@host
bundle exec knife solo cook root@host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment