Using Vagrant
    Andy Gale
Who are you?



 Andy Gale
 andy-gale.com
 @andygale
What is Vagrant?


va·grant/ˈvāgrənt/
Noun:
A person without a settled home or
regular work who wanders from place
to place and lives by begging.
What is Vagrant?
A way of managing virtual machines




 •   Run different operating systems

 •   Define 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 configuration files?
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
Vagrant sounds
    good!
Getting Started


• Install Oracle’s VirtualBox
  www.virtualbox.org/wiki/Downloads

• Install Vagrant (packages for
  Windows, Mac, Linux)
  downloads.vagrantup.com
Getting Started

               Create a
$ vagrant box add precise32
http://files.vagrantup.com/
precise32.box
       Intialise a workspace
$ cd workspace
$ vagrant init precise32
Configuration
• Simple to configure using the Vagrantfile -
  A Ruby DSL - which can be kept in version
  control

• Easy to forward ports:
    Vagrant::Config.run do |config|
      # Forward guest port 80 to
      # host port 4567
      config.vm.forward_port 80,
      4567
    end
Configuration

             Define shared folder
config.vm.share_folder "v-data", "/data", "../data"

                    Static IP
       config.vm.network '192.168.0.100'

                 Set hostname
         config.vm.host_name = "steve"

                  Boot with GUI
           config.vm.boot_mode :gui
Vagrant Defaults



• /vagrant - shares your workspace
• Default username "vagrant"
• Default password "vagrant"
Multiple VMs
Vagrant::Config.run do |config|
  config.vm.define :web do |web_config|
    web_config.vm.box = "web"
    web_config.vm.forward_port "http", 80,
8080
  end

  config.vm.define :db do |db_config|
    db_config.vm.box = "db"
    db_config.vm.forward_port "db", 3306,
3306
  end
end
It’s alive!
  Bring box up!
  $ vagrant up
Connection 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 --vagrantfile
Provisioners
You can provision with:

• Shell
• Chef Solo
• Chef Server
• Puppet
• Puppet Server
• Build your own
Provisioners
                 Shell


Vagrant::Config.run do |config|
  config.vm.provision :shell, :path =>
"test.sh"
end
Provisioners
            Chef Solo:

Vagrant::Config.run do |config|
  config.vm.provision :chef_solo do |
chef|
    chef.roles_path = "roles"
    chef.add_role("vm")
  end
end
Provisioners
             Chef Server:
Vagrant::Config.run do |config|
  config.vm.provision :chef_server do |
chef|
     chef.chef_server_url = "http://
chef.inter.net"
     chef.add_role("vm")
  end
end
Provisioners
               Puppet:
Vagrant::Config.run do |config|
  config.vm.provision :puppet do |
puppet|
     puppet.mainfests_path =
"puppetmanifests"
     puppet.mainfest_file = "newbox.pp"
  end
end
Provisioners
           Puppet Server:
Vagrant::Config.run do |config|
  config.vm.provision :puppet_server do |
puppet|
     puppet.puppet_server =
"puppet.inter.net"
     puppet.puppet_node =
"vm.internet.net"
  end
end
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

               Cookbooks
Collections of recipes, templates and other
configuration attributes to get stuff done.

                 Recipes
Chunks of Ruby code that describe what stuff
   we need to do to get that stuff done.
Chef
                         My First Recipe
                                                    Install package
package "ntp" do
  action :install
end                                                Create config file
                                                    from template
template "/etc/ntp.conf" do
  source "ntp.conf.erb"
  owner "root"                                         Restart service
  group "root"                                         when config file
  mode 0644                                               changes
  notifies :restart, resources(:service => "ntp")
end

service "ntp" do
  action :start                               Define service and
end                                              start NTP
Chef
        Scary but you’re not alone


• Community cookbooks
  http://community.opscode.com/

• IRC #chef on freenode
• Foodfight show podcast
  http://foodfightshow.org/index.html

• Ask me andygale@andy-gale.com
Example
    Get started with a CakePHP application environment
                   with Vagrant and Chef


    • Install Oracle’s VirtualBox
    • Install Vagrant
$   git clone git@github.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 fixes 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
Official Boxes
           Ubuntu Lucid 32 Bit
 http://files.vagrantup.com/lucid32.box

           Ubuntu Lucid 64 Bit
 http://files.vagrantup.com/lucid64.box

          Ubuntu Precise 32 Bit
http://files.vagrantup.com/precise32.box

          Ubuntu Precise 64 Bit
http://files.vagrantup.com/precise64.box
Other Boxes

    Example boxes here
Questions?

Using Vagrant

  • 1.
    Using Vagrant Andy Gale
  • 2.
    Who are you? Andy Gale andy-gale.com @andygale
  • 3.
    What is Vagrant? va·grant/ˈvāgrənt/ Noun: Aperson without a settled home or regular work who wanders from place to place and lives by begging.
  • 4.
    What is Vagrant? Away of managing virtual machines • Run different operating systems • Define virtual machines in code • Save your own ass
  • 5.
    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
  • 6.
    Why Vagrant? Your developmentand production environment are not the same! • Different OS? • Different version of PHP/Python/Ruby? • Diverged package versions? • Different default configuration files?
  • 7.
    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!
  • 8.
    Why Vagrant? • Vagrantlowers development environment setup time • Maximizes dev/prod parity • Makes the “works on my machine” excuse a relic of the past. from vagrantup.com
  • 9.
  • 10.
    Getting Started • InstallOracle’s VirtualBox www.virtualbox.org/wiki/Downloads • Install Vagrant (packages for Windows, Mac, Linux) downloads.vagrantup.com
  • 11.
    Getting Started Create a $ vagrant box add precise32 http://files.vagrantup.com/ precise32.box Intialise a workspace $ cd workspace $ vagrant init precise32
  • 12.
    Configuration • Simple toconfigure using the Vagrantfile - A Ruby DSL - which can be kept in version control • Easy to forward ports: Vagrant::Config.run do |config| # Forward guest port 80 to # host port 4567 config.vm.forward_port 80, 4567 end
  • 13.
    Configuration Define shared folder config.vm.share_folder "v-data", "/data", "../data" Static IP config.vm.network '192.168.0.100' Set hostname config.vm.host_name = "steve" Boot with GUI config.vm.boot_mode :gui
  • 14.
    Vagrant Defaults • /vagrant- shares your workspace • Default username "vagrant" • Default password "vagrant"
  • 15.
    Multiple VMs Vagrant::Config.run do|config|   config.vm.define :web do |web_config|     web_config.vm.box = "web"     web_config.vm.forward_port "http", 80, 8080   end   config.vm.define :db do |db_config|     db_config.vm.box = "db"     db_config.vm.forward_port "db", 3306, 3306   end end
  • 16.
    It’s alive! Bring box up! $ vagrant up Connection to box $ vagrant ssh Close down box $ vagrant halt Delete box $ vagrant destroy
  • 17.
    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
  • 18.
  • 19.
    Provisioners You can provisionwith: • Shell • Chef Solo • Chef Server • Puppet • Puppet Server • Build your own
  • 20.
    Provisioners Shell Vagrant::Config.run do |config|   config.vm.provision :shell, :path => "test.sh" end
  • 21.
    Provisioners Chef Solo: Vagrant::Config.run do |config|   config.vm.provision :chef_solo do | chef|     chef.roles_path = "roles"     chef.add_role("vm")   end end
  • 22.
    Provisioners Chef Server: Vagrant::Config.run do |config|   config.vm.provision :chef_server do | chef|      chef.chef_server_url = "http:// chef.inter.net"      chef.add_role("vm")   end end
  • 23.
    Provisioners Puppet: Vagrant::Config.run do |config|   config.vm.provision :puppet do | puppet|      puppet.mainfests_path = "puppetmanifests"      puppet.mainfest_file = "newbox.pp"   end end
  • 24.
    Provisioners Puppet Server: Vagrant::Config.run do |config|   config.vm.provision :puppet_server do | puppet|      puppet.puppet_server = "puppet.inter.net"      puppet.puppet_node = "vm.internet.net"   end end
  • 25.
    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
  • 26.
    Chef Very brief overview Cookbooks Collections of recipes, templates and other configuration attributes to get stuff done. Recipes Chunks of Ruby code that describe what stuff we need to do to get that stuff done.
  • 27.
    Chef My First Recipe Install package package "ntp" do   action :install end Create config file from template template "/etc/ntp.conf" do   source "ntp.conf.erb"   owner "root" Restart service   group "root" when config file   mode 0644 changes   notifies :restart, resources(:service => "ntp") end service "ntp" do   action :start Define service and end start NTP
  • 28.
    Chef Scary but you’re not alone • Community cookbooks http://community.opscode.com/ • IRC #chef on freenode • Foodfight show podcast http://foodfightshow.org/index.html • Ask me andygale@andy-gale.com
  • 29.
    Example Get started with a CakePHP application environment with Vagrant and Chef • Install Oracle’s VirtualBox • Install Vagrant $ git clone git@github.com:salgo/cakefest-vagrant-chef.git $ cd cakefest-vagrant-chef $ ./git-fetch-submodules.sh $ vagrant up
  • 30.
    Vagrant and onwards Will soon support new virtualisers • OpenStack • VMWare? • Linux containers? • Write your own?
  • 31.
    Vagrant Tips • Ifyou’re running OS X make sure OS updates are compatible with VirtualBox - Oracle are quick to release fixes 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
  • 32.
    Official Boxes Ubuntu Lucid 32 Bit http://files.vagrantup.com/lucid32.box Ubuntu Lucid 64 Bit http://files.vagrantup.com/lucid64.box Ubuntu Precise 32 Bit http://files.vagrantup.com/precise32.box Ubuntu Precise 64 Bit http://files.vagrantup.com/precise64.box
  • 33.
    Other Boxes Example boxes here
  • 34.