You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.sh 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #
  16. # -----------------------------------------------------------------------------
  17. # CHANGELOG
  18. # KubeEdge Authors:
  19. # Some functions derived from https://github.com/kubernetes/kubernetes/blob/v1.19.0-beta.2/hack/utils.sh
  20. # for update-vendor-licenses, verify-vendor-licenses
  21. # Example: kube::util::trap_add 'echo "in trap DEBUG"' DEBUG
  22. # See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
  23. kube::util::trap_add() {
  24. local trap_add_cmd
  25. trap_add_cmd=$1
  26. shift
  27. for trap_add_name in "$@"; do
  28. local existing_cmd
  29. local new_cmd
  30. # Grab the currently defined trap commands for this trap
  31. existing_cmd=$(trap -p "${trap_add_name}" | awk -F"'" '{print $2}')
  32. if [[ -z "${existing_cmd}" ]]; then
  33. new_cmd="${trap_add_cmd}"
  34. else
  35. new_cmd="${trap_add_cmd};${existing_cmd}"
  36. fi
  37. # Assign the test. Disable the shellcheck warning telling that trap
  38. # commands should be single quoted to avoid evaluating them at this
  39. # point instead evaluating them at run time. The logic of adding new
  40. # commands to a single trap requires them to be evaluated right away.
  41. # shellcheck disable=SC2064
  42. trap "${new_cmd}" "${trap_add_name}"
  43. done
  44. }
  45. # Opposite of kube::util::ensure-temp-dir()
  46. kube::util::cleanup-temp-dir() {
  47. rm -rf "${KUBE_TEMP}"
  48. }
  49. # Create a temp dir that'll be deleted at the end of this bash session.
  50. #
  51. # Vars set:
  52. # KUBE_TEMP
  53. kube::util::ensure-temp-dir() {
  54. if [[ -z ${KUBE_TEMP-} ]]; then
  55. KUBE_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t kubernetes.XXXXXX)
  56. kube::util::trap_add kube::util::cleanup-temp-dir EXIT
  57. fi
  58. }
  59. # outputs md5 hash of $1, works on macOS and Linux
  60. function kube::util::md5() {
  61. if which md5 >/dev/null 2>&1; then
  62. md5 -q "$1"
  63. else
  64. md5sum "$1" | awk '{ print $1 }'
  65. fi
  66. }