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