Build your own Vagrant box ready to use with chef-solo using Veewee
This is my first screencast and its a bit glitchy. The voice is not good because I’d to use the built-in microphone of my MacBook pro. I’ve to get a good one. So bare with me and the audio.
Things to shop- Ruby versions: 1.9.3-p0, 1.9.2-p290, 1.8.7-p352
- VirtualBox 4.1.6
- VirtualBox 4.1.6 Extension pack
- Ubuntu 11.10 32 bit server image file
- You know what is Virtual Box and Vagrant
- You are familiar with Git and Github
- You use Bundler and RVM
Visit https://github.com/jedi4ever/veewee and Fork the repo under your account
Git clone git clone git://github.com/millisami/veewee
cd veewee
Bundle install (if you don’t have Bundler installed, do gem install bundler --pre)
millisami at sachin in ~/gitcodes/veewee on master
± bundle install
Fetching gem metadata from http://rubygems.org/.......
Using Platform (0.4.0)
Using archive-tar-minitar (0.5.2)
...
Using bundler (1.1.rc)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
millisami at sachin in ~/gitcodes/veewee on master
Gems required
gem install vagrant
gem install veewee
Define a basebox template
First, lets see what are the templates available.
millisami at sachin in ~/gitcodes/veewee on master
± bundle exec vagrant basebox templates
The following templates are available:
...
vagrant basebox define '<boxname>' 'ubuntu-10.10-server-i386-netboot'
vagrant basebox define '<boxname>' 'ubuntu-11.04-server-amd64'
vagrant basebox define '<boxname>' 'ubuntu-11.04-server-i386'
...
So, the ubuntu 11.10 has not been defined yet. Lets build one based off the ubuntu-11.04-server-i386
millisami at sachin in ~/gitcodes/veewee on master
± bundle exec vagrant basebox define 'milli-ubuntu-11-10-32-bit' 'ubuntu-11.04-server-i386'
The basebox 'milli-ubuntu-11-10-32-bit' has been successfully created from the template ''ubuntu-11.04-server-i386'
You can now edit the definition files stored in definitions/milli-ubuntu-11-10-32-bit
or build the box with:
vagrant basebox build 'milli-ubuntu-11-10-32-bit'
Lets see the structure of the generated folder
millisami at sachin in ~/gitcodes/veewee on master
± tree definitions
definitions
└── milli-ubuntu-11-10-32-bit
├── definition.rb
├── postinstall.sh
└── preseed.cfg
1 directory, 3 files
Open up the folder in your editor
Open up the file definition.rb and change the version numbers from 11.04 to 11.10
# definition.rb
Veewee::Session.declare({
:cpu_count => '1', :memory_size=> '384',
:disk_size => '10140', :disk_format => 'VDI', :hostiocache => 'off',
:os_type_id => 'Ubuntu',
:iso_file => "ubuntu-11.10-server-i386.iso",
:iso_src => "http://releases.ubuntu.com/11.10/ubuntu-11.10-server-i386.iso",
:iso_md5 => "881d188cb1ca5fb18e3d9132275dceda",
:iso_download_timeout => "1000",
:boot_wait => "10", :boot_cmd_sequence => [
'<Esc><Esc><Enter>',
'/install/vmlinuz noapic preseed/url=http://%IP%:%PORT%/preseed.cfg ',
'debian-installer=en_US auto locale=en_US kbd-chooser/method=us ',
'hostname=%NAME% ',
'fb=false debconf/frontend=noninteractive ',
'keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false ',
'initrd=/install/initrd.gz -- <Enter>'
],
:kickstart_port => "7122", :kickstart_timeout => "10000", :kickstart_file => "preseed.cfg",
:ssh_login_timeout => "10000", :ssh_user => "vagrant", :ssh_password => "vagrant", :ssh_key => "",
:ssh_host_port => "7222", :ssh_guest_port => "22",
:sudo_cmd => "echo '%p'|sudo -S sh '%f'",
:shutdown_cmd => "shutdown -P now",
:postinstall_files => [ "postinstall.sh"], :postinstall_timeout => "10000"
})
Open up the file postinstall.sh and change libreadline5-dev to libreadline5 because the Ubuntu Oneiric doesn’t have libreadline5-dev package.
millisami at sachin in ~/gitcodes/veewee on master
± be vagrant basebox build 'milli-ubuntu-11-10-32-bit'
Build a Chef Gem From Source
I get really frustrated with Ruby packages, they promise so much and when on that special day the moon is aligned with Mars, it all just works, and life is great.
Unfortunately this doesn’t happen very often and when using a stack of Rubygems, you almost always get bitten by something.
My cause for complaint today is Vagrant and Chef, well specifically Chef Solo. Vagrant is fine, it does what you tell it to do, but for most use-cases Chef Solo is the right tool to use for provisioning your virtual server. The Vagrant docs on Chef Solo unfortunately fib you, they say you can use Data Bags with Chef Solo, but by default you cannot.
This is a big deal as many useful Chef recipes make heavy use of Data Bags. Data Bags which let you provide environment specific configuration for your provisioning is not yet supported by the stock Chef Gem (currently version 10.12.0). In order to make use of Data Bags with Chef Solo, you need version 10.14.0 and above. This means building the gem from source.
I use Veewee to build my vagrant base boxes (you should too, it’s awesome!), and you can edit the postinstall.sh file in your box definition folder to build Chef from source, rather than installing it directly via Rubygems.
You can repeat this for your local dev machine, and now you can get Chef Solo cooking up your recipes and happily using data bags.
veewee-templates-update - A Hidden Little Gem
Veewee considerably simplifies the process of creating base distribution images for use with Vagrant, but unfortunately you have to choose between using the easy to install gem (which comes with horribly out of date basebox templates), or install the latest version from source, which unfortunately uses rvm in a pretty repugnant way.
So, if you want to use veewee to setup a new amd64 Precise Pangolin basebox for vagrant, you either have to pull the latest veewee sources from github, or download the most recent releases templates and copy them over into your veewee gem folder.
This is where veewee-templates-update steps in, it automates that latter step (downloading and installing just the updated templates) for you.
Installation is simple:
$ gem install veewee-templates-update
Then, just run the updater:
$ veewee-templates-update
> Veewee: /home/aaron/.rvm/gems/ruby-1.9.3-p194/gems/veewee-0.2.3
> Downloading: https://github.com/jedi4ever/veewee/tarball/master
> Extracting: CentOS-4.8-i386 CentOS-5.5-i386-netboot CentOS-5.5-x86_64-netboot
> ...
$ vagrant basebox define precise-amd64 ubuntu-12.04-server-amd64
> ...
> (profit)