Vagrant

Gestion harmonisée des environnements de développement


Human Talks, Grenoble - Juin 2013

Damien Lelièvre | daml.fr | @daml

Vagrant

  • Projet débuté en janvier 2010 / Version actuelle 1.2.2
  • Par Mitchell Hashimoto / @mitchellh
  • Licence MIT
  • Support commercial disponible ... si ça rassure :p
  • Distribution : Mac OS (dmg), Windows (msi), Linux 32 & 64 (deb, rpm)
  • Utilisé chez : Disqus, BBC, Mozilla, Expedia, et un paquet d'autres

Le problème ?

Comprends pas, ça marchait sur ma machine ...
Oups le projet P du client C il utilise Python 2 et j'ai passé mon environnement en Python 3 depuis 18 mois.
Pour lancer les tests ? make test ! Sous Windows ?
Le stagiaire, faudrait lui installer git, ruby, capistrano, apache, php, composer, mysql, couchdb, node.js, npm, etc, etc, etc ...

Le besoin

  • Un environnement de développement semblable à la production
  • Partagé par toute l'équipe
  • Permettant de passer facilement d'un projet à un autre
  • Laissant aux développeurs le choix de leurs outils favoris (OS, éditeurs, etc)
  • Suivant l'évolution du projet et de ses différentes branches
  • Facile à organiser et à maintenir

La réponse

  • Un seul fichier ou presque
  • Versionné avec le projet
  • Contenant la configuration d'une machine virtuelle
  • + la configuration de l'environnement
  • + la configuration du projet
  • => Vagrant !

Un fichier : Vagrantfile

Le minimum


Vagrant.configure("2") do |config|
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"
end
    

Un fichier : Vagrantfile

Configuration de la machine virtuelle


Vagrant.configure("2") do |config|
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"
  config.vm.hostname = "projb-dev"
  config.vm.synced_folder ".", "/home/vagrant/PROJB"
  config.vm.network :forwarded_port, guest: 3842, host: 3842
end
    

Un fichier : Vagrantfile

Configuration de l'environnement


$script = <<SCRIPT
# Install node.js v0.10.10
wget -q -O /home/vagrant/node-v0.10.10-linux-x86.tar.gz "http://nodejs.org/dist/v0.10.10/node-v0.10.10-linux-x86.tar.gz"
tar zxf /home/vagrant/node-v0.10.10-linux-x86.tar.gz
sudo ln -sf /home/vagrant/node-v0.10.10-linux-x86/bin/node /usr/local/bin/node
sudo ln -sf /home/vagrant/node-v0.10.10-linux-x86/bin/npm /usr/local/bin/npm
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"
  config.vm.hostname = "projb-dev"
  config.vm.synced_folder ".", "/home/vagrant/PROJB"
  config.vm.network :forwarded_port, guest: 3842, host: 3842
  config.vm.provision :shell, :inline => $script
end
    

Et donc ?

$> vagrant up [enter]

Un peu plus loin ?

Configuration réseau


Vagrant.configure("2") do |config|
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  config.vm.network :private_network, ip: "10.25.101.10"
end
    

Un peu plus loin ?

Machines multiples

aka exploser la machine hôte

Vagrant.configure("2") do |config|
  config.vm.define :web do |web|
    web.vm.box = "precise32"
    web.vm.box_url = "http://files.vagrantup.com/precise32.box"
    web.vm.network :private_network, ip: "10.25.101.10"
  end

  config.vm.define :db do |db|
    db.vm.box = "precise32"
    db.vm.box_url = "http://files.vagrantup.com/precise32.box"
    db.vm.network :private_network, ip: "10.25.101.11"
  end
end
    

Un peu plus loin ?

Provisionnement avec Puppet (1/2)


Vagrant.configure("2") do |config|
  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"

  config.vm.network :private_network, ip: "10.25.101.10"

  config.vm.synced_folder "./", "/var/www"

  config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "manifests"
    puppet.options = ['--verbose']
  end
end
    

Un peu plus loin ?

Provisionnement avec Puppet (2/2)


exec { 'apt-get update':
  command => 'apt-get update',
  path    => '/usr/bin/',
  timeout => 60,
  tries   => 3,
}

package { ['apache2', 'php5', 'make', 'php-pear']:
    ensure => installed,
    require => Exec['apt-get update'],
}

exec { 'phpcs':
    command => 'pear install PHP_CodeSniffer',
    path    => '/usr/bin',
    require => Package['php-pear'],
    creates => '/usr/bin/phpcs',
}
    

Un peu plus loin ?

Démo

Les commandes

  • Lancer une VM : vagrant up
  • S'y connecter : vagrant ssh
  • Recharger le Vagrantfile : vagrant reload
  • Reprovisionner la VM : vagrant provision
  • Suspendre une VM : vagrant suspend
  • Arreter une VM : vagrant halt
  • Détruire une VM : vagrant destroy

J'en veux plus !

  • Je préfère Chef ou Puppet ? OK !
  • Je suis un flemmard n'aime pas réinventer la roue ? rubygems.org & vagrant plugin install
  • J'ai besoin d'environnements plus complexes ? OK, créez vos boxs !
  • Changer de provider en remplaçant VirtualBox par VMware ? OK ! Par AWS ? OK !
  • ... suivez le lapin blanc la doc !

Questions ?



Disponible sur daml.fr/presentations/vagrant.htm
Présentation réalisée avec l'excellent reveal.js