Vagrant - Configuring a Solr Box
Recently I started some experiments with Vagrant. Vagrant is a tool that lets you (pre-) configure development environments. This can be done based on VirtualBox, Hyper-V, VMWare or many more so-called providers. You see, another way to put it is that Vagrant is a tool that helps with configuring, building and running virtual machine instances.
The default provider is VirtualBox but the documentation actually recommends to use VMWare for more stable and performant environments.
Welcome to Vagrant
Vagrant can be installed via a manual download or your preferred package manager, such as
brew on Mac OS.
We needed to write (I guess they’re called) system tests, to test integration scenarios between an Apache Solr server instance and a library. As you can imagine this implies that an Apache Solr instance needs to be installed on every developer machine that intends to execute the entire test suite.
Now Vagrant comes into play. It’s a command-line tool that is used to setup a virtual machine with all components pre-installed.
However, instead of configuring virtual machine images from scratch, Vagrant introduces the concept of boxes. Boxes are the package format that is used to bring up identical development environments. The easiest way to use a box is to choose one from the publicy available ones.
The “precise64” Box
For our purpose, we based our Vagrant box on the “precise64” box. It contains an Ubuntu 12.04 LTS (precise) installation with some
tools pre-installed. The default box is specified in a file called
vagrant init will create a template
Vagrantfile in the current directory.
Vagrantfile is a file written in a Ruby DSL and it contains the configuration of our custom box:
The configuration specifies the pre-defined box
precise64 as “parent” vm box. In addition, it specifies the URL under which this box can be
Vagrantfile may consist of various sections. For a detailed overview of all available configuration options, please have a look at the [Vagrant documentation] (http://docs.vagrantup.com/v2/).
Next up in our configuration file is the network configuration. We use a private network,
this means we can access our guest from the host machine but the box won’t be visible from the outside. It gets an IP address in the private
address space. Plus, we define a forwarded port. In fact, this is the port under which Apache Solr listens in the default configuration. Once
localhost:8983 on the host machine, the request will be forwarded to the Vagrant virtual machine instance port 8983.
Would we start with
vagrant up we would have a running Ubuntu 12.04 LTS instance within seconds. Unfortunately, Ubuntu 12.04 doesn’t come with Solr pre-installed, so there’s some work left for us.
As the pre-defined “precise64” box doesn’t fix our use case, we need to alter the environment a bit. We need to install Java and Apache Solr in our custom box. The process of adding/tweaking stuff in a box is called provisioning. The simplest way of implementing provisioning is to write shell scripts. An advanced way would be to use tools such as Chef or Puppet.
We decided to use plain shell scripts and added a shell-script called
provision.sh to our
provision.sh shell-script defines all the commands to install Java and run the Solr instance:
The provisioning process starts with the definition of some variables and the check for the
.provision_check file. This file will be touched
once the provisioning process has gone through successfully. All files within the directory containing the
Vagrantfile will be available
in the guest at
/vagrant. The current home directory all file operations will be operated in during provisioning is
allows to configure more of these so-called synced folders but for our purposes the defaults were perfectly fine.
After the file check,
sources.list will be updated and the following lines will be added at the beginning of the file:
deb mirror:... entries are used to optimize the selected apt-get mirror. This will result in the selection of the fastest available mirror
without any hard-coded local preliminaries.
The next lines are pretty straight-forward. The
ppa:webupd8team/java repository is used to fetch Oracle JDK 7u51. As the JDK installer comes
with a modal confirmation dialog, the following lines are needed:
This quietly confirms and installs the JDK automatically.
Next up is Apache Solr installation. The Solr 4.7.1 zip is downloaded and gets extracted in the home directory. Afterwards, the example
configuration is run with
java -jar start.jar. This starts a Jetty instance. The script uses the following code to monitor when bootstrapping
Once the message “Registered new searcher” appears we can safely assume the Solr instance is started.
post.jar can be used to add documents to the index. In this script we simply add the files from the
And that’s it.
With this configuration the development environment can be started via
vagrant up. Once the machine shall be stopped, we can use
vagrant halt and
vagrant resume to resume were we stopped.
What’s really cool about it is that once we commit our
provision.sh to our git repository every developer is free to check it out and run the
vagrant command line tool with it. It results in exactly the same development virtual machine that can now be used to run our systems tests.