install_solr_service.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. if [[ $EUID -ne 0 ]]; then
  17. echo -e "\nERROR: This script must be run as root\n" 1>&2
  18. exit 1
  19. fi
  20. print_usage() {
  21. ERROR_MSG="$1"
  22. if [ "$ERROR_MSG" != "" ]; then
  23. echo -e "\nERROR: $ERROR_MSG\n" 1>&2
  24. fi
  25. echo ""
  26. echo "Usage: install_solr_service.sh <path_to_solr_distribution_archive> [OPTIONS]"
  27. echo ""
  28. echo " The first argument to the script must be a path to a Solr distribution archive, such as solr-5.0.0.tgz"
  29. echo " (only .tgz or .zip are supported formats for the archive)"
  30. echo ""
  31. echo " Supported OPTIONS include:"
  32. echo ""
  33. echo " -d Directory for live / writable Solr files, such as logs, pid files, and index data; defaults to /var/solr"
  34. echo ""
  35. echo " -i Directory to extract the Solr installation archive; defaults to /opt/"
  36. echo " The specified path must exist prior to using this script."
  37. echo ""
  38. echo " -p Port Solr should bind to; default is 8983"
  39. echo ""
  40. echo " -s Service name; defaults to solr"
  41. echo ""
  42. echo " -u User to own the Solr files and run the Solr process as; defaults to solr"
  43. echo " This script will create the specified user account if it does not exist."
  44. echo ""
  45. echo " -f Upgrade Solr. Overwrite symlink and init script of previous installation."
  46. echo ""
  47. echo " -n Do not start Solr service after install, and do not abort on missing Java"
  48. echo ""
  49. echo " NOTE: Must be run as the root user"
  50. echo ""
  51. } # end print_usage
  52. print_error() {
  53. echo $1
  54. exit 1
  55. }
  56. # Locate *NIX distribution by looking for match from various detection strategies
  57. # We start with /etc/os-release, as this will also work for Docker containers
  58. for command in "grep -E \"^NAME=\" /etc/os-release" \
  59. "lsb_release -i" \
  60. "cat /proc/version" \
  61. "uname -a" ; do
  62. distro_string=$(eval $command 2>/dev/null)
  63. unset distro
  64. if [[ ${distro_string,,} == *"debian"* ]]; then
  65. distro=Debian
  66. elif [[ ${distro_string,,} == *"red hat"* ]]; then
  67. distro=RedHat
  68. elif [[ ${distro_string,,} == *"centos"* ]]; then
  69. distro=CentOS
  70. elif [[ ${distro_string,,} == *"ubuntu"* ]]; then
  71. distro=Ubuntu
  72. elif [[ ${distro_string,,} == *"suse"* ]]; then
  73. distro=SUSE
  74. elif [[ ${distro_string,,} == *"darwin"* ]]; then
  75. echo "Sorry, this script does not support macOS. You'll need to setup Solr as a service manually using the documentation provided in the Solr Reference Guide."
  76. echo "You could also try installing via Homebrew (http://brew.sh/), e.g. brew install solr"
  77. exit 1
  78. fi
  79. if [[ $distro ]] ; then break ; fi
  80. done
  81. if [[ ! $distro ]] ; then
  82. echo -e "\nERROR: Unable to auto-detect your *NIX distribution!\nYou'll need to setup Solr as a service manually using the documentation provided in the Solr Reference Guide.\n" 1>&2
  83. exit 1
  84. fi
  85. if [ -z "$1" ]; then
  86. print_usage "Must specify the path to the Solr installation archive, such as solr-5.0.0.tgz"
  87. exit 1
  88. fi
  89. SOLR_ARCHIVE=$1
  90. if [ ! -f "$SOLR_ARCHIVE" ]; then
  91. print_usage "Specified Solr installation archive $SOLR_ARCHIVE not found!"
  92. exit 1
  93. fi
  94. # strip off path info
  95. SOLR_INSTALL_FILE=${SOLR_ARCHIVE##*/}
  96. is_tar=true
  97. if [ ${SOLR_INSTALL_FILE: -4} == ".tgz" ]; then
  98. SOLR_DIR=${SOLR_INSTALL_FILE%.tgz}
  99. elif [ ${SOLR_INSTALL_FILE: -4} == ".zip" ]; then
  100. SOLR_DIR=${SOLR_INSTALL_FILE%.zip}
  101. is_tar=false
  102. else
  103. print_usage "Solr installation archive $SOLR_ARCHIVE is invalid, expected a .tgz or .zip file!"
  104. exit 1
  105. fi
  106. SOLR_START=true
  107. if [ $# -gt 1 ]; then
  108. shift
  109. while true; do
  110. case $1 in
  111. -i)
  112. if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
  113. print_usage "Directory path is required when using the $1 option!"
  114. exit 1
  115. fi
  116. SOLR_EXTRACT_DIR=$2
  117. shift 2
  118. ;;
  119. -d)
  120. if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
  121. print_usage "Directory path is required when using the $1 option!"
  122. exit 1
  123. fi
  124. SOLR_VAR_DIR="$2"
  125. shift 2
  126. ;;
  127. -u)
  128. if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
  129. print_usage "Username is required when using the $1 option!"
  130. exit 1
  131. fi
  132. SOLR_USER="$2"
  133. shift 2
  134. ;;
  135. -s)
  136. if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
  137. print_usage "Service name is required when using the $1 option!"
  138. exit 1
  139. fi
  140. SOLR_SERVICE="$2"
  141. shift 2
  142. ;;
  143. -p)
  144. if [[ -z "$2" || "${2:0:1}" == "-" ]]; then
  145. print_usage "Port is required when using the $1 option!"
  146. exit 1
  147. fi
  148. SOLR_PORT="$2"
  149. shift 2
  150. ;;
  151. -f)
  152. SOLR_UPGRADE="YES"
  153. shift 1
  154. ;;
  155. -n)
  156. SOLR_START=false
  157. shift 1
  158. ;;
  159. -help|-usage)
  160. print_usage ""
  161. exit 0
  162. ;;
  163. --)
  164. shift
  165. break
  166. ;;
  167. *)
  168. if [ "$1" != "" ]; then
  169. print_usage "Unrecognized or misplaced argument: $1!"
  170. exit 1
  171. else
  172. break # out-of-args, stop looping
  173. fi
  174. ;;
  175. esac
  176. done
  177. fi
  178. # Test for availability of needed tools
  179. if [[ $is_tar ]] ; then
  180. tar --version &>/dev/null || print_error "Script requires the 'tar' command"
  181. else
  182. unzip -hh &>/dev/null || print_error "Script requires the 'unzip' command"
  183. fi
  184. if [[ $SOLR_START == "true" ]] ; then
  185. service --version &>/dev/null || service --help &>/dev/null || print_error "Script requires the 'service' command"
  186. java -version &>/dev/null || print_error "Solr requires java, please install or set JAVA_HOME properly"
  187. fi
  188. lsof -h &>/dev/null || echo "We recommend installing the 'lsof' command for more stable start/stop of Solr"
  189. if [ -z "$SOLR_EXTRACT_DIR" ]; then
  190. SOLR_EXTRACT_DIR=/opt
  191. fi
  192. if [ ! -d "$SOLR_EXTRACT_DIR" ]; then
  193. print_usage "Installation directory $SOLR_EXTRACT_DIR not found! Please create it before running this script."
  194. exit 1
  195. fi
  196. if [ -z "$SOLR_SERVICE" ]; then
  197. SOLR_SERVICE=solr
  198. fi
  199. if [ -z "$SOLR_VAR_DIR" ]; then
  200. SOLR_VAR_DIR="/var/$SOLR_SERVICE"
  201. fi
  202. if [ -z "$SOLR_USER" ]; then
  203. SOLR_USER=solr
  204. fi
  205. if [ -z "$SOLR_PORT" ]; then
  206. SOLR_PORT=8983
  207. fi
  208. if [ -z "$SOLR_UPGRADE" ]; then
  209. SOLR_UPGRADE=NO
  210. fi
  211. if [ ! "$SOLR_UPGRADE" = "YES" ]; then
  212. if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then
  213. 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."
  214. exit 1
  215. fi
  216. if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then
  217. print_usage "$SOLR_EXTRACT_DIR/$SOLR_SERVICE already exists! Please move this directory / link or choose a different service name using the -s option."
  218. exit 1
  219. fi
  220. fi
  221. # stop running instance
  222. if [ -f "/etc/init.d/$SOLR_SERVICE" ]; then
  223. echo -e "\nStopping Solr instance if exists ...\n"
  224. service "$SOLR_SERVICE" stop
  225. fi
  226. # create user if not exists
  227. solr_uid="`id -u "$SOLR_USER"`"
  228. if [ $? -ne 0 ]; then
  229. echo "Creating new user: $SOLR_USER"
  230. if [ "$distro" == "RedHat" ] || [ "$distro" == "CentOS" ] ; then
  231. adduser --system -U -m --home-dir "$SOLR_VAR_DIR" "$SOLR_USER"
  232. elif [ "$distro" == "SUSE" ]; then
  233. useradd --system -U -m --home-dir "$SOLR_VAR_DIR" "$SOLR_USER"
  234. else
  235. adduser --system --shell /bin/bash --group --disabled-password --home "$SOLR_VAR_DIR" "$SOLR_USER"
  236. fi
  237. fi
  238. # extract
  239. SOLR_INSTALL_DIR="$SOLR_EXTRACT_DIR/$SOLR_DIR"
  240. if [ ! -d "$SOLR_INSTALL_DIR" ]; then
  241. echo -e "\nExtracting $SOLR_ARCHIVE to $SOLR_EXTRACT_DIR\n"
  242. if $is_tar ; then
  243. tar zxf "$SOLR_ARCHIVE" -C "$SOLR_EXTRACT_DIR"
  244. else
  245. unzip -q "$SOLR_ARCHIVE" -d "$SOLR_EXTRACT_DIR"
  246. fi
  247. if [ ! -d "$SOLR_INSTALL_DIR" ]; then
  248. echo -e "\nERROR: Expected directory $SOLR_INSTALL_DIR not found after extracting $SOLR_ARCHIVE ... script fails.\n" 1>&2
  249. exit 1
  250. fi
  251. chown -R root: "$SOLR_INSTALL_DIR"
  252. find "$SOLR_INSTALL_DIR" -type d -print0 | xargs -0 chmod 0755
  253. find "$SOLR_INSTALL_DIR" -type f -print0 | xargs -0 chmod 0644
  254. chmod -R 0755 "$SOLR_INSTALL_DIR/bin"
  255. else
  256. echo -e "\nWARNING: $SOLR_INSTALL_DIR already exists! Skipping extract ...\n"
  257. fi
  258. # create a symlink for easier scripting
  259. if [ -h "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then
  260. echo -e "\nRemoving old symlink $SOLR_EXTRACT_DIR/$SOLR_SERVICE ...\n"
  261. rm "$SOLR_EXTRACT_DIR/$SOLR_SERVICE"
  262. fi
  263. if [ -e "$SOLR_EXTRACT_DIR/$SOLR_SERVICE" ]; then
  264. echo -e "\nWARNING: $SOLR_EXTRACT_DIR/$SOLR_SERVICE is not symlink! Skipping symlink update ...\n"
  265. else
  266. echo -e "\nInstalling symlink $SOLR_EXTRACT_DIR/$SOLR_SERVICE -> $SOLR_INSTALL_DIR ...\n"
  267. ln -s "$SOLR_INSTALL_DIR" "$SOLR_EXTRACT_DIR/$SOLR_SERVICE"
  268. fi
  269. # install init.d script
  270. echo -e "\nInstalling /etc/init.d/$SOLR_SERVICE script ...\n"
  271. cp "$SOLR_INSTALL_DIR/bin/init.d/solr" "/etc/init.d/$SOLR_SERVICE"
  272. chmod 0744 "/etc/init.d/$SOLR_SERVICE"
  273. chown root: "/etc/init.d/$SOLR_SERVICE"
  274. # do some basic variable substitution on the init.d script
  275. sed_expr1="s#SOLR_INSTALL_DIR=.*#SOLR_INSTALL_DIR=\"$SOLR_EXTRACT_DIR/$SOLR_SERVICE\"#"
  276. sed_expr2="s#SOLR_ENV=.*#SOLR_ENV=\"/etc/default/$SOLR_SERVICE.in.sh\"#"
  277. sed_expr3="s#RUNAS=.*#RUNAS=\"$SOLR_USER\"#"
  278. sed_expr4="s#Provides:.*#Provides: $SOLR_SERVICE#"
  279. sed -i -e "$sed_expr1" -e "$sed_expr2" -e "$sed_expr3" -e "$sed_expr4" "/etc/init.d/$SOLR_SERVICE"
  280. # install/move configuration
  281. if [ ! -d /etc/default ]; then
  282. mkdir /etc/default
  283. chown root: /etc/default
  284. chmod 0755 /etc/default
  285. fi
  286. if [ -f "$SOLR_VAR_DIR/solr.in.sh" ]; then
  287. echo -e "\nMoving existing $SOLR_VAR_DIR/solr.in.sh to /etc/default/$SOLR_SERVICE.in.sh ...\n"
  288. mv "$SOLR_VAR_DIR/solr.in.sh" "/etc/default/$SOLR_SERVICE.in.sh"
  289. elif [ -f "/etc/default/$SOLR_SERVICE.in.sh" ]; then
  290. echo -e "\n/etc/default/$SOLR_SERVICE.in.sh already exist. Skipping install ...\n"
  291. else
  292. echo -e "\nInstalling /etc/default/$SOLR_SERVICE.in.sh ...\n"
  293. cp "$SOLR_INSTALL_DIR/bin/solr.in.sh" "/etc/default/$SOLR_SERVICE.in.sh"
  294. mv "$SOLR_INSTALL_DIR/bin/solr.in.sh" "$SOLR_INSTALL_DIR/bin/solr.in.sh.orig"
  295. mv "$SOLR_INSTALL_DIR/bin/solr.in.cmd" "$SOLR_INSTALL_DIR/bin/solr.in.cmd.orig"
  296. echo "SOLR_PID_DIR=\"$SOLR_VAR_DIR\"
  297. SOLR_HOME=\"$SOLR_VAR_DIR/data\"
  298. LOG4J_PROPS=\"$SOLR_VAR_DIR/log4j2.xml\"
  299. SOLR_LOGS_DIR=\"$SOLR_VAR_DIR/logs\"
  300. SOLR_PORT=\"$SOLR_PORT\"
  301. " >> "/etc/default/$SOLR_SERVICE.in.sh"
  302. fi
  303. chown root:${SOLR_USER} "/etc/default/$SOLR_SERVICE.in.sh"
  304. chmod 0640 "/etc/default/$SOLR_SERVICE.in.sh"
  305. # install data directories and files
  306. mkdir -p "$SOLR_VAR_DIR/data"
  307. mkdir -p "$SOLR_VAR_DIR/logs"
  308. if [ -f "$SOLR_VAR_DIR/data/solr.xml" ]; then
  309. echo -e "\n$SOLR_VAR_DIR/data/solr.xml already exists. Skipping install ...\n"
  310. else
  311. cp "$SOLR_INSTALL_DIR/server/solr/"{solr.xml,zoo.cfg} "$SOLR_VAR_DIR/data/"
  312. fi
  313. if [ -f "$SOLR_VAR_DIR/log4j2.xml" ]; then
  314. echo -e "\n$SOLR_VAR_DIR/log4j2.xml already exists. Skipping install ...\n"
  315. else
  316. cp "$SOLR_INSTALL_DIR/server/resources/log4j2.xml" "$SOLR_VAR_DIR/log4j2.xml"
  317. fi
  318. chown -R "$SOLR_USER:" "$SOLR_VAR_DIR"
  319. find "$SOLR_VAR_DIR" -type d -print0 | xargs -0 chmod 0750
  320. find "$SOLR_VAR_DIR" -type f -print0 | xargs -0 chmod 0640
  321. # configure autostart of service
  322. if [[ "$distro" == "RedHat" || "$distro" == "CentOS" || "$distro" == "SUSE" ]]; then
  323. chkconfig "$SOLR_SERVICE" on
  324. else
  325. update-rc.d "$SOLR_SERVICE" defaults
  326. fi
  327. echo "Service $SOLR_SERVICE installed."
  328. echo "Customize Solr startup configuration in /etc/default/$SOLR_SERVICE.in.sh"
  329. # start service
  330. if [[ $SOLR_START == "true" ]] ; then
  331. service "$SOLR_SERVICE" start
  332. sleep 5
  333. service "$SOLR_SERVICE" status
  334. else
  335. echo "Not starting Solr service (option -n given). Start manually with 'service $SOLR_SERVICE start'"
  336. fi