util.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env bash
  2. # Copyright 2017 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. # Running cmd $RETRIES times in case of failures.
  16. function run-cmd-with-retries {
  17. RETRIES="${RETRIES:-3}"
  18. for attempt in $(seq 1 "${RETRIES}"); do
  19. local ret_val=0
  20. exec 5>&1 # Duplicate &1 to &5 for use below.
  21. # We don't use 'local' to declare result as then ret_val always gets value 0.
  22. # We use tee to output to &5 (redirected to stdout) while also storing it in the variable.
  23. result=$("$@" 2>&1 | tee >(cat - >&5)) || ret_val="$?"
  24. if [[ "${ret_val:-0}" -ne "0" ]]; then
  25. if [[ $(echo "${result}" | grep -c "already exists") -gt 0 ]]; then
  26. if [[ "${attempt}" == 1 ]]; then
  27. # shellcheck disable=SC2154 # Color defined in sourced script
  28. echo -e "${color_red}Failed to $1 $2 ${3:-} as the resource hasn't been deleted from a previous run.${color_norm}" >& 2
  29. exit 1
  30. fi
  31. # shellcheck disable=SC2154 # Color defined in sourced script
  32. echo -e "${color_yellow}Succeeded to $1 $2 ${3:-} in the previous attempt, but status response wasn't received.${color_norm}"
  33. return 0
  34. fi
  35. # shellcheck disable=SC2154 # Color defined in sourced script
  36. echo -e "${color_yellow}Attempt $attempt failed to $1 $2 ${3:-}. Retrying.${color_norm}" >& 2
  37. sleep $((attempt * 5))
  38. else
  39. # shellcheck disable=SC2154 # Color defined in sourced script
  40. echo -e "${color_green}Succeeded to $1 $2 ${3:-}.${color_norm}"
  41. return 0
  42. fi
  43. done
  44. # shellcheck disable=SC2154 # Color defined in sourced script
  45. echo -e "${color_red}Failed to $1 $2 ${3:-}.${color_norm}" >& 2
  46. exit 1
  47. }