Evan Conkle

About Evan Conkle

Evan is a Senior Enterprise Application Developer in the computer gaming industry. From application scaling to Hadoop, he masters complex data problems. He is also the founder of HadoopLogic.com.

Intro to Chef

Chef is an incredible tool, but despite its beginnings in 2008/2009 it still lacks an effective quick start, or even an official “hello world” – so it takes too long to really get started as you desperately search for tutorials/examples/use cases. The existing quick starts or tutorials take too long and fail to explain the scope or what Chef is doing. This is really unfortunate because the world could be a better place if more people used Chef (or even Puppet) and we didn’t have to guess how to configure a server for various applications.

Official Chef Explanation

Officially, Chef is a “configuration management tool” where you write “recipes”. It’s written in Ruby and you’ll need at least version 1.8.6. Here’s the official Chef Quick Start guide but I think it fails at succinctly presenting the scope of Chef or even how to use it, hence this document.

Simpler Chef Explanation

Put simpler, Chef is a Ruby DSL (domain specific language) for configuring GNU/Linux (or BSD) machines (Windows is not well supported), it has 2 flavors, “Chef Server” and “Chef Solo”, in this document I’m talking about Chef SOLO because it’s easier to get started with – and works well as a complement to Rails apps.

Simplest Chef Explanation and actual working example

Put simpler yet: Chef is a ruby script that uses “recipes” (a recipe is a Ruby file that uses the Chef DSL) to install software and run scripts on GNU/Linux servers. You can run Chef over and over again safely because most recipes know not to, for example, reinstall something that already exists (sometimes you have to code this functionality of not installing something that already exists, but most of the DSLs do it already).

Think of Chef as having 4 components:
install a binary/executable (chef-solo), installable via Ruby Gems

sudo /usr/bin/gem install chef ohai --no-rdoc --no-ri
/var/lib/gems/1.8/bin/chef-solo

create one or more ruby files that they call “recipes” in a structure like this

~/my_cookbooks/RECIPE_NAME/recipes/default.rb

Vim install recipe example
if we want a recipe for installing vim, here’s one quick and simple way to do it:

~/my_cookbooks/vim/recipes/default.rb
package('vim')

Just duplicate the directory structure I have listed above, and in the default.rb file, you only need 1 line. that “package” method knows which package management software to use depending on what OS is running and then leverages it.

Create MySQL DB recipe example

There are a lot of methods available for recipes. Take “bash” for example. Pass the “bash” method a block, and inside the block you can use methods like “code” (which executes a string of bash commands) and “user” which specifies which OS user to run the commands as.

~/my_cookbooks/create_mysql_db/recipes/default.rb

bash “really awesome way to create a mysql database from chef using the bash method” do

# dont if the db already exists

not_if('/usr/bin/mysql -uroot -pmiller_highlife_lol_jk -e'show databases' | grep #{node[:create_mysql_db][:db_name]}', :user => 'evan')

  # run as the evan user
  user 'evan'

  # a heredoc of the code to execute, note the node hash is created from the JSON file
  code <<-HEY_BRO_EOM
  mysql -uroot -ppmiller_highlife_lol_jk -e 'create database #{node[:create_mysql_db][:db_name]}'
  HEY_BRO_EOM

end

JSON file with array of recipes that you’ll point the binary at

~/my_cookbooks/roles/ottobib.json
{
  'name': 'ottobib',
  'run_list': [
    'create_mysql_db',
    'vim',
  ],
</strong>  'create_mysql_db': {
    'db_name': 'ottobib_production'
  }
}

Finally, a ruby file with more configuration options

~/my_cookbooks/chefsoloconfig.rb
file_cache_path  '/tmp/chef-solo'
cookbook_path    '/home/evan/my_cookbooks'
log_level        :info
log_location     STDOUT
ssl_verify_mode  :verify_none

NOW you can run it over and over again and your system will end up with Vim and a ottobib_production database. If you want to get CRAZY: add a recipe that checks out the latest copy of your application source code and then setup a cron job to execute your chef script every minute!

Here’s what your /home/evan/my_cookbooks dir should look like:

|-chefsoloconfig.rb
|-roles
  -ottobib.json
|-vim
  |-recipes
    -default.rb
|-create_mysql_db
  |-recipes
    -default.rb

THE ACTUAL COMMAND TO RUN CHEF!

 sudo /var/lib/gems/1.8/bin/chef-solo -c /home/evan/my_cookbooks/chefsoloconfig.rb -j /home/evan/my_cookbooks/roles/ottobib.json -ldebug

Reference: Intro to Chef from our JCG partner Evan Conkle at the Evan Conkle’s blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


+ two = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close