update-zookeeper-env.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. # This script configures zookeeper-env.sh and zoo.cfg.
  17. usage() {
  18. echo "
  19. usage: $0 <parameters>
  20. Required parameters:
  21. --prefix=PREFIX path to install into
  22. Optional parameters:
  23. --arch=i386 OS Architecture
  24. --conf-dir=/etc/zookeeper Configuration directory
  25. --log-dir=/var/log/zookeeper Log directory
  26. --pid-dir=/var/run PID file location
  27. "
  28. exit 1
  29. }
  30. template_generator() {
  31. REGEX='(\$\{[a-zA-Z_][a-zA-Z_0-9]*\})'
  32. cat $1 |
  33. while read line ; do
  34. while [[ "$line" =~ $REGEX ]] ; do
  35. LHS=${BASH_REMATCH[1]}
  36. RHS="$(eval echo "\"$LHS\"")"
  37. line=${line//$LHS/$RHS}
  38. done
  39. echo $line >> $2
  40. done
  41. }
  42. OPTS=$(getopt \
  43. -n $0 \
  44. -o '' \
  45. -l 'arch:' \
  46. -l 'prefix:' \
  47. -l 'conf-dir:' \
  48. -l 'log-dir:' \
  49. -l 'pid-dir:' \
  50. -l 'var-dir:' \
  51. -l 'uninstall' \
  52. -- "$@")
  53. if [ $? != 0 ] ; then
  54. usage
  55. fi
  56. eval set -- "${OPTS}"
  57. while true ; do
  58. case "$1" in
  59. --arch)
  60. ARCH=$2 ; shift 2
  61. ;;
  62. --prefix)
  63. PREFIX=$2 ; shift 2
  64. ;;
  65. --log-dir)
  66. LOG_DIR=$2 ; shift 2
  67. ;;
  68. --lib-dir)
  69. LIB_DIR=$2 ; shift 2
  70. ;;
  71. --conf-dir)
  72. CONF_DIR=$2 ; shift 2
  73. ;;
  74. --pid-dir)
  75. PID_DIR=$2 ; shift 2
  76. ;;
  77. --uninstall)
  78. UNINSTALL=1; shift
  79. ;;
  80. --var-dir)
  81. VAR_DIR=$2 ; shift 2
  82. ;;
  83. --)
  84. shift ; break
  85. ;;
  86. *)
  87. echo "Unknown option: $1"
  88. usage
  89. exit 1
  90. ;;
  91. esac
  92. done
  93. for var in PREFIX; do
  94. if [ -z "$(eval "echo \$$var")" ]; then
  95. echo Missing param: $var
  96. usage
  97. fi
  98. done
  99. ARCH=${ARCH:-i386}
  100. CONF_DIR=${CONF_DIR:-$PREFIX/etc/zookeeper}
  101. LIB_DIR=${LIB_DIR:-$PREFIX/lib}
  102. LOG_DIR=${LOG_DIR:-$PREFIX/var/log}
  103. PID_DIR=${PID_DIR:-$PREFIX/var/run}
  104. VAR_DIR=${VAR_DIR:-$PREFIX/var/lib}
  105. UNINSTALL=${UNINSTALL:-0}
  106. if [ "${ARCH}" != "i386" ]; then
  107. LIB_DIR=${LIB_DIR}64
  108. fi
  109. if [ "${UNINSTALL}" -eq "1" ]; then
  110. # Remove symlinks
  111. if [ -e ${PREFIX}/etc/zookeeper ]; then
  112. rm -f ${PREFIX}/etc/zookeeper
  113. fi
  114. else
  115. # Create symlinks
  116. if [ ${CONF_DIR} != ${PREFIX}/etc/zookeeper ]; then
  117. mkdir -p ${PREFIX}/etc
  118. ln -sf ${CONF_DIR} ${PREFIX}/etc/zookeeper
  119. fi
  120. mkdir -p ${LOG_DIR}
  121. chown zookeeper:hadoop ${LOG_DIR}
  122. chmod 755 ${LOG_DIR}
  123. if [ ! -d ${PID_DIR} ]; then
  124. mkdir -p ${PID_DIR}
  125. chown zookeeper:hadoop ${PID_DIR}
  126. chmod 755 ${PID_DIR}
  127. fi
  128. if [ ! -d ${VAR_DIR} ]; then
  129. mkdir -p ${VAR_DIR}/data
  130. chown -R zookeeper:hadoop ${VAR_DIR}
  131. chmod -R 755 ${VAR_DIR}
  132. fi
  133. TFILE="/tmp/$(basename $0).$$.tmp"
  134. if [ -z "${JAVA_HOME}" ]; then
  135. if [ -e /etc/debian_version ]; then
  136. JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
  137. else
  138. JAVA_HOME=/usr/java/default
  139. fi
  140. fi
  141. template_generator ${PREFIX}/share/zookeeper/templates/conf/zookeeper-env.sh $TFILE
  142. cp ${TFILE} ${CONF_DIR}/zookeeper-env.sh
  143. rm -f ${TFILE}
  144. template_generator ${PREFIX}/share/zookeeper/templates/conf/zoo.cfg $TFILE
  145. cp ${TFILE} ${CONF_DIR}/zoo.cfg
  146. rm -f ${TFILE}
  147. fi