1#!/bin/bash -eu
2
3# docker_env.sh
4#
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#
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#
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
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#
25#
26# Running this script directly will check for Docker availability and set up
27# the Docker image.
28
29# Copyright The Mbed TLS Contributors
30# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
31
32
33# default values, can be overridden by the environment
34: ${MBEDTLS_DOCKER_GUEST:=bionic}
35
36
37DOCKER_IMAGE_TAG="armmbed/mbedtls-test:${MBEDTLS_DOCKER_GUEST}"
38
39# Make sure docker is available
40if ! which docker > /dev/null; then
41    echo "Docker is required but doesn't seem to be installed. See https://www.docker.com/ to get started"
42    exit 1
43fi
44
45# Figure out if we need to 'sudo docker'
46if groups | grep docker > /dev/null; then
47    DOCKER="docker"
48else
49    echo "Using sudo to invoke docker since you're not a member of the docker group..."
50    DOCKER="sudo docker"
51fi
52
53# Figure out the number of processors available
54if [ "$(uname)" == "Darwin" ]; then
55    NUM_PROC="$(sysctl -n hw.logicalcpu)"
56else
57    NUM_PROC="$(nproc)"
58fi
59
60# Build the Docker image
61echo "Getting docker image up to date (this may take a few minutes)..."
62${DOCKER} image build \
63    -t ${DOCKER_IMAGE_TAG} \
64    --cache-from=${DOCKER_IMAGE_TAG} \
65    --build-arg MAKEFLAGS_PARALLEL="-j ${NUM_PROC}" \
66    --network host \
67    ${http_proxy+--build-arg http_proxy=${http_proxy}} \
68    ${https_proxy+--build-arg https_proxy=${https_proxy}} \
69    ${MBEDTLS_DOCKER_REGISTRY+--build-arg MY_REGISTRY="${MBEDTLS_DOCKER_REGISTRY}/"} \
70    tests/docker/${MBEDTLS_DOCKER_GUEST}
71
72run_in_docker()
73{
74    ENV_ARGS=""
75    while [ "$1" == "-e" ]; do
76        ENV_ARGS="${ENV_ARGS} $1 $2"
77        shift 2
78    done
79
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}
91