rere 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. data/.classpath +260 -0
  2. data/.gitignore +28 -0
  3. data/.project +14 -0
  4. data/.ruby-version +1 -0
  5. data/.settings/org.eclim.prefs +3 -0
  6. data/.settings/org.eclipse.jdt.core.prefs +5 -0
  7. data/.settings/org.eclipse.jdt.ui.prefs +2 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE +22 -0
  10. data/README.md +44 -0
  11. data/Rakefile +65 -0
  12. data/bin/solr +61 -0
  13. data/example/config/solr.yml +23 -0
  14. data/example/log/.gitkeep +0 -0
  15. data/example/solr/README.txt +63 -0
  16. data/example/solr/collection1/README.txt +50 -0
  17. data/example/solr/collection1/conf/admin-extra.html +24 -0
  18. data/example/solr/collection1/conf/admin-extra.menu-bottom.html +25 -0
  19. data/example/solr/collection1/conf/admin-extra.menu-top.html +25 -0
  20. data/example/solr/collection1/conf/currency.xml +67 -0
  21. data/example/solr/collection1/conf/elevate.xml +38 -0
  22. data/example/solr/collection1/conf/lang/contractions_ca.txt +8 -0
  23. data/example/solr/collection1/conf/lang/contractions_fr.txt +15 -0
  24. data/example/solr/collection1/conf/lang/contractions_ga.txt +5 -0
  25. data/example/solr/collection1/conf/lang/contractions_it.txt +23 -0
  26. data/example/solr/collection1/conf/lang/hyphenations_ga.txt +5 -0
  27. data/example/solr/collection1/conf/lang/stemdict_nl.txt +6 -0
  28. data/example/solr/collection1/conf/lang/stoptags_ja.txt +420 -0
  29. data/example/solr/collection1/conf/lang/stopwords_ar.txt +125 -0
  30. data/example/solr/collection1/conf/lang/stopwords_bg.txt +193 -0
  31. data/example/solr/collection1/conf/lang/stopwords_ca.txt +220 -0
  32. data/example/solr/collection1/conf/lang/stopwords_cz.txt +172 -0
  33. data/example/solr/collection1/conf/lang/stopwords_da.txt +108 -0
  34. data/example/solr/collection1/conf/lang/stopwords_de.txt +292 -0
  35. data/example/solr/collection1/conf/lang/stopwords_el.txt +78 -0
  36. data/example/solr/collection1/conf/lang/stopwords_en.txt +54 -0
  37. data/example/solr/collection1/conf/lang/stopwords_es.txt +354 -0
  38. data/example/solr/collection1/conf/lang/stopwords_eu.txt +99 -0
  39. data/example/solr/collection1/conf/lang/stopwords_fa.txt +313 -0
  40. data/example/solr/collection1/conf/lang/stopwords_fi.txt +95 -0
  41. data/example/solr/collection1/conf/lang/stopwords_fr.txt +184 -0
  42. data/example/solr/collection1/conf/lang/stopwords_ga.txt +110 -0
  43. data/example/solr/collection1/conf/lang/stopwords_gl.txt +161 -0
  44. data/example/solr/collection1/conf/lang/stopwords_hi.txt +235 -0
  45. data/example/solr/collection1/conf/lang/stopwords_hu.txt +209 -0
  46. data/example/solr/collection1/conf/lang/stopwords_hy.txt +46 -0
  47. data/example/solr/collection1/conf/lang/stopwords_id.txt +359 -0
  48. data/example/solr/collection1/conf/lang/stopwords_it.txt +301 -0
  49. data/example/solr/collection1/conf/lang/stopwords_ja.txt +127 -0
  50. data/example/solr/collection1/conf/lang/stopwords_lv.txt +172 -0
  51. data/example/solr/collection1/conf/lang/stopwords_nl.txt +117 -0
  52. data/example/solr/collection1/conf/lang/stopwords_no.txt +192 -0
  53. data/example/solr/collection1/conf/lang/stopwords_pt.txt +251 -0
  54. data/example/solr/collection1/conf/lang/stopwords_ro.txt +233 -0
  55. data/example/solr/collection1/conf/lang/stopwords_ru.txt +241 -0
  56. data/example/solr/collection1/conf/lang/stopwords_sv.txt +131 -0
  57. data/example/solr/collection1/conf/lang/stopwords_th.txt +119 -0
  58. data/example/solr/collection1/conf/lang/stopwords_tr.txt +212 -0
  59. data/example/solr/collection1/conf/lang/userdict_ja.txt +29 -0
  60. data/example/solr/collection1/conf/mapping-FoldToASCII.txt +3813 -0
  61. data/example/solr/collection1/conf/mapping-ISOLatin1Accent.txt +246 -0
  62. data/example/solr/collection1/conf/protwords.txt +21 -0
  63. data/example/solr/collection1/conf/schema.xml +1125 -0
  64. data/example/solr/collection1/conf/scripts.conf +24 -0
  65. data/example/solr/collection1/conf/solrconfig.xml +1816 -0
  66. data/example/solr/collection1/conf/spellings.txt +2 -0
  67. data/example/solr/collection1/conf/stopwords.txt +14 -0
  68. data/example/solr/collection1/conf/synonyms.txt +29 -0
  69. data/example/solr/collection1/conf/update-script.js +53 -0
  70. data/example/solr/collection1/conf/velocity/VM_global_library.vm +170 -0
  71. data/example/solr/collection1/conf/velocity/browse.vm +50 -0
  72. data/example/solr/collection1/conf/velocity/cluster.vm +9 -0
  73. data/example/solr/collection1/conf/velocity/clusterResults.vm +12 -0
  74. data/example/solr/collection1/conf/velocity/debug.vm +17 -0
  75. data/example/solr/collection1/conf/velocity/did_you_mean.vm +4 -0
  76. data/example/solr/collection1/conf/velocity/facet_fields.vm +15 -0
  77. data/example/solr/collection1/conf/velocity/facet_pivot.vm +3 -0
  78. data/example/solr/collection1/conf/velocity/facet_queries.vm +3 -0
  79. data/example/solr/collection1/conf/velocity/facet_ranges.vm +15 -0
  80. data/example/solr/collection1/conf/velocity/facets.vm +5 -0
  81. data/example/solr/collection1/conf/velocity/footer.vm +17 -0
  82. data/example/solr/collection1/conf/velocity/head.vm +32 -0
  83. data/example/solr/collection1/conf/velocity/header.vm +3 -0
  84. data/example/solr/collection1/conf/velocity/hit.vm +11 -0
  85. data/example/solr/collection1/conf/velocity/hitGrouped.vm +24 -0
  86. data/example/solr/collection1/conf/velocity/join-doc.vm +4 -0
  87. data/example/solr/collection1/conf/velocity/jquery.autocomplete.css +48 -0
  88. data/example/solr/collection1/conf/velocity/jquery.autocomplete.js +763 -0
  89. data/example/solr/collection1/conf/velocity/layout.vm +20 -0
  90. data/example/solr/collection1/conf/velocity/main.css +208 -0
  91. data/example/solr/collection1/conf/velocity/product-doc.vm +27 -0
  92. data/example/solr/collection1/conf/velocity/query.vm +42 -0
  93. data/example/solr/collection1/conf/velocity/queryGroup.vm +19 -0
  94. data/example/solr/collection1/conf/velocity/querySpatial.vm +40 -0
  95. data/example/solr/collection1/conf/velocity/richtext-doc.vm +114 -0
  96. data/example/solr/collection1/conf/velocity/suggest.vm +3 -0
  97. data/example/solr/collection1/conf/velocity/tabs.vm +6 -0
  98. data/example/solr/collection1/conf/xslt/example.xsl +132 -0
  99. data/example/solr/collection1/conf/xslt/example_atom.xsl +67 -0
  100. data/example/solr/collection1/conf/xslt/example_rss.xsl +66 -0
  101. data/example/solr/collection1/conf/xslt/luke.xsl +337 -0
  102. data/example/solr/collection1/conf/xslt/updateXml.xsl +70 -0
  103. data/example/solr/collection1/data/index/segments.gen +0 -0
  104. data/example/solr/collection1/data/index/segments_1 +0 -0
  105. data/example/solr/data/development/index/segments.gen +0 -0
  106. data/example/solr/data/development/index/segments_1 +0 -0
  107. data/example/solr/solr.xml +53 -0
  108. data/example/solr/zoo.cfg +17 -0
  109. data/lib/rere.rb +66 -0
  110. data/lib/rere/railtie.rb +8 -0
  111. data/lib/rere/server.rb +380 -0
  112. data/lib/rere/tasks/solr.rake +47 -0
  113. data/lib/rere/version.rb +3 -0
  114. data/pom.xml +168 -0
  115. data/rere.gemspec +26 -0
  116. data/server/README.txt +78 -0
  117. data/server/cloud-scripts/zkcli.bat +12 -0
  118. data/server/cloud-scripts/zkcli.sh +14 -0
  119. data/server/contexts/solr-jetty-context.xml +8 -0
  120. data/server/etc/create-solrtest.keystore.sh +37 -0
  121. data/server/etc/jetty.xml +205 -0
  122. data/server/etc/logging.properties +38 -0
  123. data/server/etc/solrtest.keystore +0 -0
  124. data/server/etc/webdefault.xml +527 -0
  125. data/server/exampledocs/books.csv +11 -0
  126. data/server/exampledocs/books.json +51 -0
  127. data/server/exampledocs/gb18030-example.xml +32 -0
  128. data/server/exampledocs/hd.xml +56 -0
  129. data/server/exampledocs/ipod_other.xml +60 -0
  130. data/server/exampledocs/ipod_video.xml +40 -0
  131. data/server/exampledocs/manufacturers.xml +75 -0
  132. data/server/exampledocs/mem.xml +77 -0
  133. data/server/exampledocs/money.xml +65 -0
  134. data/server/exampledocs/monitor.xml +35 -0
  135. data/server/exampledocs/monitor2.xml +34 -0
  136. data/server/exampledocs/mp500.xml +43 -0
  137. data/server/exampledocs/post.jar +0 -0
  138. data/server/exampledocs/post.sh +30 -0
  139. data/server/exampledocs/sd500.xml +38 -0
  140. data/server/exampledocs/solr.xml +38 -0
  141. data/server/exampledocs/test_utf8.sh +93 -0
  142. data/server/exampledocs/utf8-example.xml +42 -0
  143. data/server/exampledocs/vidcard.xml +62 -0
  144. data/server/lib/ext/jcl-over-slf4j-1.6.6.jar +0 -0
  145. data/server/lib/ext/jul-to-slf4j-1.6.6.jar +0 -0
  146. data/server/lib/ext/log4j-1.2.16.jar +0 -0
  147. data/server/lib/ext/slf4j-api-1.6.6.jar +0 -0
  148. data/server/lib/ext/slf4j-log4j12-1.6.6.jar +0 -0
  149. data/server/lib/jetty-continuation-8.1.8.v20121106.jar +0 -0
  150. data/server/lib/jetty-deploy-8.1.8.v20121106.jar +0 -0
  151. data/server/lib/jetty-http-8.1.8.v20121106.jar +0 -0
  152. data/server/lib/jetty-io-8.1.8.v20121106.jar +0 -0
  153. data/server/lib/jetty-jmx-8.1.8.v20121106.jar +0 -0
  154. data/server/lib/jetty-security-8.1.8.v20121106.jar +0 -0
  155. data/server/lib/jetty-server-8.1.8.v20121106.jar +0 -0
  156. data/server/lib/jetty-servlet-8.1.8.v20121106.jar +0 -0
  157. data/server/lib/jetty-util-8.1.8.v20121106.jar +0 -0
  158. data/server/lib/jetty-webapp-8.1.8.v20121106.jar +0 -0
  159. data/server/lib/jetty-xml-8.1.8.v20121106.jar +0 -0
  160. data/server/lib/servlet-api-3.0.jar +0 -0
  161. data/server/resources/log4j.properties +19 -0
  162. data/server/solr/README.txt +63 -0
  163. data/server/solr/solr.xml +53 -0
  164. data/server/solr/zoo.cfg +17 -0
  165. data/server/start.jar +0 -0
  166. data/server/webapps/solr.war +0 -0
  167. data/solr/lib/solr-winds-0.1.jar +0 -0
  168. metadata +284 -0
@@ -0,0 +1,70 @@
1
+ <!--
2
+ * Licensed to the Apache Software Foundation (ASF) under one or more
3
+ * contributor license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright ownership.
5
+ * The ASF licenses this file to You under the Apache License, Version 2.0
6
+ * (the "License"); you may not use this file except in compliance with
7
+ * the License. You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ -->
17
+
18
+ <!--
19
+ Simple transform of Solr query response into Solr Update XML compliant XML.
20
+ When used in the xslt response writer you will get UpdaateXML as output.
21
+ But you can also store a query response XML to disk and feed this XML to
22
+ the XSLTUpdateRequestHandler to index the content. Provided as example only.
23
+ See http://wiki.apache.org/solr/XsltUpdateRequestHandler for more info
24
+ -->
25
+ <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
26
+ <xsl:output media-type="text/xml" method="xml" indent="yes"/>
27
+
28
+ <xsl:template match='/'>
29
+ <add>
30
+ <xsl:apply-templates select="response/result/doc"/>
31
+ </add>
32
+ </xsl:template>
33
+
34
+ <!-- Ignore score (makes no sense to index) -->
35
+ <xsl:template match="doc/*[@name='score']" priority="100">
36
+ </xsl:template>
37
+
38
+ <xsl:template match="doc">
39
+ <xsl:variable name="pos" select="position()"/>
40
+ <doc>
41
+ <xsl:apply-templates>
42
+ <xsl:with-param name="pos"><xsl:value-of select="$pos"/></xsl:with-param>
43
+ </xsl:apply-templates>
44
+ </doc>
45
+ </xsl:template>
46
+
47
+ <!-- Flatten arrays to duplicate field lines -->
48
+ <xsl:template match="doc/arr" priority="100">
49
+ <xsl:variable name="fn" select="@name"/>
50
+
51
+ <xsl:for-each select="*">
52
+ <xsl:element name="field">
53
+ <xsl:attribute name="name"><xsl:value-of select="$fn"/></xsl:attribute>
54
+ <xsl:value-of select="."/>
55
+ </xsl:element>
56
+ </xsl:for-each>
57
+ </xsl:template>
58
+
59
+
60
+ <xsl:template match="doc/*">
61
+ <xsl:variable name="fn" select="@name"/>
62
+
63
+ <xsl:element name="field">
64
+ <xsl:attribute name="name"><xsl:value-of select="$fn"/></xsl:attribute>
65
+ <xsl:value-of select="."/>
66
+ </xsl:element>
67
+ </xsl:template>
68
+
69
+ <xsl:template match="*"/>
70
+ </xsl:stylesheet>
@@ -0,0 +1,53 @@
1
+ <?xml version="1.0" encoding="UTF-8" ?>
2
+ <!--
3
+ Licensed to the Apache Software Foundation (ASF) under one or more
4
+ contributor license agreements. See the NOTICE file distributed with
5
+ this work for additional information regarding copyright ownership.
6
+ The ASF licenses this file to You under the Apache License, Version 2.0
7
+ (the "License"); you may not use this file except in compliance with
8
+ the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ -->
18
+
19
+ <!--
20
+ This is an example of a simple "solr.xml" file for configuring one or
21
+ more Solr Cores, as well as allowing Cores to be added, removed, and
22
+ reloaded via HTTP requests.
23
+
24
+ More information about options available in this configuration file,
25
+ and Solr Core administration can be found online:
26
+ http://wiki.apache.org/solr/CoreAdmin
27
+ -->
28
+
29
+ <!--
30
+ All (relative) paths are relative to the Solr Home Directory
31
+
32
+ persistent: Save changes made via the API to this file
33
+ sharedLib: path to a lib directory that will be shared across all cores
34
+ -->
35
+ <solr persistent="true">
36
+ <!-- by default, this is 50 @ WARN
37
+ <logging enabled="true">
38
+ <watcher size="100" threshold="INFO" />
39
+ </logging>
40
+ -->
41
+
42
+ <!--
43
+ adminPath: RequestHandler path to manage cores.
44
+ If 'null' (or absent), cores will not be manageable via request handler
45
+ defaultCoreName: (optional) core to use when no core name is specified in an access url
46
+
47
+ All of the attributes in cores after defaultCoreName only apply when running in SolrCloud mode.
48
+ You can read more about SolrCloud mode at http://wiki.apache.org/solr/SolrCloud
49
+ -->
50
+ <cores adminPath="/admin/cores" defaultCoreName="collection1" host="${host:}" hostPort="${jetty.port:8983}" hostContext="${hostContext:solr}" zkClientTimeout="${zkClientTimeout:15000}">
51
+ <core name="collection1" instanceDir="collection1" />
52
+ </cores>
53
+ </solr>
@@ -0,0 +1,17 @@
1
+ # The number of milliseconds of each tick
2
+ tickTime=2000
3
+ # The number of ticks that the initial
4
+ # synchronization phase can take
5
+ initLimit=10
6
+ # The number of ticks that can pass between
7
+ # sending a request and getting an acknowledgement
8
+ syncLimit=5
9
+
10
+ # the directory where the snapshot is stored.
11
+ # dataDir=/opt/zookeeper/data
12
+ # NOTE: Solr defaults the dataDir to <solrHome>/zoo_data
13
+
14
+ # the port at which the clients will connect
15
+ # clientPort=2181
16
+ # NOTE: Solr sets this based on zkRun / zkHost params
17
+
data/lib/rere.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'net/http'
2
+ require 'rere/version'
3
+ require 'rere/server'
4
+
5
+ if defined?(Rails)
6
+ require 'rere/railtie'
7
+ end
8
+
9
+ module Rere
10
+ extend self
11
+
12
+ def start(timeout = 10)
13
+ server.start
14
+
15
+ # TODO: Maybe add this to the server.start method itself
16
+ wait_for_startup(timeout)
17
+ end
18
+
19
+ def run
20
+ server.run
21
+ end
22
+
23
+ def stop
24
+ server.stop
25
+ puts "*** Stopped solr ***"
26
+ end
27
+
28
+ def reload_core(corename)
29
+ server.reload_core(corename)
30
+ end
31
+
32
+ def log_path
33
+ server.log_path
34
+ end
35
+
36
+
37
+ def server
38
+ @server ||= begin
39
+ env = ENV['RAILS_ENV'] || 'development'
40
+ Rere::Server.new(:environment => env)
41
+ end
42
+ end
43
+
44
+
45
+ # Hmmm, should I explicitly ping the cores too?
46
+ def running?
47
+ begin
48
+ ping_uri = URI.parse("#{server.url}/admin/ping")
49
+ Net::HTTP.get(ping_uri)
50
+ true
51
+ rescue
52
+ false
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def wait_for_startup(timeout)
59
+ timeout.times do
60
+ break if running?
61
+ sleep(1)
62
+ end
63
+ raise TimeoutError, "Solr didn't start within #{timeout}" unless running?
64
+ end
65
+
66
+ end
@@ -0,0 +1,8 @@
1
+ module Rere
2
+ class Railtie < ::Rails::Railtie
3
+ rake_tasks do
4
+ load "rere/tasks/solr.rake"
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,380 @@
1
+ require 'fileutils'
2
+ require 'tempfile'
3
+ require 'escape'
4
+ require 'uri'
5
+ require 'yaml'
6
+ require 'childprocess'
7
+
8
+ class Hash
9
+ def symbolize_keys!
10
+ keys.each do |key|
11
+ self[(key.to_sym rescue key) || key] = delete(key)
12
+ end
13
+ self
14
+ end
15
+ end
16
+
17
+ module Rere
18
+
19
+ #
20
+ # Wraps the basic operations for starting/stopping Solr in the Moto
21
+ # environment.
22
+ #
23
+ # By default when starting the server will look for a config/solr.yml
24
+ # file. If found these options will be used as the default options for
25
+ # solr. Any arguments provided on the command line will override those
26
+ # in the solr.yml file.
27
+ #
28
+ # = Configuration options
29
+ #
30
+ # <tt>environment</tt> - (required) the environment solr is starting in. The
31
+ # server will look up the config options under the given environment in
32
+ # config/solr.yml.
33
+ #
34
+ # <tt>config_path</tt> - use to specify a config file path for the server
35
+ # if it is not in the default location of config/solr.yml
36
+ #
37
+ class Server
38
+
39
+ ServerError = Class.new(RuntimeError)
40
+ ConfigError = Class.new(ServerError)
41
+ AlreadyRunningError = Class.new(ServerError)
42
+ NotRunningError = Class.new(ServerError)
43
+ JavaMissing = Class.new(ServerError)
44
+
45
+
46
+ # Jetty opts
47
+ attr_reader :server_root, :solr_home, :solr_data_dir, :tmp_dir, :port, :bind_address
48
+
49
+ # Java opts
50
+ attr_reader :min_memory, :max_memory, :log_level
51
+
52
+ # Environment opts
53
+ attr_reader :environment, :install_root, :config_path
54
+
55
+ # Solr opts
56
+ attr_reader :log_dir
57
+
58
+ # TODO: maybe explicitly read the options and check for anything that is
59
+ # not understood
60
+ def initialize(opts = {})
61
+ @environment = opts.delete(:environment) {|k| raise(ArgumentError, ":environment is required") }
62
+ @install_root = opts.fetch(:install_root) { File.join(Dir.pwd) }
63
+
64
+ # load the config for the server
65
+ @config_path = opts.delete(:config_file) {|k| "#{install_root}/config/solr.yml" }
66
+ unless File.exist?(@config_path)
67
+ raise ConfigError, "No configuration found. Looking for #{@config_path}"
68
+ end
69
+ puts "Loading config from #{@config_path} and environment #{@environment}"
70
+
71
+ config = YAML::load(File.open(@config_path))[@environment]['solr']
72
+ config.symbolize_keys!
73
+
74
+ config = config.merge(opts)
75
+ @min_memory = config.fetch(:min_memory) { 1024 }
76
+ @max_memory = config.fetch(:max_memory) { 1024 }
77
+ if config[:url]
78
+ @bind_address, @port = parse_url(config[:url])
79
+ else
80
+ @bind_address = config.delete(:hostname) {|k| raise(ArgumentError, ":hostname is required") }
81
+ @port = config[:port] || 8983
82
+ end
83
+
84
+ @log_level = config[:log_level] || 'INFO'
85
+ @log_dir = log_dir_path(config[:log_dir])
86
+
87
+ @solr_data_dir = config.delete(:data_dir) { "#{install_root}/solr/data/#{environment}" }
88
+
89
+ @solr_home = config.delete(:solr_home) { "#{install_root}/solr" }
90
+
91
+ end
92
+
93
+ def log_dir_path(log_dir = nil)
94
+ if log_dir.nil?
95
+ "#{install_root}/log"
96
+ else
97
+ File.expand_path(log_dir)
98
+ end
99
+ end
100
+
101
+ def url
102
+ @url ||= begin
103
+ if bind_address == '0.0.0.0'
104
+ "http://localhost:#{port}/solr"
105
+ else
106
+ "http://#{bind_address}:#{port}/solr"
107
+ end
108
+ end
109
+ end
110
+
111
+ def parse_url(url)
112
+ uri = URI.parse(url)
113
+ return uri.host, uri.port
114
+ end
115
+
116
+ # Esssentially sends a message like:
117
+ #
118
+ # http://localhost:8983/solr/admin/cores?action=RELOAD&core=core0
119
+ #
120
+ def reload_core(corename)
121
+ reload_url = "#{url}/admin/cores?action=RELOAD&core=#{corename}"
122
+ response = Net::HTTP.get_response(URI.parse(reload_url))
123
+ if response.code.to_i == 200
124
+ puts "Reloaded #{corename} core"
125
+ else
126
+ raise "Could not reload #{corename}: response #{response.code}. " +
127
+ "Check your solr log: #{log_path}"
128
+ end
129
+ end
130
+
131
+ # There is a lag between the time solr starts and it is ready to
132
+ # accept requests. Use +running?+ to determine if the process itself
133
+ # is up.
134
+ def running?
135
+ if File.exists?(pid_path)
136
+ existing_pid = IO.read(pid_path).to_i
137
+ begin
138
+ Process.kill(0, existing_pid)
139
+ return true
140
+ rescue Errno::ESRCH
141
+ return false
142
+ end
143
+ else
144
+ return false
145
+ end
146
+ end
147
+
148
+ def status
149
+ if running?
150
+ ctime = File.stat(pid_path).ctime
151
+ pid = IO.read(pid_path).to_i
152
+ puts "Running since #{ctime} with pid #{pid}"
153
+ else
154
+ puts "stopped"
155
+ end
156
+ end
157
+
158
+ def start
159
+ # TODO make sure java is installed?
160
+
161
+ ensure_dirs!
162
+
163
+ if File.exists?(pid_path)
164
+ existing_pid = IO.read(pid_path).to_i
165
+ begin
166
+ Process.kill(0, existing_pid)
167
+ raise(AlreadyRunningError, "Server is already running with PID #{existing_pid}")
168
+ rescue Errno::ESRCH
169
+ $stderr.puts("Removing stale PID #{pid_path}")
170
+ FileUtils.rm(pid_path)
171
+ end
172
+
173
+ end
174
+
175
+ @logging_config_path = write_logging_properties
176
+
177
+ # fork the start script, which execs the call to java (thus replaces itself)
178
+ fork do
179
+ pid = fork do
180
+ Process.setsid
181
+ $stdin.reopen('/dev/null')
182
+ $stdout.reopen('/dev/null', 'a')
183
+ $stderr.reopen($stdout)
184
+ run
185
+ end
186
+ write_pid(pid)
187
+
188
+ puts '*'*80
189
+ puts "Solr starting in environment #{environment}, using config #{config_path}"
190
+ puts "and solr home #{solr_home}."
191
+ puts "Server will be available at 'http://#{bind_address}:#{port}/solr'"
192
+ puts "With process id #{pid} and pid file #{pid_path}"
193
+ puts "Logging to #{log_path}"
194
+ puts '*'*80
195
+ end
196
+
197
+ end
198
+
199
+ def stop(raise_if_not_running = true)
200
+ if File.exist?(pid_path)
201
+ pid = IO.read(pid_path).to_i
202
+ puts "Stopping solr with process #{pid} and pid #{pid_path}"
203
+
204
+ begin
205
+ Process.kill('TERM', pid)
206
+ rescue Errno::ESRCH
207
+ raise NotRunningError, "Process with PID #{pid} is not running!"
208
+ ensure
209
+ FileUtils.rm(pid_path)
210
+ end
211
+ else
212
+ # raise NotRunningError, "Solr is not running. Looking for pid file #{pid_path}"
213
+ if raise_if_not_running
214
+ raise NotRunningError, "Solr is not running. Looking for pid file #{pid_path}"
215
+ else
216
+ puts "Solr not running. Not pid found at #{pid_path}"
217
+ end
218
+
219
+ end
220
+ end
221
+
222
+ def restart
223
+ stop(false)
224
+ sleep(2)
225
+ start
226
+ end
227
+
228
+ def run
229
+ puts '*'*80
230
+ puts "Solr starting in environment #{environment}, using config #{config_path}"
231
+ puts "and solr home #{solr_home}."
232
+ puts "Server will be available at 'http://#{bind_address}:#{port}/solr'"
233
+ puts '*'*80
234
+
235
+ cmd = ["java"]
236
+ cmd << "-server" unless windows?
237
+ cmd << "-Xms#{min_memory}m"
238
+ cmd << "-Xmx#{max_memory}m"
239
+ cmd << "-Djetty.port=#{port}"
240
+ cmd << "-Drails.env=#{environment}" # Only needed while we connect to the db for synonyms
241
+ cmd << "-Dsolr.solr.home=#{solr_home}"
242
+ cmd << "-Dsolr.data.dir=#{solr_data_dir}"
243
+ cmd << "-Djava.io.tmpdir=#{tmp_dir}"
244
+ cmd << "-DRAILS_ENV=#{environment}"
245
+
246
+ if @logging_config_path
247
+ puts "Loading logging configuration from #{@logging_config_path}"
248
+ cmd << "-Dlog4j.configuration=file://#{@logging_config_path}"
249
+ end
250
+
251
+ cmd << "-jar"
252
+ cmd << "start.jar"
253
+
254
+ cmd_s = Escape.shell_command(cmd)
255
+
256
+ puts "RUNNING: #{cmd_s}"
257
+
258
+ # This doesn't work with jruby
259
+ # FileUtils.cd(server_root) do
260
+ # exec("#{cmd_s}")
261
+ # end
262
+
263
+ FileUtils.cd(File.dirname(solr_jar)) do
264
+ require "childprocess"
265
+ process = ChildProcess.build(*cmd)
266
+
267
+ trap('TERM') do
268
+ puts "Stopping solr"
269
+ process.stop
270
+ exit 0
271
+ end
272
+
273
+ trap('INT') do
274
+ puts "Stopping solr"
275
+ process.stop
276
+ exit 0
277
+ end
278
+
279
+ process.io.inherit!
280
+ process.start
281
+ process.wait
282
+ end
283
+
284
+ end
285
+
286
+ def solr_jar
287
+ "#{server_root}/start.jar"
288
+ end
289
+
290
+ def ensure_dirs!
291
+ FileUtils.mkdir_p pid_dir
292
+ end
293
+
294
+ def pid_dir
295
+ @pid_dir || File.join(tmp_dir, 'pids')
296
+ end
297
+
298
+ def pid_file
299
+ @pid_file || "solr.#{environment}.pid"
300
+ end
301
+
302
+ def pid_path
303
+ File.join(pid_dir, pid_file)
304
+ end
305
+
306
+ def write_pid(pid)
307
+ File.open(pid_path, 'w') {|f| f << pid }
308
+ end
309
+
310
+ def environment
311
+ @environment || 'development'
312
+ end
313
+
314
+ def tmp_dir
315
+ File.expand_path(@tmp_dir || "#{install_root}/tmp")
316
+ end
317
+
318
+ def gem_root
319
+ @gem_root ||= File.expand_path("../../../", __FILE__)
320
+ end
321
+
322
+ # The directory containing jetty and the solr.war.
323
+ def server_root
324
+ @server_root || File.expand_path('../../../server', __FILE__)
325
+ end
326
+
327
+ # Directory where the index directories: index and spelling
328
+ # are kept.
329
+ # Defaults to "#{install_root}/solr/data/#{environment}"
330
+ def solr_data_dir
331
+ @solr_data_dir
332
+ end
333
+ alias_method :data_dir, :solr_data_dir
334
+
335
+ def solr_home
336
+ @solr_home # || "#{install_root}/solr"
337
+ end
338
+
339
+ def log_path
340
+ @log_path || "#{log_dir}/solr.#{environment}.log"
341
+ end
342
+
343
+ # Write out a java properties files with the necessary
344
+ # key value pairs to initialize log4j correctly.
345
+ # Returns the path to where the properties file is created
346
+ def write_logging_properties
347
+ logging_config_path = File.join(tmp_dir, "solr.#{environment}.log.properties")
348
+ File.open(logging_config_path, 'w') {|f| f.puts logging_config }
349
+ logging_config_path
350
+ end
351
+
352
+ private
353
+
354
+ def logging_config
355
+ <<-LOG_CONFIG
356
+ log4j.rootLogger=#{log_level.to_s.upcase}, file, CONSOLE
357
+
358
+ log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
359
+ log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
360
+ log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
361
+
362
+ log4j.appender.file=org.apache.log4j.RollingFileAppender
363
+ log4j.appender.file.MaxFileSize=4MB
364
+ log4j.appender.file.MaxBackupIndex=9
365
+
366
+ log4j.appender.file.File=#{log_path}
367
+ log4j.appender.file.layout=org.apache.log4j.PatternLayout
368
+ log4j.appender.file.layout.ConversionPattern=%-5p - %d{yyyy-MM-dd HH:mm:ss.SSS}; %C; %m%n
369
+
370
+ log4j.logger.org.apache.zookeeper=WARN
371
+ LOG_CONFIG
372
+ end
373
+
374
+ def windows?
375
+ RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /mingw/
376
+ end
377
+
378
+ end
379
+
380
+ end