My VagrantFile

This is the Vagrantfile I am using for my development box at home and work. It isĀ determines how much ram is available and how I want to allocate, how many CPUs are available, and configures the VM for me. I use NFS for shared folders. Finally, starting to use "hostupdater" to keep my host machines hosts file current.

I would love to make that more dynamic, based on the Apache vhosts I have configured in the VM. Something to work towards I suppose.

The base box was provisionedĀ using Packer and Chef.

# -*- mode: ruby -*-
# vi: set ft=ruby :

#
# Using the following plugins:
# `vagrant plugin install vagrant-hostsupdater`
#

PARAMS = {
    'projects_path' => "#{ENV['HOME']}/Development/projects",
    'hostname' => "#{ENV['USER']}-web",
    'ip' => '192.168.33.10',
    'max_ram' => 3 # 4 = 1/4 of the system ram, 2= 1/3, 2 = 1/2, 
}

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "ns-centos65"
  config.vm.host_name = PARAMS['hostname']

  # Setup network
  config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", disabled: true
  config.vm.network :forwarded_port, guest: 22, host: 2210, auto_correct: true # ssh
  config.vm.network :forwarded_port, guest: 80, host: 8010 # http
  config.vm.network :forwarded_port, guest: 3000, host: 3000 # rails/webrick
  config.vm.network :forwarded_port, guest: 8080, host: 8080 # xhprof gui

  config.vm.network "private_network", ip: PARAMS['ip']

  # Setup synced folders
  config.vm.synced_folder PARAMS['projects_path'], "/var/www/projects", nfs: true
  
  # Setup /etc/hosts on host
  config.hostsupdater.aliases = %w(rlug.local roylindauer.local trap.local estimator.local)

  # Setup VM params based on host resources
  host = RbConfig::CONFIG['host_os']
  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  config.vm.provider "virtualbox" do |vb|
  # Give VM 1/4 system memory & access to all cpu cores on the host
  if host =~ /darwin/
    cpus = `sysctl -n hw.ncpu`.to_i
    # sysctl returns Bytes and we need to convert to MB
    mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / PARAMS['max_ram']
  elsif host =~ /linux/
    cpus = `nproc`.to_i
    # meminfo shows KB and we need to convert to MB
    mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / PARAMS['max_ram']
  else # sorry Windows folks, I can't help you
    cpus = 2
    mem = 1024
  end

    vb.gui = true
    vb.name = "Web_Development_LAMP"
    vb.customize ["modifyvm", :id, "--memory", mem]
    vb.customize ["modifyvm", :id, "--cpus", cpus]
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
  end

end