docker_env.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash -eu
  2. # docker_env.sh
  3. #
  4. # Purpose
  5. # -------
  6. #
  7. # This is a helper script to enable running tests under a Docker container,
  8. # thus making it easier to get set up as well as isolating test dependencies
  9. # (which include legacy/insecure configurations of openssl and gnutls).
  10. #
  11. # WARNING: the Dockerfile used by this script is no longer maintained! See
  12. # https://github.com/Mbed-TLS/mbedtls-test/blob/master/README.md#quick-start
  13. # for the set of Docker images we use on the CI.
  14. #
  15. # Notes for users
  16. # ---------------
  17. # This script expects a Linux x86_64 system with a recent version of Docker
  18. # installed and available for use, as well as http/https access. If a proxy
  19. # server must be used, invoke this script with the usual environment variables
  20. # (http_proxy and https_proxy) set appropriately. If an alternate Docker
  21. # registry is needed, specify MBEDTLS_DOCKER_REGISTRY to point at the
  22. # host name.
  23. #
  24. #
  25. # Running this script directly will check for Docker availability and set up
  26. # the Docker image.
  27. # Copyright The Mbed TLS Contributors
  28. # SPDX-License-Identifier: Apache-2.0
  29. #
  30. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  31. # not use this file except in compliance with the License.
  32. # You may obtain a copy of the License at
  33. #
  34. # http://www.apache.org/licenses/LICENSE-2.0
  35. #
  36. # Unless required by applicable law or agreed to in writing, software
  37. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  38. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  39. # See the License for the specific language governing permissions and
  40. # limitations under the License.
  41. # default values, can be overridden by the environment
  42. : ${MBEDTLS_DOCKER_GUEST:=bionic}
  43. DOCKER_IMAGE_TAG="armmbed/mbedtls-test:${MBEDTLS_DOCKER_GUEST}"
  44. # Make sure docker is available
  45. if ! which docker > /dev/null; then
  46. echo "Docker is required but doesn't seem to be installed. See https://www.docker.com/ to get started"
  47. exit 1
  48. fi
  49. # Figure out if we need to 'sudo docker'
  50. if groups | grep docker > /dev/null; then
  51. DOCKER="docker"
  52. else
  53. echo "Using sudo to invoke docker since you're not a member of the docker group..."
  54. DOCKER="sudo docker"
  55. fi
  56. # Figure out the number of processors available
  57. if [ "$(uname)" == "Darwin" ]; then
  58. NUM_PROC="$(sysctl -n hw.logicalcpu)"
  59. else
  60. NUM_PROC="$(nproc)"
  61. fi
  62. # Build the Docker image
  63. echo "Getting docker image up to date (this may take a few minutes)..."
  64. ${DOCKER} image build \
  65. -t ${DOCKER_IMAGE_TAG} \
  66. --cache-from=${DOCKER_IMAGE_TAG} \
  67. --build-arg MAKEFLAGS_PARALLEL="-j ${NUM_PROC}" \
  68. --network host \
  69. ${http_proxy+--build-arg http_proxy=${http_proxy}} \
  70. ${https_proxy+--build-arg https_proxy=${https_proxy}} \
  71. ${MBEDTLS_DOCKER_REGISTRY+--build-arg MY_REGISTRY="${MBEDTLS_DOCKER_REGISTRY}/"} \
  72. tests/docker/${MBEDTLS_DOCKER_GUEST}
  73. run_in_docker()
  74. {
  75. ENV_ARGS=""
  76. while [ "$1" == "-e" ]; do
  77. ENV_ARGS="${ENV_ARGS} $1 $2"
  78. shift 2
  79. done
  80. ${DOCKER} container run -it --rm \
  81. --cap-add SYS_PTRACE \
  82. --user "$(id -u):$(id -g)" \
  83. --volume $PWD:$PWD \
  84. --workdir $PWD \
  85. -e MAKEFLAGS \
  86. -e PYLINTHOME=/tmp/.pylintd \
  87. ${ENV_ARGS} \
  88. ${DOCKER_IMAGE_TAG} \
  89. $@
  90. }