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. •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...
  2. Answers: • Software requirements • Reproducible environment • Portable environment

    • Testing environment • Maximize productivity • Maximize flexibility
  3. Answers: • Skill requirement • Additional software requirements • Increased

    basic hardware requirements • Additional licensing
  4. Repeatable State! Repeatable State Repeatable State Repeatable State Repeatable State

    Repeatable State Repeatable State Repeatable State Repeatable State Repeatable State Repeatable State Repeatable State
  5. 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
  6. 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
  7. 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`
  8. 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
  9. Box Sub Commands: •$ vagrant box add •$ vagrant box

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

    • Name: Local name referenced in configuration • Location: • Local file system • Remote location accessible via HTTP
  11. 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.
  12. Exercise 3 Create your first Vagrant project $ mkdir vagrant-tutorial

    $ cd vagrant-tutorial Initialize Vagrant $ vagrant init Ubuntu-11.10
  13. 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
  14. Commands: •$ vagrant up •$ vagrant status •$ vagrant suspend

    •$ vagrant resume •$ vagrant halt •$ vagrant reload •$ vagrant destroy
  15. 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?
  16. Files: • Multiple shared locations • Support for NFS (Dependent

    upon Host*) • Use your preferred editor • No need to synchronise files
  17. 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")
  18. 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
  19. 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) )
  20. Provisioning • Scripted installation of packages • Scripted updates to

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

    Puppet Server • Chef http://www.opscode.com/chef/ • Chef Solo • Chef Server
  22. Answers: • External dependency • Familiarity • Compatibility • Simplicity

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

    then the defaults overridden within the VagrantFile chef.json = { :mysql_password => "foo" }
  26. 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
  27. Multiple VMs • Individual configuration per VM • Still contained

    in a single VagrantFile • Vagrant commands become VM specific
  28. 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
  29. 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.