Class: Chef::Solr::Application::Solr

Inherits:
Application
  • Object
show all
Defined in:
lib/chef/solr/application/solr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSolr

Returns a new instance of Solr.



117
118
119
# File 'lib/chef/solr/application/solr.rb', line 117

def initialize
  super
end

Instance Attribute Details

#logfileObject

Returns the value of attribute logfile.



31
32
33
# File 'lib/chef/solr/application/solr.rb', line 31

def logfile
  @logfile
end

Instance Method Details

#assert_solr_installed!Object



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/chef/solr/application/solr.rb', line 204

def assert_solr_installed!
  unless solr_home_exist? && solr_data_dir_exist? && solr_jetty_home_exist?
    Chef::Log.fatal "Chef Solr is not installed or solr_home_path, solr_data_path, and solr_jetty_path are misconfigured."
    Chef::Log.fatal "Your current configuration is:"
    Chef::Log.fatal "solr_home_path:  #{Chef::Config[:solr_home_path]}"
    Chef::Log.fatal "solr_data_path:  #{Chef::Config[:solr_data_path]}"
    Chef::Log.fatal "solr_jetty_path: #{Chef::Config[:solr_jetty_path]}"
    Chef::Log.fatal "You can install Chef Solr using the chef-solr-installer script."
    exit 1
  end
end

#assert_valid_schema!Object



216
217
218
219
220
221
222
223
# File 'lib/chef/solr/application/solr.rb', line 216

def assert_valid_schema!
  unless valid_schema_name? && valid_schema_version?
    Chef::Log.fatal "Your Chef Solr installation needs to be upgraded."
    Chef::Log.fatal "Expected schema version #{Chef::Solr::SCHEMA_VERSION} but version #{solr_schema_version} is installed."
    Chef::Log.fatal "Use chef-solr-installer to upgrade your Solr install after backing up your data."
    exit 1
  end
end

#check_value_of_main_index_max_field_lengthObject



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/chef/solr/application/solr.rb', line 180

def check_value_of_main_index_max_field_length
  if solr_main_index_max_field_length
    unless solr_main_index_max_field_length > 10000
      message  = "The maxFieldLimit for the mainIndex is set to #{solr_main_index_max_field_length}.  "
      message << "It's recommended to increase this value (in #{solr_config_file_path})."
      Chef::Log.warn message
    end
  else
    Chef::Log.warn "Unable to determine the maxFieldLimit for the mainIndex (in #{solr_config_file_path})"
  end
end

#close_and_reopen_log_fileObject



265
266
267
268
269
270
# File 'lib/chef/solr/application/solr.rb', line 265

def close_and_reopen_log_file
  Chef::Log.close

  STDOUT.reopen(@logfile)
  STDERR.reopen(@logfile)
end

#config_documentObject



137
138
139
140
141
142
143
# File 'lib/chef/solr/application/solr.rb', line 137

def config_document
  @config_document ||=begin
    File.open(solr_config_file_path, 'r') do |xmlsux|
      REXML::Document.new(xmlsux)
    end
  end
end

#run_applicationObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/chef/solr/application/solr.rb', line 244

def run_application
  if Chef::Config[:daemonize]
    Chef::Daemon.daemonize("chef-solr")
  end

  Dir.chdir(Chef::Config[:solr_jetty_path]) do
    command = "java -Xmx#{Chef::Config[:solr_heap_size]} -Xms#{Chef::Config[:solr_heap_size]}"
    command << " -Dsolr.data.dir=#{Chef::Config[:solr_data_path]}"
    command << " -Dsolr.solr.home=#{Chef::Config[:solr_home_path]}"
    command << " #{Chef::Config[:solr_java_opts]}" if Chef::Config[:solr_java_opts]
    command << " -jar #{File.join(Chef::Config[:solr_jetty_path], 'start.jar')}"
    Chef::Log.info("Starting Solr with #{command}")

    # Opened earlier before we dropped privileges, don't need it anymore
    close_and_reopen_log_file if @logfile

    Kernel.exec(command)

  end
end

#schema_attributesObject



145
146
147
# File 'lib/chef/solr/application/solr.rb', line 145

def schema_attributes
  @schema_attributes ||= REXML::XPath.first(schema_document, '/schema').attributes
end

#schema_documentObject



129
130
131
132
133
134
135
# File 'lib/chef/solr/application/solr.rb', line 129

def schema_document
  @schema_document ||= begin
    File.open(schema_file_path, 'r') do |xmlsux|
      REXML::Document.new(xmlsux)
    end
  end
end

#schema_file_pathObject



121
122
123
# File 'lib/chef/solr/application/solr.rb', line 121

def schema_file_path
  @schema_file_path ||= File.join(Chef::Config[:solr_home_path], 'conf', 'schema.xml')
end

#setup_applicationObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/chef/solr/application/solr.rb', line 225

def setup_application
  assert_solr_installed!
  assert_valid_schema!
  check_value_of_main_index_max_field_length

  # Need to redirect stdout and stderr so Java process inherits them.
  # If -L wasn't specified, Chef::Config[:log_location] will be an IO
  # object, otherwise it will be a String.
  #
  # Open this as a privileged user and hang onto it
  if Chef::Config[:log_location].kind_of?(String)
    @logfile = File.new(Chef::Config[:log_location], "a")
  end

  Chef::Log.level = Chef::Config[:log_level]

  Chef::Daemon.change_privilege
end

#solr_config_file_pathObject



125
126
127
# File 'lib/chef/solr/application/solr.rb', line 125

def solr_config_file_path
  @solr_config_file_path ||= File.join(Chef::Config[:solr_home_path], 'conf', 'solrconfig.xml')
end

#solr_data_dir_exist?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/chef/solr/application/solr.rb', line 196

def solr_data_dir_exist?
  File.directory?(Chef::Config[:solr_data_path])
end

#solr_home_exist?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/chef/solr/application/solr.rb', line 192

def solr_home_exist?
  File.directory?(Chef::Config[:solr_home_path])
end

#solr_jetty_home_exist?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/chef/solr/application/solr.rb', line 200

def solr_jetty_home_exist?
  File.directory?(Chef::Config[:solr_jetty_path])
end

#solr_main_index_elementsObject



149
150
151
152
# File 'lib/chef/solr/application/solr.rb', line 149

def solr_main_index_elements
  location = '/config/mainIndex/'
  @solr_main_index_elements ||= REXML::XPath.first(config_document, location).elements
end

#solr_main_index_max_field_lengthObject



162
163
164
165
166
167
168
169
170
# File 'lib/chef/solr/application/solr.rb', line 162

def solr_main_index_max_field_length
  @solr_main_index_max_field_length ||=begin
    field_length_el = solr_main_index_elements.select do |el|
      el.name == 'maxFieldLength'
    end

    field_length_el.empty? ? nil : field_length_el.first.text.to_i
  end
end

#solr_schema_nameObject



154
155
156
# File 'lib/chef/solr/application/solr.rb', line 154

def solr_schema_name
  schema_attributes["name"]
end

#solr_schema_versionObject



158
159
160
# File 'lib/chef/solr/application/solr.rb', line 158

def solr_schema_version
  schema_attributes["version"]
end

#valid_schema_name?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/chef/solr/application/solr.rb', line 172

def valid_schema_name?
  solr_schema_name == Chef::Solr::SCHEMA_NAME
end

#valid_schema_version?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/chef/solr/application/solr.rb', line 176

def valid_schema_version?
  solr_schema_version == Chef::Solr::SCHEMA_VERSION
end