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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 if [[ $EUID -ne 0 ]]; then
18 echo -e "\nERROR: This script must be run as root\n" 1>&2
25 if [ "$ERROR_MSG" != "" ]; then
26 echo -e "\nERROR: $ERROR_MSG\n" 1>&2
30 echo "Usage: install_solr_service.sh path_to_solr_distribution_archive OPTIONS"
32 echo " The first argument to the script must be a path to a Solr distribution archive, such as solr-5.0.0.tgz"
33 echo " (only .tgz or .zip are supported formats for the archive)"
35 echo " Supported OPTIONS include:"
37 echo " -d Directory for live / writable Solr files, such as logs, pid files, and index data; defaults to /var/solr"
39 echo " -i Directory to extract the Solr installation archive; defaults to /opt/"
40 echo " The specified path must exist prior to using this script."
42 echo " -p Port Solr should bind to; default is 8983"
44 echo " -s Service name; defaults to solr"
46 echo " -u User to own the Solr files and run the Solr process as; defaults to solr"
47 echo " This script will create the specified user account if it does not exist."
49 echo " -f Upgrade Solr. Overwrite symlink and init script of previous installation."
51 echo " NOTE: Must be run as the root user"
55 if [ -f "/proc/version" ]; then
56 proc_version=`cat /proc/version`
58 proc_version=`uname -a`
61 if [[ $proc_version == *"Debian"* ]]; then
63 elif [[ $proc_version == *"Red Hat"* ]]; then
65 elif [[ $proc_version == *"Ubuntu"* ]]; then
67 elif [[ $proc_version == *"SUSE"* ]]; then
70 echo -e "\nERROR: Your Linux distribution ($proc_version) not supported by this script!\nYou'll need to setup Solr as a service manually using the documentation provided in the Solr Reference Guide.\n" 1>&2
75 print_usage "Must specify the path to the Solr installation archive, such as solr-5.0.0.tgz"
80 if [ ! -f "$SOLR_ARCHIVE" ]; then
81 print_usage "Specified Solr installation archive $SOLR_ARCHIVE not found!"
86 SOLR_INSTALL_FILE=${SOLR_ARCHIVE##*/}
88 if [ ${SOLR_INSTALL_FILE: -4} == ".tgz" ]; then
89 SOLR_DIR=${SOLR_INSTALL_FILE%.tgz}
90 elif [ ${SOLR_INSTALL_FILE: -4} == ".zip" ]; then
91 SOLR_DIR=${SOLR_INSTALL_FILE%.zip}
94 print_usage "Solr installation archive $SOLR_ARCHIVE is invalid, expected a .tgz or .zip file!"
103 if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
104 print_usage "Directory path is required when using the $1 option!"
111 if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
112 print_usage "Directory path is required when using the $1 option!"
119 if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
120 print_usage "Username is required when using the $1 option!"
127 if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
128 print_usage "Service name is required when using the $1 option!"
135 if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
136 print_usage "Port is required when using the $1 option!"
155 if [ "$1" != "" ]; then
156 print_usage "Unrecognized or misplaced argument: $1!"
159 break # out-of-args, stop looping
166 if [ -z "$SOLR_EXTRACT_DIR" ]; then
167 SOLR_EXTRACT_DIR=/opt
170 if [ ! -d "$SOLR_EXTRACT_DIR" ]; then
171 print_usage "Installation directory $SOLR_EXTRACT_DIR not found! Please create it before running this script."
175 if [ -z "$SOLR_SERVICE" ]; then
179 if [ -z "$SOLR_VAR_DIR" ]; then
180 SOLR_VAR_DIR="/var/$SOLR_SERVICE"
183 if [ -z "$SOLR_USER" ]; then
187 if [ -z "$SOLR_PORT" ]; then
191 if [ -z "$SOLR_UPGRADE" ]; then
195 if [ ! "$SOLR_UPGRADE" = "YES" ]; then
196 if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then
197 print_usage "/etc/init.d/$SOLR_SERVICE already exists! Perhaps Solr is already setup as a service on this host? To upgrade Solr use the -f option."
201 if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then
202 print_usage "$SOLR_EXTRACT_DIR/$SOLR_SERVICE already exists! Please move this directory / link or choose a different service name using the -s option."
207 # stop running instance
208 if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then
209 echo -e "\nStopping Solr instance if exists ...\n"
210 service "$SOLR_SERVICE" stop
213 # create user if not exists
214 solr_uid="`id -u "$SOLR_USER"`"
215 if [ $? -ne 0 ]; then
216 echo "Creating new user: $SOLR_USER"
217 if [ "$distro" == "RedHat" ]; then
219 elif [ "$distro" == "SUSE" ]; then
220 useradd -m "$SOLR_USER"
222 adduser --system --shell /bin/bash --group --disabled-password --home "$SOLR_VAR_DIR" "$SOLR_USER"
227 SOLR_INSTALL_DIR="$SOLR_EXTRACT_DIR/$SOLR_DIR"
228 if [ ! -d "$SOLR_INSTALL_DIR" ]; then
230 echo -e "\nExtracting $SOLR_ARCHIVE to $SOLR_EXTRACT_DIR\n"
233 tar zxf "$SOLR_ARCHIVE" -C "$SOLR_EXTRACT_DIR"
235 unzip -q "$SOLR_ARCHIVE" -d "$SOLR_EXTRACT_DIR"
238 if [ ! -d "$SOLR_INSTALL_DIR" ]; then
239 echo -e "\nERROR: Expected directory $SOLR_INSTALL_DIR not found after extracting $SOLR_ARCHIVE ... script fails.\n" 1>&2
243 chown -R root: "$SOLR_INSTALL_DIR"
244 find "$SOLR_INSTALL_DIR" -type d -print0 | xargs -0 chmod 0755
245 find "$SOLR_INSTALL_DIR" -type f -print0 | xargs -0 chmod 0644
246 chmod -R 0755 "$SOLR_INSTALL_DIR/bin"
248 echo -e "\nWARNING: $SOLR_INSTALL_DIR already exists! Skipping extract ...\n"
251 # create a symlink for easier scripting
252 if [ -h "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then
253 echo -e "\nRemoving old symlink $SOLR_EXTRACT_DIR/$SOLR_SERVICE ...\n"
254 rm "$SOLR_EXTRACT_DIR/$SOLR_SERVICE"
256 if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then
257 echo -e "\nWARNING: $SOLR_EXTRACT_DIR/$SOLR_SERVICE is not symlink! Skipping symlink update ...\n"
259 echo -e "\nInstalling symlink $SOLR_EXTRACT_DIR/$SOLR_SERVICE -> $SOLR_INSTALL_DIR ...\n"
260 ln -s "$SOLR_INSTALL_DIR" "$SOLR_EXTRACT_DIR/$SOLR_SERVICE"
263 # install init.d script
264 echo -e "\nInstalling /etc/init.d/$SOLR_SERVICE script ...\n"
265 cp "$SOLR_INSTALL_DIR/bin/init.d/solr" "/etc/init.d/$SOLR_SERVICE"
266 chmod 0744 "/etc/init.d/$SOLR_SERVICE"
267 chown root: "/etc/init.d/$SOLR_SERVICE"
268 # do some basic variable substitution on the init.d script
269 sed_expr1="s#SOLR_INSTALL_DIR=.*#SOLR_INSTALL_DIR=\"$SOLR_EXTRACT_DIR/$SOLR_SERVICE\"#"
270 sed_expr2="s#SOLR_ENV=.*#SOLR_ENV=\"/etc/default/$SOLR_SERVICE.in.sh\"#"
271 sed_expr3="s#RUNAS=.*#RUNAS=\"$SOLR_USER\"#"
272 sed_expr4="s#Provides:.*#Provides: $SOLR_SERVICE#"
273 sed -i -e "$sed_expr1" -e "$sed_expr2" -e "$sed_expr3" -e "$sed_expr4" "/etc/init.d/$SOLR_SERVICE"
275 # install/move configuration
276 if [ ! -d /etc/default ]; then
278 chown root: /etc/default
279 chmod 0755 /etc/default
281 if [ -f "$SOLR_VAR_DIR/solr.in.sh" ]; then
282 echo -e "\nMoving existing $SOLR_VAR_DIR/solr.in.sh to /etc/default/$SOLR_SERVICE.in.sh ...\n"
283 mv "$SOLR_VAR_DIR/solr.in.sh" "/etc/default/$SOLR_SERVICE.in.sh"
284 elif [ -f "/etc/default/$SOLR_SERVICE.in.sh" ]; then
285 echo -e "\n/etc/default/$SOLR_SERVICE.in.sh already exist. Skipping install ...\n"
287 echo -e "\nInstalling /etc/default/$SOLR_SERVICE.in.sh ...\n"
288 cp "$SOLR_INSTALL_DIR/bin/solr.in.sh" "/etc/default/$SOLR_SERVICE.in.sh"
289 echo "SOLR_PID_DIR=\"$SOLR_VAR_DIR\"
290 SOLR_HOME=\"$SOLR_VAR_DIR/data\"
291 LOG4J_PROPS=\"$SOLR_VAR_DIR/log4j.properties\"
292 SOLR_LOGS_DIR=\"$SOLR_VAR_DIR/logs\"
293 SOLR_PORT=\"$SOLR_PORT\"
294 " >> "/etc/default/$SOLR_SERVICE.in.sh"
296 chown root: "/etc/default/$SOLR_SERVICE.in.sh"
297 chmod 0644 "/etc/default/$SOLR_SERVICE.in.sh"
299 # install data directories and files
300 mkdir -p "$SOLR_VAR_DIR/data"
301 mkdir -p "$SOLR_VAR_DIR/logs"
302 if [ -f "$SOLR_VAR_DIR/data/solr.xml" ]; then
303 echo -e "\n$SOLR_VAR_DIR/data/solr.xml already exists. Skipping install ...\n"
305 cp "$SOLR_INSTALL_DIR/server/solr/solr.xml" "$SOLR_VAR_DIR/data/solr.xml"
307 if [ -f "$SOLR_VAR_DIR/log4j.properties" ]; then
308 echo -e "\n$SOLR_VAR_DIR/log4j.properties already exists. Skipping install ...\n"
310 cp "$SOLR_INSTALL_DIR/server/resources/log4j.properties" "$SOLR_VAR_DIR/log4j.properties"
311 sed_expr="s#solr.log=.*#solr.log=\${solr.solr.home}/../logs#"
312 sed -i -e "$sed_expr" "$SOLR_VAR_DIR/log4j.properties"
314 chown -R "$SOLR_USER:" "$SOLR_VAR_DIR"
315 find "$SOLR_VAR_DIR" -type d -print0 | xargs -0 chmod 0750
316 find "$SOLR_VAR_DIR" -type f -print0 | xargs -0 chmod 0640
318 # configure autostart of service
319 if [[ "$distro" == "RedHat" || "$distro" == "SUSE" ]]; then
320 chkconfig "$SOLR_SERVICE" on
322 update-rc.d "$SOLR_SERVICE" defaults
326 service "$SOLR_SERVICE" start
328 service "$SOLR_SERVICE" status
330 echo "Service $SOLR_SERVICE installed."