CSC/ECE 517 Spring 2015/ch1a 1 DZ

From Expertiza_Wiki
Jump to navigation Jump to search

Vagrant

Vagrant is a computer software that helps create, configure and manage virtual development or work environments. With its great portability and compatibility, team members can share a common development environment. Therefore minimize the cost of time for deployment and maximize the productivity and flexibility.

Background

Vagrant works with mainstream virtualization softwares like VirtualBox, VMware and AWS or any other provider.

Virtualization Machine

Virtualization machine, which is created by virtualization software, is an emulation of a real computer that provides a complete operating system. In other words, a virtual machine(VM) is a computer with a complete set of hardwares and operating system which runs in your own operating system. In that way, for example, one using Windows operating system can easily install Linux operating system in the VM rather than actually installing it on local machine. Therefore, any changes or modifications in the VM will not propagate to the operating system on which it runs.

Vagrant Box

Boxes are the package format for Vagrant environments. In other words, a box is a packed operating system used by Vagrant. vagrant box is the utility that downloads and manages different types of boxes.
vagrant box add {title} {url} can be executed under either Windows and Unix operating system to add a new box to Vagrant. title is a identifier and can be any text, and url is the network location of the box.
For example:
vagrant box add base hashicorp/precise64 downloads a standard Ubuntu 12.04 LTS 64-bit box from Vagrant Cloud.
vagrant box add base https://dl.dropboxusercontent.com/u/29173892/vagrant-boxes/debian7.3.0-vbox4.3.6-puppet3.4.1.box downloads a Debian 7.3.0 64-bit Puppet 3.4.1 box which is shared by personal user.

Vagrantfile

Vagrantfile is a configuration file which exists in every single vagrant folder. It is written in Ruby, probably looks like this:

Vagrant.configure(2) do |config|
    config.vm.box = "hashicorp/precise32"
    # other configuration here
end

The "2" in the first line is the version of configuration object config. This number is either "1" for Vagrant 1.0.x, or "2" for Vagrant 1.1+.
This file allows you to configure some basic settings (including shared folder location, cpu core number and etc.) of the virtual machine. For example:

config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus="2"
    config.vm.network :forwarded_port, host: 8000, guest: 80
    config.vm.network :forwarded_port, host: 33060, guest: 33060
end

This section of code specifies the memory and CPU core number of the virtual machine. Also, any network traffic on localhost:8000 and localhost:33060 will be forwarded to port 80 and 33060, respectively.
Some more examples:

config.vm.synced_folder "src/", "/srv/website"

This command is used to sync folder. The first parameter is a path to a directory on the host machine, the second parameter must be an absolute path of where to share the folder within the guest machine. By default, synced folders are mounted with the SSH user owner/group setting. You can mount them with a different owner and group using the configuration below:

config.vm.synced_folder "src/", "/srv/website"
  owner: "root", group: "root"

Provision

Provision in Vagrant is a process of automatically install your own softwares and modify configurations during the VM booting up.
Also, this process needs no human interaction and therefore makes it very convenient and repeatable.
Every provisioner is configured within your Vagrantfile using the config.vm.provision method call, for example:

config.vm.provider "virtualbox" do |vb|
  config.vm.provision :shell, path: "bootstrap.sh"
end

This section of code enables bootstrap.sh every time the VM is initialized or reloaded.
Typically, provisioners are only run once, during the first vagrant up since the last vagrant destroy. If you want to run the provisioners on every vagrant up, simply set the run option to "always", as shown below:

config.vm.provider "virtualbox" do |vb|
  config.vm.provision :shell, path: "bootstrap.sh"
    run: "always"
end

How it works

Set up

Step 1: Install a mainstream VM software. (e.g. VirtualBox Download Page)
Step 2: Download Vagrant

Configure

Step 1: vagrant box add base hashicorp/precise32 to add a Ubuntu 32bit base box
The title "base" can be replaced with any text, also "hashicorp/precise32" is replaceable.
Step 2: vagrant init
If the title of the box is not "base", then you will need to specify the title of the box. For example, vagrant init "my own box"

Work

Step 1: vagrant up or vagrant up --provision if you want to run the VM in provision mode.
Step 2: vagrant ssh to login to the virtual machine.

References