Skip to content

Automating a chef-solo Installation on a CentOS 5 Instance

April 16, 2012

Chef is a tool used for configuration management of bare metal and virtual systems. Chef has a client/server model as well as a standalone tool that is very similar to the tools from Puppet Labs. (Want to automate a puppet agent installation? Check out my earlier blog post

When using the cloud and launching multiple instances with the same job, configuration management is a huge time saver. Configuration management systems will allow you to build the configuration once and use it to create exact replicas on as many instances as you need. Having the exact same configuration for every system doing the same job keeps errors and possible issues down to a minimum. Also, if your production systems are easily replicated, you can spin up smaller test environments to check your systems before pushing out the newer code to production (possibly even using Amazon for production and Eucalyptus internally for testing!).

For configuration of systems, chef uses recipes and cookbooks to give users pre-made system configurations. A recipe is a single task and cookbook is a group of tasks, usually pertaining to a single application (e.g. apache, nginx, etc). A collection of pre-made cookbooks can be downloaded from a GitHub repository run by OpsCode, the creators of Chef. These cookbooks will help you a perform a large number of installations and configurations from setting up a web server to installing applications such as vim.

Below is a script that will automate the installation of the chef-solo, a standalone version of the chef client, on a CentOS 5 system. You can run this script by hand or have it automatically run by passing to the metadata service when running an instance. This script was tested on the Eucalyptus Community Cloud using a CentOS 5 image (emi-709D1676)

#!/usr/bin/env bash

YUM=`which yum`
RPM=`which rpm`
CURL=`which curl`
HOSTNAME=`which hostname`
RUBY=""
GEM=""

SYSTEM_NAME="chef.mydomain.int"
SHORT_NAME=`echo ${SYSTEM_NAME} | cut -d'.' -f1`

TMP_DIR="/tmp/"
DEFAULT_DIR="/root/"

CHEF=""
CHEF_DIR="/var/chef-solo/"

#######
# Set the hostname of the system.
#######
hostname ${SYSTEM_NAME}
if [ -z `cat /etc/sysconfig/network | grep HOSTNAME` ]; then
    echo "HOSTNAME=${SYSTEM_NAME}" >> /etc/sysconfig/network
else
    sed -i -e "s/\(HOSTNAME=\).*/\1${SYSTEM_NAME}/" /etc/sysconfig/network
fi

sed -i -e "s/\(localhost.localdomain\)/${SYSTEM_NAME} ${SHORT_NAME} \1/" /etc/hosts

${YUM} -y update

#######
# Setup the required repos. EPEL, Aegisco, and rbel.
#######
${CURL} -o /etc/yum.repos.d/aegisco.repo http://rpm.aegisco.com/aegisco/el5/aegisco.repo

${RPM} -Uhv http://rbel.frameos.org/rbel5

${CURL} -o ${DEFAULT_DIR}/epel-release-5-4.noarch.rpm http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
${RPM} -Uhv ${DEFAULT_DIR}/epel-release-5-4.noarch.rpm 

########
# Install ruby and required tools for building the system
########
${YUM} install -y ruby-1.8.7.352 ruby-libs-1.8.7.352 ruby-devel.x86_64 ruby-ri ruby-rdoc ruby-shadow gcc gcc-c++ automake autoconf make curl dmidecode

RUBY=`which ruby`
########
# Setup RubyGems
########
curl -o ${TMP_DIR}/rubygems-1.8.10.tgz http://production.cf.rubygems.org/rubygems/rubygems-1.8.10.tgz 
tar xzvf ${TMP_DIR}/rubygems-1.8.10.tgz -C ${TMP_DIR}
${RUBY} ${TMP_DIR}/rubygems-1.8.10/setup.rb --no-format-executable

GEM=`which gem`
########
# Setup the chef ruby gem
########
${GEM} install chef --no-ri --no-rdoc

CHEF=`which chef-solo`
########
# Setup the basic configuration files needed
########
cat >>${DEFAULT_DIR}/solo.rb <>${DEFAULT_DIR}/node.json <<EOF
{
    "run_list": [ "recipe[apache2]" ]
}
EOF

########
# Setup up cookbooks directory for chef solo
########
mkdir -p ${CHEF_DIR}/cookbooks

########
# Download and untar the cookbooks provided by OpsCode on GitHub
########
${CURL} -o ${DEFAULT_DIR}/cookbooks.tgz https://nodeload.github.com/opscode/cookbooks/tarball/master
tar xzvf ${DEFAULT_DIR}/cookbooks.tgz -C ${DEFAULT_DIR}

########
# Add the apache2 cookbook to the chef solo cookbooks directory
########
cp -R ${DEFAULT_DIR}/opscode-cookbooks-*/apache2 ${CHEF_DIR}/cookbooks

########
# Run the node.rb JSON file to install apache2
########
${CHEF} -c ${DEFAULT_DIR}/solo.rb -j ${DEFAULT_DIR}/node.json 

This script will need a couple changes towards the end to be used as with chef-client and communicate with a chef server. Hopefully in a future post this will be discussed.

This script uses the chef-solo version of the chef-client that does not require a chef server. This makes testing out chef much easier. Utilizing the GitHub cookbooks repository, we can install anything available in the cookbooks along with the customized configuration that we might need. Please check out the chef-solo wiki for more details on how to extend the script above to do more for you.

With the addition of Amazon S3 or Eucalyptus Walrus or GitHub this script can be used to work with customized cookbooks to improve your infrastructure even more.

Not really interested in CentOS 5? Looking for the same for CentOS 6 or Debian? Check out my GitHub repo containing my blog scripts for two additional scripts in the chef_solo_install folder. (Note: The majority of the OpsCode cookbooks are built for Debian (probably more specifically Ubuntu) based systems so some won’t work with CentOS 6 out of the box. There are also some issues with CentOS 5 with cookbooks such as the nginx one.)

About these ads
2 Comments
  1. hspencer77 permalink

    Reblogged this on More Mind Spew-age from Harold Spencer Jr. and commented:
    More automation goodness…this time with Chef!

Trackbacks & Pingbacks

  1. Automating a chef-solo Installation on a CentOS 5 Instance … | Linux Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 53 other followers

%d bloggers like this: