1515# See the License for the specific language governing permissions and
1616# limitations under the License.
1717
18- require 'chef'
18+
19+ require 'rexml/document'
1920require 'chef/log'
2021require 'chef/config'
2122require 'chef/application'
2223require 'chef/daemon'
23- require 'chef/client'
2424require 'chef/solr'
2525
2626class Chef
@@ -116,7 +116,76 @@ def initialize
116116 super
117117 end
118118
119+ def schema_file_path
120+ @schema_file_path ||= File . join ( Chef ::Config [ :solr_home_path ] , 'conf' , 'schema.xml' )
121+ end
122+
123+
124+ def schema_document
125+ @schema_document ||= begin
126+ File . open ( schema_file_path , 'r' ) do |xmlsux |
127+ REXML ::Document . new ( xmlsux )
128+ end
129+ end
130+ end
131+
132+ def schema_attributes
133+ @schema_attributes ||= REXML ::XPath . first ( schema_document , '/schema' ) . attributes
134+ end
135+
136+ def solr_schema_name
137+ schema_attributes [ "name" ]
138+ end
139+
140+ def solr_schema_version
141+ schema_attributes [ "version" ]
142+ end
143+
144+ def valid_schema_name?
145+ solr_schema_name == Chef ::Solr ::SCHEMA_NAME
146+ end
147+
148+ def valid_schema_version?
149+ solr_schema_version == Chef ::Solr ::SCHEMA_VERSION
150+ end
151+
152+ def solr_home_exist?
153+ File . directory? ( Chef ::Config [ :solr_home_path ] )
154+ end
155+
156+ def solr_data_dir_exist?
157+ File . directory? ( Chef ::Config [ :solr_data_path ] )
158+ end
159+
160+ def solr_jetty_home_exist?
161+ File . directory? ( Chef ::Config [ :solr_jetty_path ] )
162+ end
163+
164+ def assert_solr_installed!
165+ unless solr_home_exist? && solr_data_dir_exist? && solr_jetty_home_exist?
166+ Chef ::Log . fatal "Chef Solr is not installed or solr_home_path, solr_data_path, and solr_jetty_path are misconfigured."
167+ Chef ::Log . fatal "Your current configuration is:"
168+ Chef ::Log . fatal "solr_home_path: #{ Chef ::Config [ :solr_home_path ] } "
169+ Chef ::Log . fatal "solr_data_path: #{ Chef ::Config [ :solr_data_path ] } "
170+ Chef ::Log . fatal "solr_jetty_path: #{ Chef ::Config [ :solr_jetty_path ] } "
171+ Chef ::Log . fatal "You can install Chef Solr using the chef-solr-installer script."
172+ exit 1
173+ end
174+ end
175+
176+ def assert_valid_schema!
177+ unless valid_schema_name? && valid_schema_version?
178+ Chef ::Log . fatal "Your Chef Solr installation needs to be upgraded."
179+ Chef ::Log . fatal "Expected schema version #{ Chef ::Solr ::SCHEMA_VERSION } but version #{ solr_schema_version } is installed."
180+ Chef ::Log . fatal "Use chef-solr-installer to upgrade your Solr install after backing up your data."
181+ exit 1
182+ end
183+ end
184+
119185 def setup_application
186+ assert_solr_installed!
187+ assert_valid_schema!
188+
120189 # Need to redirect stdout and stderr so Java process inherits them.
121190 # If -L wasn't specified, Chef::Config[:log_location] will be an IO
122191 # object, otherwise it will be a String.
@@ -128,47 +197,7 @@ def setup_application
128197
129198 Chef ::Log . level = Chef ::Config [ :log_level ]
130199
131- # Build up a client
132- node = Chef ::Node . new
133- node . platform = :default
134- node . platform_version = 42
135-
136200 Chef ::Daemon . change_privilege
137-
138- solr_base = File . expand_path ( File . join ( File . dirname ( __FILE__ ) , ".." , ".." , ".." , ".." , "solr" ) )
139-
140- run_context = Chef ::RunContext . new ( node , { } )
141- # Create the Jetty container
142- unless File . directory? ( Chef ::Config [ :solr_jetty_path ] )
143- Chef ::Log . warn ( "Initializing the Jetty container" )
144- solr_jetty_dir = Chef ::Resource ::Directory . new ( Chef ::Config [ :solr_jetty_path ] , run_context )
145- solr_jetty_dir . recursive ( true )
146- solr_jetty_dir . run_action ( :create )
147- solr_jetty_untar = Chef ::Resource ::Execute . new ( "untar_jetty" , run_context )
148- solr_jetty_untar . command ( "tar zxvf #{ File . join ( solr_base , 'solr-jetty.tar.gz' ) } " )
149- solr_jetty_untar . cwd ( Chef ::Config [ :solr_jetty_path ] )
150- solr_jetty_untar . run_action ( :run )
151- end
152-
153- # Create the solr home
154- unless File . directory? ( Chef ::Config [ :solr_home_path ] )
155- Chef ::Log . warn ( "Initializing Solr home directory" )
156- solr_home_dir = Chef ::Resource ::Directory . new ( Chef ::Config [ :solr_home_path ] , run_context )
157- solr_home_dir . recursive ( true )
158- solr_home_dir . run_action ( :create )
159- solr_jetty_untar = Chef ::Resource ::Execute . new ( "untar_solr_home" , run_context )
160- solr_jetty_untar . command ( "tar zxvf #{ File . join ( solr_base , 'solr-home.tar.gz' ) } " )
161- solr_jetty_untar . cwd ( Chef ::Config [ :solr_home_path ] )
162- solr_jetty_untar . run_action ( :run )
163- end
164-
165- # Create the solr data path
166- unless File . directory? ( Chef ::Config [ :solr_data_path ] )
167- Chef ::Log . warn ( "Initializing Solr data directory" )
168- solr_data_dir = Chef ::Resource ::Directory . new ( Chef ::Config [ :solr_data_path ] , run_context )
169- solr_data_dir . recursive ( true )
170- solr_data_dir . run_action ( :create )
171- end
172201 end
173202
174203 def run_application
@@ -191,7 +220,6 @@ def run_application
191220
192221 STDOUT . reopen ( @logfile )
193222 STDERR . reopen ( @logfile )
194- @logfile . close
195223 end
196224
197225 Kernel . exec ( command )
0 commit comments