Files
my-rook-config/build/run
T

195 lines
6.6 KiB
Bash
Raw Normal View History

#!/bin/bash -e
2016-11-05 17:09:05 -07:00
# 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 )"
2016-10-18 15:56:52 -07:00
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.
exit 1
fi
# build the the container if we've not done so already
2017-06-14 10:03:01 -07:00
echo ==== building the cross container \(this could take minutes the first time\)
make_cmd="make --no-print-directory -C ${scriptdir}/../images cross PULL=0"
2017-06-14 10:03:01 -07:00
make_output=$($make_cmd 2>&1) || {
cat <<EOF >&2
2017-06-26 21:52:06 -07:00
=== cross build image failed for ${CROSS_IMAGE}
${make_output}
EOF
2017-06-14 10:03:01 -07:00
exit 1
}
2016-11-13 23:23:35 -08:00
# 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
2017-06-26 21:52:06 -07:00
# setup kubectl from the host if available
if [ -d ${HOME}/.kube ]; then
KUBE_ARGS="-v ${HOME}/.kube:${BUILDER_HOME}/.kube"
fi
if [ -x ${KUBEADM_DIND_DIR}/kubectl ]; then
KUBEADM_DIND_ARGS="-v ${KUBEADM_DIND_DIR}/kubectl:/usr/bin/kubectl"
fi
2016-10-18 15:56:52 -07:00
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.
2016-11-17 21:42:30 -08:00
# 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.
2017-06-26 21:52:06 -07:00
if [[ ! $(docker volume ls | grep ${CROSS_IMAGE_VOLUME}) ]]; then
echo ==== Creating docker volume "${CROSS_IMAGE_VOLUME}" and syncing sources
2016-11-17 21:42:30 -08:00
echo ==== for first time. This could take a few seconds.
2017-06-26 21:52:06 -07:00
docker volume create --name ${CROSS_IMAGE_VOLUME} &> /dev/null
2016-10-18 15:56:52 -07:00
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)
2017-04-01 20:20:18 -07:00
# /.aws (bind mounted to host ${HOME}/.aws)
# /.docker (bind mounted to host ${HOME}/.docker)
2017-06-26 21:52:06 -07:00
# /.cache (bind mounted to host ${CACHE_DIR} if set)
# /.ccache
# /go/src/github.com/rook/rook (rsync'd from host <source>)
2016-10-18 15:56:52 -07:00
# 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/" \
2017-06-26 21:52:06 -07:00
--filter="- /.cache/" \
--filter="- /.vscode/" \
--filter="- index.lock"
2017-06-26 21:52:06 -07:00
MOUNT_OPTS="${MOUNT_OPTS} -v ${CROSS_IMAGE_VOLUME}:${BUILDER_HOME}"
2016-10-18 15:56:52 -07:00
CCACHE_ARGS="-e CCACHE_DIR=${BUILDER_HOME}/.ccache \
-e CCACHE_DIR_HOST=${BUILDER_HOME}/.ccache -e CCACHE_LOG"
2016-10-18 15:56:52 -07:00
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)
2017-04-01 20:20:18 -07:00
# /.aws (bind mounted to host ${HOME}/.aws)
# /.docker (bind mounted to host ${HOME}/.docker)
2017-06-26 21:52:06 -07:00
# /.cache (bind mounted to host ${CCACHE_DIR})
# /go
# /pkg (bind mounted to <source>/.work/cross_pkg)
# /src/github.com/rook/rook (bind mounted to <source>)
2016-10-18 15:56:52 -07:00
MOUNT_OPTS="${MOUNT_OPTS} \
-v ${scriptdir}/../.work/cross_pkg:${BUILDER_HOME}/go/pkg
2017-06-26 21:52:06 -07:00
-v ${scriptdir}/..:${BUILDER_HOME}/go/src/${BUILD_REPO}"
2016-10-18 15:56:52 -07:00
2017-03-07 01:04:08 -08:00
# if CCACHE_DIR is not set, set it to the default
if [[ -z "${CCACHE_DIR}" ]]; then
CCACHE_DIR="${HOME}/.ccache"
fi
2016-11-15 11:32:03 -08:00
if [[ -d "${CCACHE_DIR}" ]]; then
CCACHE_ARGS="-e CCACHE_DIR=${BUILDER_HOME}/.ccache \
-e CCACHE_DIR_HOST=${CCACHE_DIR} \
-e CCACHE_LOG \
-v ${CCACHE_DIR}:${BUILDER_HOME}/.ccache"
2016-11-15 11:32:03 -08:00
fi
fi
2017-03-07 01:04:08 -08:00
# if CACHE_DIR is not set bind mount it
if [[ -n "${CACHE_DIR}" ]]; then
mkdir -p ${CACHE_DIR}
2017-06-26 21:52:06 -07:00
DOWNLOAD_ARGS="-e CACHE_DIR=${BUILDER_HOME}/go/src/${BUILD_REPO}/.cache \
-v ${CACHE_DIR}:${BUILDER_HOME}/go/src/${BUILD_REPO}/.cache"
fi
2016-10-16 22:34:35 -07:00
# 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=
2016-10-16 22:34:35 -07:00
# docker seems more reliable when running with net=host.
2016-10-12 20:58:34 -07:00
if [ -z "${DISABLE_HOST_NETWORK}" ]; then
NET_ARGS="--net=host"
fi
# BUGBUG: new docker clients switch to using OSX keychain. how do we copy creds?
2016-10-16 22:34:35 -07:00
# sometimes we run docker inside docker. bind the docker config and socket
2016-10-10 22:36:15 -07:00
if [ -z "${DISABLE_NESTED_DOCKER}" ]; then
DOCKER_NESTED_ARGS="\
2017-03-07 15:23:12 -08:00
-v ${HOME}/.docker:${BUILDER_HOME}/.docker \
2016-10-10 22:36:15 -07:00
-v /var/run/docker.sock:/var/run/docker.sock"
fi
# set AWS creds
2017-03-23 20:01:31 -04:00
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 \
2017-06-26 21:52:06 -07:00
--filter="+ /_output/" \
--filter="+ /vendor/" \
--filter="- /*"
fi
}
docker run \
--rm \
2017-06-26 21:52:06 -07:00
-h ${BUILD_HOST} \
-e BUILD_REGISTRY=${BUILD_REGISTRY} \
-e GOPATH="${BUILDER_HOME}/go" \
-e GO_PKG_DIR="" \
2016-10-10 22:36:15 -07:00
-e GITHUB_TOKEN \
-e VERSION \
2017-03-23 20:01:31 -04:00
-e CHANNEL \
2017-08-09 17:34:10 -07:00
-v ${PWD}/_output:${BUILDER_HOME}/go/bin \
${TTY_ARGS} \
2017-06-26 21:52:06 -07:00
${KUBE_ARGS} \
${KUBEADM_DIND_ARGS} \
${CCACHE_ARGS} \
2017-03-07 01:04:08 -08:00
${DOWNLOAD_ARGS} \
${NETRC_ARGS} \
2017-03-23 20:01:31 -04:00
${AWS_ARGS} \
${USER_ARGS} \
2016-10-10 22:36:15 -07:00
${DOCKER_NESTED_ARGS} \
2016-10-12 20:58:34 -07:00
${NET_ARGS} \
2016-10-18 15:56:52 -07:00
${MOUNT_OPTS} \
2017-06-26 21:52:06 -07:00
-w ${BUILDER_HOME}/go/src/${BUILD_REPO} \
${CROSS_IMAGE} \
"$@" || { rsync_back; exit 1; }
2016-11-14 16:58:36 -08:00
rsync_back