Class: VagrantPlugins::GatlingRsync::GatlingRsyncAuto

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



11
12
13
# File 'lib/vagrant-gatling-rsync/command/rsync_auto.rb', line 11

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

Instance Method Details

#callback(paths, ignores, modified) ⇒ Object

This callback gets called when any directory changes.



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

def callback(paths, ignores, modified)
  @logger.info("File change callback called!")
  @logger.info("  - Paths: #{paths.inspect}")
  @logger.info("  - Ignores: #{ignores.inspect}")
  @logger.info("  - Modified: #{modified.inspect}")

  tosync = []
  paths.each do |hostpath, folders|
    # Find out if this path should be synced
    found = catch(:done) do
      modified.each do |changed|
        match = nil
        ignores.each do |ignore|
          next unless match.nil?
          match = ignore.match(changed)
        end

        next unless match.nil?
        throw :done, true if changed.start_with?(hostpath)
      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|
      ssh_info = opts[:machine].ssh_info
      VagrantPlugins::SyncedFolderRSync::RsyncHelper.rsync_single(opts[:machine], ssh_info, opts[:opts])
    end
  end
end

#executeObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vagrant-gatling-rsync/command/rsync_auto.rb', line 15

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

  opts = OptionParser.new do |o|
    o.banner = "Usage: vagrant gatling-rsync-auto [vm-name]"
    o.separator ""
  end

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

  latency = nil

  # Build up the paths that we need to listen to.
  paths = {}
  ignores = []
  with_target_vms(argv) do |machine|
    latency = machine.config.gatling.latency

    folders = synced_folders(machine)[: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.has_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 << VagrantPlugins::SyncedFolderRSync::RsyncHelper.exclude_to_regexp(hostpath, pattern.to_s)
        end
      end
    end
  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

  case RUBY_PLATFORM
  when /darwin/
    ListenOSX.new(paths, ignores, latency, @logger, self.method(:callback)).run
  when /linux/
    ListenLinux.new(paths, ignores, latency, @logger, self.method(:callback)).run
  else
    # @TODO: Raise this earlier?
    raise Errors::OnlyOSXLinuxSupportError
  end

  0
end