From 1529883231aa9745ff994b6825af87a16175661c Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Wed, 10 May 2017 16:15:22 -0700 Subject: [PATCH 1/9] Revise solr_wrapper commands - provide clean, status, stop - do not allow solr_wrapper in production, esp. clean - status returns an info string, use started? for boolean - status and started? do not check managed? because it's not reliable - clean command actually runs clean! - do not clean up the downloaded .zip file --- exe/solr_wrapper | 33 ++++++++++++++++++++++----------- lib/solr_wrapper/instance.rb | 29 ++++++++++++----------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/exe/solr_wrapper b/exe/solr_wrapper index 3363757..bfaa806 100755 --- a/exe/solr_wrapper +++ b/exe/solr_wrapper @@ -1,5 +1,9 @@ #!/usr/bin/env ruby +if ENV['RAILS_ENV'] && ENV['RAILS_ENV'] == 'production' + $stderr.puts 'Cannot use solr_wrapper in production' +end + require 'solr_wrapper' require 'optparse' @@ -73,12 +77,18 @@ end subcommands = { - 'clean' => OptionParser.new do |opts| - opts.banner = "Usage: clean" - end, - 'dir' => OptionParser.new do |opts| - opts.banner = "Usage: dir" - end, + 'clean' => OptionParser.new do |opts| + opts.banner = "Usage: clean" + end, + 'dir' => OptionParser.new do |opts| + opts.banner = "Usage: dir" + end, + 'status' => OptionParser.new do |opts| + opts.banner = "Usage: status" + end, + 'stop' => OptionParser.new do |opts| + opts.banner = "Usage: stop" + end, } @@ -100,14 +110,15 @@ instance = SolrWrapper.instance(options) case command when 'clean' - if instance.started? - $stderr.puts "Please stop solr before cleaning" - exit 1 - end + return if ENV['RAILS_ENV'] && ENV['RAILS_ENV'] == 'production' $stderr.puts "cleaning #{instance.instance_dir}..." - instance.remove_instance_dir! + instance.clean! when 'dir' puts instance.instance_dir +when 'status' + puts instance.status +when 'stop' + instance.stop else $stderr.print "Starting Solr #{instance.version} on port #{instance.port} ... " instance.wrap do |conn| diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index 770dd52..d11489c 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -75,7 +75,7 @@ def start exec('start', p: port, c: config.cloud) # Wait for solr to start - unless status + unless started? sleep config.poll_interval end @@ -103,20 +103,22 @@ def restart ## # Check the status of a managed Solr service def status - return true unless config.managed? - - out = exec('status').read - out =~ /running on port #{port}/ + exec('status').read rescue - false + 'No status information available' + end + + ## + # Is Solr running? + def started? + status =~ /running on port #{port}/ && true || false end def pid return unless config.managed? @pid ||= begin - out = exec('status').read - out.match(/process (?\d+) running on port #{port}/) do |m| + status.match(/process (?\d+) running on port #{port}/) do |m| m[:pid].to_i end end @@ -124,14 +126,8 @@ def pid nil end - ## - # Is Solr running? - def started? - !!status - end - def wait - while (Process.getpgid(pid) rescue status) + while (Process.getpgid(pid) rescue started?) sleep config.poll_interval end end @@ -222,11 +218,10 @@ def with_collection(options = {}) end ## - # Clean up any files solr_wrapper may have downloaded + # Clean up any files solr_wrapper may have downloaded, except the solr-{ver}.zip file def clean! stop remove_instance_dir! - FileUtils.remove_entry(config.download_dir, true) if File.exist?(config.download_dir) FileUtils.remove_entry(config.tmp_save_dir, true) if File.exist? config.tmp_save_dir md5.clean! FileUtils.remove_entry(config.version_file) if File.exist? config.version_file From 71534b337b88ea5ba70ac15a659cd3b670cc59b2 Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Wed, 10 May 2017 17:09:05 -0700 Subject: [PATCH 2/9] SolrWrapper::Instance - clean! calls clean to clean up everything --- exe/solr_wrapper | 2 +- lib/solr_wrapper/instance.rb | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/exe/solr_wrapper b/exe/solr_wrapper index bfaa806..639d00a 100755 --- a/exe/solr_wrapper +++ b/exe/solr_wrapper @@ -112,7 +112,7 @@ case command when 'clean' return if ENV['RAILS_ENV'] && ENV['RAILS_ENV'] == 'production' $stderr.puts "cleaning #{instance.instance_dir}..." - instance.clean! + instance.clean when 'dir' puts instance.instance_dir when 'status' diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index d11489c..8059716 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -219,14 +219,21 @@ def with_collection(options = {}) ## # Clean up any files solr_wrapper may have downloaded, except the solr-{ver}.zip file - def clean! + def clean stop remove_instance_dir! FileUtils.remove_entry(config.tmp_save_dir, true) if File.exist? config.tmp_save_dir - md5.clean! FileUtils.remove_entry(config.version_file) if File.exist? config.version_file end + ## + # Clean up everything solr_wrapper may have downloaded + def clean! + clean + md5.clean! + FileUtils.remove_entry(config.download_dir, true) if File.exist?(config.download_dir) + end + ## # Clean up any files in the Solr instance dir def remove_instance_dir! From 92b524d71795e5d08fae94323cd494fa84fed692 Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Wed, 10 May 2017 17:16:24 -0700 Subject: [PATCH 3/9] exe/solr_wrapper - remove checks on RAILS_ENV --- exe/solr_wrapper | 5 ----- 1 file changed, 5 deletions(-) diff --git a/exe/solr_wrapper b/exe/solr_wrapper index 639d00a..2803ecf 100755 --- a/exe/solr_wrapper +++ b/exe/solr_wrapper @@ -1,9 +1,5 @@ #!/usr/bin/env ruby -if ENV['RAILS_ENV'] && ENV['RAILS_ENV'] == 'production' - $stderr.puts 'Cannot use solr_wrapper in production' -end - require 'solr_wrapper' require 'optparse' @@ -110,7 +106,6 @@ instance = SolrWrapper.instance(options) case command when 'clean' - return if ENV['RAILS_ENV'] && ENV['RAILS_ENV'] == 'production' $stderr.puts "cleaning #{instance.instance_dir}..." instance.clean when 'dir' From eef5040cb67c110145b466c79b34e145d50061c4 Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Wed, 10 May 2017 17:25:28 -0700 Subject: [PATCH 4/9] SolrWrapper::Instance - status reports exception details --- lib/solr_wrapper/instance.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index 8059716..e54aef1 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -104,8 +104,12 @@ def restart # Check the status of a managed Solr service def status exec('status').read - rescue - 'No status information available' + rescue => err + [ + 'No status information available', + "message: #{err.message}", + "stack trace: #{err.backtrace}" + ].join("\n") end ## From d684ae98f105cbe835866e891cf1b2febe2b4fb9 Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Wed, 10 May 2017 17:34:51 -0700 Subject: [PATCH 5/9] SolrWrapper::Instance - add status_info, preserve status and other public API --- exe/solr_wrapper | 2 +- lib/solr_wrapper/instance.rb | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/exe/solr_wrapper b/exe/solr_wrapper index 2803ecf..ea4f6ae 100755 --- a/exe/solr_wrapper +++ b/exe/solr_wrapper @@ -111,7 +111,7 @@ when 'clean' when 'dir' puts instance.instance_dir when 'status' - puts instance.status + puts instance.status_info when 'stop' instance.stop else diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index e54aef1..c520156 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -101,8 +101,18 @@ def restart end ## - # Check the status of a managed Solr service - def status + # Is Solr running? + def started? + status_info =~ /running on port #{port}/ && true || false + end + + ## + # Check the status of a Solr service + alias status started? + + ## + # Get the status information for a Solr service + def status_info exec('status').read rescue => err [ @@ -112,17 +122,11 @@ def status ].join("\n") end - ## - # Is Solr running? - def started? - status =~ /running on port #{port}/ && true || false - end - def pid return unless config.managed? @pid ||= begin - status.match(/process (?\d+) running on port #{port}/) do |m| + status_info.match(/process (?\d+) running on port #{port}/) do |m| m[:pid].to_i end end From 0ac97042fa8947ab288732eaa0b078bed889837f Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Thu, 18 May 2017 17:39:28 -0700 Subject: [PATCH 6/9] exe/solr_wrapper - update use information and add "clean!" to purge everything --- exe/solr_wrapper | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/exe/solr_wrapper b/exe/solr_wrapper index ea4f6ae..0bf677b 100755 --- a/exe/solr_wrapper +++ b/exe/solr_wrapper @@ -7,8 +7,11 @@ options = {} collection_options = {} subtext = < OptionParser.new do |opts| opts.banner = "Usage: clean" end, + 'clean!' => OptionParser.new do |opts| + opts.banner = "Usage: clean!" + end, 'dir' => OptionParser.new do |opts| opts.banner = "Usage: dir" end, @@ -106,8 +112,11 @@ instance = SolrWrapper.instance(options) case command when 'clean' - $stderr.puts "cleaning #{instance.instance_dir}..." + $stderr.puts "cleaning solr instance: #{instance.instance_dir} ..." instance.clean +when 'clean!' + $stderr.puts "cleaning solr downloads and solr instance: #{instance.instance_dir} ..." + instance.clean! when 'dir' puts instance.instance_dir when 'status' From 8d83799ec0f638e6f2534c038bcf860e7f04a664 Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Mon, 22 May 2017 11:23:50 -0700 Subject: [PATCH 7/9] SolrWrapper::Instance#clean! - clean_downloads param can toggle download file removal --- exe/solr_wrapper | 12 ++++++------ lib/solr_wrapper/instance.rb | 18 +++++++----------- lib/solr_wrapper/md5.rb | 2 +- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/exe/solr_wrapper b/exe/solr_wrapper index 0bf677b..9daf731 100755 --- a/exe/solr_wrapper +++ b/exe/solr_wrapper @@ -8,7 +8,7 @@ collection_options = {} subtext = < OptionParser.new do |opts| opts.banner = "Usage: clean" end, - 'clean!' => OptionParser.new do |opts| - opts.banner = "Usage: clean!" + 'purge' => OptionParser.new do |opts| + opts.banner = "Usage: purge" end, 'dir' => OptionParser.new do |opts| opts.banner = "Usage: dir" @@ -113,10 +113,10 @@ instance = SolrWrapper.instance(options) case command when 'clean' $stderr.puts "cleaning solr instance: #{instance.instance_dir} ..." - instance.clean -when 'clean!' - $stderr.puts "cleaning solr downloads and solr instance: #{instance.instance_dir} ..." instance.clean! +when 'purge' + $stderr.puts "cleaning solr downloads and solr instance: #{instance.instance_dir} ..." + instance.clean!(clean_downloads: true) when 'dir' puts instance.instance_dir when 'status' diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index c520156..d555520 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -226,20 +226,16 @@ def with_collection(options = {}) end ## - # Clean up any files solr_wrapper may have downloaded, except the solr-{ver}.zip file - def clean + # Clean up any files solr_wrapper manages + def clean!(clean_downloads: false) stop remove_instance_dir! FileUtils.remove_entry(config.tmp_save_dir, true) if File.exist? config.tmp_save_dir - FileUtils.remove_entry(config.version_file) if File.exist? config.version_file - end - - ## - # Clean up everything solr_wrapper may have downloaded - def clean! - clean - md5.clean! - FileUtils.remove_entry(config.download_dir, true) if File.exist?(config.download_dir) + FileUtils.remove_entry(config.version_file) if File.exist?(config.version_file) + if clean_downloads + md5.clean! + FileUtils.remove_file(config.solr_zip_path, true) + end end ## diff --git a/lib/solr_wrapper/md5.rb b/lib/solr_wrapper/md5.rb index 12485cb..bade0d4 100644 --- a/lib/solr_wrapper/md5.rb +++ b/lib/solr_wrapper/md5.rb @@ -6,7 +6,7 @@ def initialize(config) end def clean! - FileUtils.remove_entry(config.md5sum_path) if File.exist? config.md5sum_path + FileUtils.remove_file(config.md5sum_path, true) end def validate?(file) From 417c7441710c0086d591bdc8cd0664757a95dc4e Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Mon, 22 May 2017 11:24:47 -0700 Subject: [PATCH 8/9] SolrWrapper::Instance#started? returns truthy regex match --- lib/solr_wrapper/instance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index d555520..074813c 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -103,7 +103,7 @@ def restart ## # Is Solr running? def started? - status_info =~ /running on port #{port}/ && true || false + status_info =~ /running on port #{port}/ end ## From d5106cfef0f0ad705e301fa9b17a5dbd9b0c73d1 Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Mon, 22 May 2017 12:16:18 -0700 Subject: [PATCH 9/9] SolrWrapper::Instance#started? returns boolean using String#include? --- lib/solr_wrapper/instance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/solr_wrapper/instance.rb b/lib/solr_wrapper/instance.rb index 074813c..fd991c4 100644 --- a/lib/solr_wrapper/instance.rb +++ b/lib/solr_wrapper/instance.rb @@ -103,7 +103,7 @@ def restart ## # Is Solr running? def started? - status_info =~ /running on port #{port}/ + status_info.include? "running on port #{port}" end ##