Common Vagrant Helpers for User Config, OS Detection, Etc.
At DDM, we're starting to use Vagrant a lot. Because it's just Ruby, we've found
that we are adding a little bit of programming to our Vagrantfile
to make
things easier to share among our team. So we've made this repo intended to be a
submodule in our vagrant projects.
In your git project, add this as a submodule:
cd /path/to/vagrant/root/
git submodule add https://github.com/deseretdigital/vagrant-helper.git helper
You can download the latest vagrant-helper, create a directory in your vagrant project called "helper" and then move the unziped files there.
At the top of your vagrantfile
you can include the helper libraries like
so:
require './helper/core' # Required
require './helper/utils' # If you want to use the Utils helpers
require './helper/config' # If you want to use the Config helpers
Sometimes it is helpful to allow some customization of a Vagrant project for things like the location of files you would like to mount. Many times a project will require several mounts, and these paths on the host machine can vary from developer to developer.
You can create in your project the config
directory and add a prefs.yml
file. It can then look something like this:
vm:
provider: virtualbox
box:
name: precise
network: 10.13.37.10
forward:
http: 8080
ssh: 2222
Then in your Vagrantfile you can load the User Preferences. You can even check to make sure they were loaded.
require './helper/core' # Required
require './helper/config' # If you want to use the Config helpers
Vagrant.configure("2") do |config|
# Load the user preferences in config/prefs.yml
if (prefs = UserConfig.load(:prefs)).empty?
abort("Error: no user preferences were loaded, make sure you have created 'config/prefs.yml'")
end
# [ ... continue Vagrantfile settings ... ]
end
Finally, to use a config setting you can just do this:
config.vm.network :hostonly, ip: prefs['vm']['network']
We have developers on Linux, Mac, and Windows (32 and 64 bit). Sometimes there are certain settings that are more performant than others for a given Provider and OS.
Our Utils helper allows you to detect certain platform settings so you can only apply certain settings to a particular environment.
You can't have a 64bit guest on a 32bit host, so you can decide which basebox to use like so:
arch = Vagrant::Util::Platform::bit64? ? 64 : 32
config.vm.box = "precise#{arch}"
If you're on VirtualBox & a Posix based machine (i.e. Mac, Linux) with a large amount of files to share with the guest machine, you likely will run into performance problems. So its recommended to use NFS. However, other providers, or other OSes, do not have this problem so it would be nice not to require them to have NFS on. So here is a way to do it with the Platform utils:
Note, right now there I can't find a programatic way to detect which provider is being used, so it's best to have that in your config file. See above.
provider = prefs['vm']['provider'].to_sym
config.vm.synced_folder(
'/path/to/host/www', # Host
"/var/www", # Guest
# NFS on *nix-based platforms to resolve performance issues
:nfs => (Vagrant::Util::Platform::posix? and provider == :virtualbox)
)
Vagrant::Util::Platform::mac?
Vagrant::Util::Platform::windows?
NoopCommunicator is a plugin are used in case if Vagrant does not have an access to VMs (e.g. there is no information about ip), so it just runs VMs and does not try to perform additional actions using SSH.
require './plugins/NoopCommunicator'
Vagrant.configure("2") do |config|
config.vm.communicator = :noop
end