Class: Vagrant::Syncer::Machine

Inherits:
Object
  • Object
show all
Includes:
Action::Builtin::MixinSyncedFolders
Defined in:
lib/syncer/machine.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine, polling = false) ⇒ Machine

Returns a new instance of Machine.



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

def initialize(machine, polling=false)
  @paths = []
  @syncers = []
  @excludes = []
  @logger = machine.ui

  @listener_verbose = machine.config.syncer.show_events
  @listener_interval = machine.config.syncer.interval

  listener_settings = {
    latency: @listener_interval,
    wait_for_delay: @listener_interval / 2
  }

  if polling
    require_relative 'listeners/listen'
    @listener_class = Vagrant::Syncer::Listeners::Listen
    listener_settings[:force_polling] = polling
  elsif machine.config.syncer.force_listen_gem
    require_relative 'listeners/listen'
    @listener_class = Vagrant::Syncer::Listeners::Listen
  else
    case Vagrant::Util::Platform.platform
    when /darwin/
      require_relative 'listeners/fsevents'
      @listener_class = Vagrant::Syncer::Listeners::FSEvents
    when /linux/
      require_relative 'listeners/inotify'
      @listener_class = Vagrant::Syncer::Listeners::INotify
    else
      require_relative 'listeners/listen'
      @listener_class = Vagrant::Syncer::Listeners::Listen
    end
  end

  @listener_name = @listener_class.to_s.gsub(/^.*::/, '')

  rsync_synced_folders = synced_folders(machine)[:rsync]

  if rsync_synced_folders
    rsync_synced_folders.each do |id, folder_opts|
      @paths << File.expand_path(folder_opts[:hostpath], machine.env.root_path)
      @excludes << folder_opts[:rsync__excludes]  if folder_opts[:rsync__excludes]
      @syncers << Syncers::Rsync.new(folder_opts, machine)
    end
  end

  @listener = @listener_class.new(
    @paths,
    @excludes,
    listener_settings,
    change_callback
  )
end

Instance Method Details

#full_syncObject



66
67
68
69
70
71
72
73
74
# File 'lib/syncer/machine.rb', line 66

def full_sync
  @syncers.each do |syncer|
    @logger.info(I18n.t('syncer.states.initial', {
      host_path: syncer.host_path,
      guest_path: syncer.guest_path
    }))
    syncer.sync([syncer.host_path], initial=true)
  end
end

#listenObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/syncer/machine.rb', line 76

def listen
  text = @listener_polling ? 'syncer.states.polling' : 'syncer.states.watching'
  @paths.each do |path|
    @logger.info(I18n.t(text, {
      host_path: path,
      listener: @listener_name,
      interval: @listener_interval
    }))
  end
  @listener.run
end