docker-run.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env bash
  2. set -e
  3. usage() {
  4. echo "Usage: $0 [--nopull] [docker image name] [command]"
  5. echo
  6. echo Runs command in the specified docker image with
  7. echo a CI-appropriate environment.
  8. echo
  9. echo "--nopull Skip the dockerhub image update"
  10. echo "--shell Skip command and enter an interactive shell"
  11. echo
  12. }
  13. cd "$(dirname "$0")/.."
  14. INTERACTIVE=false
  15. if [[ $1 = --shell ]]; then
  16. INTERACTIVE=true
  17. shift
  18. fi
  19. NOPULL=false
  20. if [[ $1 = --nopull ]]; then
  21. NOPULL=true
  22. shift
  23. fi
  24. IMAGE="$1"
  25. if [[ -z "$IMAGE" ]]; then
  26. echo Error: image not defined
  27. exit 1
  28. fi
  29. $NOPULL || docker pull "$IMAGE"
  30. shift
  31. ARGS=(
  32. --workdir /solana
  33. --volume "$PWD:/solana"
  34. --rm
  35. )
  36. if [[ -n $CI ]]; then
  37. if [[ -n $BUILDKITE ]]; then
  38. # I hate buildkite-esque echo is leaking into this generic shell wrapper.
  39. # but it's easiest to notify to users, and properly guarded under $BUILDKITE_ env
  40. # (2 is chosen for third time's the charm).
  41. if [[ $BUILDKITE_RETRY_COUNT -ge 2 ]]; then
  42. # Disable sccache to create a clean-room environment to preclude any
  43. # sccache-related bugs
  44. echo "--- $0 ... (with sccache being DISABLED due to many (${BUILDKITE_RETRY_COUNT}) retries)"
  45. else
  46. echo "--- $0 ... (with sccache enabled with prefix: $SCCACHE_KEY_PREFIX)"
  47. # sccache
  48. ARGS+=(
  49. --env "RUSTC_WRAPPER=/usr/local/cargo/bin/sccache"
  50. )
  51. # local disk storage for sccache (experimental; only used by dcou for now)
  52. mkdir -p "$HOME/.cache/sccache-for-docker"
  53. CONTAINER_HOME="/"
  54. ARGS+=(
  55. --volume "$HOME/.cache/sccache-for-docker:$CONTAINER_HOME/.cache/sccache"
  56. )
  57. # s3
  58. if [ -n "$AWS_ACCESS_KEY_ID" ]; then
  59. ARGS+=(
  60. --env AWS_ACCESS_KEY_ID
  61. --env AWS_SECRET_ACCESS_KEY
  62. --env SCCACHE_BUCKET
  63. --env SCCACHE_REGION
  64. --env SCCACHE_S3_KEY_PREFIX
  65. )
  66. fi
  67. # gcs
  68. if [ -n "$SCCACHE_GCS_KEY_PATH" ]; then
  69. ARGS+=(
  70. --env SCCACHE_GCS_KEY_PATH
  71. --volume "$SCCACHE_GCS_KEY_PATH:$SCCACHE_GCS_KEY_PATH"
  72. --env SCCACHE_GCS_BUCKET
  73. --env SCCACHE_GCS_RW_MODE
  74. --env SCCACHE_GCS_KEY_PREFIX
  75. )
  76. fi
  77. fi
  78. # Disable seccomp to allow io_uring operations (https://github.com/moby/moby/pull/46762)
  79. ARGS+=(--security-opt seccomp=unconfined)
  80. # Adjust memlock limit to let io_uring register buffers
  81. ARGS+=(--ulimit memlock=-1:-1)
  82. fi
  83. fi
  84. # Ensure files are created with the current host uid/gid
  85. if [[ -z "$SOLANA_DOCKER_RUN_NOSETUID" ]]; then
  86. ARGS+=(--user "$(id -u):$(id -g)")
  87. fi
  88. if [[ -n $SOLANA_ALLOCATE_TTY ]]; then
  89. # Colored output, progress bar and Ctrl-C:
  90. # https://stackoverflow.com/a/41099052/10242004
  91. ARGS+=(--interactive --tty)
  92. fi
  93. # Environment variables to propagate into the container
  94. ARGS+=(
  95. --env BUILDKITE
  96. --env BUILDKITE_AGENT_ACCESS_TOKEN
  97. --env BUILDKITE_JOB_ID
  98. --env BUILDKITE_PARALLEL_JOB
  99. --env BUILDKITE_PARALLEL_JOB_COUNT
  100. --env CI
  101. --env CI_BRANCH
  102. --env CI_BASE_BRANCH
  103. --env CI_TAG
  104. --env CI_BUILD_ID
  105. --env CI_COMMIT
  106. --env CI_JOB_ID
  107. --env CI_PULL_REQUEST
  108. --env CI_REPO_SLUG
  109. --env CRATES_IO_TOKEN
  110. )
  111. # Also propagate environment variables needed for codecov
  112. # https://docs.codecov.io/docs/testing-with-docker#section-codecov-inside-docker
  113. # We normalize CI to `1`; but codecov expects it to be `true` to detect Buildkite...
  114. # Unfortunately, codecov.io fails sometimes:
  115. # curl: (7) Failed to connect to codecov.io port 443: Connection timed out
  116. CODECOV_ENVS=$(CI=true bash <(while ! curl -sS --retry 5 --retry-delay 2 --retry-connrefused --fail https://codecov.io/env; do sleep 10; done))
  117. if $INTERACTIVE; then
  118. if [[ -n $1 ]]; then
  119. echo
  120. echo "Note: '$*' ignored due to --shell argument"
  121. echo
  122. fi
  123. set -x
  124. # shellcheck disable=SC2086
  125. exec docker run --interactive --tty "${ARGS[@]}" $CODECOV_ENVS "$IMAGE" bash
  126. fi
  127. set -x
  128. # shellcheck disable=SC2086
  129. exec docker run "${ARGS[@]}" $CODECOV_ENVS -t "$IMAGE" "$@"