docker-run.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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+=(
  87. --user "$(id -u):$(id -g)"
  88. --volume "/etc/passwd:/etc/passwd:ro"
  89. --volume "/etc/group:/etc/group:ro"
  90. --volume "/var/lib/buildkite-agent:/var/lib/buildkite-agent"
  91. )
  92. fi
  93. if [[ -n $SOLANA_ALLOCATE_TTY ]]; then
  94. # Colored output, progress bar and Ctrl-C:
  95. # https://stackoverflow.com/a/41099052/10242004
  96. ARGS+=(--interactive --tty)
  97. fi
  98. # Environment variables to propagate into the container
  99. ARGS+=(
  100. --env BUILDKITE
  101. --env BUILDKITE_AGENT_ACCESS_TOKEN
  102. --env BUILDKITE_JOB_ID
  103. --env BUILDKITE_PARALLEL_JOB
  104. --env BUILDKITE_PARALLEL_JOB_COUNT
  105. --env CI
  106. --env CI_BRANCH
  107. --env CI_BASE_BRANCH
  108. --env CI_TAG
  109. --env CI_BUILD_ID
  110. --env CI_COMMIT
  111. --env CI_JOB_ID
  112. --env CI_PULL_REQUEST
  113. --env CI_REPO_SLUG
  114. --env CRATES_IO_TOKEN
  115. --env CARGO_NET_GIT_FETCH_WITH_CLI
  116. )
  117. # Also propagate environment variables needed for codecov
  118. # https://docs.codecov.io/docs/testing-with-docker#section-codecov-inside-docker
  119. # We normalize CI to `1`; but codecov expects it to be `true` to detect Buildkite...
  120. # Unfortunately, codecov.io fails sometimes:
  121. # curl: (7) Failed to connect to codecov.io port 443: Connection timed out
  122. CODECOV_ENVS=$(CI=true bash <(while ! curl -sS --retry 5 --retry-delay 2 --retry-connrefused --fail https://codecov.io/env; do sleep 10; done))
  123. if $INTERACTIVE; then
  124. if [[ -n $1 ]]; then
  125. echo
  126. echo "Note: '$*' ignored due to --shell argument"
  127. echo
  128. fi
  129. set -x
  130. # shellcheck disable=SC2086
  131. exec docker run --interactive --tty "${ARGS[@]}" $CODECOV_ENVS "$IMAGE" bash
  132. fi
  133. set -x
  134. # shellcheck disable=SC2086
  135. exec docker run "${ARGS[@]}" $CODECOV_ENVS -t "$IMAGE" "$@"