etcd.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env 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.4.3}
  17. ETCD_HOST=${ETCD_HOST:-127.0.0.1}
  18. ETCD_PORT=${ETCD_PORT:-2379}
  19. export KUBE_INTEGRATION_ETCD_URL="http://${ETCD_HOST}:${ETCD_PORT}"
  20. kube::etcd::validate() {
  21. # validate if in path
  22. command -v etcd >/dev/null || {
  23. kube::log::usage "etcd must be in your PATH"
  24. kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
  25. exit 1
  26. }
  27. # validate etcd port is free
  28. local port_check_command
  29. if command -v ss &> /dev/null && ss -Version | grep 'iproute2' &> /dev/null; then
  30. port_check_command="ss"
  31. elif command -v netstat &>/dev/null; then
  32. port_check_command="netstat"
  33. else
  34. kube::log::usage "unable to identify if etcd is bound to port ${ETCD_PORT}. unable to find ss or netstat utilities."
  35. exit 1
  36. fi
  37. if ${port_check_command} -nat | grep "LISTEN" | grep "[\.:]${ETCD_PORT:?}" >/dev/null 2>&1; then
  38. kube::log::usage "unable to start etcd as port ${ETCD_PORT} is in use. please stop the process listening on this port and retry."
  39. kube::log::usage "$(netstat -nat | grep "[\.:]${ETCD_PORT:?} .*LISTEN")"
  40. exit 1
  41. fi
  42. # validate installed version is at least equal to minimum
  43. version=$(etcd --version | grep Version | tail -n +1 | head -n 1 | cut -d " " -f 3)
  44. if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
  45. export PATH=${KUBE_ROOT}/third_party/etcd:${PATH}
  46. hash etcd
  47. echo "${PATH}"
  48. version=$(etcd --version | head -n 1 | cut -d " " -f 3)
  49. if [[ $(kube::etcd::version "${ETCD_VERSION}") -gt $(kube::etcd::version "${version}") ]]; then
  50. kube::log::usage "etcd version ${ETCD_VERSION} or greater required."
  51. kube::log::info "You can use 'hack/install-etcd.sh' to install a copy in third_party/."
  52. exit 1
  53. fi
  54. fi
  55. }
  56. kube::etcd::version() {
  57. printf '%s\n' "${@}" | awk -F . '{ printf("%d%03d%03d\n", $1, $2, $3) }'
  58. }
  59. kube::etcd::start() {
  60. # validate before running
  61. kube::etcd::validate
  62. # Start etcd
  63. ETCD_DIR=${ETCD_DIR:-$(mktemp -d 2>/dev/null || mktemp -d -t test-etcd.XXXXXX)}
  64. if [[ -d "${ARTIFACTS:-}" ]]; then
  65. ETCD_LOGFILE="${ARTIFACTS}/etcd.$(uname -n).$(id -un).log.DEBUG.$(date +%Y%m%d-%H%M%S).$$"
  66. else
  67. ETCD_LOGFILE=${ETCD_LOGFILE:-"/dev/null"}
  68. fi
  69. kube::log::info "etcd --advertise-client-urls ${KUBE_INTEGRATION_ETCD_URL} --data-dir ${ETCD_DIR} --listen-client-urls http://${ETCD_HOST}:${ETCD_PORT} --debug > \"${ETCD_LOGFILE}\" 2>/dev/null"
  70. etcd --advertise-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --data-dir "${ETCD_DIR}" --listen-client-urls "${KUBE_INTEGRATION_ETCD_URL}" --debug 2> "${ETCD_LOGFILE}" >/dev/null &
  71. ETCD_PID=$!
  72. echo "Waiting for etcd to come up."
  73. kube::util::wait_for_url "${KUBE_INTEGRATION_ETCD_URL}/health" "etcd: " 0.25 80
  74. curl -fs -X POST "${KUBE_INTEGRATION_ETCD_URL}/v3/kv/put" -d '{"key": "X3Rlc3Q=", "value": ""}'
  75. }
  76. kube::etcd::stop() {
  77. if [[ -n "${ETCD_PID-}" ]]; then
  78. kill "${ETCD_PID}" &>/dev/null || :
  79. wait "${ETCD_PID}" &>/dev/null || :
  80. fi
  81. }
  82. kube::etcd::clean_etcd_dir() {
  83. if [[ -n "${ETCD_DIR-}" ]]; then
  84. rm -rf "${ETCD_DIR}"
  85. fi
  86. }
  87. kube::etcd::cleanup() {
  88. kube::etcd::stop
  89. kube::etcd::clean_etcd_dir
  90. }
  91. kube::etcd::install() {
  92. (
  93. local os
  94. local arch
  95. os=$(kube::util::host_os)
  96. arch=$(kube::util::host_arch)
  97. cd "${KUBE_ROOT}/third_party" || return 1
  98. if [[ $(readlink etcd) == etcd-v${ETCD_VERSION}-${os}-* ]]; then
  99. kube::log::info "etcd v${ETCD_VERSION} already installed. To use:"
  100. kube::log::info "export PATH=\"$(pwd)/etcd:\${PATH}\""
  101. return #already installed
  102. fi
  103. if [[ ${os} == "darwin" ]]; then
  104. download_file="etcd-v${ETCD_VERSION}-darwin-amd64.zip"
  105. url="https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/${download_file}"
  106. kube::util::download_file "${url}" "${download_file}"
  107. unzip -o "${download_file}"
  108. ln -fns "etcd-v${ETCD_VERSION}-darwin-amd64" etcd
  109. rm "${download_file}"
  110. else
  111. url="https://github.com/coreos/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-${arch}.tar.gz"
  112. download_file="etcd-v${ETCD_VERSION}-linux-${arch}.tar.gz"
  113. kube::util::download_file "${url}" "${download_file}"
  114. tar xzf "${download_file}"
  115. ln -fns "etcd-v${ETCD_VERSION}-linux-${arch}" etcd
  116. rm "${download_file}"
  117. fi
  118. kube::log::info "etcd v${ETCD_VERSION} installed. To use:"
  119. kube::log::info "export PATH=\"$(pwd)/etcd:\${PATH}\""
  120. )
  121. }