zkServer-initialize.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #! /usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # https://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing,
  14. # software distributed under the License is distributed on an
  15. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. # KIND, either express or implied. See the License for the
  17. # specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. #
  21. # If this scripted is run out of /usr/bin or some other system bin directory
  22. # it should be linked to and not copied. Things like java jar files are found
  23. # relative to the canonical path of this script.
  24. #
  25. # use POSIX interface, symlink is followed automatically
  26. ZOOBIN="${BASH_SOURCE-$0}"
  27. ZOOBIN="$(dirname "$ZOOBIN")"
  28. ZOOBINDIR="$(cd "$ZOOBIN" && pwd)"
  29. if [[ -e "$ZOOBIN/../libexec/zkEnv.sh" ]]; then
  30. # shellcheck source=bin/zkEnv.sh
  31. . "$ZOOBINDIR"/../libexec/zkEnv.sh
  32. else
  33. # shellcheck source=bin/zkEnv.sh
  34. . "$ZOOBINDIR"/zkEnv.sh
  35. fi
  36. usage() {
  37. # the configfile will be properly formatted as long as the
  38. # configfile path is less then 40 chars, otw the line will look a
  39. # bit weird, but otherwise it's fine
  40. printf "usage: $0 <parameters>
  41. Optional parameters:
  42. -h Display this message
  43. --help Display this message
  44. --configfile=%-40s ZooKeeper config file
  45. --myid=# Set the myid to be used, if any (1-255)
  46. --force Force creation of the data/txnlog dirs
  47. " "$ZOOCFG"
  48. exit 1
  49. }
  50. initialize() {
  51. if [[ ! -e $ZOOCFG ]]; then
  52. echo "Unable to find config file at $ZOOCFG"
  53. exit 1
  54. fi
  55. ZOO_DATADIR="$(grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')"
  56. ZOO_DATALOGDIR="$(grep "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')"
  57. if [[ -z $ZOO_DATADIR ]]; then
  58. echo "Unable to determine dataDir from $ZOOCFG"
  59. exit 1
  60. fi
  61. if [[ $FORCE -eq 1 ]]; then
  62. echo "Force enabled, data/txnlog directories will be re-initialized"
  63. else
  64. # we create if version-2 exists (ie real data), not the
  65. # parent. See comments in following section for more insight
  66. if [[ -d "$ZOO_DATADIR/version-2" ]]; then
  67. echo "ZooKeeper data directory already exists at $ZOO_DATADIR (or use --force to force re-initialization)"
  68. exit 1
  69. fi
  70. if [[ -n $ZOO_DATALOGDIR ]] && [[ -d "$ZOO_DATALOGDIR/version-2" ]]; then
  71. echo "ZooKeeper txnlog directory already exists at $ZOO_DATALOGDIR (or use --force to force re-initialization)"
  72. exit 1
  73. fi
  74. fi
  75. # remove the child files that we're (not) interested in, not the
  76. # parent. this allows for parent to be installed separately, and
  77. # permissions to be set based on overarching requirements. by
  78. # default we'll use the permissions of the user running this
  79. # script for the files contained by the parent. note also by using
  80. # -p the parent(s) will be created if it doesn't already exist
  81. rm -rf "$ZOO_DATADIR/myid" 2>/dev/null >/dev/null
  82. rm -rf "$ZOO_DATADIR/version-2" 2>/dev/null >/dev/null
  83. mkdir -p "$ZOO_DATADIR/version-2"
  84. if [[ -n $ZOO_DATALOGDIR ]]; then
  85. rm -rf "$ZOO_DATALOGDIR/myid" 2>/dev/null >/dev/null
  86. rm -rf "$ZOO_DATALOGDIR/version-2" 2>/dev/null >/dev/null
  87. mkdir -p "$ZOO_DATALOGDIR/version-2"
  88. fi
  89. if [[ -n $MYID ]]; then
  90. echo "Using myid of $MYID"
  91. echo "$MYID" >"$ZOO_DATADIR/myid"
  92. else
  93. echo "No myid provided, be sure to specify it in $ZOO_DATADIR/myid if using non-standalone"
  94. fi
  95. touch "$ZOO_DATADIR/initialize"
  96. }
  97. while [[ -n $1 ]]; do
  98. case "$1" in
  99. --configfile)
  100. ZOOCFG=$2
  101. shift 2
  102. ;;
  103. --configfile=?*)
  104. ZOOCFG=${1#*=}
  105. shift 1
  106. ;;
  107. --myid)
  108. MYID=$2
  109. shift 2
  110. ;;
  111. --myid=?*)
  112. MYID=${1#*=}
  113. shift 1
  114. ;;
  115. --force)
  116. FORCE=1
  117. shift 1
  118. ;;
  119. -h)
  120. usage
  121. ;;
  122. --help)
  123. usage
  124. ;;
  125. *)
  126. echo "Unknown option: $1"
  127. usage
  128. ;;
  129. esac
  130. done
  131. initialize