Class: VagrantPlugins::RsyncOnlyChanged::RsyncOnlyChangedAuto

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



28
29
30
# File 'lib/vagrant-rsync-only-changed/command/rsync_auto.rb', line 28

def self.synopsis
  "syncs rsync synced folders automatically when files change"
end

Instance Method Details

#callback(paths, modified, added, removed) ⇒ Object

This is the callback that is called when any changes happen



155
156
157
158
159
160
161
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/vagrant-rsync-only-changed/command/rsync_auto.rb', line 155

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

    toSyncPaths = Hash.new
  
  paths.each do |hostpath, folders|
  changed_paths = []
  [modified, added, removed].each do |changed|
    changed.each do |listenpath|
    if listenpath.start_with?(hostpath)
      changed_paths << listenpath
    end
    end
  end

  if changed_paths.any?
    toSyncPaths[folders] = changed_paths
  end
  end
  
  # Sync all the folders that need to be synced

  toSyncPaths.each do |folders, changed_paths|
  folders.each do |opts|
    # Reload so we get the latest ID

    opts[:machine].reload
    if !opts[:machine].id || opts[:machine].id == ""
    # Skip since we can't get SSH info without an ID

    next
    end

    ssh_info = opts[:machine].ssh_info
    begin
    start = Time.now
    VagrantPlugins::RsyncOnlyChanged::RsyncHelper.rsync_single(opts[:machine], ssh_info, opts[:opts], changed_paths)
    finish = Time.now
    @logger.info("Time spent in rsync: #{finish-start} (in seconds)")
    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.

    opts[:machine].ui.error(I18n.t(
      "vagrant.rsync_communicator_not_ready_callback"))
    rescue Vagrant::Errors::RSyncError => e
    # Error executing rsync, so show an error

    opts[:machine].ui.error(I18n.t(
      "vagrant.rsync_auto_rsync_error", message: e.to_s))
    end
  end
  end
end

#executeObject



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
# File 'lib/vagrant-rsync-only-changed/command/rsync_auto.rb', line 32

def execute
  @logger = Log4r::Logger.new("vagrant::commands::rsync-auto-only-changed")

  options = {}
  opts = OptionParser.new do |o|
  o.banner = "Usage: vagrant rsync-auto-only-changed [vm-name]"
  o.separator ""
  o.separator "Options:"
  o.separator ""

  o.on("--[no-]poll", "Force polling filesystem (slow)") do |poll|
    options[:poll] = poll
  end
  end

  # Parse the options and return if we don't have any target.

  argv = parse_options(opts)
  return if !argv

  # Build up the paths that we need to listen to.

  paths = {}
  ignores = []
  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.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)
  if !diff[:added].empty?
    machine.ui.warn(I18n.t("vagrant.rsync_auto_new_folders"))
  end

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

  # Get the SSH info for this machine so we can do an initial

  # sync to the VM.

  ssh_info = machine.ssh_info
  if ssh_info
    machine.ui.info(I18n.t("vagrant.rsync_auto_initial"))
    folders.each do |id, folder_opts|
    RsyncHelper.rsync_single(machine, ssh_info, folder_opts)
    end
  end

  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]

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

    if folder_opts[:exclude]
    Array(folder_opts[:exclude]).each do |pattern|
      ignores << RsyncHelper.exclude_to_regexp(hostpath, pattern.to_s)
    end
    end
  end
  end

  # Exit immediately if there is nothing to watch

  if paths.empty?
  @env.ui.info(I18n.t("vagrant.rsync_auto_no_paths"))
  return 1
  end

  # Output to the user what paths we'll be watching

  paths.keys.sort.each do |path|
  paths[path].each do |path_opts|
    path_opts[:machine].ui.info(I18n.t(
    "vagrant.rsync_auto_path",
    path: path.to_s,
    ))
  end
  end

  @logger.info("Listening to paths: #{paths.keys.sort.inspect}")
  @logger.info("Ignoring #{ignores.length} paths:")
  ignores.each do |ignore|
  @logger.info("  -- #{ignore.to_s}")
  end
  @logger.info("Listening via: #{Listen::Adapter.select.inspect}")
  callback = method(:callback).to_proc.curry[paths]
  listopts = { ignore: ignores, force_polling: !!options[:poll] }
  listener = Listen.to(*paths.keys, listopts, &callback)

  # 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 listener in a busy block so that we can cleanly

  # exit once we receive an interrupt.

  Vagrant::Util::Busy.busy(callback) do
  listener.start
  queue.pop
  listener.stop if listener.state != :stopped
  end

  0
end