Slide 1

Slide 1 text

Boost with Vagrant your dev environment Daniel Gomes @danielcsgomes June 12, 2013 europeanspaceagency © http://www.flickr.com/photos/europeanspaceagency/3704205848

Slide 2

Slide 2 text

AGENDA • Why is distributed environment a good thing • Why and what is Vagrant? • Providers • Provisioning with Puppet 2

Slide 3

Slide 3 text

3 ABOUT ME • Full Stack Web Developer @ Sedimap PT • Father, Geek • Zend Certi!ed Engineer PHP 5.3 • Certi!ed Scrum Master • @danielcsgomes

Slide 4

Slide 4 text

4 Why is distributed environment a good thing?

Slide 5

Slide 5 text

5 let's see...

Slide 6

Slide 6 text

6 http://xkcd.com/349/

Slide 7

Slide 7 text

“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

Slide 8

Slide 8 text

HOW 8 MANY TIMES DID IT HAPPEN TO YOU?! sometimes suzie ©: http://www.flickr.com/photos/22324124@N07/6186059907

Slide 9

Slide 9 text

9 I just want to code!! seamesse © http://www.#ickr.com/photos/seamesse/4722522619

Slide 10

Slide 10 text

10 What if of your co-workers? you have the same environment

Slide 11

Slide 11 text

11 What if customize and share it? you could replicate,

Slide 12

Slide 12 text

12 What if "does not work on my machine" ? you could get rid of the quote

Slide 13

Slide 13 text

13 EnterpriseMobility © http://www.#ickr.com/photos/entmobility/5140252625/ how

Slide 14

Slide 14 text

14 Distributing environment your development image source © FreeDigitalPhotos.net

Slide 15

Slide 15 text

15 with

Slide 16

Slide 16 text

http://www.vagrantup.com/ 16

Slide 17

Slide 17 text

17 Why Vagrant?

Slide 18

Slide 18 text

Is portable and easy to share 18

Slide 19

Slide 19 text

Reproducible and customizable 19

Slide 20

Slide 20 text

Virtualized and isolated 20

Slide 21

Slide 21 text

Allows you to work close to your production environment 21

Slide 22

Slide 22 text

and makes your life easier and everyone happy 22

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

use vagrant 27 how to

Slide 28

Slide 28 text

28 $ cd /path/to/my/project $ vagrant init precise64 http://files.vagrantup.com/precise64.box $ tree . └── Vagrantfile $ vagrant up $ vagrant ssh

Slide 29

Slide 29 text

29 $ cd /path/to/my/project $ vagrant init precise64 http://files.vagrantup.com/precise64.box $ tree . └── Vagrantfile $ vagrant up $ vagrant ssh

Slide 30

Slide 30 text

30 $ cd /path/to/my/project $ vagrant init precise64 http://files.vagrantup.com/precise64.box $ tree . └── Vagrantfile $ vagrant up $ vagrant ssh

Slide 31

Slide 31 text

31 $ cd /path/to/my/project $ vagrant init precise64 http://files.vagrantup.com/precise64.box $ tree . └── Vagrantfile $ vagrant up $ vagrant ssh

Slide 32

Slide 32 text

32 $ cd /path/to/my/project $ vagrant init precise64 http://files.vagrantup.com/precise64.box $ tree . └── Vagrantfile $ vagrant up $ vagrant ssh

Slide 33

Slide 33 text

33 $ cd /path/to/my/project $ vagrant init precise64 http://files.vagrantup.com/precise64.box $ tree . └── Vagrantfile $ vagrant up $ vagrant ssh

Slide 34

Slide 34 text

Demo 34 step one https://github.com/danielcsgomes/boost-your-dev-environment-with-vagrant/tree/step-one

Slide 35

Slide 35 text

Vagrantfile 35 Example of

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Demo 41 step two https://github.com/danielcsgomes/boost-your-dev-environment-with-vagrant/tree/step-two

Slide 42

Slide 42 text

vagrant init vagrant up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy 42 vagrant commands

Slide 43

Slide 43 text

43 vagrant init vagrant up vagrant halt vagrant reload vagrant provision vagrant ssh vagrant suspend vagrant resume vagrant destroy vagrant init Initializes Vagrant

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Providers 52

Slide 53

Slide 53 text

default vagrant provider http://docs.vagrantup.com/v2/virtualbox/configuration.html 53 and it's free

Slide 54

Slide 54 text

available, but’s not free http://www.vagrantup.com/vmware 54

Slide 55

Slide 55 text

and other providers are available, using vagrant plugin system http://docs.vagrantup.com/v2/plugins/providers.html 55

Slide 56

Slide 56 text

Vagrantfile configuration 56 Example of with Virtual Box custom options

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

Demo 62 step three https://github.com/danielcsgomes/boost-your-dev-environment-with-vagrant/tree/step-three

Slide 63

Slide 63 text

Provisioning with http://puppetlabs.com/ 63 http://docs.puppetlabs.com/references/latest/type.html

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Vagrant provision with puppet 65 default expected directory structure

Slide 66

Slide 66 text

66 $ tree . ├── Vagrantfile └── manifests └── default.pp

Slide 67

Slide 67 text

Vagrant with puppet 67 minimum configuration

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

Vagrant with puppet 70 custom configuration

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

Puppet manifest 79 Example of

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

Demo 84 step four https://github.com/danielcsgomes/boost-your-dev-environment-with-vagrant/tree/step-four

Slide 85

Slide 85 text

http://ansible.cc/ http://www.opscode.com/chef/ Shell scripts Ansible more provisioning tools 85

Slide 86

Slide 86 text

86 how to share your environment?

Slide 87

Slide 87 text

• 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

Slide 88

Slide 88 text

• 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

Slide 89

Slide 89 text

89 Awesome but how to use it in the real world?

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

• 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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

@danielcsgomes | [email protected] | http://danielcsgomes.com Jian Awe © http://www.#ickr.com/photos/qqjawe/6511141237 Please give feedback: http://goo.gl/3WcWw Questions?