Class: SolrWrapper::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/solr_wrapper/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Instance

Returns a new instance of Instance.



14
15
16
# File 'lib/solr_wrapper/instance.rb', line 14

def initialize options = {}
  @options = options
end

Instance Attribute Details

#options ⇒ Object (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/solr_wrapper/instance.rb', line 12

def options
  @options
end

Instance Method Details

#create(options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/solr_wrapper/instance.rb', line 145

def create options = {}
  options[:name] ||= SecureRandom.hex

  IO.popen([solr_binary, "create", "-c", options[:name], "-d", options[:dir], "-p", port, err: [:child, :out]]) do |io|
    if verbose?
      IO.copy_stream(io,$stderr)
    end
  end

  options[:name]
end

#delete(name, options = {}) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/solr_wrapper/instance.rb', line 157

def delete name, options = {}
  IO.popen([solr_binary, "delete", "-c", name, "-p", port, err: [:child, :out]]) do |io|
    if verbose?
      IO.copy_stream(io,$stderr)
    end
  end
end

#download ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/solr_wrapper/instance.rb', line 119

def download
  unless File.exists? download_path and md5sum(download_path) == expected_md5sum
    pbar = ProgressBar.create(title: File.basename(url), total: nil, format: "%t: |%B| %p%% (%e )")
    open(url, content_length_proc: lambda {|t|
      if t && 0 < t
        pbar.total = t
      end
      },
      progress_proc: lambda {|s|
        pbar.progress = s
      }) do |io|
      IO.copy_stream(io,download_path)
    end

    unless md5sum(download_path) == expected_md5sum
      raise "MD5 mismatch" unless options[:ignore_md5sum]
    end
  end

  download_path
end

#extract ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/solr_wrapper/instance.rb', line 89

def extract
  zip_path = download

  begin
    Zip::File.open(zip_path) do |zip_file|
      # Handle entries one by one
      zip_file.each do |entry|
        dest_file = File.join(tmp_save_dir,entry.name)
        FileUtils.remove_entry(dest_file,true)
        entry.extract(dest_file)
      end
    end

  rescue Exception => e
    abort "Unable to unzip #{zip_path} into #{tmp_save_dir}: #{e.message}"
  end

  begin  
    FileUtils.remove_dir(solr_dir,true)
    FileUtils.cp_r File.join(tmp_save_dir, File.basename(default_url, ".zip")), solr_dir
    FileUtils.chmod 0755, solr_binary
  rescue Exception => e
    abort "Unable to copy #{tmp_save_dir} to #{solr_dir}: #{e.message}"
  end

  solr_dir
ensure
  FileUtils.remove_entry tmp_save_dir
end

#port ⇒ Object



141
142
143
# File 'lib/solr_wrapper/instance.rb', line 141

def port
  options.fetch(:port, "8983")
end

#start ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/solr_wrapper/instance.rb', line 25

def start
  extract
  IO.popen([solr_binary, "start", "-p", port, err: [:child, :out]]) do |io|
    stringio = StringIO.new
    if verbose?
      IO.copy_stream(io,$stderr)
    else
      IO.copy_stream(io, stringio)
    end
    _, status = Process.wait2(io.pid)
    if status != 0
      stringio.rewind
      raise "Unable to start solr: #{stringio.read}"
    end
  end if managed?

  started!
end

#started? ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/solr_wrapper/instance.rb', line 85

def started?
  !!status
end

#status ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/solr_wrapper/instance.rb', line 63

def status
  return true unless managed?

  stringio = StringIO.new

  IO.popen([solr_binary, "status", "-p", port, err: [:child, :out]]) do |io|
    IO.copy_stream(io, stringio)

    _, status = Process.wait2(io.pid)

    stringio.rewind

    if status != 0
      raise "Unable to query solr status: #{stringio.read}"
    end
  end

  out = stringio.read

  out =~ /running on port #{port}/
end

#stop ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/solr_wrapper/instance.rb', line 44

def stop
  return unless started?

  IO.popen([solr_binary, "stop", "-p", port, err: [:child, :out]]) do |io|
    stringio = StringIO.new
    if verbose?
      IO.copy_stream(io,$stderr)
    else
      IO.copy_stream(io, stringio)
    end
    _, status = Process.wait2(io.pid)

    if status != 0
      stringio.rewind
      raise "Unable to start solr: #{stringio.read}"
    end
  end if managed?
end

#with_collection(options = {}) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/solr_wrapper/instance.rb', line 165

def with_collection options = {}
  name = create(options)
  begin
    yield name
  ensure
    delete name
  end
end

#wrap(&block) ⇒ Object



18
19
20
21
22
23
# File 'lib/solr_wrapper/instance.rb', line 18

def wrap &block
  start
  yield self
ensure
  stop
end