zkServer-initialize.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 this scripted is run out of /usr/bin or some other system bin directory
  18. # it should be linked to and not copied. Things like java jar files are found
  19. # relative to the canonical path of this script.
  20. #
  21. # use POSIX interface, symlink is followed automatically
  22. ZOOBIN="${BASH_SOURCE-$0}"
  23. ZOOBIN=`dirname ${ZOOBIN}`
  24. ZOOBINDIR=`cd ${ZOOBIN}; pwd`
  25. if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then
  26. . "$ZOOBINDIR"/../libexec/zkEnv.sh
  27. else
  28. . "$ZOOBINDIR"/zkEnv.sh
  29. fi
  30. usage() {
  31. # the configfile will be properly formatted as long as the
  32. # configfile path is less then 40 chars, otw the line will look a
  33. # bit weird, but otherwise it's fine
  34. printf "usage: $0 <parameters>
  35. Optional parameters:
  36. -h Display this message
  37. --help Display this message
  38. --configfile=%-40s ZooKeeper config file
  39. --myid=# Set the myid to be used, if any (1-255)
  40. --force Force creation of the data/txnlog dirs
  41. " "$ZOOCFG"
  42. exit 1
  43. }
  44. OPTS=$(getopt \
  45. -n $0 \
  46. -o 'h' \
  47. -l 'help' \
  48. -l 'configfile:' \
  49. -l 'myid:' \
  50. -l 'force' \
  51. -- "$@")
  52. if [ $? != 0 ] ; then
  53. usage
  54. exit 1
  55. fi
  56. initialize() {
  57. if [ ! -e "$ZOOCFG" ]; then
  58. echo "Unable to find config file at $ZOOCFG"
  59. exit 1
  60. fi
  61. ZOO_DATADIR=$(grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')
  62. ZOO_DATALOGDIR=$(grep "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')
  63. if [ -z "$ZOO_DATADIR" ]; then
  64. echo "Unable to determine dataDir from $ZOOCFG"
  65. exit 1
  66. fi
  67. if [ $FORCE ]; then
  68. echo "Force enabled, data/txnlog directories will be re-initialized"
  69. else
  70. # we create if version-2 exists (ie real data), not the
  71. # parent. See comments in following section for more insight
  72. if [ -d "$ZOO_DATADIR/version-2" ]; then
  73. echo "ZooKeeper data directory already exists at $ZOO_DATADIR (or use --force to force re-initialization)"
  74. exit 1
  75. fi
  76. if [ -n "$ZOO_DATALOGDIR" ] && [ -d "$ZOO_DATALOGDIR/version-2" ]; then
  77. echo "ZooKeeper txnlog directory already exists at $ZOO_DATALOGDIR (or use --force to force re-initialization)"
  78. exit 1
  79. fi
  80. fi
  81. # remove the child files that we're (not) interested in, not the
  82. # parent. this allows for parent to be installed separately, and
  83. # permissions to be set based on overarching requirements. by
  84. # default we'll use the permissions of the user running this
  85. # script for the files contained by the parent. note also by using
  86. # -p the parent(s) will be created if it doesn't already exist
  87. rm -rf "$ZOO_DATADIR/myid" 2>/dev/null >/dev/null
  88. rm -rf "$ZOO_DATADIR/version-2" 2>/dev/null >/dev/null
  89. mkdir -p "$ZOO_DATADIR/version-2"
  90. if [ -n "$ZOO_DATALOGDIR" ]; then
  91. rm -rf "$ZOO_DATALOGDIR/myid" 2>/dev/null >/dev/null
  92. rm -rf "$ZOO_DATALOGDIR/version-2" 2>/dev/null >/dev/null
  93. mkdir -p "$ZOO_DATALOGDIR/version-2"
  94. fi
  95. if [ $MYID ]; then
  96. echo "Using myid of $MYID"
  97. echo $MYID > "$ZOO_DATADIR/myid"
  98. else
  99. echo "No myid provided, be sure to specify it in $ZOO_DATADIR/myid if using non-standalone"
  100. fi
  101. }
  102. eval set -- "${OPTS}"
  103. while true; do
  104. case "$1" in
  105. --configfile)
  106. ZOOCFG=$2; shift 2
  107. ;;
  108. --myid)
  109. MYID=$2; shift 2
  110. ;;
  111. --force)
  112. FORCE=1; shift 1
  113. ;;
  114. -h)
  115. usage
  116. ;;
  117. --help)
  118. usage
  119. ;;
  120. --)
  121. initialize
  122. break
  123. ;;
  124. *)
  125. echo "Unknown option: $1"
  126. usage
  127. exit 1
  128. ;;
  129. esac
  130. done