Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Vagrant

Andy Gale
October 09, 2012

Using Vagrant

Talk given at Bristol Webfolk about using Vagrant for a consistent local development environment.

Andy Gale

October 09, 2012
Tweet

More Decks by Andy Gale

Other Decks in Programming

Transcript

  1. 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? Wednesday, 29 May 13
  2. A way of managing virtual machines • Run different operating

    systems • Define virtual machines in code • Save your own ass What is Vagrant? Wednesday, 29 May 13
  3. 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? Wednesday, 29 May 13
  4. 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? Wednesday, 29 May 13
  5. • 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? Wednesday, 29 May 13
  6. • 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 Why Vagrant? Wednesday, 29 May 13
  7. • Install Oracle’s VirtualBox www.virtualbox.org/wiki/Downloads • Install Vagrant (packages for

    Windows, Mac, Linux) downloads.vagrantup.com Getting Started Wednesday, 29 May 13
  8. Getting Started $ vagrant box add precise32 http://files.vagrantup.com/precise32.box Create a

    box Intialise a workspace $ cd workspace $ vagrant init precise32 Wednesday, 29 May 13
  9. 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 Wednesday, 29 May 13
  10. Configuration config.vm.share_folder "v-data", "/data", "../data" Define shared folder Static IP

    config.vm.network '192.168.0.100' Set hostname config.vm.host_name = "steve" Boot with GUI config.vm.boot_mode :gui Wednesday, 29 May 13
  11. Vagrant Defaults • /vagrant - shares your workspace • Default

    username "vagrant" • Default password "vagrant" Wednesday, 29 May 13
  12. 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 Wednesday, 29 May 13
  13. It’s alive! Bring box up! $ vagrant up Connection to

    box $ vagrant ssh Close down box $ vagrant halt Delete box $ vagrant destroy Wednesday, 29 May 13
  14. • 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 So what else? Wednesday, 29 May 13
  15. • Shell • Chef Solo • Chef Server • Puppet

    • Puppet Server • Build your own Provisioners You can provision with: Wednesday, 29 May 13
  16. Provisioners Vagrant::Config.run do |config| config.vm.provision :chef_solo do |chef| chef.roles_path =

    "roles" chef.add_role("vm") end end Chef Solo: Wednesday, 29 May 13
  17. Provisioners 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 Chef Server: Wednesday, 29 May 13
  18. Provisioners Vagrant::Config.run do |config| config.vm.provision :puppet do |puppet| puppet.mainfests_path =

    "puppetmanifests" puppet.mainfest_file = "newbox.pp" end end Puppet: Wednesday, 29 May 13
  19. Provisioners 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 Puppet Server: Wednesday, 29 May 13
  20. 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 Wednesday, 29 May 13
  21. 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. Wednesday, 29 May 13
  22. Chef My First Recipe package "ntp" do action :install end

    template "/etc/ntp.conf" do source "ntp.conf.erb" owner "root" group "root" mode 0644 notifies :restart, resources(:service => "ntp") end service "ntp" do action :start end Install package Create config file from template Define service and start NTP Restart service when config file changes Wednesday, 29 May 13
  23. • Community cookbooks http://community.opscode.com/ • IRC #chef on freenode •

    Foodfight show podcast http://foodfightshow.org/index.html • Ask me [email protected] Chef Scary but you’re not alone Wednesday, 29 May 13
  24. Example Get started with a CakePHP application environment with Vagrant

    and Chef • Install Oracle’s VirtualBox • Install Vagrant $ git clone [email protected]:salgo/cakefest-vagrant-chef.git $ cd cakefest-vagrant-chef $ ./git-fetch-submodules.sh $ vagrant up Wednesday, 29 May 13
  25. • OpenStack • VMWare? • Linux containers? • Write your

    own? Vagrant and onwards Will soon support new virtualisers Wednesday, 29 May 13
  26. • 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 Vagrant Tips Wednesday, 29 May 13
  27. 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 Wednesday, 29 May 13