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