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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :url (String)
  • :instance_dir (String)

    Directory to store the solr index files

  • :version (String)

    Solr version to download and install

  • :port (String)

    port to run Solr on

  • :cloud (Boolean)

    Run solr in cloud mode

  • :version_file (String)

    Local path to store the currently installed version

  • :download_dir (String)

    Local directory to store the downloaded Solr artifact and its checksum file in (overridden by :artifact_path)

  • :artifact_path (String)

    Local path for storing the downloaded Solr artifact file

  • :validate (Boolean)

    Should solr_wrapper download a new checksum and (re-)validate the tgz file? (default: true)

  • :checksum (String)

    Path/URL to checksum

  • :solr_xml (String)

    Path to Solr configuration

  • :extra_lib_dir (String)

    Path to directory containing extra libraries to copy into instance_dir/lib

  • :verbose (Boolean)

    return verbose info when running solr commands

  • :ignore_checksum (Boolean)
  • :solr_options (Hash)
  • :env (Hash)
  • :config (String)


37
38
39
40
# File 'lib/solr_wrapper/instance.rb', line 37

def initialize(options = {})
  @config = Settings.new(Configuration.new(options))
  @started = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/solr_wrapper/instance.rb', line 16

def config
  @config
end

Instance Method Details

#clean!Object

Clean up any files solr_wrapper may have downloaded



274
275
276
277
278
279
280
281
# File 'lib/solr_wrapper/instance.rb', line 274

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
  checksum_validator.clean!
  FileUtils.remove_entry(config.version_file) if File.exist? config.version_file
end

#configureObject



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/solr_wrapper/instance.rb', line 289

def configure
  raise_error_unless_extracted
  FileUtils.cp config.solr_xml, File.join(config.instance_dir, 'server', 'solr', 'solr.xml') if config.solr_xml
  FileUtils.cp_r File.join(config.extra_lib_dir, '.'), File.join(config.instance_dir, 'server', 'solr', 'lib') if config.extra_lib_dir

  config.contrib.each do |mapping|
    if File.directory? mapping[:from]
      FileUtils.cp_r mapping[:from], File.join(config.instance_dir, mapping[:to])
    else
      FileUtils.cp mapping[:from], File.join(config.instance_dir, mapping[:to])
    end
  end
end

#create(options = {}) ⇒ Object

Create a new collection in solr

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)
  • :dir (String)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/solr_wrapper/instance.rb', line 146

def create(options = {})
  tmpdir = nil

  begin
    options[:name] ||= SecureRandom.hex

    create_options = {}
    if greater_than_solr98?
      create_options[:s] = config.base_url
    elsif greater_than_solr97?
      create_options[:url] = config.base_url
    else
      create_options[:p] = port
    end
    create_options[:c] = options[:name] if options[:name]
    create_options[:n] = options[:config_name] if options[:config_name]

    if options[:dir]
      # Turn a relative path to absolute. Solr 9.7+ changes relative to be
      # relative to solr instance dir (which can be a temp dir for us),
      # instead of command CWD. Normalize to absolute is good for all.
      options[:dir] = File.absolute_path(options[:dir])

      # Solr 9.7 required that the dir argument contain a `conf` directory that
      # contains the actual configuration files.
      if greater_than_solr97? && !File.exist?(File.join(options[:dir], 'conf'))
        # ends in `conf` or `conf/`
        if options[:dir].match?(/conf\/?$/)
          create_options[:d] = File.expand_path("..", options[:dir])
        else
          tmpdir = Dir.mktmpdir

          FileUtils.mkdir("#{tmpdir}/conf")
          FileUtils.cp_r("#{options[:dir]}/.", "#{tmpdir}/conf")
          create_options[:d] = tmpdir
        end
      else
        create_options[:d] = options[:dir]
      end
    end

    Retriable.retriable do
      raise "Not started yet" unless started?
    end

    # short-circuit if we're using persisted data with an existing core/collection
    return if options[:persist] && create_options[:c] && client.exists?(create_options[:c])

    exec("create", create_options)

    options[:name]
  ensure
    FileUtils.remove_entry tmpdir if tmpdir
  end
end

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

Create a new collection in solr

Parameters:

  • name (String)

    collection name



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/solr_wrapper/instance.rb', line 241

def delete(name, _options = {})
  delete_options = { c: name }

  if greater_than_solr98?
    delete_options[:s] = config.base_url
  elsif greater_than_solr97?
    delete_options[:url] = config.base_url
  else
    delete_options[:p] = port
  end

  exec("delete", delete_options)
end

#downconfig(options = {}) ⇒ Object

Copy the collection configuration from zookeeper to a local directory

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :config_name (String)
  • :dir (String)


225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/solr_wrapper/instance.rb', line 225

def downconfig(options = {})
  options[:name] ||= SecureRandom.hex
  options[:zkhost] ||= zkhost

  downconfig_options = { n: options[:name] }
  downconfig_options[:d] = options[:dir] if options[:dir]
  downconfig_options[:z] = options[:zkhost] if options[:zkhost]

  exec ['zk', 'downconfig'], downconfig_options

  options[:name]
end

#extractString

extract a copy of solr to instance_dir Does noting if solr already exists at instance_dir

Returns:

  • (String)

    instance_dir Directory where solr has been installed



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/solr_wrapper/instance.rb', line 312

def extract
  return config.instance_dir if extracted?

  downloaded_artifact_path = download

  SolrWrapper::TgzExtractor.new(downloaded_artifact_path, destination: config.tmp_save_dir).extract!

  begin
    FileUtils.remove_dir(config.instance_dir, true)
    FileUtils.cp_r File.join(config.tmp_save_dir, File.basename(config.download_url, '.tgz')), config.instance_dir
    self.extracted_version = config.version
    FileUtils.chmod 0755, config.solr_binary
  rescue Exception => e
    abort "Unable to copy #{config.tmp_save_dir} to #{config.instance_dir}: #{e.message}"
  end

  config.instance_dir
ensure
  FileUtils.remove_entry config.tmp_save_dir if File.exist? config.tmp_save_dir
end

#extract_and_configureObject



303
304
305
# File 'lib/solr_wrapper/instance.rb', line 303

def extract_and_configure
  extract.tap { configure }
end

#hostObject



42
43
44
# File 'lib/solr_wrapper/instance.rb', line 42

def host
  config.host
end

#instance_dirObject



54
55
56
# File 'lib/solr_wrapper/instance.rb', line 54

def instance_dir
  config.instance_dir
end

#managed?Boolean

rubocop:enable Lint/RescueException

Returns:

  • (Boolean)


334
335
336
# File 'lib/solr_wrapper/instance.rb', line 334

def managed?
  config.managed?
end

#pidObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/solr_wrapper/instance.rb', line 116

def pid
  return unless managed?

  @pid ||= begin
    out = exec('status').read
    out.match(/process (?<pid>\d+) running on port #{port}/) do |m|
      m[:pid].to_i
    end
  end
rescue
  nil
end

#portObject



46
47
48
# File 'lib/solr_wrapper/instance.rb', line 46

def port
  config.port
end

#remove_instance_dir!Object

Clean up any files in the Solr instance dir



285
286
287
# File 'lib/solr_wrapper/instance.rb', line 285

def remove_instance_dir!
  FileUtils.remove_entry(instance_dir, true) if File.exist? instance_dir
end

#restartObject

Stop Solr and wait for it to finish exiting



99
100
101
102
103
# File 'lib/solr_wrapper/instance.rb', line 99

def restart
  if managed? && started?
    exec('restart', p: port, c: config.cloud)
  end
end

#startObject

Start Solr and wait for it to become available



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/solr_wrapper/instance.rb', line 72

def start
  extract_and_configure
  if managed?
    exec('start', p: port, c: config.cloud)

    @started = true

    # Wait for solr to start
    unless status
      sleep config.poll_interval
    end

    after_start
  end
end

#started?Boolean

Is Solr running?

Returns:

  • (Boolean)


131
132
133
# File 'lib/solr_wrapper/instance.rb', line 131

def started?
  !!status
end

#statusObject

Check the status of a managed Solr service



107
108
109
110
111
112
113
114
# File 'lib/solr_wrapper/instance.rb', line 107

def status
  return true unless managed?

  out = exec('status').read
  out =~ /running on port #{port}/
rescue
  false
end

#stopObject

Stop Solr and wait for it to finish exiting



90
91
92
93
94
95
# File 'lib/solr_wrapper/instance.rb', line 90

def stop
  if managed? && started?
    exec('stop', p: port)
    wait
  end
end

#upconfig(options = {}) ⇒ Object

Update the collection configuration in zookeeper

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :config_name (String)
  • :dir (String)


207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/solr_wrapper/instance.rb', line 207

def upconfig(options = {})
  options[:name] ||= SecureRandom.hex
  options[:zkhost] ||= zkhost

  upconfig_options = { n: options[:name] }
  upconfig_options[:d] = options[:dir] if options[:dir]
  upconfig_options[:z] = options[:zkhost] if options[:zkhost]

  exec ['zk', 'upconfig'], upconfig_options

  options[:name]
end

#urlObject



50
51
52
# File 'lib/solr_wrapper/instance.rb', line 50

def url
  config.url
end

#versionObject



58
59
60
# File 'lib/solr_wrapper/instance.rb', line 58

def version
  config.version
end

#waitObject



135
136
137
138
139
# File 'lib/solr_wrapper/instance.rb', line 135

def wait
  while (Process.getpgid(pid) rescue status)
    sleep config.poll_interval
  end
end

#with_collection(options = {}) ⇒ Object

Create a new collection, run the block, and then clean up the collection

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)
  • :dir (String)


260
261
262
263
264
265
266
267
268
269
270
# File 'lib/solr_wrapper/instance.rb', line 260

def with_collection(options = {})
  options = config.collection_options.merge(options)
  return yield if options.empty?

  name = create(options)
  begin
    yield name
  ensure
    delete name unless options[:persist]
  end
end

#wrap(&_block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/solr_wrapper/instance.rb', line 62

def wrap(&_block)
  extract_and_configure
  start
  yield self
ensure
  stop if @started
end