Vagrant

Entorns de desenvolupament fàcils

per Alex Muntada / @alexmuntada

cc-by-sa

Instal·lació

Dependències

  • Una versió recent de VirtualBox
  • Opcionalment les Guest Additions

Descàrrega

  • Paquets a la web oficial
  • Repositoris de paquets a les distribucions
  • Codi font a GitHub
  • Gema a RubyGems

Inici

vagrant init


$ vagrant init ubuntu/trusty64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
						

vagrant up


$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: A newer version of the box 'ubuntu/trusty64' is available! You currently
==> default: have version '20150609.0.7'. The latest is version '20150911.0.0'. Run
==> default: `vagrant box update` to update.
==> default: Setting the name of the VM: sfd-vagrant-intro_default_1442511341137_19810
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /scratch/alexm/git/sfd-vagrant-intro
						

vagrant status


$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
						

vagrant reload


$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: A newer version of the box 'ubuntu/trusty64' is available! You currently
==> default: have version '20150609.0.7'. The latest is version '20150911.0.0'. Run
==> default: `vagrant box update` to update.
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /scratch/alexm/git/sfd-vagrant-intro
						

vagrant ssh


$ vagrant ssh
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-54-generic x86_64)
...
vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt update
...
vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt install apache2
...
						

$ vagrant ssh -c uptime
 17:39:50 up 4 min,  1 user,  load average: 0.04, 0.12, 0.06
					

Configuració

Caixa


Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  # config.vm.box = "boxname"
  # config.vm.box_url = "http://www.example.com/name.box"
end
						

Proveïdor


Vagrant.configure(2) do |config|
  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.memory = "1024"
  end
end
						

Carpetes


Vagrant.configure(2) do |config|
  config.vm.synced_folder "../data", "/vagrant_data"
end
						

Provisors

  • file
  • shell
  • Ansible
  • CFEngine
  • Chef
  • Docker
  • Puppet
  • Salt

shell


Vagrant::Config.run do |config|
  config.vm.provision :shell, path: "provision.sh"
end
						

provision.sh


#!/bin/sh

set -e

sed -i 's,http:[^ ]*,mirror://mirrors.ubuntu.com/mirrors.txt,' /etc/apt/sources.list
export DEBIAN_FRONTEND=noninteractive
apt update
apt install -yy apache2

a2enmod cgi
service apache2 restart
						

vagrant provision


$ vagrant provision
==> default: Running provisioner: shell...
    default: Running: /tmp/vagrant-shell20150917-7996-uhr3lo.sh
==> default: stdin: is not a tty
==> default: WARNING: 
==> default: apt
==> default:  
==> default: does not have a stable CLI interface yet. 
==> default: Use with caution in scripts.
==> default: Ign http://ubuntu.uc3m.es/ubuntu/ trusty InRelease
==> default: Ign http://ubuntu.uc3m.es/ubuntu/ trusty-updates InRelease
==> default: Ign http://ubuntu.uc3m.es/ubuntu/ trusty-security InRelease
==> default: Get:1 http://ubuntu.uc3m.es/ubuntu/ trusty Release.gpg [933 B]
==> default: Get:2 http://ubuntu.uc3m.es/ubuntu/ trusty-updates Release.gpg [933 B]
==> default: Get:3 http://ubuntu.uc3m.es/ubuntu/ trusty-security Release.gpg [933 B]
==> default: Get:4 http://ubuntu.uc3m.es/ubuntu/ trusty Release [58.5 kB]
==> default: Get:5 http://ubuntu.uc3m.es/ubuntu/ trusty-updates Release [63.5 kB]
==> default: Get:6 http://ubuntu.uc3m.es/ubuntu/ trusty-security Release [63.5 kB]
==> default: Get:7 http://ubuntu.uc3m.es/ubuntu/ trusty/main Sources [1,064 kB]
==> default: Get:8 http://ubuntu.uc3m.es/ubuntu/ trusty/universe Sources [6,399 kB]
==> default: Get:9 http://ubuntu.uc3m.es/ubuntu/ trusty/main amd64 Packages [1,350 kB]
==> default: Get:10 http://ubuntu.uc3m.es/ubuntu/ trusty/universe amd64 Packages [5,859 kB]
==> default: Get:11 http://ubuntu.uc3m.es/ubuntu/ trusty/main Translation-en [762 kB]
==> default: Get:12 http://ubuntu.uc3m.es/ubuntu/ trusty/universe Translation-en [4,089 kB]
==> default: Get:13 http://ubuntu.uc3m.es/ubuntu/ trusty-updates/main Sources [228 kB]
==> default: Get:14 http://ubuntu.uc3m.es/ubuntu/ trusty-updates/universe Sources [131 kB]
==> default: Get:15 http://ubuntu.uc3m.es/ubuntu/ trusty-updates/main amd64 Packages [599 kB]
==> default: Get:16 http://ubuntu.uc3m.es/ubuntu/ trusty-updates/universe amd64 Packages [302 kB]
==> default: Get:17 http://ubuntu.uc3m.es/ubuntu/ trusty-updates/main Translation-en [288 kB]
==> default: Get:18 http://ubuntu.uc3m.es/ubuntu/ trusty-updates/universe Translation-en [160 kB]
==> default: Get:19 http://ubuntu.uc3m.es/ubuntu/ trusty-security/main Sources [91.1 kB]
==> default: Get:20 http://ubuntu.uc3m.es/ubuntu/ trusty-security/universe Sources [29.0 kB]
==> default: Get:21 http://ubuntu.uc3m.es/ubuntu/ trusty-security/main amd64 Packages [326 kB]
==> default: Get:22 http://ubuntu.uc3m.es/ubuntu/ trusty-security/universe amd64 Packages [113 kB]
==> default: Get:23 http://ubuntu.uc3m.es/ubuntu/ trusty-security/main Translation-en [178 kB]
==> default: Get:24 http://ubuntu.uc3m.es/ubuntu/ trusty-security/universe Translation-en [65.9 kB]
==> default: Ign http://ubuntu.uc3m.es/ubuntu/ trusty/main Translation-en_US
==> default: Ign http://ubuntu.uc3m.es/ubuntu/ trusty/universe Translation-en_US
==> default: Fetched 22.2 MB in 18s (1,173 kB/s)
==> default: Reading package lists...
==> default: WARNING: 
==> default: apt
==> default:  
==> default: does not have a stable CLI interface yet. 
==> default: Use with caution in scripts.
==> default: Reading package lists...
==> default: Building dependency tree...
==> default: Reading state information...
==> default: apache2 is already the newest version.
==> default: 0 upgraded, 0 newly installed, 0 to remove and 84 not upgraded.
==> default: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppress this message
==> default: Your MPM seems to be threaded. Selecting cgid instead of cgi.
==> default: Enabling module cgid.
==> default: To activate the new configuration, you need to run:
==> default:   service apache2 restart
==> default:  * Restarting web server apache2
==> default: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppress this message
==> default:    ...done.
						

Xarxa

  • Redirecció de ports
  • Xarxa privada
  • Multimàquina
  • Xarxa pública

Redirecció de ports


Vagrant::Config.run do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080
end
						

Xarxa privada


Vagrant::Config.run do |config|
  config.vm.network "private_network", type: "dhcp"
  config.vm.network "private_network", ip: "192.168.33.10"
end
						

Multimàquina


Vagrant.configure(2) do |config|
  config.vm.define "client" do |c|
    c.vm.hostname = "client"
    c.vm.network :private_network, ip: "10.10.10.2"
  end
  config.vm.define "server" do |s|
    s.vm.hostname = "server"
    s.vm.network :private_network, ip: "10.10.10.3"
  end
end
						

Xarxa pública

  • Poc habitual i els detalls depenen del proveïdor.
  • En el futur segurament serà substituïda per ponts.

Caixes

vagrant package


$ vagrant package
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /scratch/alexm/git/sfd-vagrant-intro/package.box
						

vagrant box


$ vagrant box list
ubuntu/trusty64      (virtualbox, 14.04)
						

$ vagrant box add package.box --name sfd2015
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'sfd2015' (v0) for provider: 
    box: Unpacking necessary files from: file:///scratch/alexm/git/sfd-vagrant-intro/package.box
==> box: Successfully added box 'sfd2015' (v0) for 'virtualbox'!
						

$ vagrant box list
sfd2015              (virtualbox, 0)
ubuntu/trusty64      (virtualbox, 14.04)
						

Extres

Proveïdors

  • VirtualBox (predeterminat)
  • VMware
  • Docker
  • Hyper-V
  • Personalitzat

Connectors

  • Ordres
  • Configuració
  • Tipus de màquines virtuals
  • Prestacions de les màquines virtuals
  • Tipus d'amfitrions
  • Prestacions dels amfitrions
  • Proveïdors (p.e. AWS)
  • Provisors

Push

Permet posar en producció l'entorn de desenvolupament copiant els fitxers a un servidor remot utilitzant FTP/SFTP o d'altres mecanismes.

Exemples

debian-perl-vagrant

Entorn de desenvolupament i test per als paquets de Perl a Debian:

  • Debian sid (unstable)
  • Entorn de pkg-perl
  • Guest Additions per vboxdrv

Act-vagrant

Entorn de desenvolupament i test per a les webs dels esdeveniments de Perl:

  • Ubuntu 6.06 (lucid) 32 bits
  • apache_1.3.42
  • mod_perl-1.31
  • libapreq-1.34
  • postgresql

gitlab-vagrant

Servidor de repositoris de codi à la GitHub.

domjudge-vagrant

Jutge automàtic per a concursos de programació.

Conclusions

Vagrant

  • és molt fàcil d'utilitzar per fer proves ràpides,
  • és prou potent per a configurar escenaris complexos,
  • és molt útil per crear entorns de prova i
  • permet compartir fàcilment els entorns.

Preguntes?

per Alex Muntada / @alexmuntada