Class: VagrantPlugins::InstantRsyncAuto::Command::RsyncAuto
- Inherits:
-
Object
- Object
- VagrantPlugins::InstantRsyncAuto::Command::RsyncAuto
- Includes:
- Vagrant::Action::Builtin::MixinSyncedFolders
- Defined in:
- lib/vagrant-instant-rsync-auto/command/instant_rsync_auto.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#callback(paths, modified, added, removed) ⇒ Object
This is the callback that is called when any changes happen.
- #execute ⇒ Object
- #rsync_helper(machine, folder_id, folder_opts) ⇒ Object
- #rsync_helpers_for_id(machine, folder_id, folder_opts, machine_helpers) ⇒ Object
- #rsync_helpers_for_machine(machine) ⇒ Object
Class Method Details
.synopsis ⇒ Object
24 25 26 |
# File 'lib/vagrant-instant-rsync-auto/command/instant_rsync_auto.rb', line 24 def self.synopsis "syncs rsync synced folders automatically when files change, faster than Vagrant's native rsync-auto" end |
Instance Method Details
#callback(paths, modified, added, removed) ⇒ Object
This is the callback that is called when any changes happen
152 153 154 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 207 208 209 210 211 212 213 |
# File 'lib/vagrant-instant-rsync-auto/command/instant_rsync_auto.rb', line 152 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}") tosync = [] paths.each do |hostpath, folders| # Find out if this path should be synced found = catch(:done) do [modified, added, removed].each do |changed| changed.each do |listenpath| throw :done, true if listenpath.start_with?(hostpath) end end # Make sure to return false if all else fails so that we # don't sync to this machine. false end # If it should be synced, store it for later tosync << folders if found end # Sync all the folders that need to be synced tosync.each do |folders| 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 begin rsync_helper = rsync_helper(opts[:machine], opts[:id], opts[:opts]) unless rsync_helper # couldn't find SSH info for that machine raise Vagrant::Errors::MachineGuestNotReady end start = Time.now rsync_helper.rsync_single finish = Time.now time_spent_msg = "Time spent in rsync: #{finish-start} (in seconds)" @logger.info(time_spent_msg) opts[:machine].ui.info(time_spent_msg) 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 |
#execute ⇒ Object
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 |
# File 'lib/vagrant-instant-rsync-auto/command/instant_rsync_auto.rb', line 28 def execute @logger = Log4r::Logger.new("vagrant::commands::instant-rsync-auto") @rsync_helpers = {} = {} opts = OptionParser.new do |o| o. = "Usage: vagrant instant-rsync-auto [vm-name]" o.separator "" o.separator "Options:" o.separator "" o.on("--[no-]poll", "Force polling filesystem (slow)") do |poll| [:poll] = poll end end # Parse the options and return if we don't have any target. argv = (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| rsync_helper(machine, id, folder_opts).rsync_single 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.(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: !![: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 |
#rsync_helper(machine, folder_id, folder_opts) ⇒ Object
215 216 217 218 219 |
# File 'lib/vagrant-instant-rsync-auto/command/instant_rsync_auto.rb', line 215 def rsync_helper(machine, folder_id, folder_opts) machine_helpers = rsync_helpers_for_machine(machine) rsync_helpers_for_id(machine, folder_id, folder_opts, machine_helpers) end |
#rsync_helpers_for_id(machine, folder_id, folder_opts, machine_helpers) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/vagrant-instant-rsync-auto/command/instant_rsync_auto.rb', line 225 def rsync_helpers_for_id(machine, folder_id, folder_opts, machine_helpers) unless machine_helpers.key?(folder_id) rsync_helper = nil ssh_info = machine.ssh_info if ssh_info folder_opts = folder_opts.merge( skip_rsync_pre_after_first_sync: true, skip_rsync_post_after_first_sync: true ) rsync_helper = RsyncHelper.new(machine, ssh_info, folder_opts) end machine_helpers[folder_id] = rsync_helper end machine_helpers[folder_id] end |
#rsync_helpers_for_machine(machine) ⇒ Object
221 222 223 |
# File 'lib/vagrant-instant-rsync-auto/command/instant_rsync_auto.rb', line 221 def rsync_helpers_for_machine(machine) @rsync_helpers[machine.id] ||= {} end |