What is Vagrant?va·grant/ˈvāgrənt/Noun:A person without a settled home orregular work who wanders from placeto place and lives by begging.
What is Vagrant?A way of managing virtual machines • Run different operating systems • Deﬁne virtual machines in code • Save your own ass
Why Vagrant? Setting up a development environment• Often considered a right of passage• Takes hours, often requires support• Requires documentation that’s always frequently out of date• Breaking your development environment can kill productivity for a day• Produces unpredictable results
Why Vagrant?Your development and production environment are not the same!• Different OS?• Different version of PHP/Python/Ruby?• Diverged package versions?• Different default conﬁguration ﬁles?
Why Vagrant?• Update a website with a new feature.• The feature relies on a PHP module that is required by your web app• Module already installed on your laptop because another project that is hosted elsewhere needed it• It’s not a the server you deploy to, so the entire website stops working• You have no idea why, the same code worked on your machine• It worked on my machine!
Why Vagrant?• Vagrant lowers development environment setup time• Maximizes dev/prod parity• Makes the “works on my machine” excuse a relic of the past. from vagrantup.com
Getting Started Create a$ vagrant box add precise32http://ﬁles.vagrantup.com/precise32.box Intialise a workspace$ cd workspace$ vagrant init precise32
Conﬁguration• Simple to conﬁgure using the Vagrantﬁle - A Ruby DSL - which can be kept in version control• Easy to forward ports: Vagrant::Conﬁg.run do |conﬁg| # Forward guest port 80 to # host port 4567 conﬁg.vm.forward_port 80, 4567 end
Conﬁguration Deﬁne shared folderconﬁg.vm.share_folder "v-data", "/data", "../data" Static IP conﬁg.vm.network 192.168.0.100 Set hostname conﬁg.vm.host_name = "steve" Boot with GUI conﬁg.vm.boot_mode :gui
Multiple VMsVagrant::Conﬁg.run do |conﬁg| conﬁg.vm.deﬁne :web do |web_conﬁg| web_conﬁg.vm.box = "web" web_conﬁg.vm.forward_port "http", 80,8080 end conﬁg.vm.deﬁne :db do |db_conﬁg| db_conﬁg.vm.box = "db" db_conﬁg.vm.forward_port "db", 3306,3306 endend
It’s alive! Bring box up! $ vagrant upConnection to box $ vagrant ssh Close down box $ vagrant halt Delete box$ vagrant destroy
So what else?• Install all the packages you need once• Make it the same as your product environment• Make a base box and distribute amongst developers• What time has this actually saved
Package$ vagrant package --vagrantﬁle
ProvisionersYou can provision with:• Shell• Chef Solo• Chef Server• Puppet• Puppet Server• Build your own
Provisioners ShellVagrant::Conﬁg.run do |conﬁg| conﬁg.vm.provision :shell, :path =>"test.sh"end
Provisioners Chef Solo:Vagrant::Conﬁg.run do |conﬁg| conﬁg.vm.provision :chef_solo do |chef| chef.roles_path = "roles" chef.add_role("vm") endend
Provisioners Chef Server:Vagrant::Conﬁg.run do |conﬁg| conﬁg.vm.provision :chef_server do |chef| chef.chef_server_url = "http://chef.inter.net" chef.add_role("vm") endend
Provisioners Puppet:Vagrant::Conﬁg.run do |conﬁg| conﬁg.vm.provision :puppet do |puppet| puppet.mainfests_path ="puppetmanifests" puppet.mainfest_ﬁle = "newbox.pp" endend
Provisioners Puppet Server:Vagrant::Conﬁg.run do |conﬁg| conﬁg.vm.provision :puppet_server do |puppet| puppet.puppet_server ="puppet.inter.net" puppet.puppet_node ="vm.internet.net" endend
Provision with Chef• A tool for automating the provisioning and management of your servers• Open source• Lots of examples and code to get you started
Chef Very brief overview CookbooksCollections of recipes, templates and otherconﬁguration attributes to get stuff done. RecipesChunks of Ruby code that describe what stuff we need to do to get that stuff done.
Chef My First Recipe Install packagepackage "ntp" do action :installend Create conﬁg ﬁle from templatetemplate "/etc/ntp.conf" do source "ntp.conf.erb" owner "root" Restart service group "root" when conﬁg ﬁle mode 0644 changes notiﬁes :restart, resources(:service => "ntp")endservice "ntp" do action :start Deﬁne service andend start NTP
Example Get started with a CakePHP application environment with Vagrant and Chef • Install Oracle’s VirtualBox • Install Vagrant$ git clone email@example.com:salgo/cakefest-vagrant-chef.git$ cd cakefest-vagrant-chef$ ./git-fetch-submodules.sh$ vagrant up
Vagrant and onwards Will soon support new virtualisers• OpenStack• VMWare?• Linux containers?• Write your own?
Vagrant Tips• If you’re running OS X make sure OS updates are compatible with VirtualBox - Oracle are quick to release ﬁxes but best to check• Keep guest additions up to date with https://github.com/dotless-de/vagrant-vbguest• You can play with Chef in Vagrant so there’s no excuse not to try