Apache Karaf on Vagrant (example Vagrantfile)

If you have a team developing OSGi applications for Apache Karaf, Vagrant provides an easy way to ensure everyone is testing local deployments in a consistent context.  Vagrant is a little like Docker, using a layered approach to build up virtual environments.  In this case, we create an Ubuntu “box”, running on a VirtualBox VM, and automatically set it up with everything necessary for Karaf testing.

Notes and caveats:

  1. This Vagrantfile is meant to be dropped into the root of your project.  When run, Vagrant will automatically mount that project directory to /vagrant in the VM.  This is handy since the bundle JAR, available in “target” (assuming Maven), is now visible to the VM and can be directly installed in Karaf.
  2. Once in a while, you’ll see a Vagrantfile install a desktop, IDE, etc. and allow that to be used for actual development.  I’d highly advise against that.  It’s more typical, and far more performant, to do development locally, then use Vagrant purely as a DEV testing environment.
  3. Ubuntu was arbitrarily chosen — I tend to prefer Debian-based distros, but this could work with any base box.
  4. I rely on a PPA to install Oracle Java 8.  Realistically, I should switch this to OpenJDK.
  5. Allocating 4 GB of memory was also arbitrary.  It’s overkill for simple needs, but may need bumped up for large applications.
  6. VT-x must be enabled in your BIOS for all CPU modes!  Without it, VirtualBox will fail to run the VM.
  7. A workaround is required for Vagrant 1.8.1 on Windows.  See the comment in the Vagrantfile.

The goods:

# -*- mode: ruby -*-
# vi: set ft=ruby :

#########################################################################
# INSTRUCTIONS
#
# 1.) Build your project, locally, either with your IDE or the command line.
# 2.) vagrant up
# 3.) vagrant ssh
# 4.) karaf
# 5.) If your project has a Karaf feature, install with:
#     feature:repo-add file:/vagrant/features.xml (assumes the root of the project)
#     Alternatively, install the bundle, also using file:/vagrant/.../yourproject.jar
# 6.) feature:install [feature name]
#     or
#     bundle:start [bundle #]
# 7.) When you're done, Ctrl+D to exit Karaf console.
# 8.) During development, before you boot Karaf back up, it's a good idea to blow away its "data" directory (contains
#     logs, cache of all running bundles, etc.).  An alias is setup: "karafClean".  Then, redo #5-#6.
#########################################################################

#########################################################################
# NOTE: VT-x must be enabled in the BIOS for all CPU modes!
#########################################################################

#########################################################################
# BUG FIX FOR VAGRANT 1.8.1 ON WINDOWS
#
# If 'vagrant up' gives you a "chown: changing ownership of ‘/vagrant’: Not a directory" error, change the
# windows_unc_path() method in C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.8.1\lib\vagrant\util\platform.rb to:
#
#         def windows_unc_path(path)
#            path = path.gsub("/", "\\")
#
#            # If the path is just a drive letter, then return that as-is
#            return path if path =~ /^[a-zA-Z]:\\?$/
#
#            # Convert to UNC path
#            path
#          end
#
# Discussed here: https://github.com/mitchellh/vagrant/issues/5933
#########################################################################

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false

    # Customize the amount of memory on the VM:
    vb.memory = "4096"
  end

  config.vm.provision "shell", inline: <<-SHELL
    sudo echo "LANG=en_US.UTF-8" >> /etc/environment
    sudo echo "LANGUAGE=en_US.UTF-8" >> /etc/environment
    sudo echo "LC_ALL=en_US.UTF-8" >> /etc/environment
    sudo echo "LC_CTYPE=en_US.UTF-8" >> /etc/environment

    # Java 8
    # I was hesitant at first to rely on a PPA, but webupd8 appears to be legit and widely used.
    sudo add-apt-repository -y ppa:webupd8team/java
    sudo apt-get update
    sudo apt-get -y upgrade
    # Automatically accept Oracle's license agreement prompts
    echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
    echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections
    sudo apt-get -y install oracle-java8-installer

    # Java env vars
    sudo echo "export JAVA_HOME=/usr/lib/jvm/java-8-oracle" >> /home/vagrant/.bashrc
    echo "export JDK_HOME=/usr/lib/jvm/java-8-oracle" >> /home/vagrant/.bashrc

    # Apache Karaf 4.0.3
    sudo wget -O /opt/apache-karaf-4.0.3.tar.gz http://supergsego.com/apache/karaf/4.0.3/apache-karaf-4.0.3.tar.gz
    sudo tar -zxvf apache-karaf-4.0.3.tar.gz -C /opt
    chmod -R 777 /opt/apache-karaf-4.0.3
    echo "export PATH=$PATH:/opt/apache-karaf-4.0.3/bin" >> /home/vagrant/.bashrc
    echo "alias karafClean='rm -rf /opt/apache-karaf-4.0.3/data'" >> /home/vagrant/.bashrc
  SHELL
end

Leave a Reply

Your email address will not be published. Required fields are marked *