zkServer-initialize.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if [ $? != 0 ] ; then
  45. usage
  46. exit 1
  47. fi
  48. initialize() {
  49. if [ ! -e "$ZOOCFG" ]; then
  50. echo "Unable to find config file at $ZOOCFG"
  51. exit 1
  52. fi
  53. ZOO_DATADIR="$(grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')"
  54. ZOO_DATALOGDIR="$(grep "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')"
  55. if [ -z "$ZOO_DATADIR" ]; then
  56. echo "Unable to determine dataDir from $ZOOCFG"
  57. exit 1
  58. fi
  59. if [ $FORCE ]; then
  60. echo "Force enabled, data/txnlog directories will be re-initialized"
  61. else
  62. # we create if version-2 exists (ie real data), not the
  63. # parent. See comments in following section for more insight
  64. if [ -d "$ZOO_DATADIR/version-2" ]; then
  65. echo "ZooKeeper data directory already exists at $ZOO_DATADIR (or use --force to force re-initialization)"
  66. exit 1
  67. fi
  68. if [ -n "$ZOO_DATALOGDIR" ] && [ -d "$ZOO_DATALOGDIR/version-2" ]; then
  69. echo "ZooKeeper txnlog directory already exists at $ZOO_DATALOGDIR (or use --force to force re-initialization)"
  70. exit 1
  71. fi
  72. fi
  73. # remove the child files that we're (not) interested in, not the
  74. # parent. this allows for parent to be installed separately, and
  75. # permissions to be set based on overarching requirements. by
  76. # default we'll use the permissions of the user running this
  77. # script for the files contained by the parent. note also by using
  78. # -p the parent(s) will be created if it doesn't already exist
  79. rm -rf "$ZOO_DATADIR/myid" 2>/dev/null >/dev/null
  80. rm -rf "$ZOO_DATADIR/version-2" 2>/dev/null >/dev/null
  81. mkdir -p "$ZOO_DATADIR/version-2"
  82. if [ -n "$ZOO_DATALOGDIR" ]; then
  83. rm -rf "$ZOO_DATALOGDIR/myid" 2>/dev/null >/dev/null
  84. rm -rf "$ZOO_DATALOGDIR/version-2" 2>/dev/null >/dev/null
  85. mkdir -p "$ZOO_DATALOGDIR/version-2"
  86. fi
  87. if [ $MYID ]; then
  88. echo "Using myid of $MYID"
  89. echo $MYID > "$ZOO_DATADIR/myid"
  90. else
  91. echo "No myid provided, be sure to specify it in $ZOO_DATADIR/myid if using non-standalone"
  92. fi
  93. touch "$ZOO_DATADIR/initialize"
  94. }
  95. while [ ! -z "$1" ]; do
  96. case "$1" in
  97. --configfile)
  98. ZOOCFG=$2; shift 2
  99. ;;
  100. --configfile=?*)
  101. ZOOCFG=${1#*=}; shift 1
  102. ;;
  103. --myid)
  104. MYID=$2; shift 2
  105. ;;
  106. --myid=?*)
  107. MYID=${1#*=}; shift 1
  108. ;;
  109. --force)
  110. FORCE=1; shift 1
  111. ;;
  112. -h)
  113. usage
  114. ;;
  115. --help)
  116. usage
  117. ;;
  118. *)
  119. echo "Unknown option: $1"
  120. usage
  121. exit 1
  122. ;;
  123. esac
  124. done
  125. initialize