Class: Warg::HostCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/warg.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHostCollection

Returns a new instance of HostCollection.



1153
1154
1155
# File 'lib/warg.rb', line 1153

def initialize
  @hosts = []
end

Class Method Details

.from(value) ⇒ Object



1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
# 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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



1175
1176
1177
1178
1179
1180
1181
# 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

#add(host_data) ⇒ Object



1165
1166
1167
1168
1169
# File 'lib/warg.rb', line 1165

def add(host_data)
  @hosts << Host.from(host_data)

  self
end

#create_file_from(content, path:, mode: 0644) ⇒ Object



1189
1190
1191
1192
1193
# 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) ⇒ Object



1201
1202
1203
1204
1205
# File 'lib/warg.rb', line 1201

def download(path)
  map do |host|
    host.download(path)
  end
end

#eachObject



1207
1208
1209
1210
1211
1212
1213
1214
1215
# File 'lib/warg.rb', line 1207

def each
  if block_given?
    @hosts.each { |host| yield host }
  else
    enum_for(:each)
  end

  self
end

#lengthObject



1185
1186
1187
# File 'lib/warg.rb', line 1185

def length
  @hosts.length
end

#oneObject



1157
1158
1159
1160
1161
1162
1163
# 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) ⇒ Object



1229
1230
1231
1232
1233
# 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) ⇒ Object



1223
1224
1225
1226
1227
# 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) ⇒ Object



1217
1218
1219
1220
1221
# 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_aObject



1235
1236
1237
# File 'lib/warg.rb', line 1235

def to_a
  @hosts.dup
end

#upload(file, to:) ⇒ Object



1195
1196
1197
1198
1199
# File 'lib/warg.rb', line 1195

def upload(file, to:)
  each do |host|
    host.upload(file, to: to)
  end
end

#with(**filters) ⇒ Object



1171
1172
1173
# File 'lib/warg.rb', line 1171

def with(**filters)
  HostCollection.from(select { |host| host.matches?(**filters) })
end