Julien's Easy Bake Vagrant environment – 3 commands and you'll be developing in our (almost) production environment | NetEngine

Julien's Easy Bake Vagrant environment – 3 commands and you'll be developing in our (almost) production environment

For a while we have been thinking about ways to use the same environment in both development and production.

As we drew closer to achieving this, we discovered another possible purpose for such an environment: NetEngine’s code school. We wanted to give a professional, hassle free environment to our students.

Here is how I built it … and how to use it.

Top Chef

A few weeks ago, we looked to change the way we build our production servers. We currently have some server images, with the basic setup we use in production, ready to be launched and configured.

To improve the way we build our servers, I looked into Chef-solo. Chef is a configuration management tool which provides the ability to easily build servers from scratch and update them later.

In my Chef kitchen, here I added many cookbooks from Opscode in order to build the same stack which we use in production (postgresql - nginx - rbenv - redis )

With a bit of configuration and few home made recipes I was able to build an environment which replicates, almost identically, our current production servers.

The Vagrant, Ubuntu

Vagrant is a solution to programmatically setup your virtual machine in VirtualBox. I relied heavily on the use of Vagrant to experiment with this server build. My VagrantFile is loading the blank ubuntu packaged by Vagrant.

Vagrant provides a handy way to package a virtual machine and to distribute it. The vagrant package command will create a lovely package, and this is what I will use to share the virtual machine with the NetEngine team; the code school students and you.

Ensuring we have an ‘easy bake’ package for our students

As the goal is to provide an extremely simple way to have a full environment running, I hacked a little to control the result of the Chef build. This setup is normally meant to be used in production.

A passwordless user

On this box, there is a “deployer” user, I decided to allow this user to have access to a log in box with no password and no key.

You can easily do this by allowing PermitEmptyPassword new line in /etc/ssh/sshdconfig and having changed “`nulloksecuretonullok”` in /etc/pam.d/common-auth.

You now only need to delete the user password with passwd -d deployer and the deployer user won’t ask for authentication anymore.

A fine shared folder

As we use a different user than what the Vagrant’s default one, I needed to tweak the rights of the share folder. You can easily do this with a small setup in the VagrantFile :

config.vm.share_folder("v-root", "/vagrant", ".", :extra => 'uid=1010,gid=1000')

In English, it says that the current folder will be mounted in /vagrant with the user id 1010 and the group id 1000.

Landing in the shared folder by default

The .bashrc is setup to change the directory to /vagrant as I wanted to limit each user having to type one command at a time when they log in, and this step will save few keystrokes for each student.

Preparing for packaging

To make it as small as possible, the box needs a few minor tweaks before it is ready for packaging.

The first step is to clean the apt files with apt-get clean

Then, spotted in the Vagrant’s postinstall.sh file, this trick will zero all your disk, giving the opportunity to compress the box efficiently.

dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY

Finally, packaging

Running the vagrant package command will generate a package.box file. You can download our box here: http://vagrant.netengine.com.au/NE_OS_v0.1.0.box

3 commands FTW

When our students will start their coding, they will have a Rails repository containing a VagrantFile similar to this:

Vagrant::Config.run do |config|
  config.vm.box = "NetEngine OS v0.1.0"
  config.vm.box_url = "http://vagrant.netengine.com.au/NE_OS_v0.1.0.box"
  config.vm.network :hostonly, ""
  config.vm.forward_port 3000, 3000
  config.vm.customize ["modifyvm", :id, "--cpuexecutioncap", "90"]
  config.vm.share_folder("v-root", "/vagrant", ".", :extra => 'uid=1010,gid=1000')

Once the repo is cloned they only have to do the following:

Change directory to the Rails app : cd <cloned repo>

Download and boot the virtual machine : vagrant up

SSH into the box : ssh deployer@

Then you can run the Rails server or the rake tasks in a complete environment.