• 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 -x
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
67mkdir -p /tmp/ccache
68
69# Mount service account dir if available.
70# If service_directory does not contain the service account JSON file,
71# some of the tests will fail.
72if [ -e "$HOME/service_account" ]
73then
74  MOUNT_ARGS+=" -v $HOME/service_account:/var/local/jenkins/service_account:ro"
75fi
76
77# Use image name based on Dockerfile checksum
78# on OSX use md5 instead of sha1sum
79if which sha1sum > /dev/null;
80then
81  BASE_IMAGE=${BASE_NAME}_$(sha1sum "tools/dockerfile/interoptest/$BASE_NAME/Dockerfile" | cut -f1 -d\ )
82else
83  BASE_IMAGE=${BASE_NAME}_$(md5 -r "tools/dockerfile/interoptest/$BASE_NAME/Dockerfile" | cut -f1 -d\ )
84fi
85
86if [ "$DOCKERHUB_ORGANIZATION" != "" ]
87then
88  BASE_IMAGE=$DOCKERHUB_ORGANIZATION/$BASE_IMAGE
89  time docker pull "$BASE_IMAGE"
90else
91  # Make sure docker image has been built. Should be instantaneous if so.
92  docker build -t "$BASE_IMAGE" --force-rm=true "tools/dockerfile/interoptest/$BASE_NAME" || exit $?
93fi
94
95# Create a local branch so the child Docker script won't complain
96git branch -f jenkins-docker
97
98CONTAINER_NAME="build_${BASE_NAME}_$(uuidgen)"
99
100# Prepare image for interop tests, commit it on success.
101# TODO: Figure out if is safe to eliminate the suppression. It's currently here
102# because $MOUNT_ARGS and $BUILD_INTEROP_DOCKER_EXTRA_ARGS can have legitimate
103# spaces, but the "correct" way to do this is to utilize proper arrays.
104# Same for $TTY_FLAG
105# shellcheck disable=SC2086
106(docker run \
107  --cap-add SYS_PTRACE \
108  -e CCACHE_DIR=/tmp/ccache \
109  -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \
110  -e THIS_IS_REALLY_NEEDED_ONCE_AGAIN='For issue 4835. See https://github.com/docker/docker/issues/14203 for why docker is awful' \
111  -i \
112  $TTY_FLAG \
113  $MOUNT_ARGS \
114  $BUILD_INTEROP_DOCKER_EXTRA_ARGS \
115  -v /tmp/ccache:/tmp/ccache \
116  --name="$CONTAINER_NAME" \
117  "$BASE_IMAGE" \
118  bash -l "/var/local/jenkins/grpc/tools/dockerfile/interoptest/$BASE_NAME/build_interop.sh" \
119  && docker commit "$CONTAINER_NAME" "$INTEROP_IMAGE" \
120  && echo "Successfully built image $INTEROP_IMAGE")
121EXITCODE=$?
122
123# remove intermediate container, possibly killing it first
124docker rm -f "$CONTAINER_NAME"
125
126exit $EXITCODE
127