Class: VagrantReflect::Command::Reflect
- Inherits:
-
Object
- Object
- VagrantReflect::Command::Reflect
- Includes:
- Vagrant::Action::Builtin::MixinSyncedFolders
- Defined in:
- lib/vagrant-reflect/command/reflect.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#callback(path, opts, options, modified, added, removed) ⇒ Object
This is the callback that is called when any changes happen.
- #check_proxy(machine) ⇒ Object
- #execute ⇒ Object
- #get_folders(machine) ⇒ Object
- #get_sync_time ⇒ Object
- #strip_path(path, item) ⇒ Object
- #strip_paths(path, items) ⇒ Object
- #sync_full(path, path_opts, modified, added, removed) ⇒ Object
- #sync_incremental(path, path_opts, modified, added, removed) ⇒ Object
Class Method Details
.synopsis ⇒ Object
20 21 22 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 20 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
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 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 159 def callback(path, opts, , 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 = [: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( get_sync_time + 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.) end end end |
#check_proxy(machine) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 131 def check_proxy(machine) return machine unless machine.provider.capability?(:proxy_machine) proxy = machine.provider.capability(:proxy_machine) return machine unless proxy machine.ui.warn( I18n.t( 'vagrant.plugins.vagrant-reflect.rsync_proxy_machine', name: machine.name.to_s, provider: machine.provider_name.to_s)) proxy end |
#execute ⇒ Object
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 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 24 def execute @logger = Log4r::Logger.new('vagrant::commands::reflect') = { poll: false, incremental: true } opts = OptionParser.new do |o| o. = 'Usage: vagrant reflect [vm-name]' end # Parse the options and return if we don't have any target. argv = (opts) return unless argv # Build up the paths that we need to listen to. paths = {} with_target_vms(argv) do |machine| machine = check_proxy(machine) folders = get_folders(machine) 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 = Util::Sync.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.(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][] ignores = [] opts.each do |path_opts| ignores += Util::Excludes.convert(path_opts[:opts][:excludes] || []) 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: [: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 |
#get_folders(machine) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 146 def get_folders(machine) 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 cached[:rsync] end |
#get_sync_time ⇒ Object
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 236 def get_sync_time() # TODO: Hold this configuration per machine when we refactor with_target_vms(nil, single_target: true) do |vm| if vm.config.reflect.show_sync_time == true return '(' + Time.now.strftime("%H:%M:%S") + ') ' end end '' end |
#strip_path(path, item) ⇒ Object
253 254 255 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 253 def strip_path(path, item) item[path.length..-1] end |
#strip_paths(path, items) ⇒ Object
247 248 249 250 251 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 247 def strip_paths(path, items) items.map do |item| item[path.length..-1] end end |
#sync_full(path, path_opts, modified, added, removed) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 188 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
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/vagrant-reflect/command/reflect.rb', line 209 def sync_incremental(path, path_opts, modified, added, removed) sync_time = get_sync_time 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( sync_time + 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( sync_time + I18n.t('vagrant.plugins.vagrant-reflect.rsync_auto_increment_remove', path: item)) end end |