warg 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/warg.rb +111 -31
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 424f245fa2ac9ae05aff9e1f4c66e9c5a7a9b5660436e0d5d91a5653fc062d44
4
- data.tar.gz: a8bc6efa101af3b880fb2a5e012f55e8a8b3510180fe33248df0cffa3f54c724
3
+ metadata.gz: 1dae787424c000c9f5503d44a1a0f4653c5f2ae9fcc50e0afe24a030de34716b
4
+ data.tar.gz: 925e761cbb6409606f42791bd5c18c8cfc2c7648f996ace762d01201eca527c7
5
5
  SHA512:
6
- metadata.gz: 97c01e3dfdf6df8786a91ba9532917bf3a9e270256eea18bf0cb33e2754fad957403ab9139072bb69ff1a0d6cfbcd082bbdf8cd017916531eb6846bd1fadb986
7
- data.tar.gz: fd3d3247c2635dded91bfe9fb954a3d18fe55a6b789656e50b593579b069621421da7e6ddb339bb87ea9585604e27556bb4ca97dd890d6069b66e01f716882b0
6
+ metadata.gz: 3604a78c642404593ca9ee445e6cf8e861b688ac6df2aeadb700f21dafa20ad283adf4a05d0d375cecc854d3a93d20966656fcdede08c2b49b5a74574ad914ec
7
+ data.tar.gz: 0f8557c8b1ab338728813b6d4b4da8ccaa8629f47ee381d0585c4efe011741943edfdc0fdd84cf6539ab0ab46429388783a06e6b81d7fc067edc73861fc75c44
data/lib/warg.rb CHANGED
@@ -1159,22 +1159,8 @@ module Warg
1159
1159
  @hosts.length
1160
1160
  end
1161
1161
 
1162
- def create_file_from(content, path:, mode: 0644)
1163
- each do |host|
1164
- host.create_file_from(content, path: path, mode: mode)
1165
- end
1166
- end
1167
-
1168
- def upload(file, to:)
1169
- each do |host|
1170
- host.upload(file, to: to)
1171
- end
1172
- end
1173
-
1174
- def download(path)
1175
- map do |host|
1176
- host.download(path)
1177
- end
1162
+ def lazy
1163
+ LazilyFilteredHostCollection.new(self)
1178
1164
  end
1179
1165
 
1180
1166
  def each
@@ -1187,24 +1173,46 @@ module Warg
1187
1173
  self
1188
1174
  end
1189
1175
 
1190
- def run_script(script, order: :parallel, &setup)
1191
- run(order: order) do |host, result|
1192
- result.update host.run_script(script, &setup)
1176
+ module Interaction
1177
+ def create_file_from(content, path:, mode: 0644)
1178
+ each do |host|
1179
+ host.create_file_from(content, path: path, mode: mode)
1180
+ end
1193
1181
  end
1194
- end
1195
1182
 
1196
- def run_command(command, order: :parallel, &setup)
1197
- run(order: order) do |host, result|
1198
- result.update host.run_command(command, &setup)
1183
+ def upload(file, to:)
1184
+ each do |host|
1185
+ host.upload(file, to: to)
1186
+ end
1187
+ end
1188
+
1189
+ def download(path)
1190
+ map do |host|
1191
+ host.download(path)
1192
+ end
1193
+ end
1194
+
1195
+ def run_script(script, order: :parallel, &setup)
1196
+ run(order: order) do |host, result|
1197
+ result.update host.run_script(script, &setup)
1198
+ end
1199
1199
  end
1200
- end
1201
1200
 
1202
- def run(order:, &block)
1203
- strategy = Executor.for(order)
1204
- executor = strategy.new(self)
1205
- executor.run(&block)
1201
+ def run_command(command, order: :parallel, &setup)
1202
+ run(order: order) do |host, result|
1203
+ result.update host.run_command(command, &setup)
1204
+ end
1205
+ end
1206
+
1207
+ def run(order:, &block)
1208
+ strategy = Executor.for(order)
1209
+ executor = strategy.new(self)
1210
+ executor.run(&block)
1211
+ end
1206
1212
  end
1207
1213
 
1214
+ include Interaction
1215
+
1208
1216
  def to_a
1209
1217
  @hosts.dup
1210
1218
  end
@@ -1214,6 +1222,78 @@ module Warg
1214
1222
  attr_reader :hosts
1215
1223
  end
1216
1224
 
1225
+ class LazilyFilteredHostCollection
1226
+ include Enumerable
1227
+ include HostCollection::Interaction
1228
+
1229
+ attr_reader :filters
1230
+
1231
+ def self.from(value)
1232
+ new HostCollection.from(value)
1233
+ end
1234
+
1235
+ def initialize(hosts)
1236
+ @hosts = hosts
1237
+ @filters = {}
1238
+ end
1239
+
1240
+ def one
1241
+ host = to_a.sample
1242
+
1243
+ if host.nil?
1244
+ raise "no hosts matched `#{inspect}'"
1245
+ else
1246
+ HostCollection.from(host)
1247
+ end
1248
+ end
1249
+
1250
+ def add(host_data)
1251
+ host = Host.from(host_data)
1252
+
1253
+ if host.matches?(**@filters)
1254
+ @hosts.add(host)
1255
+ self
1256
+ else
1257
+ false
1258
+ end
1259
+ end
1260
+
1261
+ def with(**filters)
1262
+ combined = @filters.merge(filters)
1263
+
1264
+ HostCollection.new(@hosts.select { |host| host.matches?(combined) })
1265
+ end
1266
+
1267
+ def ==(other)
1268
+ case other
1269
+ when HostCollection, LazilyFilteredHostCollection
1270
+ to_a == other.to_a
1271
+ when Enumerable
1272
+ to_a == other
1273
+ else
1274
+ super
1275
+ end
1276
+ end
1277
+
1278
+ alias eql? ==
1279
+
1280
+ def length
1281
+ count
1282
+ end
1283
+
1284
+ def each
1285
+ if block_given?
1286
+ @hosts.each do |host|
1287
+ if host.matches?(**@filters)
1288
+ yield host
1289
+ end
1290
+ end
1291
+ else
1292
+ enum_for(:each)
1293
+ end
1294
+ end
1295
+ end
1296
+
1217
1297
  class Config
1218
1298
  attr_accessor :default_user
1219
1299
  attr_reader :hosts
@@ -1357,7 +1437,6 @@ module Warg
1357
1437
  @argv = argv
1358
1438
  @playlist = playlist
1359
1439
  @parser = OptionParser.new
1360
- @filters = {}
1361
1440
 
1362
1441
  @parser.on("-t", "--target HOSTS", Array, "hosts to use") do |hosts_data|
1363
1442
  hosts_data.each { |host_data| hosts.add(host_data) }
@@ -1366,15 +1445,16 @@ module Warg
1366
1445
  @parser.on("-f", "--filter key=value", String, "filters to apply to hosts") do |filter|
1367
1446
  key, value = filter.split("=", 2)
1368
1447
 
1369
- @filters[key] = value
1448
+ @hosts.filters[key] = value
1370
1449
  end
1371
1450
 
1372
1451
  super()
1452
+
1453
+ @hosts = @hosts.lazy
1373
1454
  end
1374
1455
 
1375
1456
  def parse_options!
1376
1457
  @parser.parse(@argv)
1377
- @hosts = @hosts.with(**@filters)
1378
1458
  end
1379
1459
 
1380
1460
  def copy(config)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo Gutierrez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-11 00:00:00.000000000 Z
11
+ date: 2023-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt_pbkdf