Class: VagrantPlugins::SyncedFolderRSyncPull::RsyncPullHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-rsync-pull/helper.rb

Class Method Summary collapse

Class Method Details

.rsync_single(machine, opts) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vagrant-rsync-pull/helper.rb', line 7

def self.rsync_single(machine, opts)
  ssh_info = machine.ssh_info
  guestpath = opts[:guestpath]
  hostpath = opts[:hostpath]
  hostpath = File.expand_path(hostpath, machine.env.root_path)
  hostpath = Vagrant::Util::Platform.fs_real_path(hostpath).to_s

  if Vagrant::Util::Platform.windows?
    hostpath = Vagrant::Util::Platform.cygwin_path(hostpath)
  end

  if !guestpath.end_with?("/")
    guestpath += "/"
  end

  if !hostpath.end_with?("/")
    hostpath += "/"
  end

  opts[:owner] ||= ssh_info[:username]
  opts[:group] ||= ssh_info[:username]

  username = ssh_info[:username]
  host = ssh_info[:host]
  proxy_command = ""
  if ssh_info[:proxy_command]
    proxy_command = "-o ProxyCommand='#{ssh_info[:proxy_command]}' "
  end

  rsh = [
    "ssh -p #{ssh_info[:port]} " +
    proxy_command +
    "-o StrictHostKeyChecking=no " +
    "-o UserKnownHostsFile=/dev/null",
    ssh_info[:private_key_path].map { |p| "-i '#{p}'" },
  ].flatten.join(" ")

  excludes = ['.vagrant/', 'Vagrantfile']
  excludes += Array(opts[:exclude]).map(&:to_s) if opts[:exclude]
  excludes.uniq!

  args = nil
  args = Array(opts[:args]).dup if opts[:args]
  args ||= ["--verbose", "--archive", "--delete", "-z", "--copy-links"]

  if Vagrant::Util::Platform.windows? && !args.any? { |arg| arg.start_with?("--chmod=") }
    args << "--chmod=ugo=rwX"

    args << "--no-perms" if args.include?("--archive") || args.include?("-a")
  end

  args << "--no-owner" unless args.include?("--owner") || args.include?("-o")
  args << "--no-group" unless args.include?("--group") || args.include?("-g")

  if machine.guest.capability?(:rsync_command)
    args << "--rsync-path"<< machine.guest.capability(:rsync_command)
  end

  command = [
    "rsync",
    args,
    "-e", rsh,
    excludes.map { |e| ["--exclude", e] },
    "#{username}@#{host}:#{guestpath}",
    hostpath,
  ].flatten

  command_opts = {}
  command_opts[:workdir] = machine.env.root_path.to_s

  machine.ui.info(I18n.t(
    "vagrant.rsync_folder", guestpath: guestpath, hostpath: hostpath))
  if excludes.length > 1
    machine.ui.info(I18n.t(
      "vagrant.rsync_folder_excludes", excludes: excludes.inspect))
  end

  if machine.guest.capability?(:rsync_pre)
    machine.guest.capability(:rsync_pre, opts)
  end

  command = command + [command_opts]

  r = Vagrant::Util::Subprocess.execute(*command)
  if r.exit_code != 0
    raise Vagrant::Errors::SyncedFolderRSyncPullError,
      command: command.inspect,
      guestpath: guestpath,
      hostpath: hostpath,
      stderr: r.stderr
  end

  if machine.guest.capability?(:rsync_post)
    machine.guest.capability(:rsync_post, opts)
  end
end