Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cooking up a Virtual Development Environment

Cooking up a Virtual Development Environment

In this technical tutorial we will cover the use Vagrant & Chef to script and automate the setup of a Virtual development environment tailored to PHP.

We will begin by investigating the problems associated with differing environments within a development team when working with PHP and LAMP stacks, explaining how differing software stacks can lead to unexpected defects. Most notably introducing risk to our projects during deployment based on environment differences.

Based on example lead steps, delegates will build a virtual environment using a scripted Chef recipe. Over the course of the tutorial delegates will build a local virtual server with a full LAMP stack and their preferred development tools such as PHPUnit, PHPCode_Sniffer and PHPSpec. They will create a shared file system between the virtual server and their laptop to allow them to continue to use their editor of choice.

By the end of the tutorial delegates will have created a fully automated process for creating a repeatable and distributable virtual environment. They will then be able to apply these practices within their own development teams to streamline the creation of a unified environment for their software projects.

Alistair Stead

June 06, 2012
Tweet

More Decks by Alistair Stead

Other Decks in Programming

Transcript

  1. COOKING UP A
    Virtual Development Environment
    @alistairstead
    Vagrant + VirtualBox + Chef

    View Slide

  2. @alistairstead
    Technical Assurance Manager
    @inviqa
    [email protected]
    Me!

    View Slide

  3. •1 x 20 minute break for coffee
    •Email during this breaks
    •Silence your mobile
    •Value everybody’s opinion
    •Parking lot for ideas as we go
    Local rules...

    View Slide

  4. Questions?
    before we start...

    View Slide

  5. Why do we need or
    use virtualisation?
    Question:

    View Slide

  6. Answers:
    • Software requirements
    • Reproducible environment
    • Portable environment
    • Testing environment
    • Maximize productivity
    • Maximize flexibility

    View Slide

  7. What problems does
    virtualisation help
    avoid?
    Question:

    View Slide

  8. Answers:
    • Skill requirement
    • Additional software requirements
    • Increased basic hardware requirements
    • Additional licensing

    View Slide

  9. What virtualisation
    solutions have you
    used?
    Question:

    View Slide

  10. Answers:

    View Slide

  11. Questions?
    moving on...

    View Slide

  12. Repeatable State!
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State
    Repeatable State

    View Slide

  13. Vagrant + VirtualBox

    View Slide

  14. Vagrant
    Vagrant is a tool for building and distributing
    virtualised development environments
    • Ruby gem
    • Provides simple interface for VirtualBox
    • Interacts with VirtualBox API
    • Extendable through a plug-in interface

    View Slide

  15. Features
    • Automated creation of VMs
    • Automated provisioning of VMs
    • Scripted configuration of VM properties
    • Simple interface to manage virtual environment
    • Reduce setup to a single command
    • Scripted configuration of shared file system
    • Scripted configuration of network

    View Slide

  16. Chef
    Scripted Infrastructure

    View Slide

  17. Questions?
    moving on...

    View Slide

  18. Exercise 1
    Download and install VirtualBox
    https://www.virtualbox.org/wiki/Downloads
    Install the vagrant gem
    $ sudo gem install vagrant

    View Slide

  19. help:
    Usage: vagrant [-v] [-h] command []
    -v, --version Print the version and exit.
    -h, --help Print this help.
    Available subcommands:
    basebox
    box
    destroy
    gem
    halt
    init
    package
    provision
    reload
    resume
    ssh
    ssh-config
    status
    suspend
    up
    For help on any individual command run `vagrant COMMAND -h`

    View Slide

  20. Base Boxes
    • Simple VM Images
    • Distribution and Architecture specific
    • Setup with a minimal set of packages
    • Include a common SSH key for access
    • Include VirtualBox tools

    View Slide

  21. Box Sub Commands:
    •$ vagrant box add
    •$ vagrant box list
    •$ vagrant box remove
    •$ vagrant box repackage

    View Slide

  22. Add base box:
    $ vagrant box add
    Parameters
    • Name: Local name referenced in configuration
    • Location:
    • Local file system
    • Remote location accessible via HTTP

    View Slide

  23. Exercise 2
    Add a new base box for Ubuntu 11.10 to your local
    system from:
    http://vagrantbox.es/
    Name the box Ubuntu-11.10
    List the locally available base boxes.

    View Slide

  24. Questions?
    moving on...

    View Slide

  25. Exercise 3
    Create your first Vagrant project
    $ mkdir vagrant-tutorial
    $ cd vagrant-tutorial
    Initialize Vagrant
    $ vagrant init Ubuntu-11.10

    View Slide

  26. Configuration
    • Ruby DSL
    • Host environment conditions
    • Executed Ruby code
    • Extensible

    View Slide

  27. Vagrantfile
    • VM(s) Configuration
    • Shared File System
    • Provisioning

    View Slide

  28. Exercise 4
    Update your VagrantFile to set the name of the VM,
    assign a static IP address and assign 1G of RAM.
    Once you have updated the configuration run:
    $ vagrant up

    View Slide

  29. Questions?
    moving on...

    View Slide

  30. Coffee or
    Tea
    time for a break...

    View Slide

  31. Commands:
    •$ vagrant up
    •$ vagrant status
    •$ vagrant suspend
    •$ vagrant resume
    •$ vagrant halt
    •$ vagrant reload
    •$ vagrant destroy

    View Slide

  32. Access:
    Key based authentication
    $ vagrant ssh
    Password based access
    $ ssh vagrant@localhost
    $ ssh [email protected]
    Password : vagrant

    View Slide

  33. Exercise 5
    After creating your VM pause it and start it again
    ssh into the VM and install a package such as
    apache
    $ sudo apt-get install apache2
    Then destroy the VM before recreating it again.
    Once recreated can you use apache?

    View Slide

  34. Questions?
    moving on...

    View Slide

  35. Files:
    • Multiple shared locations
    • Support for NFS (Dependent upon Host*)
    • Use your preferred editor
    • No need to synchronise files

    View Slide

  36. Exercise 6
    Update your VagrantFile to include a shared folder
    and then reload your VM to apply the changes.
    config.vm.share_folder("Sites", "/mnt/Sites", "#{ENV['HOME']}/
    Sites")

    View Slide

  37. NFS
    VirtualBox Shared Folders: 5m 14s
    Host File System: 10s
    Native VM File System: 13s
    NFS Shared Folders: 22s
    NFS Shared Folders (warm cache): 14s

    View Slide

  38. Hosts
    Not all host OS are made equal.
    Windows does not support NFS for example!

    View Slide

  39. Exercise 7
    Update your VagrantFile to use NFS mounted file
    system if you host machine is not running
    Windows.
    require 'ffi'
    config.vm.share_folder(
    "Sites",
    "/mnt/Sites",
    "#{ENV['HOME']}/Sites",
    :nfs => (FFI::Platform::IS_WINDOWS ? false: true)
    )

    View Slide

  40. Questions?
    moving on...

    View Slide

  41. Provisioning
    • Scripted installation of packages
    • Scripted updates to configuration
    • Scripted starting of services

    View Slide

  42. Provisioning
    • Shell
    • Puppet http://puppetlabs.com/
    • Puppet Solo
    • Puppet Server
    • Chef http://www.opscode.com/chef/
    • Chef Solo
    • Chef Server

    View Slide

  43. Why would we
    choose a specific
    provisioning
    solution?
    Question:

    View Slide

  44. Answers:
    • External dependency
    • Familiarity
    • Compatibility
    • Simplicity
    • Ease of learning
    • Extensibility
    • Personal preference
    • Cost

    View Slide

  45. Add recipes:
    Chef cookbooks include recipes. Each recipe
    installs or updates a software package and applies
    the defined configuration
    chef.add_recipe("apt")
    chef.add_recipe("mysql")
    chef.add_recipe("apache2")
    chef.add_recipe("php")
    chef.add_recipe("project")

    View Slide

  46. Exercise 8
    Configure the paths for chef-solo provisioning of
    your VM. Assign a recipe to your runlist. Run the
    provisioning command for your VM to execute the
    runlist.

    View Slide

  47. Configuration
    Each software package can be configured with
    sensible defaults then the defaults overridden
    within the VagrantFile
    chef.json = { :mysql_password => "foo" }

    View Slide

  48. Exercise 9
    Update the mysql server root password by
    overriding it within the VagrantFile. This will not
    take effect until you re-build the virtual machine

    View Slide

  49. Questions?
    moving on...

    View Slide

  50. Multiple VMs
    • Individual configuration per VM
    • Still contained in a single VagrantFile
    • Vagrant commands become VM specific

    View Slide

  51. Multi Config:
    config.vm.define :web do |web_config|
    /.....
    web_config.vm.network :hostonly, "192.168.33.10"
    end
    config.vm.define :db do |db_config|
    /......
    db_config.vm.network :hostonly, "192.168.33.11"
    end

    View Slide

  52. Exercise 10
    Split the configuration into two VMs and configure
    one as a Web Server and the second as a Database
    Server. Remember to update the IP assignment and
    select the correct recipes for the machine.

    View Slide

  53. Questions?
    Thank you!
    @alistairstead

    View Slide

  54. http://bit.ly/Mg1sIf
    All references and slides

    View Slide

  55. Come Join us...
    http://inviqa.com/careers

    View Slide