Automating virtualization with veewee and vagrant

In order to consolidate my development environment a friend (thank you @bascht) mentioned veewee to me. Veewee automates and simplifys vagrant basebox creation. It comes with a plethora of templates for operating systems and versions. I use it to quickly build Ubuntu server on VirtualBox.

I started with a standard template (vagrant basebox define ‘appbox’ ‘ubuntu-12.10-server-amd64’) and customized the postinstall.sh. Including replacing the ruby install with rvm. This leads to problems when using chef which I don’t necessarily use so I can live with the inconvenience rather than with having to ruby setups.

I keep my own definitions for veewee in a separate directory to be able to version control it. Basebox creation itself is done by a little script. This setup requires git, and VirtualBox to be installed. I clone veewee parallel to the directory holding my definitions.


cd ..
BASE=$(pwd)

echo “nnBuilding dbesbox and exportingnn”
if [ ! -d $BASE/veewee/definitions ]; then
mkdir $BASE/veewee/definitions/
fi
cp -r $BASE/my-devenv/appbox/ $BASE/veewee/definitions/appbox/
cp -r $BASE/my-devenv/databasebox/ $BASE/veewee/definitions/databasebox/
cd $BASE/veewee/
veewee vbox build ‘appbox’ —force
veewee vbox export ‘appbox’ —force
echo “nnDone building and exporting appboxnn”
mv $BASE/veewee/appbox.box $BASE/my-devenv/

veewee vbox build ‘databasebox’ —force
veewee vbox export ‘databasebox’ —force
echo “nnDone building and exporting databaseboxnn”
mv $BASE/veewee/databasebox.box $BASE/my-devenv/

cd $BASE/my-devenv/
veewee alias delete vagrant #Eradicate the “Gemfile could not be found” error
source ~/.zshrc

After building the two boxes I add them with

vagrant box add 'appbox' 'appbox.box'

and databasebox accordingly.

My Vagrantfile looks like this:

Vagrant::Config.run do |config|

config.vm.define :app do |app_config|
app_config.vm.box = "appbox"
app_config.vm.network :hostonly, "10.10.1.2" #10.10.1.1 is the host
app_config.vm.provision :shell, :path => "provisions/common_base.sh"
app_config.vm.provision :shell, :path => "provisions/app_base.sh"
end

config.vm.define :db do |db_config|
db_config.vm.box = “databasebox”
db_config.vm.network :hostonly, “10.10.1.3”
db_config.vm.provision :shell, :path => “provisions/common_base.sh”
db_config.vm.provision :shell, :path => “provisions/db_base.sh”
end

end

My provisioner is shell, but a »gem install chef« would enable you to use chef subsequently to provision. (Since rvm installation requires a closing and restarting of the shell or login/logout I found this to be more reliable.)

Now a simple ‘vagrant up’ starts both boxes and pointing the browser at 10.10.1.2 is answered by the nginx in appbox. My application is configured to use the db server at 10.10.1.3

Advertisements

Update on the .zshrc

I needed a little more information from my prompt so I extended it a bit

HISTFILE=~/.histfile
HISTSIZE=1000
SAVEHIST=1000
setopt autocd
setopt promptsubst

autoload -U colors && colors
autoload -Uz vcs_info && vcs_info

precmd() { vcs_info }
zstyle ‘:vcs_info:*’ enable git hg bzr
zstyle ‘:vcs_info:*’ check-for-changes true
zstyle ‘:vcs_info:*’ get-unapplied true
zstyle ‘:vcs_info:*’ unstagedstr “!”
zstyle ‘:vcs_info:*’ formats “%F{5}[%s:%r|%b]%u”
zstyle ‘:vcs_info:*’ actionformats “%F{5}[%s:%r|%b-%a]”

PROMPT=”%F{2}%n@%M:%F{6}%d%F{11}» “
RPROMPT=’${vcs_info_msg_0_}’

The result is a shell that lets me switch into a directory without typing cd and if the dir is version controlled it shows me the versioning system, the repo name, the branch I’m on and whether there are unstaged changes (indicated by !)