Sunday, February 16, 2020

Vagrant Plugins

1: The disksize plugin
Ever created a VM with a specific size for the disk and after using it for a while, you find out you would have liked to have a larger size disk? You can resize the VDI in for example.
you can ask Vagrant to give you the disk size you want to have?
Enter the vagrant-disksize plugin.
Install it with
vagrant plugin install vagrant-disksize
And you can use it in your Vagrant file like:
config.disksize.size = '75GB'
It will figure out what to do and do it!

2: The vbguest plugin
Getting tired of installing guest additions after each and every new installation and keeping them updated after a new version of VirtualBox is released? Mounting an ISO, installing the required dependencies, running the installer… You don’t have to worry about those things anymore! The vagrant-vbguest plugin takes care of this for you.
Install it with:
vagrant plugin install vagrant-vbguest
And add to your Vagrantfile a line like:
config.vbguest.auto_update = true
This installs the correct version of the guest additions and also installs dependencies if required to for example build the module.

3: Install missing plugins
If I want to share a Vagrantfile, When my Vagrantfile depends on plugins such as above, it won’t work if they are missing. Luckily installing missing plugins is easy to automate within your Vagrantfile like below:
unless Vagrant.has_plugin?("vagrant-disksize")
puts 'Installing vagrant-disksize Plugin...'
system('vagrant plugin install vagrant-disksize')
end
unless Vagrant.has_plugin?("vagrant-vbguest")
puts 'Installing vagrant-vbguest Plugin...'
system('vagrant plugin install vagrant-vbguest')
end
unless Vagrant.has_plugin?("vagrant-reload")
puts 'Installing vagrant-reload Plugin...'
system('vagrant plugin install vagrant-reload')
end


This way you don’t have to first install the plugins before you can use the Vagrantfile. Inspiration from the Oracle provided Vagrantfiles here.

4: Execute a command once after a reboot
Suppose you want to have a specific command executed only once after a reboot. This can happen because a specific command could require certain files not to be in use or for example the vagrant user not to be logged in. You can do this with a pre-script (prepare), the reload plugin and a post script (cleanup). The below example should work for most Linux distributions:
Inside your Vagrantfile:
config.vm.provision :shell, path: "prescript.sh"
config.vm.provision :reload
config.vm.provision :shell, path: "postscript.sh"
Pre-script: prescript.sh
chmod +x /etc/rc.d/rc.local
echo ‘COMMAND_YOU_WANT_TO_HAVE_EXECUTED’ >> /etc/rc.d/rc.local
Post-script: postscript.sh
chmod -x /etc/rc.d/rc.local
sed -i ‘$ d’ /etc/rc.d/rc.local

The pre-script adds a line to rc.local and makes it executable. The postscript removes the line again.

5: Installing docker and docker-compose
For installing docker and docker-compose (and running containers) there are 3 main options.
Using Vagrant plugins
This is the easiest option and does not require specific provisioning file entries. The Docker provisioner is provided as part of Vagrant out of the box. Docker-compose requires the installation of a plugin.
vagrant plugin install vagrant-docker-compose
Inside your Vagrantfile
config.vm.provision :docker
config.vm.provision :docker_compose

Using these plugins, you might have to look into how you can force it to use a specific version should you require it.
Using a provisioning script and OS repositories