Debian Codegraph @

kokoro/testutils/docker_execute.sh

raw
1 #!/bin/bash
2 # Copyright 2023 Google LLC
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ################################################################################
16
17 # Utility script to run a command in a new docker container.
18 #
19 # This script must be run from inside the Tink library to run the command for.
20 #
21 # NOTE: When running in a new container, this script mounts the parent folder of
22 # `pwd`. Other dependencies, if any, are assumed to be located there. For
23 # example, if running tink-py tests, this script assumes:
24 # - pwd => /path/to/parent/tink-py
25 # - mount path => /path/to/parent
26 # - ls /path/to/parent => tink_cc tink_py.
27
28 set -eo pipefail
29
30 usage() {
31 cat <<EOF
32 Usage: $0 [-c <container image>] [-k <service key file path>] <command>
33 -c: [Optional] Container image to run the command on.
34 -k: [Optional] Service key file path for pulling the image from the Google Artifact Registry (https://cloud.google.com/artifact-registry).
35 -e: [Optional] File containing a list of environment variables to pass to Docker using --env-file (see https://docs.docker.com/engine/reference/commandline/run/#env).
36 -m: [Optional] Additional --mount argument to pass to the Docker command (see https://docs.docker.com/reference/cli/docker/container/run/#mount).
37 -h: Help. Print this usage information.
38 EOF
39 exit 1
40 }
41
42 # Args.
43 COMMAND=
44
45 # Options.
46 CONTAINER_IMAGE_NAME=
47 GCR_SERVICE_KEY_PATH=
48 DOCKER_ENV_FILE=
49 DOCKER_MOUNT_ARG=
50
51 #######################################
52 # Process command line arguments.
53 #######################################
54 process_args() {
55 # Parse options.
56 while getopts "hc:k:e:m:" opt; do
57 case "${opt}" in
58 c) CONTAINER_IMAGE_NAME="${OPTARG}" ;;
59 k) GCR_SERVICE_KEY_PATH="${OPTARG}" ;;
60 e) DOCKER_ENV_FILE="${OPTARG}" ;;
61 m) DOCKER_MOUNT_ARG="${OPTARG}" ;;
62 *) usage ;;
63 esac
64 done
65 shift $((OPTIND - 1))
66 readonly CONTAINER_IMAGE_NAME
67 readonly GCR_SERVICE_KEY_PATH
68 readonly DOCKER_ENV_FILE
69 readonly DOCKER_MOUNT_ARG
70 readonly COMMAND=("$@")
71 }
72
73 main() {
74 process_args "$@"
75
76 if [[ -z "${CONTAINER_IMAGE_NAME:-}" ]]; then
77 echo "Running command on the host"
78 time "${COMMAND[@]}"
79 else
80 echo "Running command on a new container from image ${CONTAINER_IMAGE_NAME}"
81 if [[ ! -z "${GCR_SERVICE_KEY_PATH:-}" ]]; then
82 # Activate service account to read from a private artifact registry repo.
83 gcloud auth activate-service-account --key-file="${GCR_SERVICE_KEY_PATH}"
84 gcloud config set project tink-test-infrastructure
85 gcloud auth configure-docker us-docker.pkg.dev --quiet
86 fi
87 local -r path_to_mount="$(dirname "$(pwd)")"
88 local -r library_to_test="$(basename "$(pwd)")"
89 time docker pull "${CONTAINER_IMAGE_NAME}"
90
91 local docker_opts=(
92 --network="host"
93 --mount type=bind,src="${path_to_mount}",dst=/deps
94 --workdir=/deps/"${library_to_test}"
95 --rm
96 )
97 if [[ -n "${DOCKER_ENV_FILE}" ]]; then
98 docker_opts+=( --env-file="${DOCKER_ENV_FILE}" )
99 fi
100 if [[ -n ${DOCKER_MOUNT_ARG} ]] ; then
101 docker_opts+=( --mount "${DOCKER_MOUNT_ARG}" )
102 fi
103 readonly docker_opts
104 time docker run "${docker_opts[@]}" "${CONTAINER_IMAGE_NAME}" \
105 bash -c "$(echo "${COMMAND[@]}")"
106 fi
107 }
108
109 main "$@"