#!/bin/bash -e # Copyright 2016 The Rook Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${scriptdir}/common.sh" check_git if [ ! -z $DOCKER_HOST ]; then echo ERROR: we only support the case where docker is running locally for now. return 1 fi # if the container does not exist the pull or build it if [[ ! $(docker images -q ${container_image}) ]]; then echo ==== Pulling ${container_image} docker pull ${container_image} fi # run the container with the same uid and gid of the user to avoid # permissions issues when sharing volumes with the host USER_ARGS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g )" BUILDER_HOME=/home/rook if [ "`uname -s`" != "Linux" ]; then # On non-linux hosts, its is EXTREMELY slow to mount the source # tree inside the container. As such we use rsync to copy the source # tree to a new docker volumer on each run. The first rsync take a # little bit of time, but subsequent ones go by fast. This approach # is MUCH faster than using osxfs, NFS or other approaches. # create a new volume to hold our go workspace. NOTE: while concurrent # runs of the build container are supported they will share the same volume # and we will be rsyncing to it at different times. This could lead to # undefined behavior but this should be a rare case on non-linux envs. if [[ ! $(docker volume ls | grep ${container_volume}) ]]; then echo ==== Creating docker volume "${container_volume}" and syncing sources echo ==== for first time. This could take a few seconds. docker volume create --name ${container_volume} &> /dev/null fi # On non-linux the layout is as follows: # # /var/run/docker.sock (bind mounted to host /var/run/docker.sock) # /${BUILDER_HOME} (bind mounted to an independent docker volume) # /.netrc (bind mounted to host ${HOME}/.netrc) # /.aws (bind mounted to host ${HOME}/.aws) # /.docker (bind mounted to host ${HOME}/.docker) # /.download (bind mounted to host ${DOWNLOADDIR} if set) # /.ccache # /go/src/github.com/rook/rook (rsync'd from host ) # now copy the source tree to the container volume. Note this also # copies the .git directory but not the index.lock files which # change frequently and sometimes cause rsync to fail. rsync_host_to_container \ --filter="- /.work/" \ --filter="- /.download/" \ --filter="- /.vscode/" \ --filter="- index.lock" MOUNT_OPTS="${MOUNT_OPTS} -v ${container_volume}:${BUILDER_HOME}" CCACHE_ARGS="-e CCACHE_DIR=${BUILDER_HOME}/.ccache -e CCACHE_LOG" else # On linux bind mounts are cheap so we dont need to rsync # the layout is as follows: # # /var/run/docker.sock (bind mounted to host /var/run/docker.sock) # /${BUILDER_HOME} # /.netrc (bind mounted to host ${HOME}/.netrc) # /.aws (bind mounted to host ${HOME}/.aws) # /.docker (bind mounted to host ${HOME}/.docker) # /.ccache (bind mounted to host ${CCACHE_DIR}) # /.download (bind mounted to host ${DOWNLOADDIR} if set) # /go (bind mounted to /.work/cross ) # /src/github.com/rook/rook (bind mounted to ) MOUNT_OPTS="${MOUNT_OPTS} \ -v ${scriptdir}/../.work/cross:${BUILDER_HOME}/go \ -v ${scriptdir}/..:${BUILDER_HOME}/go/src/${source_repo}" # if CCACHE_DIR is not set, set it to the default if [[ -z "${CCACHE_DIR}" ]]; then CCACHE_DIR="${HOME}/.ccache" fi if [[ -d "${CCACHE_DIR}" ]]; then CCACHE_ARGS="-e CCACHE_DIR=${BUILDER_HOME}/.ccache -e CCACHE_LOG \ -v ${CCACHE_DIR}:${BUILDER_HOME}/.ccache" fi fi # if DOWNLOADDIR is not set bind mount it if [[ -n "${DOWNLOADDIR}" ]]; then mkdir -p ${DOWNLOADDIR} DOWNLOAD_ARGS="-e DOWNLOADDIR=${BUILDER_HOME}/go/src/${source_repo}/.download \ -v ${DOWNLOADDIR}:${BUILDER_HOME}/go/src/${source_repo}/.download" fi # we copy credential files for github access [[ -f ${HOME}/.netrc ]] && NETRC_ARGS="-v ${HOME}/.netrc:${BUILDER_HOME}/.netrc" tty -s && TTY_ARGS=-ti || TTY_ARGS= # docker seems more reliable when running with net=host. if [ -z "${DISABLE_HOST_NETWORK}" ]; then NET_ARGS="--net=host" fi # sometimes we run docker inside docker. bind the docker config and socket if [ -z "${DISABLE_NESTED_DOCKER}" ]; then DOCKER_NESTED_ARGS="\ -v ${HOME}/.docker:${BUILDER_HOME}/.docker \ -v /var/run/docker.sock:/var/run/docker.sock" fi AWS_ARGS="\ -e AWS_ACCESS_KEY_ID \ -e AWS_SECRET_ACCESS_KEY \ -e AWS_DEFAULT_REGION \ -v ${HOME}/.aws:${BUILDER_HOME}/.aws" rsync_back() { if [ "`uname -s`" != "Linux" ]; then # rsync generated files back to the host. IMPORTANT: we never rsync the .git directory back # since the host's version might have changed. rsync_container_to_host \ --filter="+ /bin/" \ --filter="+ /release/" \ --filter="+ /vendor/" \ --filter="- /*" fi } docker run \ --rm \ -e GOPATH="${BUILDER_HOME}/go" \ -e GO_PKG_DIR="" \ -e GITHUB_TOKEN \ -e VERSION \ -e LINKMODE=static \ -e CHANNEL \ -e ALWAYS_BUILD \ ${TTY_ARGS} \ ${CCACHE_ARGS} \ ${DOWNLOAD_ARGS} \ ${NETRC_ARGS} \ ${AWS_ARGS} \ ${USER_ARGS} \ ${DOCKER_NESTED_ARGS} \ ${NET_ARGS} \ ${MOUNT_OPTS} \ -w ${BUILDER_HOME}/go/src/${source_repo} \ ${container_image} \ "$@" || { rsync_back; exit 1; } rsync_back