It Works On My Machine: Vagrant for Software Development

Embed Size (px)

Citation preview

Blue Elegance1

It Works on My Machine:Vagrant for Software Development

Colorado Springs Open Source Software Meetup1/20/2016

Carlos [email protected]

Survey:

Do you virtualize your development environment? Production environment?

VirtualBox? VMWare? Others?

Traditional Dev. Environment

SCM / CI

Dev. 1 w/ app stackMac OS X

Test / Stage /Prod

Dev. 2 w/ app stackLinux Ubuntu

Dev. 3 w/ app stackMS Windows

Dev. 4 w/ app stack

Everyone has their own application stack and machine dependencies. This is known as the 'works on my machine' problem.

Traditional development environments have everyone work on native host platforms, IDE's and tool chains.

Byte-code languages (.NET, Java) and portable interpreters (PHP, Python) have been able to firewall developers from host-specific differences.

Model works reasonably well in shops that control dev. environment (e.g. the IT locked down laptop), but in BYOM environments, consistency and stability is difficult.

When it is time to move your app to the cloud, it is not likely that your laptop environment will be easily replicated.

Source: TIOBE Index for January 2016http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

TIOBE Index of Popular Programming Languages

Developers work with a number of programming languages and environments.

Some languages, such as C, C++, Assembly are tightly coupled to the machine architecture.

Some platforms, such as ARM, require cross-compilers and vendor tool chains that are tricky and difficult to get setup.

Why Use Vagrant?

SOLUTION: Have everyone work on the 'same machine' by declaratively specifying the configuration for a VM instance

Based on virtual machines you already know (VirtualBox, VMWare, Hyper-V, etc.)

Easy to configure, reproducible, portable

SCM friendly files (git, tfvc)

DevOps friendly: write and test provisioning 'scripts' (Chef, Puppet, Ansible, etc.)

Provisioning a dev. environment is time consuming-- automate and streamline the process with Vagrant.

A virtualized dev. environment, if done correctly, mimics your production environment.

Vagrant also is a path for shops with strong DevOps workflow-- DevOps can (and should) be involved with building Vagrant instances.

CONS of using Vagrant:

Need powerful machine and lots of disc space

Virtualization may not work well for all cases: driver or RTOS embedded development, or non-web GUI development (e.g. Qt)

Vagrant Architecture

VMHost OS

Guest OS: CentOS

/home/user1/projects/web1 => /vagrantWeb BrowserIDE / EditorgitApachePHPMySQLport 2222 => 22; port 8000 => 80Virtualbox

port forwards

synced folders

Vagrant is a 'shell' for starting and stopping VM's.

Host: serves as your front end to your IDE, browser, and SCM.

Guest: typically runs headless using port forwards and synced folders. Access using SSH.

When developing in dynamically typed languages such as PHP, may not even need to ssh in to start services

Basic Demo

mkdir foocd foovagrant init ubuntu/trusty32vagrant upls -lsatvagrant ssh-configvagrant sshvagrant haltvagrant destroyrm -rf .vagrantcd ..rmdir foo

Vagrant

Open source, started in 2010 by Mitchell Hashimoto

MIT License

Website: https://www.vagrantup.com

Docs: https://docs.vagrantup.com/v2/

Installation:Vagrant (apt-get, .deb, .dmg, .msi)

VM (https://www.virtualbox.org)

Windows: PuTTY, PuTTYgen

Vagrant is open source!

Written in Ruby.

Terminology

Box: VM base image for guest (a.k.a. template)

Provider: virtualization layer on host (VirtualBox, VMWare, etc.)

Provisioner: system for automating software installations (shell, chef, etc.)

Project: directory containing Vagrantfile and typically the root of the 'synced folder'

Basic Commands

vagrant help

vagrant global-status: show environments

vagrant status: show project status

vagrant box list: show installed boxes

Vagrant Up Flow

ABox repositorydownloadBLocal boxdirectoryCVM instancedirectoryimport1) vagrant up2) If box does not exist, download from 'A' and add to 'B'3) If VM does not exist, import from local box directory 'B' and copy to 'C'4) Start VM, networking, port forwards, import SSH keys5) Mount shared folders6) Provision VM7) vagrant ssh

https://atlas.hashicorp.com/ubuntu/boxes/trusty32/versions/20151218.0.1/provider/virtualbox.box

~/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-trusty32

~/VirtualBox\ VMs/trusty32_default_1452718546328_49107

'vagrant ssh-config' displays current ssh configuration

'vagrant rdp' allows one to connect to a machine using the RDP protocol.

'vagrant powershell' for Windows servers.

'vagrant port' shows the port forwards in use.

Where to Find Boxes?

Hashicorp Atlas (https://atlas.hashicorp.com/)Cataloged using the vendor/boxname notation

Public catalogs (http://www.vagrantbox.es/)Think craigslist-- boxes may not be secure

Build your own using packer or vagrant packageInstallation ISO + packer + JSON config = your box

Host on your own web server

'vagrant init' will default to Atlas if no URL is secified for box URL

Vagrant Lifecycle Commands

vagrant up instantiate, provision, start VM

vagrant halt: stop / poweroff VM

vagrant destory: stop / delete VM

vagrant suspend / vagrant resume

runningpoweredoffsuspendednon-existentresumesuspenduphaltdestroyup'vagrant snapshot' allows you to create and restore snapshots of your VM

Vagrant Snapshot

Vagrant supports VM snapshotsvagrant snapshot push / pop

vagrant snapshot save / restore

vagrant snapshot list

vagrant snapshot delete

Note: do not mix push/pop and save/restore

snapshots

VMinstance

push

pop

The Vagrantfile

Vagrantfile in project controls 'up' processRuby language

Located in project directory and checked into SCM

vagrant up creates .vagrant directory (add to .gitignore)

Other Vagrantfiles are consulted during vagrant up (process called 'merging')~/.vagrant.d/boxes/.../Vagrantfile, include/_Vagrantfile

~/.vagrant.d

Vagrantfile in the project hierarchy

Initializing a Project

$ cd ~/project1$ vagrant init ubuntu/trusty32$ vagrant up

'vagrant init' will create a Vagrantfile for you with the box set to 'ubuntu/trusty32'. By default, it will use the Atlas URL to find boxes.

A .vagrant directory is created in the project folder.

Provisioning

Provisioning occurs after a box is imported into a VM

Provisioning usually occurs once, but can be run againvagrant provision

Vagrantfile config.vm.provision run: 'always', etc.

... config.vm.provision "shell", inline: