From 29f68345923e8099ea53b20250e49b0350971e65 Mon Sep 17 00:00:00 2001 From: Bassam Tabbara Date: Thu, 17 Nov 2016 21:42:30 -0800 Subject: [PATCH] build: support concurrent runs of build/run concurrent runs of build/run are now supported. This is mostly to benefit jenkins scenarios. removed the need to name containers and also refactored the rsync path. Fixes #202 --- build/clean | 13 +++++++------ build/common.sh | 32 ++++++++++++++++++++------------ build/run | 27 ++++++--------------------- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/build/clean b/build/clean index 282130c18..ec69f210a 100755 --- a/build/clean +++ b/build/clean @@ -17,13 +17,14 @@ scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${scriptdir}/common.sh" -if [[ $(docker ps -f name=${container_name} | grep -w ${container_name}) ]]; then - docker stop ${container_name} - docker rm ${container_name} -fi +# this should only clean up leaked containers during rsync +for c in $(docker ps -a -q --filter=ancestor=${container_image}); do + echo removing container ${c} + docker stop ${c} + docker rm ${c} +done if [[ $(docker volume ls | grep ${container_volume}) ]]; then + echo removing volume ${container_volume} docker volume rm ${container_volume} fi - -echo SUCCESS \ No newline at end of file diff --git a/build/common.sh b/build/common.sh index 8f7f67526..be9d6465a 100644 --- a/build/common.sh +++ b/build/common.sh @@ -18,8 +18,7 @@ source_repo=github.com/rook/rook container_version=$(cat ${scriptdir}/cross-image/version) container_image=quay.io/rook/cross-build:${container_version} -container_name=cross-build -container_volume=${container_name}-volume +container_volume=cross-build-volume rsync_port=10873 function ver() { @@ -51,11 +50,20 @@ function wait_for_rsync() { exit 1 } -function start_and_wait_for_rsyncd() { +function cleanup_container() { + id=$1 + + docker stop ${id} &> /dev/null || true + docker rm ${id} &> /dev/null || true +} + +function run_rsync() { + source=$1 + dest=$2 + # run the container as an rsyncd daemon so that we can copy the # source tree to the container volume. - docker run \ - --name "${container_name}" \ + id=$(docker run \ -d \ -e OWNER=root \ -e GROUP=root \ @@ -64,13 +72,11 @@ function start_and_wait_for_rsyncd() { --entrypoint /bin/bash \ -v ${container_volume}:/volume \ ${container_image} \ - /build/rsyncd.sh &> /dev/null + /build/rsyncd.sh) # wait for rsync to come up - wait_for_rsync -} + wait_for_rsync || cleanup_container ${id} -function run_rsync() { rsync \ --archive \ --delete \ @@ -80,6 +86,8 @@ function run_rsync() { --filter='- /.vscode/' \ --filter='- /bin/' \ --filter='- /release/' \ - ${1} \ - ${2} -} \ No newline at end of file + ${source} \ + ${dest} || cleanup_container ${id} + + cleanup_container ${id} +} diff --git a/build/run b/build/run index 58b607c8c..bdf687f41 100755 --- a/build/run +++ b/build/run @@ -35,11 +35,6 @@ fi USER_ARGS="-e BUILDER_UID=$( id -u ) -e BUILDER_GID=$( id -g )" BUILDER_HOME=/home/rook -cleanup() { - docker stop ${container_name} &> /dev/null || true - docker rm ${container_name} &> /dev/null || true -} - # mount arguments MOUNT_OPTS="\ -v ${scriptdir}/../bin:${BUILDER_HOME}/go/src/${source_repo}/bin \ @@ -67,18 +62,16 @@ if [ "`uname -s`" != "Linux" ]; then # /src/github.com/rook/rook (rsync'd from host ) # - # create a new volume to hold our go workspace + # 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 ==== Syncing sources for first time. This could take a few seconds. + 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 - trap "cleanup" EXIT - cleanup - - # start rsyncd inside the container so that we can rsync files into it - start_and_wait_for_rsyncd - # now copy the source tree to the container volume run_rsync ${scriptdir}/.. rsync://localhost:${rsync_port}/volume/src/${source_repo} @@ -128,12 +121,8 @@ if [ -z "${DISABLE_NESTED_DOCKER}" ]; then -v /var/run/docker.sock:/var/run/docker.sock" fi -trap "cleanup" EXIT -cleanup - docker run \ --rm \ - --name "${container_name}" \ -e GOPATH="${BUILDER_HOME}/go" \ -e WORKDIR="${BUILDER_HOME}/go/.work" \ -e GO_PKG_DIR="" \ @@ -154,10 +143,6 @@ docker run \ # copy some folders back to the source. We avoid a bind mount here # due to https://github.com/rook/rook/issues/93 if [ "`uname -s`" != "Linux" ]; then - - start_and_wait_for_rsyncd - run_rsync rsync://localhost:${rsync_port}/volume/src/${source_repo}/vendor ${scriptdir}/.. run_rsync rsync://localhost:${rsync_port}/volume/src/${source_repo}/tools ${scriptdir}/.. - fi