Sunday, January 27, 2019

Vagrant Basics Presentation Notes

What is Vagrant?

Vagrant is a tool for building complete development environments, sandboxed in a virtual machine. Vagrant lowers development environment setup time, increases de‐ velopment/production parity, and brings the idea of disposable compute resources down to the desktop.

- Create a virtual machine 
- Modify RAM, number of cps etc of the virtual machine
- Setup shared folders
- Boot virtual machine
- Provision software on the machine via a shell script, Chef, Puppet etc

Manage Lifecycle of the Machine

- SSH into the machine
- Shut down the machine
- Destroy the machine (delete all virtual hard drive)
- Suspend or resume the machine
- Package the machine state and distribute it to other developers

Why Vagrant?

- Installations can be difficult
- Configuration is even more difficult
- Manual setups result in difference between development and production
- Multiple projects become very difficult (different configurations)
- Difficult to keep development environments in sync for everyone in the team

Vagrant encourages automation to setup the development environment. Vagrant puts your development environment into a virtual machine, working with multiple projects is easy, because each project just get its own virtual machine. Finally, working with a team is easier than ever, since you can share the virtual machine image. And bringing a new team member on board is as simple as telling them to build their Vagrant machine with a single command.

Setting up Vagrant

1. Install VirtualBox
2. Install Vagrant (Installers for various platforms)

First Vagrant Machine


vagrant up

vagrant ssh

vagrant destroy

The Vagrantfile

mkdir vagrant_ex
cd vagrant_ex


[Vagrantfile for Vagrant 2 goes here]

Boxes

Vagrant::Config.run do |config|
  config.vm.box = "precise64"
end

vagrant up

vagrant status

vagrant ssh

By default, Vagrant shares the project directory (the directory with the Vagrantfile) to /vagrant inside the virtual machine. After SSHing into the virtual machine, this can be verified by listing the files in that directory:

vagrant@precise64:~$ ls /vagrant/
    Vagrantfile
You can over-ride the shared folder:

config.vm.share_folder "v-root", "/foo", "."

First,anidentifierforthesharedfolder.Inthiscase,byspecifyingv-root,thedefault shared folder that Vagrant sets up is overridden.

Next,/fooisthepathwherethefolderwillexistintheguestmachine.Thispathwill be created if it doesn’t already exist. If it does already exist, the location will be replaced with the contents of the shared folder.

• Thethirdparameter,"."isthepathofthefoldertobesharedfromthehostmachine. This can be an absolute or relative path. If it is relative, like the example, it is relative to the project root. So in the example, it is sharing the project root.

vagrant reload

Restarts the machine with the new configuration

Basic Networking

config.vm.forward_port 80, 8080

vagrant reload

vagrant ssh
cd /vagrant
sudo python -m SimpleHTTPServer 80

vagrant suspend
vagrant status

vagrant up
vagrant halt

vagrant halt --force
vagrant up

vagrant destroy
vagrant status

vagrant destroy --force
vagrant up

Provisioning Vagrant VM

Create a provision.sh

#!/usr/bin/env bash
echo "Installing Apache and setting it up..."
apt-get update >/dev/null 2>&1
apt-get install -y apache2 >/dev/null 2>&1
rm -rf /var/www
ln -fs /vagrant /var/www

Add this line to Vagrantfile
    config.vm.provision "shell", path: "provision.sh"

Vagrant::Config.run do |config|
  config.vm.box = "precise64" config.vm.forward_port 80, 8080 config.vm.provision "shell", path: "provision.sh"
end

vagrant up

Boxes

Boxes are the base images upon which Vagrant environments are built. 

Boxes are an optimization so that Vagrant doesn’t have to install a complete operating system on every vagrant up. Installing an operating system from scratch generally takes up to 30 minutes on a good computer.

    $ vagrant box add precise64 http://files.vagrantup.com/precise64.box
$ vagrant box list
    $ vagrant box remove precise64 virtualbox