diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 3e12d4de2c6..388e21bf4a0 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -583,6 +583,8 @@ Other Changes * SOLR-8299: ConfigSet DELETE operation no longer allows deletion of config sets that are currently in use by other collections (Anshum Gupta) +* SOLR-8101: Improve Linux service installation script (Sergey Urushkin via Timothy Potter) + ================== 5.3.1 ================== Bug Fixes diff --git a/solr/bin/init.d/solr b/solr/bin/init.d/solr index 5fb91d37384..e73e0d68300 100644 --- a/solr/bin/init.d/solr +++ b/solr/bin/init.d/solr @@ -33,7 +33,7 @@ # update-rc.d solr enable # Where you extracted the Solr distribution bundle -SOLR_INSTALL_DIR=/opt/solr +SOLR_INSTALL_DIR="/opt/solr" if [ ! -d "$SOLR_INSTALL_DIR" ]; then echo "$SOLR_INSTALL_DIR not found! Please check the SOLR_INSTALL_DIR setting in your $0 script." @@ -44,7 +44,7 @@ fi # variables used by the bin/solr script. It's highly recommended to define this script so # that you can keep the Solr binary files separated from live files (pid, logs, index data, etc) # see bin/solr.in.sh for an example -SOLR_ENV=/var/solr/solr.in.sh +SOLR_ENV="/etc/default/solr.in.sh" if [ ! -f "$SOLR_ENV" ]; then echo "$SOLR_ENV not found! Please check the SOLR_ENV setting in your $0 script." @@ -53,10 +53,10 @@ fi # Specify the user to run Solr as; if not set, then Solr will run as root. # Running Solr as root is not recommended for production environments -RUNAS=solr +RUNAS="solr" # verify the specified run as user exists -runas_uid=`id -u $RUNAS` +runas_uid="`id -u "$RUNAS"`" if [ $? -ne 0 ]; then echo "User $RUNAS not found! Please create the $RUNAS user before running this script." exit 1 @@ -64,7 +64,7 @@ fi case "$1" in start|stop|restart|status) - SOLR_CMD=$1 + SOLR_CMD="$1" ;; *) echo "Usage: $0 {start|stop|restart|status}" @@ -72,7 +72,7 @@ case "$1" in esac if [ -n "$RUNAS" ]; then - su -c "SOLR_INCLUDE=$SOLR_ENV $SOLR_INSTALL_DIR/bin/solr $SOLR_CMD" - $RUNAS + su -c "SOLR_INCLUDE=\"$SOLR_ENV\" \"$SOLR_INSTALL_DIR/bin/solr\" $SOLR_CMD" - "$RUNAS" else - SOLR_INCLUDE=$SOLR_ENV $SOLR_INSTALL_DIR/bin/solr $SOLR_CMD + SOLR_INCLUDE="$SOLR_ENV" "$SOLR_INSTALL_DIR/bin/solr" "$SOLR_CMD" fi diff --git a/solr/bin/install_solr_service.sh b/solr/bin/install_solr_service.sh index 57a0264482d..c91777ab7d8 100644 --- a/solr/bin/install_solr_service.sh +++ b/solr/bin/install_solr_service.sh @@ -46,6 +46,8 @@ print_usage() { echo " -u User to own the Solr files and run the Solr process as; defaults to solr" echo " This script will create the specified user account if it does not exist." echo "" + echo " -f Upgrade Solr. Overwrite symlink and init script of previous installation." + echo "" echo " NOTE: Must be run as the root user" echo "" } # end print_usage @@ -137,6 +139,10 @@ if [ $# -gt 1 ]; then SOLR_PORT="$2" shift 2 ;; + -f) + SOLR_UPGRADE="YES" + shift 1 + ;; -help|-usage) print_usage "" exit 0 @@ -171,7 +177,7 @@ if [ -z "$SOLR_SERVICE" ]; then fi if [ -z "$SOLR_VAR_DIR" ]; then - SOLR_VAR_DIR=/var/$SOLR_SERVICE + SOLR_VAR_DIR="/var/$SOLR_SERVICE" fi if [ -z "$SOLR_USER" ]; then @@ -182,37 +188,51 @@ if [ -z "$SOLR_PORT" ]; then SOLR_PORT=8983 fi -if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then - echo -e "\nERROR: /etc/init.d/$SOLR_SERVICE already exists! Perhaps Solr is already setup as a service on this host?\n" 1>&2 - exit 1 +if [ -z "$SOLR_UPGRADE" ]; then + SOLR_UPGRADE=NO fi -if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then - print_usage "$SOLR_EXTRACT_DIR/$SOLR_SERVICE already exists! Please move this directory / link or choose a different service name using the -s option." - exit 1 -fi +if [ ! "$SOLR_UPGRADE" = "YES" ]; then + if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then + 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." + exit 1 + fi -solr_uid=`id -u $SOLR_USER` -if [ $? -ne 0 ]; then - echo "Creating new user: $SOLR_USER" - if [ "$distro" == "RedHat" ]; then - adduser $SOLR_USER - elif [ "$distro" == "SUSE" ]; then - useradd -m $SOLR_USER - else - adduser --system --shell /bin/bash --group --disabled-password --home /home/$SOLR_USER $SOLR_USER + if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then + print_usage "$SOLR_EXTRACT_DIR/$SOLR_SERVICE already exists! Please move this directory / link or choose a different service name using the -s option." + exit 1 fi fi -SOLR_INSTALL_DIR=$SOLR_EXTRACT_DIR/$SOLR_DIR +# stop running instance +if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then + echo -e "\nStopping Solr instance if exists ...\n" + service "$SOLR_SERVICE" stop +fi + +# create user if not exists +solr_uid="`id -u "$SOLR_USER"`" +if [ $? -ne 0 ]; then + echo "Creating new user: $SOLR_USER" + if [ "$distro" == "RedHat" ]; then + adduser "$SOLR_USER" + elif [ "$distro" == "SUSE" ]; then + useradd -m "$SOLR_USER" + else + adduser --system --shell /bin/bash --group --disabled-password --home "$SOLR_VAR_DIR" "$SOLR_USER" + fi +fi + +# extract +SOLR_INSTALL_DIR="$SOLR_EXTRACT_DIR/$SOLR_DIR" if [ ! -d "$SOLR_INSTALL_DIR" ]; then - echo "Extracting $SOLR_ARCHIVE to $SOLR_EXTRACT_DIR" + echo -e "\nExtracting $SOLR_ARCHIVE to $SOLR_EXTRACT_DIR\n" if $is_tar ; then - tar zxf $SOLR_ARCHIVE -C $SOLR_EXTRACT_DIR + tar zxf "$SOLR_ARCHIVE" -C "$SOLR_EXTRACT_DIR" else - unzip -q $SOLR_ARCHIVE -d $SOLR_EXTRACT_DIR + unzip -q "$SOLR_ARCHIVE" -d "$SOLR_EXTRACT_DIR" fi if [ ! -d "$SOLR_INSTALL_DIR" ]; then @@ -220,51 +240,91 @@ if [ ! -d "$SOLR_INSTALL_DIR" ]; then exit 1 fi - chown -R $SOLR_USER: $SOLR_INSTALL_DIR + chown -R root: "$SOLR_INSTALL_DIR" + find "$SOLR_INSTALL_DIR" -type d -print0 | xargs -0 chmod 0755 + find "$SOLR_INSTALL_DIR" -type f -print0 | xargs -0 chmod 0644 + chmod -R 0755 "$SOLR_INSTALL_DIR/bin" else echo -e "\nWARNING: $SOLR_INSTALL_DIR already exists! Skipping extract ...\n" fi # create a symlink for easier scripting -ln -s $SOLR_INSTALL_DIR $SOLR_EXTRACT_DIR/$SOLR_SERVICE -chown -h $SOLR_USER: $SOLR_EXTRACT_DIR/$SOLR_SERVICE - -mkdir -p $SOLR_VAR_DIR/data -mkdir -p $SOLR_VAR_DIR/logs -cp $SOLR_INSTALL_DIR/server/solr/solr.xml $SOLR_VAR_DIR/data/ -cp $SOLR_INSTALL_DIR/bin/solr.in.sh $SOLR_VAR_DIR/ -cp $SOLR_INSTALL_DIR/server/resources/log4j.properties $SOLR_VAR_DIR/log4j.properties -sed_expr="s#solr.log=.*#solr.log=\${solr.solr.home}/../logs#" -sed -i -e "$sed_expr" $SOLR_VAR_DIR/log4j.properties -chown -R $SOLR_USER: $SOLR_VAR_DIR - -echo "SOLR_PID_DIR=$SOLR_VAR_DIR -SOLR_HOME=$SOLR_VAR_DIR/data -LOG4J_PROPS=$SOLR_VAR_DIR/log4j.properties -SOLR_LOGS_DIR=$SOLR_VAR_DIR/logs -SOLR_PORT=$SOLR_PORT -" >> $SOLR_VAR_DIR/solr.in.sh - -echo "Creating /etc/init.d/$SOLR_SERVICE script ..." -cp $SOLR_INSTALL_DIR/bin/init.d/solr /etc/init.d/$SOLR_SERVICE -chmod 744 /etc/init.d/$SOLR_SERVICE -chown root:root /etc/init.d/$SOLR_SERVICE - -# do some basic variable substitution on the init.d script -sed_expr1="s#SOLR_INSTALL_DIR=.*#SOLR_INSTALL_DIR=$SOLR_EXTRACT_DIR/$SOLR_SERVICE#" -sed_expr2="s#SOLR_ENV=.*#SOLR_ENV=$SOLR_VAR_DIR/solr.in.sh#" -sed_expr3="s#RUNAS=.*#RUNAS=$SOLR_USER#" -sed_expr4="s#Provides:.*#Provides: $SOLR_SERVICE#" -sed -i -e "$sed_expr1" -e "$sed_expr2" -e "$sed_expr3" -e "$sed_expr4" /etc/init.d/$SOLR_SERVICE - -if [[ "$distro" == "RedHat" || "$distro" == "SUSE" ]]; then - chkconfig $SOLR_SERVICE on +if [ -h "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then + echo -e "\nRemoving old symlink $SOLR_EXTRACT_DIR/$SOLR_SERVICE ...\n" + rm "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" +fi +if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then + echo -e "\nWARNING: $SOLR_EXTRACT_DIR/$SOLR_SERVICE is not symlink! Skipping symlink update ...\n" else - update-rc.d $SOLR_SERVICE defaults + echo -e "\nInstalling symlink $SOLR_EXTRACT_DIR/$SOLR_SERVICE -> $SOLR_INSTALL_DIR ...\n" + ln -s "$SOLR_INSTALL_DIR" "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" fi -service $SOLR_SERVICE start +# install init.d script +echo -e "\nInstalling /etc/init.d/$SOLR_SERVICE script ...\n" +cp "$SOLR_INSTALL_DIR/bin/init.d/solr" "/etc/init.d/$SOLR_SERVICE" +chmod 0744 "/etc/init.d/$SOLR_SERVICE" +chown root: "/etc/init.d/$SOLR_SERVICE" +# do some basic variable substitution on the init.d script +sed_expr1="s#SOLR_INSTALL_DIR=.*#SOLR_INSTALL_DIR=\"$SOLR_EXTRACT_DIR/$SOLR_SERVICE\"#" +sed_expr2="s#SOLR_ENV=.*#SOLR_ENV=\"/etc/default/$SOLR_SERVICE.in.sh\"#" +sed_expr3="s#RUNAS=.*#RUNAS=\"$SOLR_USER\"#" +sed_expr4="s#Provides:.*#Provides: $SOLR_SERVICE#" +sed -i -e "$sed_expr1" -e "$sed_expr2" -e "$sed_expr3" -e "$sed_expr4" "/etc/init.d/$SOLR_SERVICE" + +# install/move configuration +if [ ! -d /etc/default ]; then + mkdir /etc/default + chown root: /etc/default + chmod 0755 /etc/default +fi +if [ -f "$SOLR_VAR_DIR/solr.in.sh" ]; then + echo -e "\nMoving existing $SOLR_VAR_DIR/solr.in.sh to /etc/default/$SOLR_SERVICE.in.sh ...\n" + mv "$SOLR_VAR_DIR/solr.in.sh" "/etc/default/$SOLR_SERVICE.in.sh" +elif [ -f "/etc/default/$SOLR_SERVICE.in.sh" ]; then + echo -e "\n/etc/default/$SOLR_SERVICE.in.sh already exist. Skipping install ...\n" +else + echo -e "\nInstalling /etc/default/$SOLR_SERVICE.in.sh ...\n" + cp "$SOLR_INSTALL_DIR/bin/solr.in.sh" "/etc/default/$SOLR_SERVICE.in.sh" + echo "SOLR_PID_DIR=\"$SOLR_VAR_DIR\" +SOLR_HOME=\"$SOLR_VAR_DIR/data\" +LOG4J_PROPS=\"$SOLR_VAR_DIR/log4j.properties\" +SOLR_LOGS_DIR=\"$SOLR_VAR_DIR/logs\" +SOLR_PORT=\"$SOLR_PORT\" +" >> "/etc/default/$SOLR_SERVICE.in.sh" +fi +chown root: "/etc/default/$SOLR_SERVICE.in.sh" +chmod 0644 "/etc/default/$SOLR_SERVICE.in.sh" + +# install data directories and files +mkdir -p "$SOLR_VAR_DIR/data" +mkdir -p "$SOLR_VAR_DIR/logs" +if [ -f "$SOLR_VAR_DIR/data/solr.xml" ]; then + echo -e "\n$SOLR_VAR_DIR/data/solr.xml already exists. Skipping install ...\n" +else + cp "$SOLR_INSTALL_DIR/server/solr/solr.xml" "$SOLR_VAR_DIR/data/solr.xml" +fi +if [ -f "$SOLR_VAR_DIR/log4j.properties" ]; then + echo -e "\n$SOLR_VAR_DIR/log4j.properties already exists. Skipping install ...\n" +else + cp "$SOLR_INSTALL_DIR/server/resources/log4j.properties" "$SOLR_VAR_DIR/log4j.properties" + sed_expr="s#solr.log=.*#solr.log=\${solr.solr.home}/../logs#" + sed -i -e "$sed_expr" "$SOLR_VAR_DIR/log4j.properties" +fi +chown -R "$SOLR_USER:" "$SOLR_VAR_DIR" +find "$SOLR_VAR_DIR" -type d -print0 | xargs -0 chmod 0750 +find "$SOLR_VAR_DIR" -type f -print0 | xargs -0 chmod 0640 + +# configure autostart of service +if [[ "$distro" == "RedHat" || "$distro" == "SUSE" ]]; then + chkconfig "$SOLR_SERVICE" on +else + update-rc.d "$SOLR_SERVICE" defaults +fi + +# start service +service "$SOLR_SERVICE" start sleep 5 -service $SOLR_SERVICE status +service "$SOLR_SERVICE" status echo "Service $SOLR_SERVICE installed." diff --git a/solr/bin/solr b/solr/bin/solr index 9a8da9ad492..a398d24b412 100755 --- a/solr/bin/solr +++ b/solr/bin/solr @@ -90,7 +90,7 @@ if [ -z "$SOLR_INCLUDE" ]; then "$HOME/.solr.in.sh" \ /usr/share/solr/solr.in.sh \ /usr/local/share/solr/solr.in.sh \ - /var/solr/solr.in.sh \ + /etc/default/solr.in.sh \ /opt/solr/solr.in.sh; do if [ -r "$include" ]; then . "$include"