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.

7ad4c8a7218e44fdf1600b4ebc451738?s=128

Alistair Stead

June 06, 2012
Tweet

Transcript

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

    + Chef
  2. @alistairstead Technical Assurance Manager @inviqa astead@inviqa.com Me!

  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...
  4. Questions? before we start...

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

  6. Answers: • Software requirements • Reproducible environment • Portable environment

    • Testing environment • Maximize productivity • Maximize flexibility
  7. What problems does virtualisation help avoid? Question:

  8. Answers: • Skill requirement • Additional software requirements • Increased

    basic hardware requirements • Additional licensing
  9. What virtualisation solutions have you used? Question:

  10. Answers:

  11. Questions? moving on...

  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
  13. Vagrant + VirtualBox

  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
  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
  16. Chef Scripted Infrastructure

  17. Questions? moving on...

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

    gem $ sudo gem install vagrant
  19. help: Usage: vagrant [-v] [-h] command [<args>] -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`
  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
  21. Box Sub Commands: •$ vagrant box add •$ vagrant box

    list •$ vagrant box remove •$ vagrant box repackage
  22. Add base box: $ vagrant box add <name> <location> Parameters

    • Name: Local name referenced in configuration • Location: • Local file system • Remote location accessible via HTTP
  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.
  24. Questions? moving on...

  25. Exercise 3 Create your first Vagrant project $ mkdir vagrant-tutorial

    $ cd vagrant-tutorial Initialize Vagrant $ vagrant init Ubuntu-11.10
  26. Configuration • Ruby DSL • Host environment conditions • Executed

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

  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
  29. Questions? moving on...

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

  31. Commands: •$ vagrant up •$ vagrant status •$ vagrant suspend

    •$ vagrant resume •$ vagrant halt •$ vagrant reload •$ vagrant destroy
  32. Access: Key based authentication $ vagrant ssh Password based access

    $ ssh vagrant@localhost $ ssh vagrant@192.168.33.10 Password : vagrant
  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?
  34. Questions? moving on...

  35. Files: • Multiple shared locations • Support for NFS (Dependent

    upon Host*) • Use your preferred editor • No need to synchronise files
  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")
  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
  38. Hosts Not all host OS are made equal. Windows does

    not support NFS for example!
  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) )
  40. Questions? moving on...

  41. Provisioning • Scripted installation of packages • Scripted updates to

    configuration • Scripted starting of services
  42. Provisioning • Shell • Puppet http://puppetlabs.com/ • Puppet Solo •

    Puppet Server • Chef http://www.opscode.com/chef/ • Chef Solo • Chef Server
  43. Why would we choose a specific provisioning solution? Question:

  44. Answers: • External dependency • Familiarity • Compatibility • Simplicity

    • Ease of learning • Extensibility • Personal preference • Cost
  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")
  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.
  47. Configuration Each software package can be configured with sensible defaults

    then the defaults overridden within the VagrantFile chef.json = { :mysql_password => "foo" }
  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
  49. Questions? moving on...

  50. Multiple VMs • Individual configuration per VM • Still contained

    in a single VagrantFile • Vagrant commands become VM specific
  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
  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.
  53. Questions? Thank you! @alistairstead

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

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