2006-01-26 00:37:29 -05:00
#!/bin/bash
#
2006-11-08 13:25:52 -05:00
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
2006-03-21 15:25:41 -05:00
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2006-01-26 00:37:29 -05:00
#
# Shell script to install a snapshot into place as the Lucene collection
2006-03-02 23:33:24 -05:00
# for a Solr server
2006-01-26 00:37:29 -05:00
2006-03-21 15:25:41 -05:00
orig_dir=$(pwd)
cd ${0%/*}/..
solr_root=$(pwd)
cd ${orig_dir}
2006-01-26 00:37:29 -05:00
2006-07-06 15:14:40 -04:00
unset master_host master_status_dir data_dir user verbose debug
2006-03-21 15:25:41 -05:00
. ${solr_root}/bin/scripts-util
2006-01-26 00:37:29 -05:00
# set up variables
prog=${0##*/}
2006-03-21 15:25:41 -05:00
log=${solr_root}/logs/${prog}.log
2006-01-26 00:37:29 -05:00
2009-06-02 11:41:21 -04:00
LOCKDIR="${solr_root}/logs/snapinstaller-lock"
PIDFILE="${LOCKDIR}/PID"
2006-01-26 00:37:29 -05:00
# define usage string
USAGE="\
2006-03-21 15:25:41 -05:00
usage: $prog [-M master] [-S sdir] [-d dir] [-u username] [-v]
-M master specify hostname of master server from where to pull index
snapshot
-S specify directory holding snapshot status on master server
-d specify directory holding index data on local machine
-u specify user to sudo to before running script
2006-01-26 00:37:29 -05:00
-v increase verbosity
2006-07-06 15:14:40 -04:00
-V output debugging info
2006-01-26 00:37:29 -05:00
"
# parse args
2006-07-06 15:14:40 -04:00
while getopts M:S:d:u:vV OPTION
2006-01-26 00:37:29 -05:00
do
case $OPTION in
2006-03-21 15:25:41 -05:00
M)
master_host="$OPTARG"
2006-01-26 00:37:29 -05:00
;;
2006-03-21 15:25:41 -05:00
S)
master_status_dir="$OPTARG"
;;
d)
data_dir="$OPTARG"
;;
u)
user="$OPTARG"
2006-01-26 00:37:29 -05:00
;;
v)
verbose="v"
;;
2006-07-06 15:14:40 -04:00
V)
debug="V"
;;
2006-01-26 00:37:29 -05:00
*)
echo "$USAGE"
exit 1
esac
done
2006-07-06 15:14:40 -04:00
[[ -n $debug ]] && set -x
2006-03-21 15:25:41 -05:00
if [[ -z ${master_host} ]]
2006-01-26 00:37:29 -05:00
then
echo "name of master server missing in $confFile or command line."
echo "$USAGE"
exit 1
fi
2006-03-21 15:25:41 -05:00
if [[ -z ${master_status_dir} ]]
2006-01-26 00:37:29 -05:00
then
2006-03-21 15:25:41 -05:00
echo "directory holding snapshot status on master server missing in $confFile or command line."
2006-01-26 00:37:29 -05:00
echo "$USAGE"
exit 1
fi
2006-03-21 15:25:41 -05:00
fixUser "$@"
2006-03-24 09:59:43 -05:00
# use default value for data_dir if not specified
2006-03-21 15:25:41 -05:00
# relative path starts at ${solr_root}
if [[ -z ${data_dir} ]]
then
data_dir=${solr_root}/data
elif [[ "`echo ${data_dir}|cut -c1`" != "/" ]]
then
data_dir=${solr_root}/${data_dir}
fi
# assume relative path to start at ${solr_root}
if [[ "`echo ${master_status_dir}|cut -c1`" != "/" ]]
then
master_status_dir=${solr_root}/${master_status_dir}
fi
2006-01-26 00:37:29 -05:00
2007-07-17 08:31:03 -04:00
setStartTime
2006-01-26 00:37:29 -05:00
2009-06-02 11:41:21 -04:00
if test -r $PIDFILE
then
OTHERPID="$(cat "${PIDFILE}")"
if ! kill -0 $OTHERPID &>/dev/null; then
logMessage removing stale lock ${OTHERPID}
rm -rf "${LOCKDIR}"
else
logExit "lock failed, PID ${OTHERPID} is active" 1
fi
fi
mkdir "${LOCKDIR}" &>/dev/null
echo "$$" >"${PIDFILE}"
2006-03-21 15:25:41 -05:00
logMessage started by $oldwhoami
logMessage command: $0 $@
2006-01-26 00:37:29 -05:00
# get directory name of latest snapshot
2009-03-04 10:25:27 -05:00
name=`perl -e 'chdir q|'${data_dir}'|; print ((sort grep {/^snapshot[.][1-9][0-9]{13}$/} <*>)[-1])'`
2006-01-26 00:37:29 -05:00
# clean up after INT/TERM
2006-03-21 15:25:41 -05:00
trap 'echo "caught INT/TERM, exiting now but partial installation may have already occured";/bin/rm -rf ${data_dir"/index.tmp$$;logExit aborted 13' INT TERM
2006-01-26 00:37:29 -05:00
# is there a snapshot
if [[ "${name}" == "" ]]
then
logMessage no shapshot available
logExit ended 0
fi
2007-04-16 22:25:28 -04:00
name=${data_dir}/${name}
2006-01-26 00:37:29 -05:00
# has snapshot already been installed
2006-03-21 15:25:41 -05:00
if [[ ${name} == `cat ${solr_root}/logs/snapshot.current 2>/dev/null` ]]
2006-01-26 00:37:29 -05:00
then
logMessage latest snapshot ${name} already installed
logExit ended 0
fi
# make sure master has directory for hold slaves stats/state
if
2006-03-21 15:25:41 -05:00
! ssh -o StrictHostKeyChecking=no ${master_host} mkdir -p ${master_status_dir}
2006-01-26 00:37:29 -05:00
then
2006-03-21 15:25:41 -05:00
logMessage failed to ssh to master ${master_host}, snapshot status not updated on master
2006-01-26 00:37:29 -05:00
fi
# install using hard links into temporary directory
# remove original index and then atomically copy new one into place
logMessage installing snapshot ${name}
2008-08-08 11:23:48 -04:00
if [[ "${OS}" == "SunOS" || "${OS}" == "Darwin" || "${OS}" == "FreeBSD" ]]
2007-07-12 08:35:01 -04:00
then
orig_dir=$(pwd)
mkdir ${data_dir}/index.tmp$$ && \
cd ${name} && \
find . -print|cpio -pdlmu ${data_dir}/index.tmp$$ 1>/dev/null 2>&1 && \
/bin/rm -rf ${data_dir}/index && \
mv -f ${data_dir}/index.tmp$$ ${data_dir}/index
cd ${orig_dir}
else
cp -lr ${name}/ ${data_dir}/index.tmp$$ && \
/bin/rm -rf ${data_dir}/index && \
mv -f ${data_dir}/index.tmp$$ ${data_dir}/index
fi
2006-01-26 00:37:29 -05:00
# update distribution stats
2006-03-21 15:25:41 -05:00
echo ${name} > ${solr_root}/logs/snapshot.current
2006-01-26 00:37:29 -05:00
# push stats/state to master
if
2006-03-21 15:25:41 -05:00
! scp -q -o StrictHostKeyChecking=no ${solr_root}/logs/snapshot.current ${master_host}:${master_status_dir}/snapshot.current.`uname -n`
2006-01-26 00:37:29 -05:00
then
2006-03-21 15:25:41 -05:00
logMessage failed to ssh to master ${master_host}, snapshot status not updated on master
2006-01-26 00:37:29 -05:00
fi
2006-03-02 23:33:24 -05:00
# notify Solr to open a new Searcher
logMessage notifing Solr to open a new Searcher
2006-03-21 15:25:41 -05:00
${solr_root}/bin/commit
2006-01-26 00:37:29 -05:00
if [[ $? != 0 ]]
then
2006-03-21 15:25:41 -05:00
logMessage failed to connect to Solr server
2006-03-02 23:33:24 -05:00
logMessage snapshot installed but Solr server has not open a new Searcher
2006-01-26 00:37:29 -05:00
logExit failed 1
fi
2009-06-02 11:41:21 -04:00
rm -rf "${LOCKDIR}"
2006-01-26 00:37:29 -05:00
logExit ended 0