Vagrant in a Mixed Development Environment
On some projects we have a mixture of OSX and Windows development machines. Normally, this isn’t a problem because we develop inside a VM so we don’t need to worry about inconstancies between our development environments and production. The issue is that OSX machines do much better if you’re using nfs instead of the virtualbox provider. Unfortunately, Windows doesn’t support NFS natively so we need to have some way to determine if the machine is running on an OSX host.
Add the following line to the very bottom of your Vagrantfile:
module OS
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
end
This allows us to quickly determine if the host is OSX (darwin) or not by using a simple if
statement within our Vagrantfile:
if (OS.mac?)
// mac specific code here
else
// Windows specific code here
end
Now we can change our synced_folder from what it normally looks like:
v.vm.synced_folder "./", "/var/www", id: "vagrant-root"
to:
if (OS.mac?)
v.vm.synced_folder "./", "/var/www", id: "vagrant-root", :create=>true, :nfs=>true
else
v.vm.synced_folder "./", "/var/www", id: "vagrant-root"
end
You can now vagrant up
and work in your development VM with a speed increase (my Symfony setup takes about 10% of the time it did as before).
The issue we run into with this system is when we try to run our provisioning script inside our VM:
The issue stems from the fact that nfs uses the permissions from OSX so you end up with permissions that MIGHT look like this:
This can actually be different depending on when your account was created and what your default permissions are.
The quick fix is to run chmod -R 777 .
in your project directory but if new files are added you have to run that command over and over again (and we have). To fix this problem where going to use vagrant-bindfs which is described by it’s GitHub page as:
A Vagrant plugin to automate bindfs mount in the VM. This allow you to change owner, group and permissions on files and, for example, work around NFS share permissions issues.
Installation is simple:
vagrant plugin install vagrant-bindfs
Then we just need to change our mount configuration to look like the following:
if (OS.mac?)
devel.vm.synced_folder "./", "/vagrant-nfs", id: "vagrant-root", :create=>true, :nfs=>true
devel.bindfs.bind_folder "/vagrant-nfs", "/var/www",
perms: "u=rwx:g=rwx:o=rwx",
create_as_user:true
else
devel.vm.synced_folder "./", "/var/www", id: "vagrant-root"
end
To explain this a little bit we’re mounting our project at /vagrant-nfs using NFS. And then we’re using bindfs to remap the /vagrant-nfs directory to /var/www/ (where we tell Apache to look for our repo) with all the permissions set.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows