Files
my-rook-config/build/common.sh
T

124 lines
3.9 KiB
Bash
Raw Normal View History

2016-10-18 15:56:52 -07:00
#!/bin/bash -e
2021-08-06 12:01:57 +05:30
set -u
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.
2017-06-26 21:52:06 -07:00
BUILD_HOST=$(hostname)
BUILD_REPO=github.com/rook/rook
BUILD_ROOT=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd -P)
SHA256CMD=${SHA256CMD:-shasum -a 256}
2021-08-06 12:01:57 +05:30
BUILD_REGISTRY=build-$(echo "${BUILD_HOST}"-"${BUILD_ROOT}" | ${SHA256CMD} | cut -c1-8)
2016-10-18 15:56:52 -07:00
DOCKERCMD=${DOCKERCMD:-docker}
2021-08-06 12:01:57 +05:30
export scriptdir
scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export OUTPUT_DIR=${BUILD_ROOT}/_output
export WORK_DIR=${BUILD_ROOT}/.work
2021-09-21 15:35:50 -06:00
export CACHE_DIR=${BUILD_ROOT}/.cache
2017-06-26 21:52:06 -07:00
CROSS_IMAGE=${BUILD_REGISTRY}/cross-amd64
CROSS_IMAGE_VOLUME=cross-volume
CROSS_RSYNC_PORT=10873
2016-10-18 15:56:52 -07:00
function ver() {
2021-08-06 12:01:57 +05:30
local full_ver maj min bug build
full_ver="$1" # functions should name input params for easier understanding
maj="$(echo "${full_ver}" | cut -f1 -d'.')" # when splitting a param, name the components for easier understanding
min="$(echo "${full_ver}" | cut -f2 -d'.')"
bug="$(echo "${full_ver}" | cut -f3 -d'.')"
build="$(echo "${full_ver}" | cut -f4 -d'.')"
printf "%d%03d%03d%03d" "${maj}" "${min}" "${bug}" "${build}"
2016-10-18 15:56:52 -07:00
}
function check_git() {
# git version 2.6.6+ through 2.8.3 had a bug with submodules. this makes it hard
# to share a cloned directory between host and container
# see https://github.com/git/git/blob/master/Documentation/RelNotes/2.8.3.txt#L33
2021-08-06 12:01:57 +05:30
local gitversion
gitversion=$(git --version | cut -d" " -f3)
if (( $(ver "${gitversion}") > $(ver 2.6.6) && $(ver "${gitversion}") < $(ver 2.8.3) )); then
echo WARN: you are running git version "${gitversion}" which has a bug related to relative
2016-10-18 15:56:52 -07:00
echo WARN: submodule paths. Please consider upgrading to 2.8.3 or later
fi
}
2016-11-18 13:40:09 -08:00
function start_rsync_container() {
${DOCKERCMD} run \
2016-11-18 13:40:09 -08:00
-d \
-e OWNER=root \
-e GROUP=root \
2017-06-26 21:52:06 -07:00
-e MKDIRS="/volume/go/src/${BUILD_REPO}" \
-p ${CROSS_RSYNC_PORT}:873 \
-v ${CROSS_IMAGE_VOLUME}:/volume \
2017-06-14 10:03:01 -07:00
--entrypoint "/tini" \
2021-08-06 12:01:57 +05:30
"${CROSS_IMAGE}" \
2017-06-14 10:03:01 -07:00
-- /build/rsyncd.sh
2016-11-18 13:40:09 -08:00
}
2016-10-18 15:56:52 -07:00
function wait_for_rsync() {
# wait for rsync to come up
2017-06-26 21:52:06 -07:00
local tries=100
2021-08-06 12:01:57 +05:30
while (( tries > 0 )); do
2017-06-26 21:52:06 -07:00
if rsync "rsync://localhost:${CROSS_RSYNC_PORT}/" &> /dev/null ; then
2016-10-18 15:56:52 -07:00
return 0
fi
2021-08-06 12:01:57 +05:30
(( tries-- ))
2016-10-18 15:56:52 -07:00
sleep 0.1
done
echo ERROR: rsyncd did not come up >&2
exit 1
}
2016-11-14 16:58:36 -08:00
2016-11-18 13:40:09 -08:00
function stop_rsync_container() {
2017-06-26 21:52:06 -07:00
local id=$1
2016-11-17 21:42:30 -08:00
2021-08-06 12:01:57 +05:30
${DOCKERCMD} stop "${id}" &> /dev/null || true
${DOCKERCMD} rm "${id}" &> /dev/null || true
2016-11-17 21:42:30 -08:00
}
function run_rsync() {
2017-06-26 21:52:06 -07:00
local src=$1
shift
2017-06-26 21:52:06 -07:00
local dst=$1
shift
2016-11-17 21:42:30 -08:00
2016-11-14 16:58:36 -08:00
# run the container as an rsyncd daemon so that we can copy the
# source tree to the container volume.
2021-08-06 12:01:57 +05:30
local id
id=$(start_rsync_container)
2016-11-14 16:58:36 -08:00
# wait for rsync to come up
2021-08-06 12:01:57 +05:30
wait_for_rsync || { stop_rsync_container "${id}"; return 1; }
2016-11-14 16:58:36 -08:00
# NOTE: add --progress to show files being syncd
rsync \
--archive \
--delete \
--prune-empty-dirs \
"$@" \
2021-08-06 12:01:57 +05:30
"$src" "$dst" || { stop_rsync_container "${id}"; return 1; }
2016-11-18 13:40:09 -08:00
2021-08-06 12:01:57 +05:30
stop_rsync_container "${id}"
2016-11-17 21:42:30 -08:00
}
function rsync_host_to_container() {
2021-08-06 12:01:57 +05:30
run_rsync "${scriptdir}"/.. rsync://localhost:${CROSS_RSYNC_PORT}/volume/go/src/${BUILD_REPO} "$@"
}
function rsync_container_to_host() {
2021-08-06 12:01:57 +05:30
run_rsync rsync://localhost:${CROSS_RSYNC_PORT}/volume/go/src/${BUILD_REPO}/ "${scriptdir}"/.. "$@"
}