Introduction to Vagrant

guillermo{at}guerreroibarra{dot}com

Follow me on github: https://github.com/ryanfox1985

February 17th, 2016

Why Vagrant?

  • Project dependencies:
    • Databases
    • Programming languages
  • Isolation
  • Easy to use
  • Multiplatform
  • Compatible with multiple virtualization software:
    • VirtualBox / VMware / Parallels
    • Snapshots

How it works

$ vagrant [command]

  • init
  • up / halt
  • provision
  • ssh
  • suspend / resume
  • destroy

More at vagrantup cli

Workflow

Provisioners

  • Shell
  • Puppet
  • Ansible
  • Chef
    • Chef zero
    • Berkshelf
    • Knife

Vagrant plugins

$ vagrant plugin install [name_of_plugin]

Installation

Example Vagrantfile

                    
Vagrant.configure (2) do |config|
    config.vm.box = 'ubuntu/trusty64'
    config.ssh.forward_agent = true
    config.vm.network 'forwarded_port', :guest => 80, :host => 8000

    config.vm.provider 'virtualbox' do |vb|
        vb.memory = '1024'
        vb.gui = false
    end
end
                    
                

Vagrant boxes

Atlas hashicorp

Shell provisioning

Inline:

                    
config.vm.provision 'shell', inline: <<-SHELL
  sudo apt−get update
  sudo apt−get install −y apache2
SHELL
                    
                

File:

                    
# Setup when the VM is first created
app.vm.provision 'shell', path: 'vagrant/setup.sh'

# This script is running every time we start the VM.
app.vm.provision 'shell', run: 'always', path: 'vagrant/start.sh'
                    
                

Provisioning

  • Chef
  • Berkself

Chef market

Supermarket[https://supermarket.chef.io]

Berkshelf: Vagrantfile

                  
Vagrant.configure(2) do |config|
  ...
  # Enable Berkshelf support
  config.berkshelf.enabled = true
  config.berkshelf.berksfile_path = './Berksfile'
  ...
end
                
              

Berkshelf: Berksfile

                  
source "https://supermarket.getchef.com"

cookbook 'apt'
cookbook 'git'
cookbook 'build-essential'
cookbook 'nodejs'
cookbook 'mysql'
cookbook 'rvm'
cookbook 'openssl'
cookbook 'app', path: './site-cookbooks/app'
                  
              

Chef: Vagrantfile

                  
...
config.vm.provision :chef_solo do |chef|
  chef.add recipe 'apt'
  chef.add recipe 'build−essential'
  chef.add recipe 'git'
  chef.add recipe 'nodejs'
  chef.add recipe 'app::mysql'
end
                  
              
Note: Recipe order is important.

Berkshelf: site-cookbooks

                    
site-cookbooks/app
  attributes
    list_of_attributes.rb
  recipes
    list_of_recipies.rb
  templates
    defaults_files
  Berksfile
  metadata.rb
                    
                

Berkshelf: example recipie

                    
mysql_service 'default' do
  port '3306'
  version '5.5'
  initial_root_password 'put_here_a_password'
  action :create
end
                    
                

An example

[Link to github] An example that sets up a Ruby on Rails development box based on Ubuntu with RVM, Ruby 2.1.0, MySQL 5.5.

For noobs

Coobooks are very unstable.
You need a good computer with Ram.

Vagrant with docker

Multimedia services

[Link to github]: A box with Btsync, DYNDNS, SickRage, Couch potato and Deluge

Inspired in Freenas

Vagrantfile

                
Vagrant.configure(2) do |config|
  ...

  # Ubuntu
  config.vm.box = 'ubuntu/trusty64'

  # Install latest docker
  config.vm.provision 'docker'

  # Setup the containers when the VM is first created
  config.vm.provision 'shell', path: 'vagrant/setup.sh'

  # Make sure the correct containers are running
  # every time we start the VM.
  config.vm.provision 'shell', run: 'always', path: 'vagrant/start.sh'
end
                
            

Setup.sh

                
# Stop and remove any existing containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

# Run and link the containers
docker run -d -v /vagrant/ddclient/etc:/etc/ddclient --name dynamicdns \
  ryanfox1985/docker-dynamicdns
docker run -d -p 8888:8888 -v /vagrant/ddclient/etc:/etc/ddclient \
  --name sync bittorrent/sync
...
              
            

Start.sh

                
# Commands required to ensure correct docker containers are started when the vm is rebooted.

docker start dynamicdns
docker start sync
docker start sickrage
docker start couchpotato
docker start deluge
              
            

Google trends: Vagrant

Questions?