Category Archives: Uncategorized

LittleChef: playing with Chef recipes

For small number of servers and push deployment, I show how to run LittleChef here:

Using RVM for Ruby needed for Chef’s knife later.

ubuntu@testbox:~/littlechef$ rvm use 1.9.2

Using virtrualenv for Python, so as not to clutter environment’s Python installation.
Genenerate metadata.json for Chef recipes.

ubuntu@testbox:~/littlechef$ workon lchef
ubuntu@testbox:~/littlechef$ cd kitchen/cookbooks
(lchef)ubuntu@testbox:~/littlechef/kitchen/cookbooks$ knife cookbook metadata -a  -o ./
Generating metadata for alestic_ppa from /home/ubuntu/littlechef/kitchen/cookbooks/alestic_ppa/metadata.rb
Generating metadata for apt from /home/ubuntu/littlechef/kitchen/cookbooks/apt/metadata.rb
Generating metadata for graylog2 from /home/ubuntu/littlechef/kitchen/cookbooks/graylog2/metadata.rb
Generating metadata for java from /home/ubuntu/littlechef/kitchen/cookbooks/java/metadata.rb
Generating metadata for java_sun from /home/ubuntu/littlechef/kitchen/cookbooks/java_sun/metadata.rb
Generating metadata for logrotate from /home/ubuntu/littlechef/kitchen/cookbooks/logrotate/metadata.rb
Generating metadata for mdadm from /home/ubuntu/littlechef/kitchen/cookbooks/mdadm/metadata.rb
Generating metadata for memcached from /home/ubuntu/littlechef/kitchen/cookbooks/memcached/metadata.rb
Generating metadata for mongodb from /home/ubuntu/littlechef/kitchen/cookbooks/mongodb/metadata.rb
Generating metadata for mongodbx from /home/ubuntu/littlechef/kitchen/cookbooks/mongodbx/metadata.rb
Generating metadata for nginx from /home/ubuntu/littlechef/kitchen/cookbooks/nginx/metadata.rb
Generating metadata for quick_start from /home/ubuntu/littlechef/kitchen/cookbooks/quick_start/metadata.rb
Generating metadata for runit from /home/ubuntu/littlechef/kitchen/cookbooks/runit/metadata.rb
Generating metadata for xfs from /home/ubuntu/littlechef/kitchen/cookbooks/xfs/metadata.rb

List nodes using LittleChef:

(lchef)ubuntu@testbox:~/littlechef/$ fix list_nodes
ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com
  Roles: 
  Recipes: alestic_ppa
  Node attributes:

server2
  Roles: 
  Recipes: mdadm, memcached
  Node attributes:
    mdadm: {'DEVICE': '/dev/sdj'}
    memcached: {'memory': '512'}

List of files on my kichen directory:

 
(lchef)ubuntu@testbox:~/littlechef/kitchen$ ls
auth.cfg  cookbooks  data_bags  nodes  roles  site-cookbooks

(lchef)ubuntu@testbox:~/littlechef/kitchen$ ls cookbooks/
alestic_ppa  apt  graylog2  java  java_sun  logrotate  mdadm  memcached  mongodb  mongodbx  nginx  quick_start  README  runit  xfs
 

Ok, let’s run the nginx installation using LittleChef to lucid32 server.
For simplicity, /etc/hosts file contains the IP mapping of lucid32 server.

(lchef)ubuntu@testbox:~/littlechef/kitchen$ fix node:lucid32 recipe:nginx

== Applying recipe 'nginx' on node lucid32 ==
Saving node configuration to nodes/lucid32.json...
Synchronizing node, cookbooks, roles and data bags...
ubuntu@lucid32's password: 

Cooking...
[Mon, 06 Feb 2012 23:13:40 -0800] INFO: *** Chef 0.10.8 ***
[Mon, 06 Feb 2012 23:13:40 -0800] INFO: Setting the run_list to ["recipe[nginx]"] from JSON
[Mon, 06 Feb 2012 23:13:40 -0800] INFO: Run List is [recipe[nginx]]
[Mon, 06 Feb 2012 23:13:40 -0800] INFO: Run List expands to [nginx]
[Mon, 06 Feb 2012 23:13:40 -0800] INFO: Starting Chef Run for lucid32
[Mon, 06 Feb 2012 23:13:40 -0800] INFO: Running start handlers
[Mon, 06 Feb 2012 23:13:40 -0800] INFO: Start handlers complete.
[Mon, 06 Feb 2012 23:13:41 -0800] INFO: execute[apt-get update] sh(apt-get update)
[Mon, 06 Feb 2012 23:13:44 -0800] INFO: execute[apt-get update] ran successfully
[Mon, 06 Feb 2012 23:13:44 -0800] INFO: directory[/var/cache/local] mode changed to 755
[Mon, 06 Feb 2012 23:13:44 -0800] INFO: Chef Run complete in 4.193466 seconds
[Mon, 06 Feb 2012 23:13:44 -0800] INFO: Running report handlers
[Mon, 06 Feb 2012 23:13:44 -0800] INFO: Report handlers complete

SUCCESS: Node correctly configured

Done.
Disconnecting from lucid32... done.

Now to check for the running Nginx server:

(lchef)ubuntu@testbox:~/littlechef/kitchen$ fix node:lucid32 ssh:"sudo netstat -lp | grep nginx "

Executing the command 'sudo netstat -lp | grep nginx ' on the node lucid32...
tcp        0      0 *:www                   *:*                     LISTEN      814/nginx       

Done.
Disconnecting from lucid32... done.

Vagrant: Productivity for both Developers and SysAdmins

Update Feb 22, 2012: I just found out another ‘cool’ tool at http://keminglabs.com/vagrant-ec2/ using vagrant for local image and with EC2 instance.

Vagrant is a productive tool in building and distributing virtual images for development.

1. Setup Virtualbox
2. Setup RVM
3. Install Ruby 1.9.2 inside RVM
4. Install Vagrant

$ gem install vagrant
$ vagrant box add base http://files.vagrantup.com/lucid32.box

5. Install chef (optional)

Once everything is set, a simple configuration on Vagrantfile can have the lines:

Vagrant::Config.run do |config|
  config.vm.box = "lucid32"
  config.vm.network "33.33.33.10"

end 

Start initializing and running our base image:

$ vagrant init base
$ vagrant up 
$ vagrant ssh

Now to start with chef-solo, you can download the getting started cookbooks:

Adding more cookbooks later, I need to generate the metadata.json.

$ cd /tmp
$ wget https://github.com/opscode/cookbooks/tarball/master -O cookbook-ops.tgz
$ tar -zxvf cookbook-ops.tgz
$ cd ops* 
$ knife cookbook metadata -a  -o ./ 

Generating metadata for 7-zip from /tmp/opscode-cookbooks-c156223/7-zip/metadata.rb
Generating metadata for activemq from /tmp/opscode-cookbooks-c156223/activemq/metadata.rb
Generating metadata for ant from /tmp/opscode-cookbooks-c156223/ant/metadata.rb
...

In my case, I copy only the nginx cookbook to vagrant cookbook directory, now the cookbook directory:

cookbooks/ 
    apache2      
	apt          
	nginx        
	vagrant_main

I need to edit the cookbooks/vagrant_main/recipes/default.rb for nginx:

require_recipe "apt"
require_recipe "nginx"

Now the new Vagrantfile will have the lines:

Vagrant::Config.run do |config|

  config.vm.box = "lucid32"
  config.vm.network "33.33.33.10"

  config.vm.provision :chef_solo do |chef|
     chef.log_level = "debug"
     chef.cookbooks_path = "cookbooks"
     chef.add_recipe "vagrant_main"
   end
end

And then we can start provisioning:

$ vagrant provision 

[default] Running provisioner: Vagrant::Provisioners::ChefSolo...
[default] Generating chef JSON and uploading...
[default] Running chef-solo...
[default] stdin: is not a tty
...

$ vagrant ssh
Linux lucid32 2.6.32-33-generic #70-Ubuntu SMP Thu Jul 7 21:09:46 UTC 2011 i686 GNU/Linux
Ubuntu 10.04.3 LTS

Welcome to Ubuntu!
 * Documentation:  https://help.ubuntu.com/
Last login: Fri Dec  2 01:27:30 2011 from 10.0.2.2
vagrant@lucid32:~$ sudo netstat -lp | grep www
tcp        0      0 *:www                   *:*                     LISTEN      739/nginx       
vagrant@lucid32:~$ 

Our nginx is running. 🙂 Let’s package our new nginx VM.

$ vagrant package --include Vagrantfile
[default] Attempting graceful shutdown of linux...
[default] Clearing any previously set forwarded ports...
[default] Cleaning previously set shared folders...
[default] Creating temporary directory for export...
[default] Exporting VM...
Progress: 0%Progress: 14%Progress: 28%Progress: 31%Progress: 34%Progress: 36%Progress: 46%Progress: 48%Progress: 57%Progress: 61%Progress: 68%Progress: 83%Progress: 86%Progress: 93%[default] Compressing package to: /Users/cocoy/vagrant/lucid32/package.box
[default] Packaging additional file: Vagrantfile

For distribution and restoring the new image:

$ vagrant box add newlucid package.box 
[vagrant] Downloading with Vagrant::Downloaders::File...
[vagrant] Copying box to temporary location...
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

Note: We can edit the Vagrantfile to change the IP first if we need to.

$ vagrant up
[default] Importing base box 'newlucid'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
...

Distributing VM’s for developers to sync configs/software packages/files with production setup is easy.
Easy replication of servers via chef-solo recipes.
Testing integration with different parts of the system may it be database, cache servers, or web servers.

Good references about Vagrant:
Getting-the-most-out-of-Chef-with-Scalarium-and-vagrant
Vagrant-testing-testing-one-two
Vagrant Getting Started