| 1 |
#!/bin/bash |
| 2 |
# Copyright 2015 gRPC authors. |
| 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 |
# This script is invoked by run_interop_tests.py to build the docker image |
| 17 |
# for interop testing. You should never need to call this script on your own. |
| 18 |
|
| 19 |
set -ex |
| 20 |
|
| 21 |
# Params: |
| 22 |
# INTEROP_IMAGE - name of tag of the final interop image |
| 23 |
# BASE_NAME - base name used to locate the base Dockerfile and build script |
| 24 |
# BUILD_INTEROP_DOCKER_EXTRA_ARGS - optional args to be passed to the |
| 25 |
# docker run command |
| 26 |
# GRPC_ROOT - grpc base directory, default to top of this tree. |
| 27 |
# GRPC_GO_ROOT - grpc-go base directory, default to '$GRPC_ROOT/../grpc-go' |
| 28 |
# GRPC_JAVA_ROOT - grpc-java base directory, default to '$GRPC_ROOT/../grpc-java' |
| 29 |
|
| 30 |
cd "$(dirname "$0")/../../.." |
| 31 |
echo "GRPC_ROOT: ${GRPC_ROOT:=$(pwd)}" |
| 32 |
MOUNT_ARGS="-v $GRPC_ROOT:/var/local/jenkins/grpc:ro" |
| 33 |
|
| 34 |
echo "GRPC_JAVA_ROOT: ${GRPC_JAVA_ROOT:=$(cd ../grpc-java && pwd)}" |
| 35 |
if [ -n "$GRPC_JAVA_ROOT" ] |
| 36 |
then |
| 37 |
MOUNT_ARGS+=" -v $GRPC_JAVA_ROOT:/var/local/jenkins/grpc-java:ro" |
| 38 |
else |
| 39 |
echo "WARNING: grpc-java not found, it won't be mounted to the docker container." |
| 40 |
fi |
| 41 |
|
| 42 |
echo "GRPC_GO_ROOT: ${GRPC_GO_ROOT:=$(cd ../grpc-go && pwd)}" |
| 43 |
if [ -n "$GRPC_GO_ROOT" ] |
| 44 |
then |
| 45 |
MOUNT_ARGS+=" -v $GRPC_GO_ROOT:/var/local/jenkins/grpc-go:ro" |
| 46 |
else |
| 47 |
echo "WARNING: grpc-go not found, it won't be mounted to the docker container." |
| 48 |
fi |
| 49 |
|
| 50 |
echo "GRPC_DART_ROOT: ${GRPC_DART_ROOT:=$(cd ../grpc-dart && pwd)}" |
| 51 |
if [ -n "$GRPC_DART_ROOT" ] |
| 52 |
then |
| 53 |
MOUNT_ARGS+=" -v $GRPC_DART_ROOT:/var/local/jenkins/grpc-dart:ro" |
| 54 |
else |
| 55 |
echo "WARNING: grpc-dart not found, it won't be mounted to the docker container." |
| 56 |
fi |
| 57 |
|
| 58 |
echo "GRPC_NODE_ROOT: ${GRPC_NODE_ROOT:=$(cd ../grpc-node && pwd)}" |
| 59 |
if [ -n "$GRPC_NODE_ROOT" ] |
| 60 |
then |
| 61 |
MOUNT_ARGS+=" -v $GRPC_NODE_ROOT:/var/local/jenkins/grpc-node:ro" |
| 62 |
else |
| 63 |
echo "WARNING: grpc-node not found, it won't be mounted to the docker container." |
| 64 |
fi |
| 65 |
|
| 66 |
echo "GRPC_DOTNET_ROOT: ${GRPC_DOTNET_ROOT:=$(cd ../grpc-dotnet && pwd)}" |
| 67 |
if [ -n "$GRPC_DOTNET_ROOT" ] |
| 68 |
then |
| 69 |
MOUNT_ARGS+=" -v $GRPC_DOTNET_ROOT:/var/local/jenkins/grpc-dotnet:ro" |
| 70 |
else |
| 71 |
echo "WARNING: grpc-dotnet not found, it won't be mounted to the docker container." |
| 72 |
fi |
| 73 |
|
| 74 |
# Mount service account dir if available. |
| 75 |
# If service_directory does not contain the service account JSON file, |
| 76 |
# some of the tests will fail. |
| 77 |
if [ -e "$HOME/service_account" ] |
| 78 |
then |
| 79 |
MOUNT_ARGS+=" -v $HOME/service_account:/var/local/jenkins/service_account:ro" |
| 80 |
fi |
| 81 |
|
| 82 |
# Use image name based on Dockerfile checksum |
| 83 |
# on OSX use md5 instead of sha1sum |
| 84 |
if command -v sha1sum > /dev/null; |
| 85 |
then |
| 86 |
BASE_IMAGE=${BASE_NAME}:$(sha1sum "tools/dockerfile/interoptest/$BASE_NAME/Dockerfile" | cut -f1 -d\ ) |
| 87 |
else |
| 88 |
BASE_IMAGE=${BASE_NAME}:$(md5 -r "tools/dockerfile/interoptest/$BASE_NAME/Dockerfile" | cut -f1 -d\ ) |
| 89 |
fi |
| 90 |
|
| 91 |
if [ "$DOCKERHUB_ORGANIZATION" != "" ] |
| 92 |
then |
| 93 |
BASE_IMAGE=$DOCKERHUB_ORGANIZATION/$BASE_IMAGE |
| 94 |
time docker pull "$BASE_IMAGE" |
| 95 |
else |
| 96 |
# Make sure docker image has been built. Should be instantaneous if so. |
| 97 |
docker build -t "$BASE_IMAGE" --force-rm=true "tools/dockerfile/interoptest/$BASE_NAME" || exit $? |
| 98 |
fi |
| 99 |
|
| 100 |
# If TTY is available, the running container can be conveniently terminated with Ctrl+C. |
| 101 |
if [[ -t 0 ]]; then |
| 102 |
DOCKER_TTY_ARGS=("-it") |
| 103 |
else |
| 104 |
# The input device on kokoro is not a TTY, so -it does not work. |
| 105 |
DOCKER_TTY_ARGS=() |
| 106 |
fi |
| 107 |
|
| 108 |
CONTAINER_NAME="build_${BASE_NAME}_$(uuidgen)" |
| 109 |
|
| 110 |
# Prepare image for interop tests, commit it on success. |
| 111 |
# TODO: Figure out if is safe to eliminate the suppression. It's currently here |
| 112 |
# because $MOUNT_ARGS and $BUILD_INTEROP_DOCKER_EXTRA_ARGS can have legitimate |
| 113 |
# spaces, but the "correct" way to do this is to utilize proper arrays. |
| 114 |
# shellcheck disable=SC2086 |
| 115 |
(docker run \ |
| 116 |
--cap-add SYS_PTRACE \ |
| 117 |
--env-file "tools/run_tests/dockerize/docker_propagate_env.list" \ |
| 118 |
"${DOCKER_TTY_ARGS[@]}" \ |
| 119 |
$MOUNT_ARGS \ |
| 120 |
$BUILD_INTEROP_DOCKER_EXTRA_ARGS \ |
| 121 |
--name="$CONTAINER_NAME" \ |
| 122 |
"$BASE_IMAGE" \ |
| 123 |
bash -l "/var/local/jenkins/grpc/tools/dockerfile/interoptest/$BASE_NAME/build_interop.sh" \ |
| 124 |
&& docker commit "$CONTAINER_NAME" "$INTEROP_IMAGE" \ |
| 125 |
&& echo "Successfully built image $INTEROP_IMAGE") |
| 126 |
EXITCODE=$? |
| 127 |
|
| 128 |
# remove intermediate container, possibly killing it first |
| 129 |
docker rm -f "$CONTAINER_NAME" |
| 130 |
|
| 131 |
exit $EXITCODE |