toolbox.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env -S bash -e
  2. # Copyright 2016 The Rook Authors. All rights reserved.
  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. CEPH_CONFIG="/etc/ceph/ceph.conf"
  16. MON_CONFIG="/etc/rook/mon-endpoints"
  17. KEYRING_FILE="/etc/ceph/keyring"
  18. # create a ceph config file in its default location so ceph/rados tools can be used
  19. # without specifying any arguments
  20. write_endpoints() {
  21. endpoints=$(cat ${MON_CONFIG})
  22. # filter out the mon names
  23. # external cluster can have numbers or hyphens in mon names, handling them in regex
  24. # shellcheck disable=SC2001
  25. mon_endpoints=$(echo "${endpoints}"| sed 's/[a-z0-9_-]\+=//g')
  26. DATE=$(date)
  27. echo "$DATE writing mon endpoints to ${CEPH_CONFIG}: ${endpoints}"
  28. cat <<EOF > ${CEPH_CONFIG}
  29. [global]
  30. mon_host = ${mon_endpoints}
  31. [client.admin]
  32. keyring = ${KEYRING_FILE}
  33. EOF
  34. }
  35. # watch the endpoints config file and update if the mon endpoints ever change
  36. watch_endpoints() {
  37. # get the timestamp for the target of the soft link
  38. real_path=$(realpath ${MON_CONFIG})
  39. initial_time=$(stat -c %Z "${real_path}")
  40. while true; do
  41. real_path=$(realpath ${MON_CONFIG})
  42. latest_time=$(stat -c %Z "${real_path}")
  43. if [[ "${latest_time}" != "${initial_time}" ]]; then
  44. write_endpoints
  45. initial_time=${latest_time}
  46. fi
  47. sleep 10
  48. done
  49. }
  50. # read the secret from an env var (for backward compatibility), or from the secret file
  51. ceph_secret=${ROOK_CEPH_SECRET}
  52. if [[ "$ceph_secret" == "" ]]; then
  53. ceph_secret=$(cat /var/lib/rook-ceph-mon/secret.keyring)
  54. fi
  55. # create the keyring file
  56. cat <<EOF > ${KEYRING_FILE}
  57. [${ROOK_CEPH_USERNAME}]
  58. key = ${ceph_secret}
  59. EOF
  60. # write the initial config file
  61. write_endpoints
  62. # continuously update the mon endpoints if they fail over
  63. if [ "$1" != "--skip-watch" ]; then
  64. watch_endpoints
  65. fi