Install Vagrant & VirtualBox.
Be aware that this is my first packaged Vagrant box, and it is probably not great.
Motivation
I want to explore Mojolicious framework for Perl along with MongoDB. Both of these are available for each operating system I use. Unfortunately, each operating system is a unique environment, with its own quirks. I usually work my way around these quirks, but I also want to explore a number of virtualization tools that have become popular. This is an opportunity to learn how to set up a Vagrant box for my Mojolicious / MongoDB explorations.
Creating The Box
A virtual machine is basically a simulated operating system running on whatever your host machine is: Windows, Linux, OS X - whatever. That virtual machine lives its life as if it has its own environment. It is useful for application development, testing, application hosting, and security research. You can have a library of guest virtual machines, each dedicated to a particular task.
I have now exhausted my knowledge of virtualization, so let us move on.
Vagrant provides a single configuration and command set for managing aspects of multiple virtual machine managers. You can use Vagrant to create identical virtual machine “boxes” on host operating systems. These definitions — and the boxes themselves — can be shared with others.
Although multiple virtual machine managers are supported by Vagrant, VirtualBox is widely used and has the best support.
My Vagrant box needs an operating system. This is my first time through, so I will keep it simple: Ubuntu 14.04. I figure that I can find plenty of resources online if I get stuck.
Go to Vagrant Cloud for packaged Vagrant boxes with your favorite distribution and virtual machine provider. Vagrant Cloud provides a social network approach to shared boxes, allowing you to find and favorite useful options or even sharing your own. There is an Ubuntu account on Vagrant Cloud with a trusty64 box, presenting a 14.04 64 bit release that can be used in VirtualBox.
The vagrant init
command initializes a new Vagrant box. I will
reference trusty64 to give this box a starting point.
I could start the box up now and have a more or less blank starting point. Instead I will provision it so that it has the packages and tools needed for me to start development quickly. My goal is to get the latest releases of MongoDB, Perl 5, Mojolicious, and Mango — a pure Perl MongoDB driver written by the same developer who created Mojolicious.
Vagrant provisioning can be done via several approaches, but I go for the short and sweet method of writing a shell script.
I am going to install MongoDB via the provided Ubuntu packages, so that I do not have to worry about startup scripts and all that. I will also install build-essential
so that I can build Perlbrew.
bootstrap/system.sh
system.sh
invokes user.sh
as the default vagrant user. user.sh
contains the instructions to install Perlbrew and the latest Perl. Once that is out of the way, cpanm will install Mojolicious and Mango.
bootstrap/user.sh
My Vagrantfile varies only a little from the default. One thing to notice is that I have set up port forwarding so I can view running Mojolicious applications from a browser in the host system.
Vagrantfile
Now just run vagrant up
and wait.
I end up waiting quite a while.
Okay, okay. I admit that it took me a couple of tries to get those shell scripts working right. After each correction, vagrant provision
reran the provisioning stage of vagrant up
.
Eventually it finishes.
Testing the Box
I will not even pretend I know what I am doing here. The whole point of this exercise is to learn Mojolicious, MongoDB, and Mango. Oh yeah, and Vagrant. I just copy the sample application from the Mango Github README.
app.pl
I would like to configure my editor to invoke needed commands through Vagrant. Perhaps later. For now, SSH will do.
Back in my browser, I hit http://localhost:3000
a couple times and get:
Well how about that? Everything works!
Packaging
Installing everything took a long time. I do not want to wait for the full provisioning process every time I create a new box. Can I take a snapshot of this box and use it for other projects?
Of course I can. The Vagrant docs provide some guidelines for creating a new box for packaging.
WARNING
There is a warning in the packaging documentation that looks serious.
Advanced topic! Creating a base box can be a time consuming and tedious process, and is not recommended for new Vagrant users. If you’re just getting started with Vagrant, we recommend trying to find existing base boxes to use first.
Blah, blah, blah. If I listened to every warning, I wouldn’t know what my hair smells like when it’s on fire. You may want to be more careful, though.
The Vagrant package command takes your virtual machine and wraps it up into a single box file that you can reuse or share with others. Maybe I can just package my trusty64-derived box.
Apparently yes. The resulting box is about 750MB, which seems large.
William Walker wrote a post about Creating a Custom Vagrant Box, with many useful instructions. I am ignoring most of those useful instructions right now, though I will come back to them later. What caught my eye was his suggestion for reducing the size of the box. You can use dd
to clear out some space from inside the box.
I rebuild my package. That brings it down to 649MB - still large, but better than 750. I will come back later when I have the time and see if I can cut it down a little more.
I test it with the same Mojolicious application I used above and — hot dog — it works.
Sharing
Now is the part where I jump straight off the cliff of rational thinking and share every horrible mistake I made with you and anyone else who wants it.
Should you use MongoDB in production? I have no idea, but I bet that it’s a really bad idea to use my trusty-mongo-mojo box in production. Still — could be interesting to play with.
Have fun.
Was It Worth It?
That’s a good question. It was great as a learning experience. I enjoyed learning more about Vagrant. I don’t know yet whether it was worth my time to create this bundle. Mojolicious and MongoDB are already fairly easy to install on whatever platform. We’ll see. I do know that I’d like to revisit this package, clean it up, and maybe follow up with a similar package for Dancer. It’s just plain fun to make these packages.
What Now?
All that’s left now is to learn all the things. The online Perl documentation is current with Perl 5.20. Mojolicious::Lite is as good a place as any to start with learning Mojolicious. There’s a MongoDB manual to peruse. Mango does not yet have the polished guides that Mojolicious does, but browsing the Mango MetaCPAN page will get me a ways.
Oh yeah. When I’m done for the day, I’ll exit my vagrant session and suspend the virtual machine.
Added to vault 2024-01-15. Updated on 2024-02-01