Python library for building and controlling Vagrant boxes via Fabric.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Basebox is a small Python library for building and interacting with Vagrant boxes using Fabric. Its goals are somewhat similar to the veewee project, but is specifically geared toward developing and testing Fabric deployments.


  • Vagrant
  • VirtualBox

A simple example: the @basebox decorator

from fabric.api import sudo
from import basebox

@basebox(name='sample', base='')
def build_sample_box(*packages):
    for package in packages:
        sudo('apt-get install -y %s' % package)

This example performs the following actions:

  • Constructs a temporary vagrant box based on base
  • Brings the box up, connects to it, and executes the contents of build_sample_box()
  • Halts, packages, and installs the box as sample
  • Cleans up after itself


> fab build_sample_box:nginx,postgresql,rabbitmq-server

installs a sample box with the specified packages preinstalled on it.

The parameters to basebox can be overridden in the call to the function it decorates, so the following commands will result in an identical box being installed as 'base':

> vagrant box add precise64
> fab build_sample_box:nginx,postgresql,rabbitmq-server,name=base,base=precise64

Finer grained control with the tempbox context manager

While basebox instantiates, boots, and connects to its box to execute its context, sometimes this is undesirable. The tempbox context manager merely creates a vagrant context with a minimalist Vagrantfile, and cleans up the context upon exiting. Any actions on the vagrant box, such as booting, connecting, and packaging, must happen through the yielded VagrantContext object ('box' in the following example).

import os

from fabric.api import sudo
from import tempbox
from cuisine import mode_local

def build_sample_box(packages=[], name='sample', base=''):
    with tempbox(basebox=base) as box:
        with box.connect():
            for package in packages:
                sudo('apt-get install -y %s' % package)

with mode_local():
    build_sample_box(packages=['nginx', 'postgresql', 'rabbitmq-server'])

This code is more verbose, but it does essentially the same thing as the @basebox example, and allows more control over the box's lifecycle. When used in conjunction with the methods for interacting with the underlying VirtualBox, this can enable more advanced build functionality. This sample packages a box with an alternate NIC (a step I've used when the default NIC was causing network reliability and speed issues):

from import tempbox
from cuisine import mode_local

def package_with_alternate_nic(nic, package_out):
    with tempbox() as box:
        # Power-cycle the box to make sure it's instantiated

        # Run 'VBoxManage modifyvm' to update the box's settings

        # Package modified box

with mode_local():
    package_with_alternate_nic('virtio', '')