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

Boost your dev environment with Vagrant

Boost your dev environment with Vagrant

Do you work in more than one project or with more than one person in or per project? If yes, this talk is for you.

One of the tasks where developers usually spend lots of time is in the configuration of the project development environment. What if you configure it by running a simple command? Without to have to hurry about product versions, without install anything in your machine?

This talk will be an introduction to Vagrant with Puppet that let’s you distribute your development environment. I will use a pratical example to explain and demonstrate how Vagrant and Puppet works.

Vagrant is a tool that manages virtual machines under VirtualBox or VMware. The virtual machines can be provisioning with bash scritps, puppet or chef.

Daniel Gomes

June 13, 2013
Tweet

More Decks by Daniel Gomes

Other Decks in Programming

Transcript

  1. Boost with Vagrant your dev environment Daniel Gomes @danielcsgomes June

    12, 2013 europeanspaceagency © http://www.flickr.com/photos/europeanspaceagency/3704205848
  2. AGENDA • Why is distributed environment a good thing •

    Why and what is Vagrant? • Providers • Provisioning with Puppet 2
  3. 3 ABOUT ME • Full Stack Web Developer @ Sedimap

    PT • Father, Geek • Zend Certi!ed Engineer PHP 5.3 • Certi!ed Scrum Master • @danielcsgomes
  4. “I use *unix” “I use Windows” “You need to change

    to project B” “Your code does not work in my machine” “I need to install an older version of software y” “New team member? Install all this stu!” “Need to reinstall everything again” “I’m tired to con"gure it over and over again” “Lost one day to con"gure my machine” 7
  5. HOW 8 MANY TIMES DID IT HAPPEN TO YOU?! sometimes

    suzie ©: http://www.flickr.com/photos/22324124@N07/6186059907
  6. 12 What if "does not work on my machine" ?

    you could get rid of the quote
  7. va•grant (ˈveɪ grənt) Noun: a person without a settled home

    or regular work who wanders from place to place and lives by begging DEFINITION 23
  8. What is Vagrant? • Manage virtual machines (such as Virtual

    Box, VMware, AWS) • Define the configuration in code using a single file • Can use Shell scripts, Puppet, Chef and other tools to provisioning the Virtual Machines • Written in Ruby 24
  9. How does it work? Will assume you already have Vagrant

    and a provider installed in your machine (will use Virtual Box because it’s free) 25 http://downloads.vagrantup.com/tags/v1.2.2 https://www.virtualbox.org/wiki/Downloads
  10. Workflow of vagrant up 26 Vagrantfile Provisioning manifest has provisioning

    manifest? yes start provisioning the VM VM is running vagrant up downloads the box if needed and loads vagrant configurations (share folders, port forwarding, etc) and starts the VM
  11. 36 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" # port forwarding config.vm.network :forwarded_port, guest: 80, host: 8080 # setup a private network with a static IP Address # nfs requires a static IP address config.vm.network :private_network, ip: "10.5.0.2" # sharing folders # Vagrant 1.1+ automatically ignores nfs on Windows OS config.vm.synced_folder ".", "/vagrant", :nfs => true end
  12. 37 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" # port forwarding config.vm.network :forwarded_port, guest: 80, host: 8080 # setup a private network with a static IP Address # nfs requires a static IP address config.vm.network :private_network, ip: "10.5.0.2" # sharing folders # Vagrant 1.1+ automatically ignores nfs on Windows OS config.vm.synced_folder ".", "/vagrant", :nfs => true end
  13. 38 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" # port forwarding config.vm.network :forwarded_port, guest: 80, host: 8080 # setup a private network with a static IP Address # nfs requires a static IP address config.vm.network :private_network, ip: "10.5.0.2" # sharing folders # Vagrant 1.1+ automatically ignores nfs on Windows OS config.vm.synced_folder ".", "/vagrant", :nfs => true end
  14. 39 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" # port forwarding config.vm.network :forwarded_port, guest: 80, host: 8080 # setup a private network with a static IP Address # nfs requires a static IP address config.vm.network :private_network, ip: "10.5.0.2" # sharing folders # Vagrant 1.1+ automatically ignores nfs on Windows OS config.vm.synced_folder ".", "/vagrant", :nfs => true end
  15. 40 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" # port forwarding config.vm.network :forwarded_port, guest: 80, host: 8080 # setup a private network with a static IP Address # nfs requires a static IP address config.vm.network :private_network, ip: "10.5.0.2" # sharing folders # Vagrant 1.1+ automatically ignores nfs on Windows OS config.vm.synced_folder ".", "/vagrant", :nfs => true end
  16. vagrant init vagrant up vagrant halt vagrant reload vagrant provision

    vagrant ssh vagrant suspend vagrant resume vagrant destroy 42 vagrant commands
  17. 43 vagrant init vagrant up vagrant halt vagrant reload vagrant

    provision vagrant ssh vagrant suspend vagrant resume vagrant destroy vagrant init Initializes Vagrant
  18. 44 vagrant init vagrant up vagrant halt vagrant reload vagrant

    provision vagrant ssh vagrant suspend vagrant resume vagrant destroy vagrant up Create and configure the Virtual Machine
  19. 45 vagrant halt Shutdown the Virtual Machine vagrant init vagrant

    up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy
  20. 46 vagrant reload Restart the Virtual Machine vagrant init vagrant

    up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy
  21. 47 vagrant provision Provision a started Virtual Machine vagrant init

    vagrant up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy
  22. 48 vagrant ssh Access the Virtual Machine via SSH in

    *unix Outputs the SSH configuration in Windows vagrant init vagrant up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy
  23. 49 vagrant suspend Suspends the Virtual Machine vagrant init vagrant

    up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy
  24. 50 vagrant resume Resume a suspended Virtual Machine vagrant init

    vagrant up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy
  25. 51 vagrant destroy Delete the created Virtual Machine vagrant init

    vagrant up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy
  26. http://docs.vagrantup.com/v2/virtualbox/configuration.html 57 # Vagranfile Vagrant.configure("2") do |config| config.vm.box = "precise64"

    config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provider "virtualbox" do |vb| vb.name = "phplx" # custom VM name vb.gui = true # enables GUI, defaults is false # Vagrant exposes a way to call any command against VBoxManage # Example: Change the memory of the VM vb.customize ["modifyvm", :id, "--memory", "512"] end end
  27. http://docs.vagrantup.com/v2/virtualbox/configuration.html 58 # Vagranfile Vagrant.configure("2") do |config| config.vm.box = "precise64"

    config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provider "virtualbox" do |vb| vb.name = "phplx" # custom VM name vb.gui = true # enables GUI, defaults is false # Vagrant exposes a way to call any command against VBoxManage # Example: Change the memory of the VM vb.customize ["modifyvm", :id, "--memory", "512"] end end
  28. http://docs.vagrantup.com/v2/virtualbox/configuration.html 59 # Vagranfile Vagrant.configure("2") do |config| config.vm.box = "precise64"

    config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provider "virtualbox" do |vb| vb.name = "phplx" # custom VM name vb.gui = true # enables GUI, defaults is false # Vagrant exposes a way to call any command against VBoxManage # Example: Change the memory of the VM vb.customize ["modifyvm", :id, "--memory", "512"] end end
  29. http://docs.vagrantup.com/v2/virtualbox/configuration.html 60 # Vagranfile Vagrant.configure("2") do |config| config.vm.box = "precise64"

    config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provider "virtualbox" do |vb| vb.name = "phplx" # custom VM name vb.gui = true # enables GUI, defaults is false # Vagrant exposes a way to call any command against VBoxManage # Example: Change the memory of the VM vb.customize ["modifyvm", :id, "--memory", "512"] end end
  30. http://docs.vagrantup.com/v2/virtualbox/configuration.html 61 # Vagranfile Vagrant.configure("2") do |config| config.vm.box = "precise64"

    config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provider "virtualbox" do |vb| vb.name = "phplx" # custom VM name vb.gui = true # enables GUI, defaults is false # Vagrant exposes a way to call any command against VBoxManage # Example: Change the memory of the VM vb.customize ["modifyvm", :id, "--memory", "512"] end end
  31. What is Puppet? • Declarative and model-based approach to IT

    automation • Performs administrative tasks (such as adding users, create folders, installing packages, etc) • Cross OS • Written in Ruby 64
  32. 68 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true # Vagrant will look inside the "manifests" folder # for the default manifest file "default.pp" config.vm.provision :puppet end
  33. 69 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true # Vagrant will look inside the "manifests" folder # for the default manifest file "default.pp" config.vm.provision :puppet end
  34. 71 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provision :puppet do |puppet| # to the manifests folder puppet.manifests_path = "vagrant/puppet" # initial manifest puppet.manifest_file = "init.pp" end end
  35. 72 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provision :puppet do |puppet| # to the manifests folder puppet.manifests_path = "vagrant/puppet" # initial manifest puppet.manifest_file = "init.pp" end end
  36. 73 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provision :puppet do |puppet| # to the manifests folder puppet.manifests_path = "vagrant/puppet" # initial manifest puppet.manifest_file = "init.pp" end end
  37. 74 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provision :puppet do |puppet| # to the manifests folder puppet.manifests_path = "vagrant/puppet" # initial manifest puppet.manifest_file = "init.pp" end end
  38. Vagrant with puppet 75 modules and options configuration puppet modules

    - http://docs.puppetlabs.com/puppet/2.7/reference/modules_fundamentals.html vagrant docs for puppet provisioning - http://docs.vagrantup.com/v2/provisioning/puppet_apply.html
  39. 76 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provision :puppet do |puppet| puppet.manifests_path = "vagrant/puppet" puppet.manifest_file = "init.pp" # defines the puppet modules folder puppet.module_path = "vagrant/modules" # set puppet options puppet.options = [ '--verbose', '--debug' ] end end
  40. 77 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provision :puppet do |puppet| puppet.manifests_path = "vagrant/puppet" puppet.manifest_file = "init.pp" # defines the puppet modules folder puppet.module_path = "vagrant/modules" # set puppet options puppet.options = [ '--verbose', '--debug' ] end end
  41. 78 # Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "precise64" config.vm.box_url

    = "http://files.vagrantup.com/precise64.box" config.vm.network :forwarded_port, guest: 80, host: 8080 config.vm.network :private_network, ip: "10.5.0.2" config.vm.synced_folder ".", "/vagrant", :nfs => true config.vm.provision :puppet do |puppet| puppet.manifests_path = "vagrant/puppet" puppet.manifest_file = "init.pp" # defines the puppet modules folder puppet.module_path = "vagrant/modules" # set puppet options puppet.options = [ '--verbose', '--debug' ] end end
  42. 80 # vagrant/puppet/init.pp class apt_update { exec { "aptGetUpdate": command

    => "sudo apt-get update", path => ["/bin", "/usr/bin"] } } class os_tools { package { "curl": ensure => present, require => Exec["aptGetUpdate"] } } include apt_update include os_tools
  43. 81 # vagrant/puppet/init.pp class apt_update { exec { "aptGetUpdate": command

    => "sudo apt-get update", path => ["/bin", "/usr/bin"] } } class os_tools { package { "curl": ensure => present, require => Exec["aptGetUpdate"] } } include apt_update include os_tools
  44. 82 # vagrant/puppet/init.pp class apt_update { exec { "aptGetUpdate": command

    => "sudo apt-get update", path => ["/bin", "/usr/bin"] } } class os_tools { package { "curl": ensure => present, require => Exec["aptGetUpdate"] } } include apt_update include os_tools
  45. 83 # vagrant/puppet/init.pp class apt_update { exec { "aptGetUpdate": command

    => "sudo apt-get update", path => ["/bin", "/usr/bin"] } } class os_tools { package { "curl": ensure => present, require => Exec["aptGetUpdate"] } } include apt_update include os_tools
  46. • Use (D)VCS (Git, SVN, Mercurial, etc.) • Add the

    Vagrantfile and provisioning files (puppet, chef, shell scripts, etc) to your repository • Add .vagrant folder to your repository ignore file or your global ignore file. 87
  47. • When working in multiple projects use port forwarding and

    networks carefully • Avoid port collision by adding auto_correct: true for each port forwarding • Use --debug on your provision options when something is wrong • Have guest additions always up to date • Improve share folders performance by enabling nfs (is not available on windows) • You can prevent vagrant from running provision using vagrant up --no-provision • Use vewee tool to create base boxes • If you are using Puppet modules, don't forget to add the module dependencies as well Tips and Tricks 88
  48. 90 step !ve a real world use case Demo of

    • Ubuntu 12.04 • Apache 2.2 • MySQL 5.5 • PHP 5.4 https://github.com/danielcsgomes/boost-your-dev-environment-with-vagrant/tree/step-five
  49. • Sharing your dev environment saves you time and makes

    your life easier and everyone happy • Vagrant manages the Virtual Machines using a configuration file • Default provider is Virtual Box, but VMware and AWS are available too • Vagrant can provision the VM using Puppet, Chef, Shell scripts or other tools Sum up 91
  50. RESOURCES • Documentation • Vagrant - http://docs.vagrantup.com/v2/ • Puppet -

    http://docs.puppetlabs.com/references/latest/type.html • Chef - http://docs.opscode.com/chef/index.html • Ansible - http://ansible.cc/docs/ • Vewee - https://github.com/jedi4ever/veewee • Downloads • Vagrant - http://downloads.vagrantup.com/ • Virtual Box - https://www.virtualbox.org/wiki/Downloads • VMware - http://www.vmware.com/products/player/ • Source code of the talk demos - http://goo.gl/s1WnR • List of base boxes - http://www.vagrantbox.es/ 92