elasticsearch-extensions 0.0.23 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 929b42632d0c89e1007c16e6d08d465df863ad58
4
- data.tar.gz: 8ebc114b63d1cf9d64346a7da06d4fc385a401f6
3
+ metadata.gz: 7248c6fbddfe31ef013db4c097d59ba4427df2e3
4
+ data.tar.gz: 565df7537b10de2a83cbf0181a65b7580901ba83
5
5
  SHA512:
6
- metadata.gz: 053e210e305b080280e75ef955a5a6b7981cadc604cab50250308ce6a335ea0ef06e5eef655e11789b78a501604bc7399a4b747e4db6ab292812c209a7cf014e
7
- data.tar.gz: c19de28219676b133fe3850cadadd42087c9ebc3da86da055563e3e2be68c5bfa1e4d7c4aff79b68cdc629acc7dbf8674b751d4f97336558f8dff36f81deecdd
6
+ metadata.gz: 5d56926d940b28be59070ab4d98adaba0beedb9228ca8df892db425e9066342bb54b29ed6f4ef7b94b6f1d94214df876e451aae3313092ef1c277b20d8241266
7
+ data.tar.gz: 397ab511b86ae27c15c77037df6c8d598e618299bf1683d7927c0cb6bc72823e7a212285ae052c7d1d613bdabdd6518fcdcb74a3f7a3ed4c4b5436117f859158
@@ -68,7 +68,8 @@ module Elasticsearch
68
68
  #
69
69
  # reindex = Elasticsearch::Extensions::Reindex.new \
70
70
  # source: { index: 'test1', client: client },
71
- # target: { index: 'test2', transform: lambda { |doc| doc['_source']['category'].upcase! } }
71
+ # target: { index: 'test2' },
72
+ # transform: lambda { |doc| doc['_source']['category'].upcase! }
72
73
  #
73
74
  #
74
75
  # The reindexing process works by "scrolling" an index and sending
@@ -7,6 +7,7 @@ require 'json'
7
7
  require 'ansi'
8
8
 
9
9
  STDOUT.sync = true
10
+ STDERR.sync = true
10
11
 
11
12
  class String
12
13
 
@@ -24,9 +25,20 @@ module Elasticsearch
24
25
  # A convenience Ruby class for starting and stopping an Elasticsearch cluster,
25
26
  # eg. for integration tests
26
27
  #
27
- # @example Start a cluster with default configuration
28
+ # @example Start a cluster with default configuration,
29
+ # assuming `elasticsearch` is on $PATH.
30
+ #
31
+ # require 'elasticsearch/extensions/test/cluster'
32
+ # Elasticsearch::Extensions::Test::Cluster.start
33
+ #
34
+ # @example Start a cluster with a specific Elasticsearch launch script,
35
+ # eg. from a downloaded `.tar.gz` distribution
36
+ #
37
+ # system 'wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.1.tar.gz'
38
+ # system 'tar -xvf elasticsearch-5.1.1.tar.gz'
39
+ #
28
40
  # require 'elasticsearch/extensions/test/cluster'
29
- # Elasticsearch::Extensions::Test::Cluster::Cluster.new.start
41
+ # Elasticsearch::Extensions::Test::Cluster.start command: 'elasticsearch-5.1.1/bin/elasticsearch'
30
42
  #
31
43
  # @see Cluster#initialize
32
44
  #
@@ -185,6 +197,7 @@ module Elasticsearch
185
197
  # @option arguments [Integer] :timeout Timeout when starting the cluster (default: 30)
186
198
  # @option arguments [String] :network_host The host that nodes will bind on and publish to
187
199
  # @option arguments [Boolean] :clear_cluster Wipe out cluster content on startup (default: true)
200
+ # @option arguments [Boolean] :quiet Disable printing to STDERR (default: false)
188
201
  #
189
202
  # You can also use environment variables to set the constructor options (see source).
190
203
  #
@@ -205,6 +218,7 @@ module Elasticsearch
205
218
  @arguments[:timeout] ||= ENV.fetch('TEST_CLUSTER_TIMEOUT', 30).to_i
206
219
  @arguments[:number_of_nodes] ||= ENV.fetch('TEST_CLUSTER_NODES', 2).to_i
207
220
  @arguments[:network_host] ||= ENV.fetch('TEST_CLUSTER_NETWORK_HOST', __default_network_host)
221
+ @arguments[:quiet] ||= ! ENV.fetch('QUIET', '').empty?
208
222
 
209
223
  @clear_cluster = !!@arguments[:clear_cluster] || (ENV.fetch('TEST_CLUSTER_CLEAR', 'true') != 'false')
210
224
 
@@ -239,22 +253,23 @@ module Elasticsearch
239
253
  #
240
254
  def start
241
255
  if self.running?
242
- STDOUT.print "[!] Elasticsearch cluster already running".ansi(:red)
256
+ __log "[!] Elasticsearch cluster already running".ansi(:red)
243
257
  return false
244
258
  end
245
259
 
246
260
  __remove_cluster_data if @clear_cluster
247
261
 
248
- STDOUT.print "Starting ".ansi(:faint) + arguments[:number_of_nodes].to_s.ansi(:bold, :faint) +
249
- " Elasticsearch nodes..".ansi(:faint)
262
+ __log "Starting ".ansi(:faint) + arguments[:number_of_nodes].to_s.ansi(:bold, :faint) +
263
+ " Elasticsearch #{arguments[:number_of_nodes] < 2 ? 'node' : 'nodes'}..".ansi(:faint), :print
264
+
250
265
  pids = []
251
266
 
252
- STDERR.puts "Using Elasticsearch version [#{version}]" if ENV['DEBUG']
267
+ __log "\nUsing Elasticsearch version [#{version}]" if ENV['DEBUG']
253
268
 
254
269
  arguments[:number_of_nodes].times do |n|
255
270
  n += 1
256
271
  command = __command(version, arguments, n)
257
- STDERR.puts command.gsub(/ {1,}/, ' ') if ENV['DEBUG']
272
+ __log command.gsub(/ {1,}/, ' ').ansi(:bold) if ENV['DEBUG']
258
273
 
259
274
  pid = Process.spawn(command)
260
275
  Process.detach pid
@@ -264,7 +279,7 @@ module Elasticsearch
264
279
 
265
280
  __check_for_running_processes(pids)
266
281
  wait_for_green
267
- __print_cluster_info
282
+ __log __cluster_info
268
283
 
269
284
  return true
270
285
  end
@@ -286,7 +301,7 @@ module Elasticsearch
286
301
  begin
287
302
  nodes = __get_nodes
288
303
  rescue Exception => e
289
- STDERR.puts "[!] Exception raised when stopping the cluster: #{e.inspect}".ansi(:red)
304
+ __log "[!] Exception raised when stopping the cluster: #{e.inspect}".ansi(:red)
290
305
  nil
291
306
  end
292
307
 
@@ -295,13 +310,14 @@ module Elasticsearch
295
310
  pids = nodes['nodes'].map { |id, info| info['process']['id'] }
296
311
 
297
312
  unless pids.empty?
298
- STDOUT.print "\nStopping Elasticsearch nodes... ".ansi(:faint)
313
+ __log "Stopping Elasticsearch nodes... ".ansi(:faint), :print
314
+
299
315
  pids.each_with_index do |pid, i|
300
316
  ['INT','KILL'].each do |signal|
301
317
  begin
302
318
  Process.kill signal, pid
303
319
  rescue Exception => e
304
- STDOUT.print "[#{e.class}] PID #{pid} not found. ".ansi(:red)
320
+ __log "[#{e.class}] PID #{pid} not found. ".ansi(:red), :print
305
321
  end
306
322
 
307
323
  # Give the system some breathing space to finish...
@@ -313,14 +329,15 @@ module Elasticsearch
313
329
  # `getpgid` will raise error if pid is dead, so if we get here, try next signal
314
330
  next
315
331
  rescue Errno::ESRCH
316
- STDOUT.print "Stopped PID #{pid}".ansi(:green) +
332
+ __log "Stopped PID #{pid}".ansi(:green) +
317
333
  (ENV['DEBUG'] ? " with #{signal} signal".ansi(:green) : '') +
318
- ". ".ansi(:green)
334
+ ". ".ansi(:green), :print
319
335
  break # pid is dead
320
336
  end
321
337
  end
322
338
  end
323
- STDOUT.puts
339
+
340
+ __log "\n"
324
341
  else
325
342
  return false
326
343
  end
@@ -416,23 +433,35 @@ module Elasticsearch
416
433
  jar = Dir.entries(path_to_lib).select { |f| f.start_with? 'elasticsearch' }.first if File.exist? path_to_lib
417
434
 
418
435
  version = if jar
419
- if m = jar.match(/elasticsearch\-(\d+\.\d+.\d+).*/)
436
+ __log "Determining version from [#{jar}]" if ENV['DEBUG']
437
+ if m = jar.match(/elasticsearch\-(\d+\.\d+\.\d+).*/)
420
438
  m[1]
421
439
  else
422
440
  raise RuntimeError, "Cannot determine Elasticsearch version from jar [#{jar}]"
423
441
  end
424
442
  else
425
- STDERR.puts "[!] Cannot find Elasticsearch .jar from path to command [#{arguments[:command]}], using `#{arguments[:command]} --version`" if ENV['DEBUG']
443
+ __log "[!] Cannot find Elasticsearch .jar from path to command [#{arguments[:command]}], using `#{arguments[:command]} --version`" if ENV['DEBUG']
426
444
 
427
445
  unless File.exist? arguments[:command]
428
- raise Errno::ENOENT, "File [#{arguments[:command]}] does not exist -- did you pass a correct path to the Elasticsearch launch script"
446
+ __log "File [#{arguments[:command]}] does not exists, checking full path by `which`: ", :print if ENV['DEBUG']
447
+
448
+ begin
449
+ full_path = `which #{arguments[:command]}`.strip
450
+ __log "#{full_path.inspect}\n", :print if ENV['DEBUG']
451
+ rescue Exception => e
452
+ raise RuntimeError, "Cannot determine full path to [#{arguments[:command]}] with 'which'"
453
+ end
454
+
455
+ if full_path.empty?
456
+ raise Errno::ENOENT, "Cannot find Elasticsearch launch script from [#{arguments[:command]}] -- did you pass a correct path?"
457
+ end
429
458
  end
430
459
 
431
460
  output = ''
432
461
 
433
462
  begin
434
463
  # First, try the new `--version` syntax...
435
- STDERR.puts "Running [#{arguments[:command]} --version] to determine version" if ENV['DEBUG']
464
+ __log "Running [#{arguments[:command]} --version] to determine version" if ENV['DEBUG']
436
465
  rout, wout = IO.pipe
437
466
  pid = Process.spawn("#{arguments[:command]} --version", out: wout)
438
467
 
@@ -444,10 +473,12 @@ module Elasticsearch
444
473
  end
445
474
  rescue Timeout::Error
446
475
  # ...else, the old `-v` syntax
447
- STDERR.puts "Running [#{arguments[:command]} -v] to determine version" if ENV['DEBUG']
476
+ __log "Running [#{arguments[:command]} -v] to determine version" if ENV['DEBUG']
448
477
  output = `#{arguments[:command]} -v`
449
478
  ensure
450
- Process.kill('INT', pid) if pid
479
+ if pid
480
+ Process.kill('INT', pid) rescue Errno::ESRCH # Most likely the process has terminated already
481
+ end
451
482
  wout.close unless wout.closed?
452
483
  rout.close unless rout.closed?
453
484
  end
@@ -511,33 +542,33 @@ module Elasticsearch
511
542
  Timeout::timeout(timeout) do
512
543
  loop do
513
544
  response = __get_cluster_health(status)
514
- STDERR.puts response if ENV['DEBUG']
545
+ __log response if ENV['DEBUG']
515
546
 
516
547
  if response && response['status'] == status && ( arguments[:number_of_nodes].nil? || arguments[:number_of_nodes].to_i == response['number_of_nodes'].to_i )
517
548
  break
518
549
  end
519
550
 
520
- STDOUT.print '.'.ansi(:faint)
551
+ __log '.'.ansi(:faint), :print
521
552
  sleep 1
522
553
  end
523
554
  end
524
555
  rescue Timeout::Error => e
525
556
  message = "\nTimeout while waiting for cluster status [#{status}]"
526
557
  message += " and [#{arguments[:number_of_nodes]}] nodes" if arguments[:number_of_nodes]
527
- STDOUT.puts message.ansi(:red, :bold)
558
+ __log message.ansi(:red, :bold)
528
559
  raise e
529
560
  end
530
561
 
531
562
  return true
532
563
  end
533
564
 
534
- # Print information about the cluster on STDOUT
565
+ # Return information about the cluster
535
566
  #
536
567
  # @api private
537
568
  #
538
- # @return Nil
569
+ # @return String
539
570
  #
540
- def __print_cluster_info
571
+ def __cluster_info
541
572
  health = JSON.parse(Net::HTTP.get(URI("#{__cluster_url}/_cluster/health")))
542
573
  nodes = if version == '0.90'
543
574
  JSON.parse(Net::HTTP.get(URI("#{__cluster_url}/_nodes/?process&http")))
@@ -546,21 +577,23 @@ module Elasticsearch
546
577
  end
547
578
  master = JSON.parse(Net::HTTP.get(URI("#{__cluster_url}/_cluster/state")))['master_node']
548
579
 
549
- puts "\n",
550
- ('-'*80).ansi(:faint),
551
- 'Cluster: '.ljust(20).ansi(:faint) + health['cluster_name'].to_s.ansi(:faint),
552
- 'Status: '.ljust(20).ansi(:faint) + health['status'].to_s.ansi(:faint),
553
- 'Nodes: '.ljust(20).ansi(:faint) + health['number_of_nodes'].to_s.ansi(:faint)
580
+ result = ["\n",
581
+ ('-'*80).ansi(:faint),
582
+ 'Cluster: '.ljust(20).ansi(:faint) + health['cluster_name'].to_s.ansi(:faint),
583
+ 'Status: '.ljust(20).ansi(:faint) + health['status'].to_s.ansi(:faint),
584
+ 'Nodes: '.ljust(20).ansi(:faint) + health['number_of_nodes'].to_s.ansi(:faint)].join("\n")
554
585
 
555
586
  nodes['nodes'].each do |id, info|
556
587
  m = id == master ? '*' : '+'
557
- puts ''.ljust(20) +
558
- "#{m} ".ansi(:faint) +
559
- "#{info['name'].ansi(:bold)} ".ansi(:faint) +
560
- "| version: #{info['version'] rescue 'N/A'}, ".ansi(:faint) +
561
- "pid: #{info['process']['id'] rescue 'N/A'}, ".ansi(:faint) +
562
- "address: #{info['http']['bound_address'] rescue 'N/A'}".ansi(:faint)
588
+ result << ''.ljust(20) +
589
+ "#{m} ".ansi(:faint) +
590
+ "#{info['name'].ansi(:bold)} ".ansi(:faint) +
591
+ "| version: #{info['version'] rescue 'N/A'}, ".ansi(:faint) +
592
+ "pid: #{info['process']['id'] rescue 'N/A'}, ".ansi(:faint) +
593
+ "address: #{info['http']['bound_address'] rescue 'N/A'}".ansi(:faint)
563
594
  end
595
+
596
+ result
564
597
  end
565
598
 
566
599
  # Tries to load cluster health information
@@ -598,7 +631,7 @@ module Elasticsearch
598
631
  #
599
632
  def __check_for_running_processes(pids)
600
633
  if `ps -p #{pids.join(' ')}`.split("\n").size < arguments[:number_of_nodes]+1
601
- STDERR.puts "", "[!!!] Process failed to start (see output above)".ansi(:red)
634
+ __log "\n[!!!] Process failed to start (see output above)".ansi(:red)
602
635
  exit(1)
603
636
  end
604
637
  end
@@ -610,6 +643,12 @@ module Elasticsearch
610
643
  def __get_nodes
611
644
  JSON.parse(Net::HTTP.get(URI("#{__cluster_url}/_nodes/process")))
612
645
  end
646
+
647
+ # Print to STDERR
648
+ #
649
+ def __log(message, mode=:puts)
650
+ STDERR.__send__ mode, message unless @arguments[:quiet]
651
+ end
613
652
  end
614
653
  end
615
654
  end
@@ -1,5 +1,5 @@
1
1
  module Elasticsearch
2
2
  module Extensions
3
- VERSION = "0.0.23"
3
+ VERSION = "0.0.24"
4
4
  end
5
5
  end
@@ -102,6 +102,13 @@ class Elasticsearch::Extensions::TestClusterTest < Test::Unit::TestCase
102
102
  @subject.__remove_cluster_data
103
103
  end
104
104
 
105
+ should "not log when :quiet" do
106
+ c = Cluster::Cluster.new quiet: true
107
+
108
+ STDERR.expects(:puts).never
109
+ c.__log 'QUIET'
110
+ end
111
+
105
112
  context "when starting a cluster, " do
106
113
  should "return false when it's already running" do
107
114
  Process.expects(:spawn).never
@@ -127,7 +134,7 @@ class Elasticsearch::Extensions::TestClusterTest < Test::Unit::TestCase
127
134
  c.expects(:wait_for_green).returns(true)
128
135
  c.expects(:__check_for_running_processes).returns(true)
129
136
  c.expects(:__determine_version).returns('5.0')
130
- c.expects(:__print_cluster_info).returns(true)
137
+ c.expects(:__cluster_info).returns('CLUSTER INFO')
131
138
 
132
139
  assert_equal true, c.start
133
140
  end
@@ -198,8 +205,6 @@ class Elasticsearch::Extensions::TestClusterTest < Test::Unit::TestCase
198
205
  end
199
206
 
200
207
  should "return true" do
201
- @subject.stubs(:__print_cluster_info)
202
-
203
208
  @subject
204
209
  .expects(:__get_cluster_health)
205
210
  .with('yellow')
@@ -254,6 +259,7 @@ class Elasticsearch::Extensions::TestClusterTest < Test::Unit::TestCase
254
259
 
255
260
  should "return version from `elasticsearch --version`" do
256
261
  File.expects(:exist?).with('/foo/bar/bin/../lib/').returns(false)
262
+ File.expects(:exist?).with('/foo/bar/bin/elasticsearch').returns(true)
257
263
 
258
264
  Process.stubs(:wait)
259
265
  Process.expects(:spawn)
@@ -270,7 +276,7 @@ class Elasticsearch::Extensions::TestClusterTest < Test::Unit::TestCase
270
276
  end
271
277
 
272
278
  should "raise an exception when the version cannot be parsed from .jar" do
273
- # Incorrect jar version
279
+ # Incorrect jar version (no dots)
274
280
  File.expects(:exist?).with('/foo/bar/bin/../lib/').returns(true)
275
281
  Dir.expects(:entries).with('/foo/bar/bin/../lib/').returns(['elasticsearch-100.jar'])
276
282
 
@@ -279,6 +285,7 @@ class Elasticsearch::Extensions::TestClusterTest < Test::Unit::TestCase
279
285
 
280
286
  should "raise an exception when the version cannot be parsed from command output" do
281
287
  File.expects(:exist?).with('/foo/bar/bin/../lib/').returns(false)
288
+ File.expects(:exist?).with('/foo/bar/bin/elasticsearch').returns(true)
282
289
 
283
290
  Process.stubs(:wait)
284
291
  Process.expects(:spawn)
@@ -300,6 +307,16 @@ class Elasticsearch::Extensions::TestClusterTest < Test::Unit::TestCase
300
307
 
301
308
  assert_raise(RuntimeError) { @subject.__determine_version }
302
309
  end
310
+
311
+ should "raise an exception when the command cannot be found" do
312
+ @subject = Elasticsearch::Extensions::Test::Cluster::Cluster.new
313
+
314
+ File.expects(:exist?).with('./../lib/').returns(false)
315
+ File.expects(:exist?).with('elasticsearch').returns(false)
316
+ @subject.expects(:`).returns('')
317
+
318
+ assert_raise(Errno::ENOENT) { @subject.__determine_version }
319
+ end
303
320
  end
304
321
  end
305
322
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-18 00:00:00.000000000 Z
11
+ date: 2017-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ansi