migrate-if-needed.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/bin/sh
  2. # Copyright 2016 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. # NOTES
  16. # This script performs etcd upgrade based on the following environmental
  17. # variables:
  18. # TARGET_STORAGE - API of etcd to be used (supported: 'etcd3')
  19. # TARGET_VERSION - etcd release to be used (supported: '3.0.17', '3.1.12',
  20. # '3.2.24', "3.3.17", "3.4.3")
  21. # DATA_DIRECTORY - directory with etcd data
  22. #
  23. # The current etcd version and storage format is detected based on the
  24. # contents of "${DATA_DIRECTORY}/version.txt" file (if the file doesn't
  25. # exist, we default it to "3.0.17/etcd2".
  26. #
  27. # The update workflow support the following upgrade steps:
  28. # - 3.0.17/etcd3 -> 3.1.12/etcd3
  29. # - 3.1.12/etcd3 -> 3.2.24/etcd3
  30. # - 3.2.24/etcd3 -> 3.3.17/etcd3
  31. # - 3.3.17/etcd3 -> 3.4.3/etcd3
  32. #
  33. # NOTE: The releases supported in this script has to match release binaries
  34. # present in the etcd image (to make this script work correctly).
  35. #
  36. # Based on the current etcd version and storage format we detect what
  37. # upgrade step from this should be done to get reach target configuration
  38. set -o errexit
  39. set -o nounset
  40. # NOTE: BUNDLED_VERSION has to match release binaries present in the
  41. # etcd image (to make this script work correctly).
  42. BUNDLED_VERSIONS="3.0.17, 3.1.12, 3.2.24, 3.3.17, 3.4.3"
  43. ETCD_NAME="${ETCD_NAME:-etcd-$(hostname)}"
  44. if [ -z "${DATA_DIRECTORY:-}" ]; then
  45. echo "DATA_DIRECTORY variable unset - unexpected failure"
  46. exit 1
  47. fi
  48. case "${DATA_DIRECTORY}" in
  49. *event*)
  50. ETCD_PEER_PORT=2381
  51. ETCD_CLIENT_PORT=18631
  52. ;;
  53. *)
  54. ETCD_PEER_PORT=2380
  55. ETCD_CLIENT_PORT=18629
  56. ;;
  57. esac
  58. if [ -z "${INITIAL_CLUSTER:-}" ]; then
  59. echo "Warn: INITIAL_CLUSTER variable unset - defaulting to ${ETCD_NAME}=http://localhost:${ETCD_PEER_PORT}"
  60. INITIAL_CLUSTER="${ETCD_NAME}=http://localhost:${ETCD_PEER_PORT}"
  61. fi
  62. if [ -z "${LISTEN_PEER_URLS:-}" ]; then
  63. echo "Warn: LISTEN_PEER_URLS variable unset - defaulting to http://localhost:${ETCD_PEER_PORT}"
  64. LISTEN_PEER_URLS="http://localhost:${ETCD_PEER_PORT}"
  65. fi
  66. if [ -z "${INITIAL_ADVERTISE_PEER_URLS:-}" ]; then
  67. echo "Warn: INITIAL_ADVERTISE_PEER_URLS variable unset - defaulting to http://localhost:${ETCD_PEER_PORT}"
  68. INITIAL_ADVERTISE_PEER_URLS="http://localhost:${ETCD_PEER_PORT}"
  69. fi
  70. if [ -z "${TARGET_VERSION:-}" ]; then
  71. echo "TARGET_VERSION variable unset - unexpected failure"
  72. exit 1
  73. fi
  74. if [ -z "${TARGET_STORAGE:-}" ]; then
  75. echo "TARGET_STORAGE variable unset - unexpected failure"
  76. exit 1
  77. fi
  78. ETCD_DATA_PREFIX="${ETCD_DATA_PREFIX:-/registry}"
  79. ETCD_CREDS="${ETCD_CREDS:-}"
  80. # Correctly support upgrade and rollback to non-default version.
  81. if [ "${DO_NOT_MOVE_BINARIES:-}" != "true" ]; then
  82. cp "/usr/local/bin/etcd-${TARGET_VERSION}" "/usr/local/bin/etcd"
  83. cp "/usr/local/bin/etcdctl-${TARGET_VERSION}" "/usr/local/bin/etcdctl"
  84. fi
  85. /usr/local/bin/migrate \
  86. --name "${ETCD_NAME}" \
  87. --port "${ETCD_CLIENT_PORT}" \
  88. --listen-peer-urls "${LISTEN_PEER_URLS}" \
  89. --initial-advertise-peer-urls "${INITIAL_ADVERTISE_PEER_URLS}" \
  90. --data-dir "${DATA_DIRECTORY}" \
  91. --bundled-versions "${BUNDLED_VERSIONS}" \
  92. --initial-cluster "${INITIAL_CLUSTER}" \
  93. --target-version "${TARGET_VERSION}" \
  94. --target-storage "${TARGET_STORAGE}" \
  95. --etcd-data-prefix "${ETCD_DATA_PREFIX}" \
  96. --ttl-keys-directory "${TTL_KEYS_DIRECTORY:-${ETCD_DATA_PREFIX}/events}" \
  97. --etcd-server-extra-args "${ETCD_CREDS}"