class Warg::HostCollection

Attributes

hosts[R]

Public Class Methods

from(value) click to toggle source
# File lib/warg.rb, line 1112
def self.from(value)
  case value
  when String, Host
    new.add(value)
  when HostCollection
    value
  when Array
    is_array_host_specification = value.any? do |item|
      # Check key=value items by looking for `=` missing `?`.  If it has `?`, then we
      # assume it is in the form `host?key=value`
      String === item and item.index("=") and not item.index("?")
    end

    if is_array_host_specification
      new.add(value)
    else
      value.inject(new) { |collection, host_data| collection.add(host_data) }
    end
  when Hash
    value.inject(new) do |collection, (property, hosts_data)|
      name, value = property.to_s.split(":", 2)

      if value.nil?
        value = name
        name = "stage"
      end

      from(hosts_data).each do |host|
        host[name] = value
        collection.add(host)
      end

      collection
    end
  when nil
    new
  else
    raise InvalidHostDataError.new(value)
  end
end
new() click to toggle source
# File lib/warg.rb, line 1153
def initialize
  @hosts = []
end

Public Instance Methods

==(other) click to toggle source
# File lib/warg.rb, line 1175
def ==(other)
  self.class == other.class &&
    length == other.length &&
    # both are the same length and their intersection is the same length (all the same
    # elements in common)
    length == @hosts.&(other.hosts).length
end
Also aliased as: eql?
add(host_data) click to toggle source
# File lib/warg.rb, line 1165
def add(host_data)
  @hosts << Host.from(host_data)

  self
end
create_file_from(content, path:, mode: 0644) click to toggle source
# File lib/warg.rb, line 1189
def create_file_from(content, path:, mode: 0644)
  each do |host|
    host.create_file_from(content, path: path, mode: mode)
  end
end
download(path) click to toggle source
# File lib/warg.rb, line 1201
def download(path)
  map do |host|
    host.download(path)
  end
end
each() { |host| ... } click to toggle source
# File lib/warg.rb, line 1207
def each
  if block_given?
    @hosts.each { |host| yield host }
  else
    enum_for(:each)
  end

  self
end
eql?(other)
Alias for: ==
length() click to toggle source
# File lib/warg.rb, line 1185
def length
  @hosts.length
end
one() click to toggle source
# File lib/warg.rb, line 1157
def one
  if @hosts.empty?
    raise "cannot pick a host from `#{inspect}'; collection is empty"
  end

  HostCollection.from @hosts.sample
end
run(order:, &block) click to toggle source
# File lib/warg.rb, line 1229
def run(order:, &block)
  strategy = Executor.for(order)
  executor = strategy.new(self)
  executor.run(&block)
end
run_command(command, order: :parallel, &setup) click to toggle source
# File lib/warg.rb, line 1223
def run_command(command, order: :parallel, &setup)
  run(order: order) do |host, result|
    result.update host.run_command(command, &setup)
  end
end
run_script(script, order: :parallel, &setup) click to toggle source
# File lib/warg.rb, line 1217
def run_script(script, order: :parallel, &setup)
  run(order: order) do |host, result|
    result.update host.run_script(script, &setup)
  end
end
to_a() click to toggle source
# File lib/warg.rb, line 1235
def to_a
  @hosts.dup
end
upload(file, to:) click to toggle source
# File lib/warg.rb, line 1195
def upload(file, to:)
  each do |host|
    host.upload(file, to: to)
  end
end
with(**filters) click to toggle source
# File lib/warg.rb, line 1171
def with(**filters)
  HostCollection.from(select { |host| host.matches?(**filters) })
end