Vagrant is a very awesome tool that makes it very easy to develop your projects in virtual machines on your laptop or PC. It’s a wrapper around VirtualBox. It also makes them very easy to maintain and they can be used by a whole group of developers. This way all the developers that work on a project keep the same development environment. So that kicks the argument “But on my machine it works” out of the door.. finally!
Read all about it at http://www.vagrantup.com/.
There are a few provisioners available like Cheff, Puppet, but also Shell. Because most developers already now Shell, I choose that as provisioner. I made a very handy Shell script that the provisioner runs and like to share it with you!
It makes sure the latest version of the hosts file is set and the latest version of the custom MySQL settings file is set. If the MySQL settings file is provisioned, then the MySQL server will also be restarted, but only then. The last command is about the creation of a symlink to phpMyAdmin if it doesn’t exist.
#!/bin/bash
# Ensures if the specified file is present and the md5 checksum is equal
ensureFilePresentMd5 () {
source=$1
target=$2
if [ "$3" != "" ]; then description=" $3"; else description=" $source"; fi
md5source=`md5sum ${source} | awk '{ print $1 }'`
if [ -f "$target" ]; then md5target=`md5sum $target | awk '{ print $1 }'`; else md5target=""; fi
if [ "$md5source" != "$md5target" ];
then
echo "Provisioning$description file to $target..."
cp $source $target
echo "...done"
return 1
else
return 0
fi
}
# Ensures that the specified symbolic link exists and creates it otherwise
ensureSymlink () {
target=$1
symlink=$2
if ! [ -L "$symlink" ];
then
ln -s $target $symlink
echo "Created symlink $symlink that references $target"
return 1
else
return 0
fi
}
# Provision commands
provision() {
# Hosts file
ensureFilePresentMd5 /vagrant/vagrant-data/system/hosts /etc/hosts "hosts"
# MySQL custom settings
ensureFilePresentMd5 /vagrant/vagrant-data/mysql/custom.cnf /etc/mysql/conf.d/custom.cnf "custom MySQL settings"
if [ "$?" = 1 ]; then echo "Restarting MySQL..."; service mysql restart; echo "...done"; fi
# Set symbolic link to phpMyAdmin
ensureSymlink /usr/share/phpmyadmin /vagrant/exampleproject/public/phpmyadmin
return 0
}
provision
My file “Vagrantfile” looks like:
# Add to /etc/hosts on your host system
##
# # Projects
# 192.168.33.10 projects-box
# 192.168.33.10 project1.local project2.local project3.local
##
# Vagrant config
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "precise64"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
# Boot with a GUI so you can see the screen. (Default is headless)
config.vm.boot_mode = :gui
# Host name
config.vm.host_name = "projects-box"
# Assign this VM to a host-only network IP, allowing you to access it
# via the IP. Host-only networks can talk to the host machine as well as
# any other machines on the same network, but cannot be accessed (through this
# network interface) by any external networks.
config.vm.network :hostonly, "192.168.33.10"
# Assign this VM to a bridged network, allowing you to connect directly to a
# network using the host's network device. This makes the VM appear as another
# physical device on your network.
# config.vm.network :bridged
# Forward a port from the guest to the host, which allows for outside
# computers to access the VM, whereas host only networking does not.
# config.vm.forward_port 80, 8080
# Customization
config.vm.customize ["modifyvm", :id, "--memory", 1024] # 1 GB RAM
config.vm.customize ["modifyvm", :id, "--chipset", "ich9"] # Modern ICH9 chipset
# Share an additional folder to the guest VM. The first argument is
# an identifier, the second is the path on the guest to mount the
# folder, and the third is the path on the host to the actual folder.
# config.vm.share_folder "v-data", "/vagrant_data", "../data"
config.vm.share_folder("v-root", "/vagrant", ".", :nfs => true)
# Enable shell provisioning
config.vm.provision :shell, :path => "vagrant-data/provision.sh"
# Enable provisioning with Puppet stand alone. Puppet manifests
# are contained in a directory path relative to this Vagrantfile.
# You will need to create the manifests directory and a manifest in
# the file base.pp in the manifests_path directory.
#
# An example Puppet manifest to provision the message of the day:
#
# # group { "puppet":
# # ensure => "present",
# # }
# #
# # File { owner => 0, group => 0, mode => 0644 }
# #
# # file { '/etc/motd':
# # content => "Welcome to your Vagrant-built virtual machine!
# # Managed by Puppet.\n"
# # }
#
# config.vm.provision :puppet do |puppet|
# puppet.manifests_path = "manifests"
# puppet.manifest_file = "base.pp"
# end
# Enable provisioning with chef solo, specifying a cookbooks path, roles
# path, and data_bags path (all relative to this Vagrantfile), and adding
# some recipes and/or roles.
#
# config.vm.provision :chef_solo do |chef|
# chef.cookbooks_path = "../my-recipes/cookbooks"
# chef.roles_path = "../my-recipes/roles"
# chef.data_bags_path = "../my-recipes/data_bags"
# chef.add_recipe "mysql"
# chef.add_role "web"
#
# # You may also specify custom JSON attributes:
# chef.json = { :mysql_password => "foo" }
# end
# Enable provisioning with chef server, specifying the chef server URL,
# and the path to the validation key (relative to this Vagrantfile).
#
# The Opscode Platform uses HTTPS. Substitute your organization for
# ORGNAME in the URL and validation key.
#
# If you have your own Chef Server, use the appropriate URL, which may be
# HTTP instead of HTTPS depending on your configuration. Also change the
# validation key to validation.pem.
#
# config.vm.provision :chef_client do |chef|
# chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
# chef.validation_key_path = "ORGNAME-validator.pem"
# end
#
# If you're using the Opscode platform, your validator client is
# ORGNAME-validator, replacing ORGNAME with your organization name.
#
# IF you have your own Chef Server, the default validation client name is
# chef-validator, unless you changed the configuration.
#
# chef.validation_client_name = "ORGNAME-validator"
end
In this file you see that I run multiple project on the same virtualbox. Because all the project have the same environment requirements that is possible. If a project differs much, you’ll probably better off to create a Vagrantfile / VM for just that project. But if the projects communicate with eachother, keep in mind that you will have to run multiple virtual machines on your machine at the same time. This can consume a lot of memory! So check that you have enough.