Class: VagrantReflect::Command::Reflect

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Action::Builtin::MixinSyncedFolders
Defined in:
lib/vagrant-reflect/command/reflect.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



18
19
20
# File 'lib/vagrant-reflect/command/reflect.rb', line 18

def self.synopsis
  'a better rsync-auto'
end

Instance Method Details

#callback(path, opts, options, modified, added, removed) ⇒ Object

This is the callback that is called when any changes happen



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/vagrant-reflect/command/reflect.rb', line 162

def callback(path, opts, options, modified, added, removed)
  @logger.info("File change callback called for #{path}!")
  @logger.info("  - Modified: #{modified.inspect}")
  @logger.info("  - Added: #{added.inspect}")
  @logger.info("  - Removed: #{removed.inspect}")

  callback = options[:incremental] ? :sync_incremental : :sync_full

  # Perform the sync for each machine
  opts.each do |path_opts|
    begin
      # If disabled incremental, do a full
      send callback, path, path_opts, modified, added, removed

      path_opts[:machine].ui.info(
        I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_synced'))
    rescue Vagrant::Errors::MachineGuestNotReady
      # Error communicating to the machine, probably a reload or
      # halt is happening. Just notify the user but don't fail out.
      path_opts[:machine].ui.error(
        I18n.t('vagrant.plugins.vagrant-reflect.'\
               'rsync_communicator_not_ready_callback'))
    rescue Vagrant::Errors::VagrantError => e
      path_opts[:machine].ui.error(e.message)
    end
  end
end

#executeObject



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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vagrant-reflect/command/reflect.rb', line 22

def execute
  @logger = Log4r::Logger.new('vagrant::commands::reflect')

  options = {
    poll:        false,
    incremental: true
  }
  opts = OptionParser.new do |o|
    o.banner = 'Usage: vagrant reflect [vm-name]'
    o.separator ''
    o.separator 'Options:'
    o.separator ''

    o.on('--[no-]poll', 'Force polling filesystem (slow)') do |poll|
      options[:poll] = poll
    end
    o.on(
      '--[no-]incremental',
      'Perform incremental copies of changes where possible (fast)'
    ) do |incremental|
      options[:incremental] = incremental
    end
  end

  # Parse the options and return if we don't have any target.
  argv = parse_options(opts)
  return unless argv

  # Build up the paths that we need to listen to.
  paths = {}
  with_target_vms(argv) do |machine|
    if machine.provider.capability?(:proxy_machine)
      proxy = machine.provider.capability(:proxy_machine)
      if proxy
        machine.ui.warn(
          I18n.t(
            'vagrant.plugins.vagrant-reflect.rsync_proxy_machine',
            name: machine.name.to_s,
            provider: machine.provider_name.to_s))

        machine = proxy
      end
    end

    cached = synced_folders(machine, cached: true)
    fresh  = synced_folders(machine)
    diff   = synced_folders_diff(cached, fresh)
    unless diff[:added].empty?
      machine.ui.warn(
        I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_new_folders'))
    end

    folders = cached[:rsync]
    next if !folders || folders.empty?

    folders.each do |id, folder_opts|
      # If we marked this folder to not auto sync, then
      # don't do it.
      next if folder_opts.key?(:auto) && !folder_opts[:auto]

      # Push on .vagrant exclude
      folder_opts = folder_opts.dup
      folder_opts[:exclude] ||= []
      folder_opts[:exclude] << '.vagrant/'

      syncer = Syncer.new(machine, folder_opts)

      machine.ui.info(
        I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_initial'))
      syncer.sync_full

      hostpath = folder_opts[:hostpath]
      hostpath = File.expand_path(hostpath, machine.env.root_path)
      paths[hostpath] ||= []
      paths[hostpath] << {
        id:      id,
        machine: machine,
        opts:    folder_opts,
        syncer:  syncer
      }
    end
  end

  # Exit immediately if there is nothing to watch
  if paths.empty?
    @env.ui.info(
      I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_no_paths'))
    return 1
  end

  @logger.info(
    "Listening via: #{Driskell::Listen::Adapter.select.inspect}")

  # Create a listener for each path so the callback can easily
  # distinguish which path changed
  listeners = paths.keys.sort.collect do |path|
    opts = paths[path]
    callback = method(:callback).to_proc.curry[path][opts][options]

    ignores = []
    opts.each do |path_opts|
      ignores += path_opts[:syncer].excludes_to_regexp
      path_opts[:machine].ui.info(
        I18n.t(
          'vagrant.plugins.vagrant-reflect.rsync_auto_path',
          path: path.to_s))
    end

    @logger.info("Listening to path: #{path}")
    @logger.info("Ignoring #{ignores.length} paths:")
    ignores.each do |ignore|
      @logger.info("-- #{ignore}")
    end

    listopts = { ignore: ignores, force_polling: options[:poll] }
    Driskell::Listen.to(path, listopts, &callback)
  end

  # Create the callback that lets us know when we've been interrupted
  queue    = Queue.new
  callback = lambda do
    # This needs to execute in another thread because Thread
    # synchronization can't happen in a trap context.
    Thread.new { queue << true }
  end

  # Run the listeners in a busy block so that we can cleanly
  # exit once we receive an interrupt.
  Vagrant::Util::Busy.busy(callback) do
    listeners.each(&:start)
    queue.pop
    listeners.each do |listener|
      listener.stop if listener.state != :stopped
    end
  end

  0
end

#strip_path(path, item) ⇒ Object



240
241
242
# File 'lib/vagrant-reflect/command/reflect.rb', line 240

def strip_path(path, item)
  item[path.length..-1]
end

#strip_paths(path, items) ⇒ Object



234
235
236
237
238
# File 'lib/vagrant-reflect/command/reflect.rb', line 234

def strip_paths(path, items)
  items.map do |item|
    item[path.length..-1]
  end
end

#sync_full(path, path_opts, modified, added, removed) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/vagrant-reflect/command/reflect.rb', line 190

def sync_full(path, path_opts, modified, added, removed)
  [modified, added].flatten.each do |change|
    path_opts[:machine].ui.info(
      I18n.t(
        'vagrant.plugins.vagrant-reflect.rsync_auto_full_change',
        path: strip_path(path, change)))
  end

  removed.each do |remove|
    path_opts[:machine].ui.info(
      I18n.t(
        'vagrant.plugins.vagrant-reflect.rsync_auto_full_remove',
        path: strip_path(path, remove)))
  end

  path_opts[:machine].ui.info(
    I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_full'))

  path_opts[:syncer].sync_full
end

#sync_incremental(path, path_opts, modified, added, removed) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/vagrant-reflect/command/reflect.rb', line 211

def sync_incremental(path, path_opts, modified, added, removed)
  if !modified.empty? || !added.empty?
    # Pass the list of changes to rsync so we quickly synchronise only
    # the changed files instead of the whole folder
    items = strip_paths(path, modified + added)
    path_opts[:syncer].sync_incremental(items) do |item|
      path_opts[:machine].ui.info(
        I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_increment_change',
               path: item))
    end
  end

  return if removed.empty?

  # Pass list of changes to a remove command
  items = strip_paths(path, removed)
  path_opts[:syncer].sync_removals(items) do |item|
    path_opts[:machine].ui.info(
      I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_increment_remove',
             path: item))
  end
end