note about downloading solr dists
[lui-solr.git] / dist / install_solr6_service.sh
1 #!/usr/bin/env bash
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 if [[ $EUID -ne 0 ]]; then
18   echo -e "\nERROR: This script must be run as root\n" 1>&2
19   exit 1
20 fi
21
22 print_usage() {
23   ERROR_MSG="$1"
24
25   if [ "$ERROR_MSG" != "" ]; then
26     echo -e "\nERROR: $ERROR_MSG\n" 1>&2
27   fi
28
29   echo ""
30   echo "Usage: install_solr_service.sh path_to_solr_distribution_archive OPTIONS"
31   echo ""
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)"
34   echo ""
35   echo "  Supported OPTIONS include:"
36   echo ""
37   echo "    -d     Directory for live / writable Solr files, such as logs, pid files, and index data; defaults to /var/solr"
38   echo ""
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."
41   echo ""
42   echo "    -p     Port Solr should bind to; default is 8983"
43   echo ""
44   echo "    -s     Service name; defaults to solr"
45   echo ""
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."
48   echo ""
49   echo "    -f     Upgrade Solr. Overwrite symlink and init script of previous installation."
50   echo ""
51   echo " NOTE: Must be run as the root user"
52   echo ""
53 } # end print_usage
54
55 if [ -f "/proc/version" ]; then
56   proc_version=`cat /proc/version`
57 else
58   proc_version=`uname -a`
59 fi
60
61 if [[ $proc_version == *"Debian"* ]]; then
62   distro=Debian
63 elif [[ $proc_version == *"Red Hat"* ]]; then
64   distro=RedHat
65 elif [[ $proc_version == *"Ubuntu"* ]]; then
66   distro=Ubuntu
67 elif [[ $proc_version == *"SUSE"* ]]; then
68   distro=SUSE
69 else
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
71   exit 1
72 fi
73
74 if [ -z "$1" ]; then
75   print_usage "Must specify the path to the Solr installation archive, such as solr-5.0.0.tgz"
76   exit 1
77 fi
78
79 SOLR_ARCHIVE=$1
80 if [ ! -f "$SOLR_ARCHIVE" ]; then
81   print_usage "Specified Solr installation archive $SOLR_ARCHIVE not found!"
82   exit 1
83 fi
84
85 # strip off path info
86 SOLR_INSTALL_FILE=${SOLR_ARCHIVE##*/}
87 is_tar=true
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}
92   is_tar=false
93 else
94   print_usage "Solr installation archive $SOLR_ARCHIVE is invalid, expected a .tgz or .zip file!"
95   exit 1
96 fi
97
98 if [ $# -gt 1 ]; then
99   shift
100   while true; do
101     case $1 in
102         -i)
103             if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
104               print_usage "Directory path is required when using the $1 option!"
105               exit 1
106             fi
107             SOLR_EXTRACT_DIR=$2
108             shift 2
109         ;;
110         -d)
111             if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
112               print_usage "Directory path is required when using the $1 option!"
113               exit 1
114             fi
115             SOLR_VAR_DIR="$2"
116             shift 2
117         ;;
118         -u)
119             if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
120               print_usage "Username is required when using the $1 option!"
121               exit 1
122             fi
123             SOLR_USER="$2"
124             shift 2
125         ;;
126         -s)
127             if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
128               print_usage "Service name is required when using the $1 option!"
129               exit 1
130             fi
131             SOLR_SERVICE="$2"
132             shift 2
133         ;;
134         -p)
135             if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
136               print_usage "Port is required when using the $1 option!"
137               exit 1
138             fi
139             SOLR_PORT="$2"
140             shift 2
141         ;;
142         -f)
143             SOLR_UPGRADE="YES"
144             shift 1
145         ;;
146         -help|-usage)
147             print_usage ""
148             exit 0
149         ;;
150         --)
151             shift
152             break
153         ;;
154         *)
155             if [ "$1" != "" ]; then
156               print_usage "Unrecognized or misplaced argument: $1!"
157               exit 1
158             else
159               break # out-of-args, stop looping
160             fi
161         ;;
162     esac
163   done
164 fi
165
166 if [ -z "$SOLR_EXTRACT_DIR" ]; then
167   SOLR_EXTRACT_DIR=/opt
168 fi
169
170 if [ ! -d "$SOLR_EXTRACT_DIR" ]; then
171   print_usage "Installation directory $SOLR_EXTRACT_DIR not found! Please create it before running this script."
172   exit 1
173 fi
174
175 if [ -z "$SOLR_SERVICE" ]; then
176   SOLR_SERVICE=solr
177 fi
178
179 if [ -z "$SOLR_VAR_DIR" ]; then
180   SOLR_VAR_DIR="/var/$SOLR_SERVICE"
181 fi
182
183 if [ -z "$SOLR_USER" ]; then
184   SOLR_USER=solr
185 fi
186
187 if [ -z "$SOLR_PORT" ]; then
188   SOLR_PORT=8983
189 fi
190
191 if [ -z "$SOLR_UPGRADE" ]; then
192   SOLR_UPGRADE=NO
193 fi
194
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."
198     exit 1
199   fi
200
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."
203     exit 1
204   fi
205 fi
206
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
211 fi
212
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
218     adduser "$SOLR_USER"
219   elif [ "$distro" == "SUSE" ]; then
220     useradd -m "$SOLR_USER"
221   else
222     adduser --system --shell /bin/bash --group --disabled-password --home "$SOLR_VAR_DIR" "$SOLR_USER"
223   fi
224 fi
225
226 # extract
227 SOLR_INSTALL_DIR="$SOLR_EXTRACT_DIR/$SOLR_DIR"
228 if [ ! -d "$SOLR_INSTALL_DIR" ]; then
229
230   echo -e "\nExtracting $SOLR_ARCHIVE to $SOLR_EXTRACT_DIR\n"
231
232   if $is_tar ; then
233     tar zxf "$SOLR_ARCHIVE" -C "$SOLR_EXTRACT_DIR"
234   else
235     unzip -q "$SOLR_ARCHIVE" -d "$SOLR_EXTRACT_DIR"
236   fi
237
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
240     exit 1
241   fi
242
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"
247 else
248   echo -e "\nWARNING: $SOLR_INSTALL_DIR already exists! Skipping extract ...\n"
249 fi
250
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"
255 fi
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"
258 else
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"
261 fi
262
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"
274
275 # install/move configuration
276 if [ ! -d /etc/default ]; then
277   mkdir /etc/default
278   chown root: /etc/default
279   chmod 0755 /etc/default
280 fi
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"
286 else
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"
295 fi
296 chown root: "/etc/default/$SOLR_SERVICE.in.sh"
297 chmod 0644 "/etc/default/$SOLR_SERVICE.in.sh"
298
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"
304 else
305   cp "$SOLR_INSTALL_DIR/server/solr/solr.xml" "$SOLR_VAR_DIR/data/solr.xml"
306 fi
307 if [ -f "$SOLR_VAR_DIR/log4j.properties" ]; then
308   echo -e "\n$SOLR_VAR_DIR/log4j.properties already exists. Skipping install ...\n"
309 else
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"
313 fi
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
317
318 # configure autostart of service
319 if [[ "$distro" == "RedHat" || "$distro" == "SUSE" ]]; then
320   chkconfig "$SOLR_SERVICE" on
321 else
322   update-rc.d "$SOLR_SERVICE" defaults
323 fi
324
325 # start service
326 service "$SOLR_SERVICE" start
327 sleep 5
328 service "$SOLR_SERVICE" status
329
330 echo "Service $SOLR_SERVICE installed."