utils.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Modified from https://gitlab.com/gitlab-org/gitlab/-/blob/master/scripts/utils.sh
  2. # before each job, we need to check if this job is filtered by bot stage/job filter
  3. function apply_bot_filter() {
  4. python "${IDF_PATH}"/tools/ci/apply_bot_filter.py || exit 0
  5. }
  6. function add_ssh_keys() {
  7. local key_string="${1}"
  8. mkdir -p ~/.ssh
  9. chmod 700 ~/.ssh
  10. echo -n "${key_string}" >~/.ssh/id_rsa_base64
  11. base64 --decode --ignore-garbage ~/.ssh/id_rsa_base64 >~/.ssh/id_rsa
  12. chmod 600 ~/.ssh/id_rsa
  13. }
  14. function add_gitlab_ssh_keys() {
  15. add_ssh_keys "${GITLAB_KEY}"
  16. echo -e "Host gitlab.espressif.cn\n\tStrictHostKeyChecking no\n" >>~/.ssh/config
  17. # For gitlab geo nodes
  18. if [ "${LOCAL_GITLAB_SSH_SERVER:-}" ]; then
  19. SRV=${LOCAL_GITLAB_SSH_SERVER##*@} # remove the chars before @, which is the account
  20. SRV=${SRV%%:*} # remove the chars after :, which is the port
  21. printf "Host %s\n\tStrictHostKeyChecking no\n" "${SRV}" >>~/.ssh/config
  22. fi
  23. }
  24. function add_github_ssh_keys() {
  25. add_ssh_keys "${GH_PUSH_KEY}"
  26. echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >>~/.ssh/config
  27. }
  28. function add_doc_server_ssh_keys() {
  29. local key_string="${1}"
  30. local server_url="${2}"
  31. local server_user="${3}"
  32. add_ssh_keys "${key_string}"
  33. echo -e "Host ${server_url}\n\tStrictHostKeyChecking no\n\tUser ${server_user}\n" >>~/.ssh/config
  34. }
  35. function fetch_submodules() {
  36. python "${SUBMODULE_FETCH_TOOL}" -s "${SUBMODULES_TO_FETCH}"
  37. }
  38. function get_all_submodules() {
  39. git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | sed -e 's|$|/**|' | xargs | sed -e 's/ /,/g'
  40. }
  41. function set_component_ut_vars() {
  42. local exclude_list_fp="${IDF_PATH}/tools/ci/component_ut_excludes.txt"
  43. export COMPONENT_UT_DIRS=$(find components/ -name test_apps -type d)
  44. export COMPONENT_UT_EXCLUDES=$([ -r $exclude_list_fp ] && cat $exclude_list_fp | xargs)
  45. echo "COMPONENT_UT_DIRS, COMPONENT_UT_EXCLUDES written into export"
  46. }
  47. function error() {
  48. printf "\033[0;31m%s\n\033[0m" "${1}" >&2
  49. }
  50. function info() {
  51. printf "\033[0;32m%s\n\033[0m" "${1}" >&2
  52. }
  53. function warning() {
  54. printf "\033[0;33m%s\n\033[0m" "${1}" >&2
  55. }
  56. function run_cmd() {
  57. local start=$(date +%s)
  58. eval "$@"
  59. local ret=$?
  60. local end=$(date +%s)
  61. local duration=$((end - start))
  62. if [[ $ret -eq 0 ]]; then
  63. info "(\$ $*) succeeded in ${duration} seconds."
  64. return 0
  65. else
  66. error "(\$ $*) failed in ${duration} seconds."
  67. return $ret
  68. fi
  69. }
  70. # Retries a command RETRY_ATTEMPTS times in case of failure
  71. # Inspired by https://stackoverflow.com/a/8351489
  72. function retry_failed() {
  73. local max_attempts=${RETRY_ATTEMPTS-3}
  74. local timeout=${RETRY_TIMEWAIT-1}
  75. local attempt=1
  76. local exitCode=0
  77. whole_start=$(date +%s)
  78. while true; do
  79. if run_cmd "$@"; then
  80. exitCode=0
  81. break
  82. else
  83. exitCode=$?
  84. fi
  85. if ((attempt >= max_attempts)); then
  86. break
  87. fi
  88. error "Retrying in ${timeout} seconds..."
  89. sleep $timeout
  90. attempt=$((attempt + 1))
  91. timeout=$((timeout * 2))
  92. done
  93. local duration=$(($(date '+%s') - whole_start))
  94. if [[ $exitCode != 0 ]]; then
  95. error "Totally failed! Spent $duration sec in total"
  96. else
  97. info "Done! Spent $duration sec in total"
  98. fi
  99. return $exitCode
  100. }