mercredi 4 décembre 2013

Créer son environnement de développement : vagrant

Cette série d'articles à pour but de décrire avec précision, la création d'un environnement de développement PHP sur plateforme Windows.

Ce premier article décrit l'installation d'un environnement virtualisé avec Vagrant.
D'abord, il faut télécharger et installer VirtualBox, puis faire de même avec Vagrant.
Pour mon environnement, j'ai décidé de partir sur une Debian Wheezy. Pour cela, téléchargez cette box : debian-70rc1-x64-vbox4210.box

Comme, j'ai deux disques durs, j'installe mon environnement sur mon lecteur "D:". Du coup, j'ai créé l'arborescence suivante : D:\vagrant\vm.
Dans ce répertoire, j'ai créé deux répertoires :

  • boxes : pour y mettre les boxes Vagrant que je télécharge (Vagrant peut le faire pour moi, mais comme j'ai un mauvais débit, et que je pense devoir faire plusieurs essais, j'ai choisi cette technique).
  • wheezy : pour y installer ma VM (Virtual Machine) de développement.
Maintenant, on ouvre un interpréteur de commandes Windows (touche "windows" + cmd.exe), et on installe le plugin de snapshot pour créer des images de la VM afin de pouvoir revenir en arrière et faire des essais d'installation.
C:\Users\Frederic>vagrant plugin install vagrant-vbox-snapshot
Installing the 'vagrant-vbox-snapshot' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbox-snapshot (0.0.4)'!
Je me déplace dans le répertoire wheezy et j'initialise ma VM
C:\Users\Frederic>d:
D:\>cd vagrant\vm\wheezy
D:\vagrant\vm\wheezy>vagrant init
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.
A ce stade, un fichier "Vagrantfile" est créé dans le répertoire "wheezy". Je l'édite (Notepad++ par exemple), j'efface tout le contenu pour y mettre : 
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "developpement"
  config.vm.box_url = "D:\\vagrant\\vm\\boxes\\debian-70rc1-x64-vbox4210.box"
  config.vm.network :private_network, ip: "192.168.56.2"
end
Enfin, je lance la VM:
D:\vagrant\vm\wheezy>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'developpement'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...
[default] Machine booted and ready!
[default] The guest additions on this VM do not match the installed version
of VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.


Guest Additions Version: 4.2.10
VirtualBox Version: 4.3
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

Désormais, j'ai une VM qui fonctionne, je vais faire une photo, histoire de pouvoir revenir dans cette état initial :
D:\vagrant\vm\wheezy>vagrant snapshot take initial
Taking snapshot initial
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Pour se connecter à la VM, on peut utiliser "vagrant ssh", seulement, l'invite de commandes Windows n'est vraiment pas pratique. Du coup, j'ai installé putty, et je me connecte à la VM comme ceci (touche windows) : 
putty 192.168.56.2
login as: vagrant
vagrant@192.168.56.2's password: 
Linux debian-70rc1-x64-vbox4210 3.2.0-4-amd64 #1 SMP Debian 3.2.35-2 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

Last login: Mon Apr  8 21:39:23 2013 from 10.0.2.2
vagrant@debian-70rc1-x64-vbox4210:~$
(mot de passe : "vagrant")

Je me déconnecte et j'arrête la VM :
D:\vagrant\vm\wheezy>vagrant halt
[default] Attempting graceful shutdown of VM...

Voilà !

Dans l'article suivant, on va voir l'utilisation de puppet pour configurer la VM.

Aucun commentaire:

Enregistrer un commentaire