Vagrant unidirectional synced folder - vagrant

I have been using synced folders in Vagrant for a while, but the behaviour is not what I expected. It seems both changes in the host machines and changes in the guest machine are synced. However, I want syncing to be in only one direction: from the host to the guest. Part of the reason I want to create a Vagrant machine is that, if my code accidentally does something destructive, like deleting itself, I want that behaviour to be contained in a safe environment.
For reference, this is the Vagrantfile I am using:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# 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 = "hashicorp/precise32"
config.vm.provision :shell, :path => "bootstrap.sh"
config.vm.network :forwarded_port, host: 8080, guest: 80
config.vm.synced_folder "test", "/test", :nfs => true
config.vm.network "private_network", type: "dhcp"
end
where bootstrap.h just executes apt-get install nfs-common portmap.
What is the easiest way I can configure unidirectional syncing in Vagrant?

Starting from Vagrant 1.5 you can use rsync as sync folder method:
Vagrant.configure("2") do |config|
config.vm.synced_folder "test", "/test", type: "rsync",
end
This is unidirectional.

Related

Vagrant synced folder causes contents to be erased

I'm trying to add add an additional synced folder besides the default /vagrant.
I've read the documentation which indicates that this should be as simple as:
config.vm.synced_folder "/path/to/somewhere/on/host", "/path/to/guest_directory", create: true
When I add this to my Vagrantfile and then reprovision it, the guest_directory folder shows up in the expected place on the host, but it's empty. And the folder in the guest OS is also empty... which breaks my site because those are the website files. Interestingly, if I remove that from the Vagrantfile and reprovision again, the files reappear in the guest OS where they were originally.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
# NEITHER OF THESE WORK...
# config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/home/vagrant/web__public_web", create: true
# config.vm.synced_folder "./web__public_web", "/home/vagrant/web__public_web", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
You can see that I've tried a two different type: options and got the same wrong result with both. I also tried placing a symlink to the project files inside /vagrant/ and that didn't work either. (The broken symlink showed up in the host os directory instead of the files I want to access)
Here are a couple more details about my setup, just in case they're relevant:
In the guest OS the project files live in /home/vagrant/web__public_web but the document root is /srv/www/... which contains a symlink pointing to the former path.
VirtualBox: Version 5.2.18 r124319 (Qt5.6.3)
Vagrant: v2.1.0
OSX: 10.13.6
What am I doing wrong?
Each mount requires a unique id, if the box is already built you'll need to rebuild it.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/path/where/mounted/on/vm", id: "unique_id_1", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.synced_folder "/home/vagrant/web__public_web", "/path/where/mounted/on/vm2", id: "unique_id_2", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
I answered my own question...
It turns out the sync was working all along. But I misunderstood the directionality of the sync. I was expecting the files in the guest OS to show up in the host directory, but it's the other way around. The empty folder on the host directory was getting synced to the guest and wiping out all the files.
After I manually copied all of the files from the guest to the host directory, the same configurations worked as expected.
Bottom line: if you want to sync an existing guest folder with files in it, you have to manually move it into place on the host OS first.

Access django server in vagrant virtualbox on host machine?

I am using windows and putty to ssh to vagrant virtualbox.I cannot access the django server running in vagrant virtualbox using http://localhost:9991
I have disabled my firewall as well
here's my vagrant file:
VAGRANTFILE_API_VERSION = "2"
def command?(name)
`which #{name}`
$?.success?
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# For LXC. VirtualBox hosts use a different box, described below.
config.vm.box = "fgrehm/trusty64-lxc"
# The Zulip development environment runs on 9991 on the guest.
config.vm.network "forwarded_port", guest: 9991, host: 9991, host_ip: "127.0.0.1"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.synced_folder ".", "/srv/zulip"
# Specify LXC provider before VirtualBox provider so it's preferred.
config.vm.provider "lxc" do |lxc|
if command? "lxc-ls"
LXC_VERSION = `lxc-ls --version`.strip unless defined? LXC_VERSION
if LXC_VERSION >= "1.1.0"
# Allow start without AppArmor, otherwise Box will not Start on Ubuntu 14.10
# see https://github.com/fgrehm/vagrant-lxc/issues/333
lxc.customize 'aa_allow_incomplete', 1
end
end
end
config.vm.provider "virtualbox" do |vb, override|
override.vm.box = "ubuntu/trusty64"
# 2GiB seemed reasonable here. The VM OOMs with only 1024MiB.
vb.memory = 2048
end
$provision_script = <<SCRIPT
set -x
set -e
sudo apt-get update
sudo apt-get install -y python-pbs
/usr/bin/python /srv/zulip/provision.py
SCRIPT
config.vm.provision "shell",
# We want provision.py to be run with the permissions of the vagrant user.
privileged: false,
inline: $provision_script
end
How do i access the server from host(Windows)?
I would suggest (on the Guest):
sudo netstat -lnutp
and having a look at what ports are open, and the process which owns them. If the one you want is missing, make sure the service which is responsible for it has been started, or start it yourself. From the looks of your Vagrantfile, this would be the "Zulip development environment".

Vagrant not listening to my port and using default port

I'm using Vagrant for multiple projects now with a per-project-installation, meaning I have multiple boxes. One for each project (or for production enviroment)
When I want to access my first Vagrantbox I access it via http://sitea.com:8080. My second Vagrantbox should listen to 1337 (I tried multiple ports here) but when I call http://siteb.com:1337 I'm getting an error in all browsers (ERR_CONNECTION_REFUSED in Chrome).
However, when I'm calling my second site like http://siteb.com it works without any problems. It seems that my second Vagrantbox just doesn't listen to the ports and uses the default port. But why is that?
First box (Which works with port only)
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "puppetlabs/debian-7.8-64-puppet"
config.vm.provision :shell, path: "vagrant_ressources/bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.50.4"
config.vm.synced_folder ".", "/var/www", type: "nfs"
end
Second box (Which works without port only)
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "puppetlabs/debian-7.8-64-puppet"
config.vm.provision :shell, path: "vagrant_ressources/bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 1337
config.vm.network "private_network", ip: "192.168.50.5"
config.vm.synced_folder ".", "/var/www", type: "nfs"
end
Both have the same vhost configuration, and my hosts file looks like this
# Vagrant
192.168.50.4 sitea.com
192.168.50.5 siteb.com
When using port forwarding with Vagrant you'll usually access localhost since the forwarded port is available at your host machine. Like this:
http://localhost:8080 -> should deliver the website siteA.com
http://localhost:1337 -> should deliver the website siteB.com
If you are able to access http://siteB[:80] it means that the DNS entry of siteB points to 192.168.50.5, which is the private network ip of that box.
Check:
getent hosts | grep siteB\.com
I bet it will give you 192.168.50.5.

Vagrant 'permission denied' on Windows

I am having trouble accessing files through Vagrant on Windows. I have been using it on OS X for quite some time and have my Vagrantfile setup correctly which works every time.
I have sent my colleague the same Vagrant file, he is on Windows and receives 'Permission Denied' when trying to access files through the browser.
Just to be clear, the permission errors are returned by the server when accessing 'dev.local' in the browser and not from Vagrant itself... it will be a configuration error on Windows or within the VM.
The VM is CentOS 6.5
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# 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 = "chef/centos-6.5"
config.vm.network "private_network", ip: "192.168.33.21"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.provision :shell, :path => "install.sh"
config.vm.hostname = "dev.local"
config.vm.synced_folder ".", "/home", id: "vagrant", :nfs => false, :mount_options => ["dmode=777","fmode=777"]
config.ssh.insert_key = false
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
end
Can any Windows Vagrant users shed any light on this?
It was VBGuestAdditions out of date. The permission error was caused by not being able to sync to my local folder (which contained an index.php) so it was using the servers /home folder which didn't contain anything and since viewing directory structure is disabled it returned permission errors.
I had 4.X.X installed, and VirtualBox is on 5.X.X Here is the fix:
run command: vagrant plugin install vagrant-vbguest
the run vagrant up which may still throw an error as the plugin fails to copy a file.
vagrant ssh to get into the box and run the following command:
sudo ln -s /opt/VBoxGuestAdditions-5.X.X/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
Replace 5.X.X with the version of your VirtualBox.
logout and run vagrant reload
do a happy dance

vagrant Vagrantfile mount point ignored

I have a Vagrantfile as below running under libvirt. When the box boots, the project directory is mounted under "/vagrant" not "/path/to/source". It works fine on another machine under VirtualBox.
Any ideas? Symlinking /vagrant to the actual place I want the mount is hacky.
vagrant up --debug shows that it isn't even attempting it. The port forwarding works OK though.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "mybox"
file = File.open("#{Dir.home}/.mybox_key", "rb")
key = file.read
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.synced_folder "./", "/path/to/source/", type: "nfs"
end
If you're running an older version of Vagrant that doesn't support this option (e.g. 1.3.5), it may fail silently instead of trying to setup the NFS share. Ensure you're running at least Vagrant 1.5.1.

Resources