CC BY-SA 3.0 Justin Filip
- https://creativecommons.org/licenses/by-sa/3.0/
Code resources: https://github.com/jfilip/vagrant-workshop
Have you ever heard or used the phrase “well, it worked on my machine”? Have you ever tried to replicate your production environment locally and found it to be incredibly difficult, hard to accurately reproduce or completely impossible? Are you trying to do anything at all on a Windows machine?
Vagrant is probably the answer to all of your problems. Whether you are developing by yourself, working with a team, or pushing to a very specific production environment, Vagrant can help you build repeatable, reliable virtual machines in Windows, Mac OS and Linux dev environments.
We will be using Vagrant to create a Ruby development environment that will allow us to create a sample Rails application running on a PostgreSQL database backend. We will touch on topics such as:
• box discovery and providers
• machine provisioning
• plugins
• deployment
This workshop is suited for developers and people involved in deployment and release management. Designers who work in an existing code base for applications in development could also benefit from learning how to create a contained, sane environment to work in.
No coding or Virtual Machine experience is required but it will help with understanding advanced concepts.
1. Virtually improve your dev workflow
with Vagrant
Justin Filip - @jfilip
boltmade.com
CC BY-SA 3.0 Justin Filip 1
2. 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
3. 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
4. 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
5. 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
6. 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
7. 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
8. 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
9. 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
10. 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
11. 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
12. 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
13. 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
14. 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
16. 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
17. 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
18. 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 -a
Linux vagrant-ubuntu-trusty-64 3.13.0-65-generic
#106-Ubuntu SMP Fri Oct 2 22:08:27 UTC 2015
x86_64 x86_64 x86_64 GNU/Linux
CC BY-SA 3.0 Justin Filip 18
19. 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.sh
config.vm.provision 'shell', path: 'http://bit.ly/1GDfJrY'
# postgres9.sh
config.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
20. 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
21. 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
22. 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
23. 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
24. 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
25. 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
27. 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
28. 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
30. (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
31. (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
32. 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 = 1024
end
And run:
$ vagrant reload
CC BY-SA 3.0 Justin Filip 32
33. 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