Skip to content

Instantly share code, notes, and snippets.

@slucero
Last active January 25, 2018 22:55
Show Gist options
  • Save slucero/ecc79fd92fea7cae71b0 to your computer and use it in GitHub Desktop.
Save slucero/ecc79fd92fea7cae71b0 to your computer and use it in GitHub Desktop.
Local Vagrant Configuration
# ~/.vagrant.d/Vagrantfile
# Local configuration to use across all Vagrant machines
Vagrant.configure("2") do |config|
config.vm.provider :virtualbox do |vb|
host = RbConfig::CONFIG['host_os']
# Force settings for allowed resources
if host =~ /darwin/
# Dynamically look up resources for Macs
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
elsif host =~ /linux/
# Improve linux performance by only counting physical cores and not hyperthreads
cpus = `grep "cpu cores" /proc/cpuinfo |sort -u |cut -d":" -f2`.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
else # sorry Windows folks, I can't help you
# Fall back to statically set values
# **Note**: Update these for your system if using this
cpus = "4"
mem = "4096"
end
# Adjust values here if you don't want to allow access to all resources
# Eg. mem = mem / 4 # This would limit access to only a quarter of total memory
# Increase allowed memory usage and cpus
vb.customize ["modifyvm", :id, "--cpuexecutioncap", "90", "--memory", mem, "--cpus", cpus]
end
# vagrant-cachier plugin configuration
if Vagrant.has_plugin?("vagrant-cachier")
# Cache downloaded packages per box
config.cache.scope = :box
end
# vagrant-exec plugin configuration
if Vagrant.has_plugin?("vagrant-exec")
# By default run vagrant-exec commands within the /vagrant directory
config.exec.commands '*', directory: '/vagrant'
# Run all drush commands within the Drupal root
config.exec.commands 'drush', directory: '/vagrant/docroot'
end
end
@slucero
Copy link
Author

slucero commented Feb 2, 2016

Updated this to dynamically calculate available resources where possible.

@slucero
Copy link
Author

slucero commented Feb 2, 2016

Updated to fix the following error:

There was an error loading a Vagrantfile. The file being loaded
and the error message are shown below. This is usually caused by
a syntax error.

Path: <provider config: virtualbox>
Line number: 8
Message: NameError: undefined local variable or method `host' for main:Object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment