• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
19set -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#  TTY_FLAG - optional -t flag to make docker allocate tty
25#  BUILD_INTEROP_DOCKER_EXTRA_ARGS - optional args to be passed to the
26#    docker run command
27#  GRPC_ROOT - grpc base directory, default to top of this tree.
28#  GRPC_GO_ROOT - grpc-go base directory, default to '$GRPC_ROOT/../grpc-go'
29#  GRPC_JAVA_ROOT - grpc-java base directory, default to '$GRPC_ROOT/../grpc-java'
30
31cd "$(dirname "$0")/../../.."
32echo "GRPC_ROOT: ${GRPC_ROOT:=$(pwd)}"
33MOUNT_ARGS="-v $GRPC_ROOT:/var/local/jenkins/grpc:ro"
34
35echo "GRPC_JAVA_ROOT: ${GRPC_JAVA_ROOT:=$(cd ../grpc-java && pwd)}"
36if [ -n "$GRPC_JAVA_ROOT" ]
37then
38  MOUNT_ARGS+=" -v $GRPC_JAVA_ROOT:/var/local/jenkins/grpc-java:ro"
39else
40  echo "WARNING: grpc-java not found, it won't be mounted to the docker container."
41fi
42
43echo "GRPC_GO_ROOT: ${GRPC_GO_ROOT:=$(cd ../grpc-go && pwd)}"
44if [ -n "$GRPC_GO_ROOT" ]
45then
46  MOUNT_ARGS+=" -v $GRPC_GO_ROOT:/var/local/jenkins/grpc-go:ro"
47else
48  echo "WARNING: grpc-go not found, it won't be mounted to the docker container."
49fi
50
51echo "GRPC_DART_ROOT: ${GRPC_DART_ROOT:=$(cd ../grpc-dart && pwd)}"
52if [ -n "$GRPC_DART_ROOT" ]
53then
54  MOUNT_ARGS+=" -v $GRPC_DART_ROOT:/var/local/jenkins/grpc-dart:ro"
55else
56  echo "WARNING: grpc-dart not found, it won't be mounted to the docker container."
57fi
58
59echo "GRPC_NODE_ROOT: ${GRPC_NODE_ROOT:=$(cd ../grpc-node && pwd)}"
60if [ -n "$GRPC_NODE_ROOT" ]
61then
62  MOUNT_ARGS+=" -v $GRPC_NODE_ROOT:/var/local/jenkins/grpc-node:ro"
63else
64  echo "WARNING: grpc-node not found, it won't be mounted to the docker container."
65fi
66
67echo "GRPC_DOTNET_ROOT: ${GRPC_DOTNET_ROOT:=$(cd ../grpc-dotnet && pwd)}"
68if [ -n "$GRPC_DOTNET_ROOT" ]
69then
70  MOUNT_ARGS+=" -v $GRPC_DOTNET_ROOT:/var/local/jenkins/grpc-dotnet:ro"
71else
72  echo "WARNING: grpc-dotnet not found, it won't be mounted to the docker container."
73fi
74
75# Mount service account dir if available.
76# If service_directory does not contain the service account JSON file,
77# some of the tests will fail.
78if [ -e "$HOME/service_account" ]
79then
80  MOUNT_ARGS+=" -v $HOME/service_account:/var/local/jenkins/service_account:ro"
81fi
82
83# Use image name based on Dockerfile checksum
84# on OSX use md5 instead of sha1sum
85if command -v sha1sum > /dev/null;
86then
87  BASE_IMAGE=${BASE_NAME}:$(sha1sum "tools/dockerfile/interoptest/$BASE_NAME/Dockerfile" | cut -f1 -d\ )
88else
89  BASE_IMAGE=${BASE_NAME}:$(md5 -r "tools/dockerfile/interoptest/$BASE_NAME/Dockerfile" | cut -f1 -d\ )
90fi
91
92if [ "$DOCKERHUB_ORGANIZATION" != "" ]
93then
94  BASE_IMAGE=$DOCKERHUB_ORGANIZATION/$BASE_IMAGE
95  time docker pull "$BASE_IMAGE"
96else
97  # Make sure docker image has been built. Should be instantaneous if so.
98  docker build -t "$BASE_IMAGE" --force-rm=true "tools/dockerfile/interoptest/$BASE_NAME" || exit $?
99fi
100
101CONTAINER_NAME="build_${BASE_NAME}_$(uuidgen)"
102
103# Prepare image for interop tests, commit it on success.
104# TODO: Figure out if is safe to eliminate the suppression. It's currently here
105# because $MOUNT_ARGS and $BUILD_INTEROP_DOCKER_EXTRA_ARGS can have legitimate
106# spaces, but the "correct" way to do this is to utilize proper arrays.
107# Same for $TTY_FLAG
108# shellcheck disable=SC2086
109(docker run \
110  --cap-add SYS_PTRACE \
111  -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \
112  -e THIS_IS_REALLY_NEEDED_ONCE_AGAIN='For issue 4835. See https://github.com/docker/docker/issues/14203 for why docker is awful' \
113  -i \
114  $TTY_FLAG \
115  $MOUNT_ARGS \
116  $BUILD_INTEROP_DOCKER_EXTRA_ARGS \
117  --name="$CONTAINER_NAME" \
118  "$BASE_IMAGE" \
119  bash -l "/var/local/jenkins/grpc/tools/dockerfile/interoptest/$BASE_NAME/build_interop.sh" \
120  && docker commit "$CONTAINER_NAME" "$INTEROP_IMAGE" \
121  && echo "Successfully built image $INTEROP_IMAGE")
122EXITCODE=$?
123
124# remove intermediate container, possibly killing it first
125docker rm -f "$CONTAINER_NAME"
126
127exit $EXITCODE
128