etcd.sh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # A set of helpers for starting/running etcd for tests
  16. ETCD_VERSION=${ETCD_VERSION:-3.0.4}
  17. ETCD_HOST=${ETCD_HOST:-127.0.0.1}
  18. ETCD_PORT=${ETCD_PORT:-2379}
  19. kube::etcd::start() {
  20. which etcd >/dev/null || {
  21. kube::log::usage "etcd must be in your PATH"
  22. exit 1
  23. }
  24. if pgrep etcd >/dev/null 2>&1; then
  25. kube::log::usage "etcd appears to already be running on this machine (`pgrep -l etcd`) (or its a zombie and you need to kill its parent)."
  26. kube::log::usage "retry after you resolve this etcd error."
  27. exit 1
  28. fi
  29. version=$(etcd --version | head -n 1 | cut -d " " -f 3)
  30. if [[ "${version}" < "${ETCD_VERSION}" ]]; then
  31. kube::log::usage "etcd version ${ETCD_VERSION} or greater required."
  32. kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
  33. exit 1
  34. fi
  35. # Start etcd
  36. ETCD_DIR=${ETCD_DIR:-$(mktemp -d 2>/dev/null || mktemp -d -t test-etcd.XXXXXX)}
  37. if [[ -d "${ARTIFACTS_DIR:-}" ]]; then
  38. ETCD_LOGFILE="${ARTIFACTS_DIR}/etcd.$(uname -n).$(id -un).log.DEBUG.$(date +%Y%m%d-%H%M%S).$$"
  39. else
  40. ETCD_LOGFILE=/dev/null
  41. fi
  42. kube::log::info "etcd --advertise-client-urls http://${ETCD_HOST}:${ETCD_PORT} --data-dir ${ETCD_DIR} --listen-client-urls http://${ETCD_HOST}:${ETCD_PORT} --debug > \"${ETCD_LOGFILE}\" 2>/dev/null"
  43. etcd --advertise-client-urls http://${ETCD_HOST}:${ETCD_PORT} --data-dir ${ETCD_DIR} --listen-client-urls http://${ETCD_HOST}:${ETCD_PORT} --debug 2> "${ETCD_LOGFILE}" >/dev/null &
  44. ETCD_PID=$!
  45. echo "Waiting for etcd to come up."
  46. kube::util::wait_for_url "http://${ETCD_HOST}:${ETCD_PORT}/v2/machines" "etcd: " 0.25 80
  47. curl -fs -X PUT "http://${ETCD_HOST}:${ETCD_PORT}/v2/keys/_test"
  48. }
  49. kube::etcd::stop() {
  50. kill "${ETCD_PID-}" >/dev/null 2>&1 || :
  51. wait "${ETCD_PID-}" >/dev/null 2>&1 || :
  52. }
  53. kube::etcd::clean_etcd_dir() {
  54. rm -rf "${ETCD_DIR-}"
  55. }
  56. kube::etcd::cleanup() {
  57. kube::etcd::stop
  58. kube::etcd::clean_etcd_dir
  59. }
  60. kube::etcd::install() {
  61. (
  62. cd "${KUBE_ROOT}/third_party"
  63. if [[ $(uname) == "Darwin" ]]; then
  64. download_file="etcd-v${ETCD_VERSION}-darwin-amd64.zip"
  65. curl -fsSLO --retry 3 --keepalive-time 2 https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/"${download_file}"
  66. unzip -o "${download_file}"
  67. ln -fns "etcd-v${ETCD_VERSION}-darwin-amd64" etcd
  68. rm "${download_file}"
  69. else
  70. curl -fsSL --retry 3 --keepalive-time 2 https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz | tar xzf -
  71. ln -fns "etcd-v${ETCD_VERSION}-linux-amd64" etcd
  72. fi
  73. kube::log::info "etcd v${ETCD_VERSION} installed. To use:"
  74. kube::log::info "export PATH=\${PATH}:$(pwd)/etcd"
  75. )
  76. }