Skip to content

Instantly share code, notes, and snippets.

@Remiz
Created May 13, 2020 21:12
Show Gist options
  • Save Remiz/5c8123eda2853980c752a3cd16fdbe03 to your computer and use it in GitHub Desktop.
Save Remiz/5c8123eda2853980c752a3cd16fdbe03 to your computer and use it in GitHub Desktop.
Vagrantfile default
Vagrant.configure(2) do |config|
config.vm.box = 'bento/ubuntu-16.04'
config.vm.define "project"
config.vm.hostname = "boxname"
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.ssh.forward_agent = true
# Syncing using NFS and shorten cache life for faster reload
config.vm.synced_folder "../project_path/", "/var/www/project/", type: "nfs",
mount_options: ['rw', 'vers=3', 'tcp', 'actimeo=1'],
# VM ressources
config.vm.provider "virtualbox" do |vb|
# Detecting cpus and memory for every OS
host = RbConfig::CONFIG['host_os']
mem_ratio = 3/4 # Change memory ratio to use here
cpu_exec_cap = 75 # Change CPU execution cap here
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^2 * mem_ratio
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 * mem_ratio
else # Windows folks
cpus = `wmic cpu get NumberOfCores`.split("\n")[2].to_i
mem = `wmic OS get TotalVisibleMemorySize`.split("\n")[2].to_i / 1024 * mem_ratio
end
puts "Provisioning VM with #{cpus} CPU's (at #{cpu_exec_cap}%) and #{mem/1024} GB RAM."
v.customize ["modifyvm", :id, "--memory", mem]
v.customize ["modifyvm", :id, "--cpus", cpus]
v.customize ["modifyvm", :id, "--cpuexecutioncap", cpu_exec_cap]
end
# Provisioning with ansible
config.vm.provision "ansible" do |ansible|
ansible.compatibility_mode = "2.0"
ansible.playbook = "playbook.yml"
ansible.become = "root"
ansible.groups = {
"webservers" => ["project"],
"dev" => ["project"],
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment