A recurring problem in the world of web development is the complexity associated with setting up a suitable environment for each new project. If we’re lucky, we have up-to-date textual information to perform that setup. Even in that shiny world where we’re lucky, the time required to follow those textual instructions vary depending on the technical skills of the developer. Even when those instructions are up-to-date, it’s not rare at all to routinely spend many hours trying to set this new environment up.
Here at SFL, we recently started integrating Vagrant to our projects in order to modernize our deployment methods and results so far are very encouraging.
What is Vagrant?
Vagrant is a command line library allowing to quickly create and provision virtual environments through VirtualBox. It allows, with one vagrant up
command, to create a VM from scratch and automatically configure it to host your project. For the configuration part, it’s integrated with Puppet, which is good for us because that’s already what we used here at SFL to manage our machines.
Benefits
Environment setup speed. What used to be a more or less relevant set of textual instructions is now executable Puppet code. This means that a new developer being brought to a project has one single command to type:
fab deploy
and within minutes, the site is ready to be used locally.
Setup reliability. Gone are the times of textual instructions that fall into obsolescence and make new developers want to tear all their hair off (well, the hair tearing part is not entirely gone, but at least it’s not over environment setup anymore). If the Puppet code becomes incorrect over time, it’s very easy to spot immediately and correct it.
Homogenous deployment environments. There used to be a time when we developers had to describe to our sysadmins the production environment that we needed for a new project, and then they would do their best to understand what we meant and build it. Now, with Puppet, developers and sysadmins speak the same language. The same code that is used to build a local environment can be reliably used by sysadmins to replicate that environment elsewhere.
How to use it?
How you use Vagrant depends on the type of project you use it with. For example, our Django projects use a mix of Vagrant and Fabric to perform setup and deployment. The Puppet side takes care of “OS specific” setup (installing packages, configuring Apache, creating databases) and Fabric takes care of “Python code specific” setup (pushing the code to the environment, pip dependencies, South migrations).
I’ve published, on Github, a sample Vagrant-enabled Django project that looks a lot like what we do internally so you can easily experience the potential of that solution. If you have Git, VirtualBox, Vagrant and Fabric installed on your machine, you can run this project (from nothing) with these commands:
git clone https://github.com/savoirfairelinux/django-vagrant-demo
cd django-vagrant-demo/deploy
fab deploy
Then, you can visit http://demo-django.local:8080 and see the newly-deployed site.
The deploy script will ask for your sudo password because Vagrant and the Fabric script need to modify your /etc/hosts file to make demo-django.local to 127.0.0.1.
Wow. That is so elegant and logical and clearly explained. Keep it up! I follow up your blog for future post.