utils.sh 3.3 KB

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