More and more sites are using a variety of architectures, and corresponding backend services, that are putting a lot of pressure on local development setup. Fortunately there’s a lot of work going on in the devops space to help this out. I basically wanted to be able to created a local dev environment, develop, and then push up live and know that my environment would be the same without futzing about. I started going down the path of using Vagrant for a dev environment and configuring with Chef. I was really hoping it would all just work, unfortunately I wasn’t so lucky. Before we get to that though, lets develop a simple test app that we can test out deploying:
I’m not going to add any functionality to this app right now, but it will show as the basis for our deployments.
Chef
Chef is where I ran into the most trouble. Opscode has extensive documentation, but it is definitely geared towards their hosted environment. While their hosted environment seems great, its more well suited to a team trying to automate their infrastructure not a lone developer trying to get things going off the ground. I decided to go with Chef Solo. Fortunately I also found the great gem librarian. Librarian is bundler for your chef recipes and it works as advertised. To get librarian setup in your project run the following:
Vagrant
To get started, Ryan Bates has an excellent screencast on Vagrant. I’m going to present a short version, but if you’re interested in a little more detail I highly suggest you check it out. Vagrant uses virtual box for virtualization. In order to get going first go visit virtual boxand download the latest version. As of this writing I have version 4.1.10 installed. Next from a cmd line:
This takes care of all the background work required to have vagrant up and running.
Pulling it together
So we have a rails project with chef dependancies managed by librarian & vagrant. Now we want to be able to actually deploy a minimal rails stack to a virtual box. First thing linux (our deploy target) does not come with a javascript environment so make sure uncomment “gem ‘therubyracer’” in your Gemfile. Also, since we’re using unicorn uncomment “gem ‘unicorn’” while you’re there.
Now lets setup our chef dependancies. These are managed through the Cheffile. Edit your Cheffile so it looks like this:
Now just run librarian-chef install to download those deps to your cookbook directory
You might notice the last dependency, rails-lastmile. I created this cookbook to pull together the other cookbooks: unicorn, ngnix, etc. Over time I plan on added a lot more to this. It will also be the way I keep a consistent deployment structure between Vagrant, EC2, and others. More on this in a future post.
Next lets configure Vagrant to use our dependencies. Open up Vagrantfile in your favorite editor and put in the following:
That should be all you need to do to get ready to deploy. Just run vagrant up and watch the magic happen. The rails application is being hosted in the virtual environment using your code base. Click here to see it. Since the code base is actually on your system, as you edit files you’ll be able to see the changes live in the hosted environment. The only exception being dependancies. If you need to rely on a new dependency run vagrant reload to have the vagrant restart and pull in new bundled dependancies. Next up I’ll be looking at both adding some more infrastructure (like MongoDb) and deploying to EC2.