Class: Vagrant::Syncer::Syncers::Rsync

Inherits:
Object
  • Object
show all
Defined in:
lib/syncer/syncers/rsync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_opts, machine) ⇒ Rsync

Returns a new instance of Rsync.



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
# File 'lib/syncer/syncers/rsync.rb', line 11

def initialize(path_opts, machine)
  @machine = machine
  @logger = machine.ui

  @machine_path = machine.env.root_path.to_s
  @host_path = parse_host_path(path_opts[:hostpath])
  @guest_path = path_opts[:guestpath]
  @rsync_verbose = path_opts[:rsync__verbose] || false
  @rsync_args = parse_rsync_args(path_opts[:rsync__args],
    path_opts[:rsync__rsync_path])
  @ssh_command = parse_ssh_command(machine.config.syncer.ssh_args)
  @exclude_args = parse_exclude_args(path_opts[:rsync__exclude])

  ssh_username = machine.ssh_info[:username]
  ssh_host = machine.ssh_info[:host]
  @ssh_target = "#{ssh_username}@#{ssh_host}:#{@guest_path}"

  @vagrant_command_opts = {
    workdir: @machine_path
  }

  @vagrant_rsync_opts = {
    guestpath: @guest_path,
    chown: path_opts[:rsync__chown],
    owner: path_opts[:owner],
    group: path_opts[:group]
  }

  @vagrant_rsync_opts[:chown] = true  if @vagrant_rsync_opts[:chown].nil?
  @vagrant_rsync_opts[:owner] = ssh_username  if @vagrant_rsync_opts[:owner].nil?

  if @vagrant_rsync_opts[:group].nil?
    machine.communicate.execute('id -gn') do |type, output|
      @vagrant_rsync_opts[:group] = output.chomp  if type == :stdout
    end
  end
end

Instance Attribute Details

#guest_pathObject (readonly)

Returns the value of attribute guest_path.



9
10
11
# File 'lib/syncer/syncers/rsync.rb', line 9

def guest_path
  @guest_path
end

#host_pathObject (readonly)

Returns the value of attribute host_path.



9
10
11
# File 'lib/syncer/syncers/rsync.rb', line 9

def host_path
  @host_path
end

Instance Method Details

#sync(changed_paths = [], initial = false) ⇒ Object



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
# File 'lib/syncer/syncers/rsync.rb', line 49

def sync(changed_paths=[], initial=false)
  rsync_command = [
    "rsync",
    @rsync_args,
    "-e", @ssh_command,
    changed_paths.map { |path| ["--include", path] },
    @exclude_args,
    @host_path,
    @ssh_target
  ].flatten

  rsync_vagrant_command = rsync_command + [@vagrant_command_opts]
  if !initial && @rsync_verbose
    @vagrant_command_opts[:notify] = [:stdout, :stderr]
    result = Vagrant::Util::Subprocess.execute(*rsync_vagrant_command) do |io_name, data|
      data.each_line do |line|
        if io_name == :stdout
          @logger.success("Rsynced: #{line}")
        elsif io_name == :stderr && !line =~ /Permanently added/
          @logger.warn("Rsync stderr'ed: #{line}")
        end
      end
    end
  else
    result = Vagrant::Util::Subprocess.execute(*rsync_vagrant_command)
  end

  if result.exit_code != 0
    @logger.error(I18n.t('syncer.rsync.failed', error: result.stderr))
    @logger.error(I18n.t('syncer.rsync.failed_command', command: rsync_command.join(' ')))
    return
  end

  # Set owner and group after the files are transferred.
  if @machine.guest.capability?(:rsync_post)
    @machine.guest.capability(:rsync_post, @vagrant_rsync_opts)
  end
end