docker_env.sh 3.0 KB

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