Cooking with Vagrant, like a Chef.
Long time ago (almost like a year to be precisely) here in Crowd we traveled to the 2011'RailsConf conferences in Baltimore. That week, I took all my credit cards and founds, and replaced my old laptop (switching from Linux to OSX).
So, doing everything on my new laptop was hard. 8 years using linux, makes me confident on my *nix skills. But those days, I just felt stupid. I wasn't able to do nothing, installing annoying libraries like ImageMagic or even compile Ruby was horrible and painful.
One day we met Vagrant, and my first thought was:
Well, this is great! it's like bundler doing my life easier, but for services. It's easy to install, and remove. And I still use Linux!.. and it's virtualized wow!
I started to play with it. I loved the idea of moving all my specific project servers and libs outside my personal OS, and be able to reuse my Linux settings and hacks. Keeping my personal system, clean and stable. No more nasty things like Mac Ports to install linux-based services, or any of that shit.
So many things changed since those days. Now, we have Vagrant 1.0, we have a new Ubuntu Precise box (sorry crappy Lucid64), Postgresql 9.1 is more likely now. Now, I'm responsible of all of the recipes from my new project, and we reduced the total new-setup-guide time, from almost a day to just 5 minutes (and it's virtualized remember :D) Doing the same stuff like before, but automatically, and virtualized. Using using Vagrant boxes for Linux, and Chef-Solo recipes.
Why we should care about Chef & Vagrant?
Your first recipe.
I wrote a blog post about how to install postgresql on vagrant in the past.
btw, it was really awful (please don't read it). Let's try to fix that:
Vagrant uses a
Vagrantfile to configure the box. We suggest to use one Vagrantfile per project. So don't do nasty things like having all your projects running on the same box. Keep it clean. At the end, you won't need more than one box running at the time isn't? why install everything on that? doesn't make sense.
# verify the vagrant installation (restart your shell if fails): $ vagrant --version Vagrant version 1.0.0 # create a new Vagrantfile: $ vagrant init; … # see the current status vagrant status;
**NOTE:* Remove all the default-comments from the Vagrantfile (feels like the old apache.conf documentation, isn't it's).* Read them if you want, but at the end just leave:
So, it says:
This box uses a
precise64template box. If you don't have this template, download it from this
url. This should use
188.8.131.52ip address. We want to forward all the comunication from
127.0.0.1:80(and 5432). Also, we use
Chef-Soloto provision this box, please include the recipes called
postgres. And make it fast! Pronto!
Ok, now that we have the Vagrantfile, let's try to start our new virtualized linux, and apply those recipes.
# turn on the machine! vagrant up; … # should said running vagrant status;
You will see the booting process log. Also (as green/red messages) the Chef-Solo execution. You will be notified if there is any problem starting the box, or applying the recipes. If you see anything in red, you're screwed.
At this point, you have a new Precise Ubuntu 64bits, ready. And all of our recipes where applied. Let's start a ssh session:
vagrant ssh; … # inside the virtual uname -a; exit; … # outside the virtual vagrant halt; … # should said halted (turned off) vagrant status;
The Cookbooks folder
Vagrant use Chef-Solo to apply the recipes commands inside the virtualized system. Chef use
cookbooks folder to group similar the recipes by folders, for example the
databases recipes, the
system recipes, or whatever.
Each recipe can handle multiple variations, like
databases::postgres, but it's up to you. We found that sometimes the most simple thing is have just one variation per recipe (the default). Keep it simple. We suggest to store all the recipes inside the the
Chef use three basic folders on every recipe,
templates. The recipes is just the commands to be executed, like "apt-get install", "copy", "mkdir" but using Chef syntaxis. The templates are files that can be interpolated, so it could have "variables" and replace values from them. Files are templates without any interpolation. Or whatever other file like images, commands, zip, files.
Learn how, make tasty things
Easy, just go to the Opscode Chef-Solo reference page. Take the examples, and try to use them to install your favorite service using Chef-Solo, Vagrant and Virtualbox.