hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.16k stars 4.43k forks source link

vagrant rsync silently failing #9650

Open martijnthe opened 6 years ago

martijnthe commented 6 years ago

Please note that the Vagrant issue tracker is reserved for bug reports and enhancements. For general usage questions, please use the Vagrant mailing list: https://groups.google.com/forum/#!forum/vagrant-up. Thank you!

Vagrant version

2.0.3

Host operating system

macOS

Guest operating system

Ubuntu 16.04

Vagrantfile

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

Vagrant.require_version ">= 2.0.0"

VAGRANTFILE_API_VERSION = "2"

# Use VirtualBox if no `--provider` argument is provided
ENV["VAGRANT_DEFAULT_PROVIDER"] = "virtualbox"

module Host
  module_function
  def self.get_host
    RbConfig::CONFIG["host_os"]
  end

  def self.is_windows
    self.get_host() =~ /mswin|mingw|cygwin/
  end

  def self.is_linux
   self.get_host() =~ /linux/
  end

  def self.is_mac
    self.get_host() =~ /darwin/
  end

  # Adapted from:
  # https://stefanwrobel.com/how-to-make-vagrant-performance-not-suck
  def self.get_mem_mb
    if self.is_mac()
      # sysctl returns Bytes and we need to convert to MB
      `sysctl -n hw.memsize`.to_i / 1024 / 1024
    elsif self.is_linux()
      # meminfo shows KB and we need to convert to MB
      `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024
    elsif self.is_windows()
      # Windows code via https://github.com/rdsubhas/vagrant-faster
      `wmic computersystem Get TotalPhysicalMemory`.split[1].to_i / 1024 / 1024
    else
      raise Vagrant::Errors::VagrantError.new,
        "Unknown host: #{self.get_host()}"
    end
  end
end

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define :td do |td|
    # Install required plugins
    required_plugins = [
      # Caches apt, pip, etc. packages
      "vagrant-cachier",
      # Configures the VM proxy settings
      "vagrant-proxyconf",
      # Convenience to rsync back shared folders from the VM
      "vagrant-rsync-back",
    ]
    # Adapted from:
    # http://matthewcooper.net/2015/01/15/automatically-installing-vagrant-plugin-dependencies/
    required_plugins.each do |plugin|
        exec "vagrant plugin install #{plugin};vagrant #{ARGV.join(" ")}" unless Vagrant.has_plugin? plugin || ARGV[0] == "plugin"
    end

    if Vagrant.has_plugin?("vagrant-proxyconf")
      td.proxy.http = "http://REDACTED"
      td.proxy.https = "http://REDACTED"
      td.proxy.no_proxy = "localhost,127.0.0.1,.REDACTED.com"
      # Disable proxyconf for npm as we handle that ourselves in provisioning
      td.proxy.enabled = { npm: false }
    end

    # FIXME: pip caching doesn't currently work because it
    # requires pip to already be installed in the box before provisioning, but
    # we currently install it as part of provisioning
    if Vagrant.has_plugin?("vagrant-cachier")
      # NOTE: The "box" scope caches apt, pip, etc. packages under the user's
      # home directory in `~/.vagrant.d/cache/`
      # More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage
      td.cache.scope = :box
      # Setting these options eliminates these warnings:
      #   Can't drop privileges for downloading as file ... couldn't be accessed
      #   by user '_apt'. - pkgAcquire::Run (13: Permission denied)
      # https://github.com/fgrehm/vagrant-cachier/issues/175
      td.cache.synced_folder_opts = {
        owner: "_apt",
        group: "_apt",
        mount_options: ["dmode=777", "fmode=666"],
      }
    end

    # Bento boxes work with VirtualBox, Parallels, and VMWare
    td.vm.box = "bento/ubuntu-16.04"

    # VirtualBox-specific configuration
    td.vm.provider "virtualbox" do |vb|
      # Specify VirtualBox should sync the time with the host anytime it becomes
      # out-of-sync by more than 1 second; this eliminates these Make warnings:
      #     Warning: File xxx has modification time 0.58 s in the future
      vb.customize [
        "guestproperty", "set", :id,
        "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 1000
      ]
      # Change the network card hardware for better performance
      vb.customize ["modifyvm", :id, "--nictype1", "virtio" ]
      vb.customize ["modifyvm", :id, "--nictype2", "virtio" ]

      # Suggested fix for slow network performance
      # See https://github.com/mitchellh/vagrant/issues/1807
      vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]

      # More than 1 CPU on VirtualBox actually degrades performance:
      # https://ruin.io/benchmarking-virtualbox-multiple-core-performance/
      vb.cpus = 1

      # Give VM 1/4 system memory
      vb.memory = Host.get_mem_mb() / 4
    end

    # Docker-specific configuration
    td.vm.provider "docker" do |d, override|
      # Use a Docker-friendly Ubuntu 16.04 Vagrant box
      override.vm.box = "tknerr/baseimage-ubuntu-16.04"
      # Specify the name of the Docker container
      d.name = "td"
      # Enable `vagrant ssh` access (and thus also rsync)
      d.has_ssh = true
    end

    # Rsync the mono_repo folder to the guest VM
    td.vm.synced_folder ".", "/home/vagrant/mono_repo/", type: "rsync",
      # List of arguments to supply to rsync
      rsync__args: [
        # Recurse into directories
        "--recursive",
        # Preserve permissions
        "--perms",
        # Preserve times
        "--times",
        # Preserve group
        "--group",
        # Preserve owner (super-user only)
        "--owner",
        # Preserve device files (super-user only)
        "--devices",
        # Preserve special files
        "--specials",
        # Skip files that are newer on the receiver
        "--update",
        # Delete extraneous files from dest dirs
        "--delete",
        # Copy files whole (without rsync algorithm)
        "--whole-file",
        # Copy symlinks as symlink
        "--links"
      ],
      rsync__exclude: [
        # Git folder
        ".git/",
        # Intellij project files
        ".idea/",
        # CMake build files
        "cmake-build-*/",
        "*.pyc",
        # macOS spotlight
        ".DS_Store",
        # npm/JavaScript packages
        "node_modules/"
      ]

    # Provision the VM as the vagrant user (assumes CWD contains `mono_repo/`)
    td.vm.provision "shell",
      inline: "./mono_repo/provision-ubuntu.sh",
      privileged: false

    td.vm.network "forwarded_port", guest: 1719, host: 1719, id: "port fwd"

    # Enable X11 forwarding by default for convenience.
    td.ssh.forward_x11 = true
  end
end

Debug output

https://gist.github.com/martijnthe/f0614a5e5235afd7dc612ed6febcd665

Expected behavior

Logs should be printed on what it being rsync'd. The folder should be rsync'd when the vagrant rsync command finishes. In case there is a problem, the exit status should be non-zero.

Actual behavior

No logs are being printed when running vagrant rsync. No folders have been synced. The exit status is zero (Running echo $? immediately after it prints 0).

Running with --debug also doesn't reveal much. I don't see any ERROR or WARN logs.

Steps to reproduce

  1. I'm not sure how I got into this state. It worked fine before. This is the second time I got into this state. I did use --provider vmware_fusion when up'ing the machine for the first time. I do also have other issues related to Cisco Anyconnect messing with vmware's networking, but based on the debug output of vagrant rsync --debug, the SSH connection appears to be working. I can also use vagrant ssh just fine. To work-around / fix the VPN networking issues causing vagrant up to fail, I usually start the VM using VMWare's GUI app.

  2. Run vagrant rsync.

References

martijnthe commented 6 years ago

Data point:

$ vagrant halt

# Disabled Cisco Anyconnect VPN here (otherwise `vagrant up` will fail)

$ vagrant up   # Completed successfully
$ vagrant rsync  # Working again

# Enabled Cisco Anyconnect VPN here.

$ vagrant rsync  # Still working.
jkugler commented 5 years ago

I'm hitting this as well. Host: OS X, Guest FreeBSD. Due to a network issue (or repo issue) the install for rsync was failing, but it did not error out when it could not rsync the files. Logging in to the machine after it is up revealed no rsync installed.