Red Green Repeat Adventures of a Spec Driven Junkie

Staying Productive with Vagrant - Consistency

Théophile-Alexandre Steinlen - Vagrant of Paris

I am preparing to do a lunch & learn talk at work soon and I want to work out the topic here before presenting.

Consider this part of a larger article. This article will focus on: consistency.

As a backend developer, my livelihood depends on being able to deliver features on a server. Currently, the servers I work with run Linux and run the Ruby on Rails web server.

If I want to be productive, I need consistency. Vagrant provides consistency in these ways:

  • having a system configured to my preference
  • destroy and rebuild a system easily
  • providing a working state

Vagrant provides this for me because I work as a backend server developer. I would not be able to do this as easily if I had to deliver mobile applications, but maybe?

If you work with a backend server and want more consistency with it, Vagrant will help you too.

What is Vagrant?

Quick introduction if you do not know what Vagrant is.

Vagrant is an application that can interface with virtualized computers, such as VirtualBox, ZenDesk, etc.

VirtualBox is a software emulation of a computer that can work within your computer. Terminology:

  • Host: the actual computer you are running
  • Guest: the computer emulated by VirtualBox

Vagrant provides a way to write scripts to configure a virtual computer through basic scripting. The following Vagrantfile is one I use for new systems and has these components:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64" # what operating system to use

  config.vm.provider "virtualbox" do |v| # how to configure the computer
    v.memory = 256
    v.cpus = 2
  end

  # networking features
  config.vm.network "forwarded_port", guest: 4000, host: 4000

  # additional software to install after first start
  config.vm.provision "shell", inline: <<-SHELL
    # setup emacs
    # https://emacs.stackexchange.com/a/12774

    # setup apt-add-repository first
    apt-get install -y python-software-properties
    apt-add-repository -y ppa:ubuntu-elisp

    apt-get update

    # libcurl for htmlproofer
    # apt-add-repository depends on python-software-properties
    apt-get install -y \
      emacs-snapshot
  SHELL
end

The above Vagrantfile will create a virtual computer that runs with:

  • Ubuntu 16.04
  • two processors
  • 256MB of RAM
  • connect port 4000 with the local system’s port 4000
  • install emacs

Once your computer system has VirtualBox and Vagrant installed, in the same directory as this Vagrantfile, just run: $ vagrant up and it will magically appear and accessible by: $ vagrant ssh.

Configuration

As Vagrant uses the Vagrantfile to configure a system and will run the commands in the provision section of the Vagrantfile on initial creation.

In the above Vagrantfile, the provision commands are:

  config.vm.provision "shell", inline: <<-SHELL
    # setup emacs
    # https://emacs.stackexchange.com/a/12774

    # setup apt-add-repository first
    apt-get install -y python-software-properties
    apt-add-repository -y ppa:ubuntu-elisp

    apt-get update

    # libcurl for htmlproofer
    # apt-add-repository depends on python-software-properties
    apt-get install -y \
      emacs-snapshot
  SHELL

This completes all the steps to install emacs. (For vim users, there would be no entry here. :-) ) So, everytime I create this computer, Vagrant installs emacs, as specified in the provision section.

I do not remember the steps to install, such as add python-software-properties or Ubuntu’s elisp repository to the system as these steps ae done.

Rebuild on Demand

Over time and with use, the system will become inconsistent. Installing new software, upgrading libraries or packages, an auto-updating operating system.

Any of these things may cause a system to become unstable.

At that point, what do you do?

If it was a real computer, it would be a format and manual re-install, which is a painful experience. Questions that go through my head: v

  • What did I install?
  • How did I install it?
  • How do I search for the article that explains how I install something?
  • Do I have time to re-install everything?

Vagrant answers all of those questions because the Vagrantfile contains the answers (so it pays to be diligent!)

To format and re-install with Vagrant, it is two commands:

$ vagrant destroy
$ vagrant up

How long will rebuilding take? Shorter than figuring out installed items! Yes, it’s that fast.

Always in a Working State

With a maintained Vagrantfile, you can restore the system to a working state, the system will always be ready for you to work on.

To achieve this: record any additional installations, libraries, upgrades, commands, etc. in the Vagrantfile. (It’s like having good git commit messages: you don’t need it now but when you do, you will appreciate it!)

Combine a maintained Vagrantfile with rebuilding on demand, customized system to your preference, and there will always be a consistent system ready for you to work on, no matter the situation.

Conclusion

Vagrant can provide a consistent backend developer experience as it can:

  • Provide you a working system.
  • A freshly built system on demand.
  • A system customized to your preference.

This allows me to deliver features and fix bugs quickly as the system is consistent. When the system becomes inconsistent, I rebuild.

Try Vagrant today and enjoy a consistent backend developer experience.