utils.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 error() {
  38. printf "\033[0;31m%s\n\033[0m" "${1}" >&2
  39. }
  40. function info() {
  41. printf "\033[0;32m%s\n\033[0m" "${1}" >&2
  42. }
  43. function warning() {
  44. printf "\033[0;33m%s\n\033[0m" "${1}" >&2
  45. }
  46. function run_cmd() {
  47. local start=$(date +%s)
  48. eval "$@"
  49. local ret=$?
  50. local end=$(date +%s)
  51. local duration=$((end - start))
  52. if [[ $ret -eq 0 ]]; then
  53. info "(\$ $*) succeeded in ${duration} seconds."
  54. return 0
  55. else
  56. error "(\$ $*) failed in ${duration} seconds."
  57. return $ret
  58. fi
  59. }
  60. # Retries a command RETRY_ATTEMPTS times in case of failure
  61. # Inspired by https://stackoverflow.com/a/8351489
  62. function retry_failed() {
  63. local max_attempts=${RETRY_ATTEMPTS-3}
  64. local timeout=${RETRY_TIMEWAIT-1}
  65. local attempt=1
  66. local exitCode=0
  67. whole_start=$(date +%s)
  68. while true; do
  69. if run_cmd "$@"; then
  70. exitCode=0
  71. break
  72. else
  73. exitCode=$?
  74. fi
  75. if ((attempt >= max_attempts)); then
  76. break
  77. fi
  78. error "Retrying in ${timeout} seconds..."
  79. sleep $timeout
  80. attempt=$((attempt + 1))
  81. timeout=$((timeout * 2))
  82. done
  83. local duration=$(($(date '+%s') - whole_start))
  84. if [[ $exitCode != 0 ]]; then
  85. error "Totally failed! Spent $duration sec in total"
  86. else
  87. info "Done! Spent $duration sec in total"
  88. fi
  89. return $exitCode
  90. }
  91. function internal_pip_install() {
  92. project=$1
  93. package=$2
  94. token_name=${3:-${BOT_TOKEN_NAME}}
  95. token=${4:-${BOT_TOKEN}}
  96. python=${5:-python}
  97. $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}
  98. }