Virtualize your dev workflow with Vagrant

Post on 22-Jan-2018

953 views 3 download

Transcript of Virtualize your dev workflow with Vagrant

Virtually improve your dev workflow with Vagrant

Justin Filip - @jfilip

boltmade.com

CC BY-SA 3.0 Justin Filip 1

What even is Vagrant?

Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant

lowers development environment setup time, increases development/production parity, and makes the "works on my

machine" excuse a relic of the past.

-- official Vagrant docs

CC BY-SA 3.0 Justin Filip 2

Supported environments

• The Vagrant application can be run inside the following host environments:

• Windows / OS X / Linux (both DEB and RPM packages available)

• both 32 and 64 bit versions of each OS are supported

More or less any VM that can run in any of the supported virtualization providers will run with Vagrant, provided you have a compatible .box file.

CC BY-SA 3.0 Justin Filip 3

What's a .box file?

Some Vagrant concepts we will learn today:

• Boxes

• Provisioning

• Networking

• Synced Folders

• Providers

• Plugins

CC BY-SA 3.0 Justin Filip 4

Boxes

Basically a specially packaged VM image with some extra metadata information contained inside to help control Vagrant.

CC BY-SA 3.0 Justin Filip 5

Provisioning

Steps that are run when the VM is initially created and can, optionally, be re-run afterward. Supported provisioners:

• file uploads, inline shell scripts and files

• Ansible, Chef, Puppet, Salt

• ... and more!

CC BY-SA 3.0 Justin Filip 6

Networking

Allows for easy configuration of the network interfaces running inside the VM. Makes it easy to configure:

• private internal IP addresses

• port forwarding

• connecting to public networks

CC BY-SA 3.0 Justin Filip 7

Synced Folders

Allows you to get folders from your machine into the Vagrant VM. Supported methods (host OS dependent):

• NFS

• RSync

• SMB

• provider-specific shared folders (plugin dependent)

CC BY-SA 3.0 Justin Filip 8

Providers

The software runs the VM, including:

• VirtualBox

• VMWare

• Docker

• Hyper-V

• Parallels (with a plugin)

• Others, see wiki page

CC BY-SA 3.0 Justin Filip 9

Plugins

• Large list of available plugins in the Vagrant wiki

• Some examples I use:

• vagrant-vbguest -- automatically update VirtualBox guest additions if necessary

• vagrant-cachier -- caches packages for different managers like apt, yum, Rubygems, NPM, Bower, etc.

• vagrant-parallels -- allows running guests with Parallels on OS X

CC BY-SA 3.0 Justin Filip 10

Extras

Tools that are used to build Vagrant environments for you

• https://github.com/jedi4ever/veewee

• https://puphpet.com/

Running OS X guest VMs with Vagrant:

• https://github.com/AndrewDryga/vagrant-box-osx

• https://github.com/radeksimko/vagrant-osx

CC BY-SA 3.0 Justin Filip 11

Conventions for this workshop

Commands:

# Commands run in your native OS, prefixed with '$'$ vagrant up$ ls -aFh

# Commands run inside Vagrant VM, prefixed with '>'> uname -a> sudo apt-get update

CC BY-SA 3.0 Justin Filip 12

Requirements for this workshop

Required software (for your specific operating system):

• VirtualBox -- https://virtualbox.org/

• Vagrant -- https://vagrantup.com/

• GitBash -- https://git-scm.com/download/ (for Windows users only)

CC BY-SA 3.0 Justin Filip 13

Our goal for today

We are going to create a Linux environment for doing Ruby on Rails development:

1. Use Ubuntu Linux 14.04 (Trusty) running on VirtualBox (64 bit or 32 bit depending on if whether you are running a 32bit / 64bit OS)

2. Use a simple, repeatable provisioning process to setup our environment

3. Install Ruby 2.2.3 and Postgres 9.3

4. Get a default Rails application up and running

CC BY-SA 3.0 Justin Filip 14

Let's Rock

CC BY-SA 3.0 Justin Filip 15

Step 1: Find a .box to start with

We are going to make our own custom environment so we will pick a base box image that doesn't really have any extras pre-installed.

We will use the Atlas box search feature to find a base Ubuntu box. In your browser, go to:

• https://vagrantcloud.com/boxes/search

• search for "ubuntu trusty64"

• optionally click the virtualbox provider filter to narrow the search

CC BY-SA 3.0 Justin Filip 16

Step 2: Initialize our Vagrantfile

We want to create a default machine just to make sure our environment is working at all. In your command terminal (GitBash for Windows users), enter the following commands:

$ mkdir vagrant-workshop$ cd vagrant-workshop/$ vagrant init ubuntu/trusty64$ vagrant up --provider=virtualbox

CC BY-SA 3.0 Justin Filip 17

Step 3: Make sure everything is working fine

$ vagrant ssh

You should see a default Ubuntu login message.

You are now logged into your Vagrant VM. Enter the following:

> uname -aLinux vagrant-ubuntu-trusty-64 3.13.0-65-generic#106-Ubuntu SMP Fri Oct 2 22:08:27 UTC 2015x86_64 x86_64 x86_64 GNU/Linux

CC BY-SA 3.0 Justin Filip 18

Step 4: Start to customize our VM

Open your Vagrantfile with your favourite editor. Below the config.vm.box = "ubuntu/trusty64" line, add the following:

# ruby22.shconfig.vm.provision 'shell', path: 'http://bit.ly/1GDfJrY'# postgres9.shconfig.vm.provision 'shell', path: 'http://bit.ly/1R5ZEAy'

Save the file and run (from your host OS):

> exit

$ vagrant provision

CC BY-SA 3.0 Justin Filip 19

Step 5: Name the VM and setup our shared folder

Edit your Vagrantfile to add the following lines below what you added in Step 4:

config.vm.hostname = 'rails-getting-started'config.vm.synced_folder '.', '/vagrant'

Save the file and run (from your host OS):

$ vagrant reload

CC BY-SA 3.0 Justin Filip 20

Step 6: Redirect to shared folder on login

Edit your Vagrantfile to add the following lines below what you added in:

config.vm.provision 'shell', inline: 'echo -e "\ncd /vagrant" >> /home/vagrant/.bashrc'

Restart the VM, login and check where we end up:

$ vagrant provision$ vagrant ssh

> pwd/vagrant

CC BY-SA 3.0 Justin Filip 21

Step 7: Install Ruby on Rails

Create a new file Gemfile and enter the following inside of it:

source 'https://rubygems.org'ruby '2.2.3'

gem 'rails', '4.2.4'gem 'pg'

Inside of the VM, run:

> bundle install --path vendor/bundle

CC BY-SA 3.0 Justin Filip 22

Step 8: Create a new Rails application

Inside the VM:

> bundle exec rails new blog -d postgresql

Edit the file blog/Gemfile with your editor, uncomment the following line and save the file:

# gem 'therubyracer', platforms: :ruby

CC BY-SA 3.0 Justin Filip 23

Step 9: Install packages and setup the database

Execute the following in the VM:

> cd blog/> bundle install --path vendor/bundle> bundle exec rake db:create db:migrate

CC BY-SA 3.0 Justin Filip 24

Step 10: Start the rails server

> bundle exec rails s

Great! Everything looks good here, we're probably done so let's go to the URL that the server says it is running on:

• http://localhost:3000/

CC BY-SA 3.0 Justin Filip 25

Why didn't this work?

CC BY-SA 3.0 Justin Filip 26

Step 11: Forward a port into the VM

We need to forward a port into the VM from our host so we can access it in the browser. Edit your Vagrantfile and add the following line:

config.vm.network 'forwarded_port', guest: 3000, host: 3000

And then run:

$ vagrant reload

CC BY-SA 3.0 Justin Filip 27

Step 12: Start the rails server (again)

$ vagrant ssh

> cd blog/> bundle exec rails s -b 0.0.0.0

Now, load the site up in your browser:

• http://localhost:3000/

CC BY-SA 3.0 Justin Filip 28

Success?

CC BY-SA 3.0 Justin Filip 29

(Un-)Fuck up the VM 1

$ vagrant ssh

> # Dangerous commands hidden to prevent disasters> exit

$ vagrant reload

Whoops. The SSH command isn't responding. We messed up the VM and prevented it from being bootable. That's bad.

CC BY-SA 3.0 Justin Filip 30

(Un-)Fuck up the VM 2

Fortunately our code lives outside of the VM so we can just recreate it from scratch without loosing any work!

$ vagrant destroy -f$ vagrant up$ vagrant ssh

> cd blog/> bundle install --path vendor/bundle> bundle exec db:create db:migrate> bundle exec rails s -b 0.0.0.0

CC BY-SA 3.0 Justin Filip 31

VM performance tuning

We are running a database server and a web server at the same time inside our VM. It might be nice to use two CPUs to do this.

Edit your Vagrantfile to add:

config.vm.provider 'virtualbox' do |vb| vb.cpus = 2 vb.memory = 1024end

And run:

$ vagrant reload

CC BY-SA 3.0 Justin Filip 32

Want to continue?

Now that you have a Rails dev environment setup and configured, you can follow along with the Rails Getting Started Guide, if you wish:

• http://guides.rubyonrails.org/getting_started.html

CC BY-SA 3.0 Justin Filip 33

Resources

• Official Vagrant site

• Vagrant Support

• Atlas Vagrant Box Search

CC BY-SA 3.0 Justin Filip 34

Thanks!

Slides available at -- http://bit.ly/1VOzCmI

Code available at -- http://bit.ly/1Mm3hyG

CC BY-SA 3.0 Justin Filip 35