Skip to content

Commit 96c1bc1

Browse files
committed
Set shell options for reliability.
Tweak a few other small things in our shell scripts.
1 parent d43a6ec commit 96c1bc1

37 files changed

+305
-199
lines changed

build/build-image/common.sh

+12-15
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ function kube::build::make_binary() {
4646
}
4747

4848
function kube::build::make_binaries() {
49-
if [[ ${#targets[@]} -eq 0 ]]; then
50-
targets=(
51-
cmd/proxy
52-
cmd/apiserver
53-
cmd/controller-manager
54-
cmd/kubelet
55-
cmd/kubecfg
56-
plugin/cmd/scheduler
57-
)
49+
local -a targets=(
50+
cmd/proxy
51+
cmd/apiserver
52+
cmd/controller-manager
53+
cmd/kubelet
54+
cmd/kubecfg
55+
plugin/cmd/scheduler
56+
)
57+
58+
if [[ -n "${1-}" ]]; then
59+
targets=("$1")
5860
fi
5961

60-
binaries=()
62+
local -a binaries=()
6163
local target
6264
for target in "${targets[@]}"; do
6365
binaries+=("${KUBE_GO_PACKAGE}/${target}")
@@ -66,11 +68,6 @@ function kube::build::make_binaries() {
6668
ARCH_TARGET="${KUBE_TARGET}/${GOOS}/${GOARCH}"
6769
mkdir -p "${ARCH_TARGET}"
6870

69-
if [[ -n "$1" ]]; then
70-
kube::build::make_binary "$1"
71-
exit 0
72-
fi
73-
7471
local b
7572
for b in "${binaries[@]}"; do
7673
kube::build::make_binary "$b"

build/build-image/make-binaries.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
set -e
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
1820

1921
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
2022
source "${KUBE_ROOT}/build/build-image/common.sh"

build/build-image/make-cross.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
set -e
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
1820

1921
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
2022
source "${KUBE_ROOT}/build/build-image/common.sh"

build/build-image/run-integration.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
set -e
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
1820

1921
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
2022
source "${KUBE_ROOT}/build/build-image/common.sh"

build/build-image/run-tests.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
set -e
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
1820

1921
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/../..
2022
source "${KUBE_ROOT}/build/build-image/common.sh"
2123

2224
echo "+++ Running unit tests"
2325

24-
if [[ -n "$1" ]]; then
26+
if [[ -n "${1-}" ]]; then
2527
godep go test -cover -coverprofile=tmp.out "$KUBE_GO_PACKAGE/$1"
2628
exit 0
2729
fi

build/common.sh

+45-38
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ readonly LOCAL_OUTPUT_BUILD="${LOCAL_OUTPUT_ROOT}/build"
5353
readonly REMOTE_OUTPUT_ROOT="/go/src/${KUBE_GO_PACKAGE}/_output"
5454
readonly REMOTE_OUTPUT_DIR="${REMOTE_OUTPUT_ROOT}/build"
5555
readonly DOCKER_CONTAINER_NAME=kube-build
56-
readonly DOCKER_MOUNT="-v ${LOCAL_OUTPUT_BUILD}:${REMOTE_OUTPUT_DIR}"
56+
readonly DOCKER_MOUNT_ARGS=(--volume "${LOCAL_OUTPUT_BUILD}:${REMOTE_OUTPUT_DIR}")
5757

5858
readonly KUBE_CLIENT_BINARIES=(
5959
kubecfg
@@ -114,6 +114,7 @@ function kube::build::verify_prereqs() {
114114
echo " - On Mac OS X, boot2docker VM isn't started" >&2
115115
echo " - On Mac OS X, DOCKER_HOST env variable isn't set approriately" >&2
116116
echo " - On Linux, user isn't in 'docker' group. Add and relogin." >&2
117+
echo " Something like 'sudo usermod -a -G docker ${USER-user}'" >&2
117118
echo " - On Linux, Docker daemon hasn't been started or has crashed" >&2
118119
return 1
119120
fi
@@ -207,25 +208,23 @@ function kube::build::run_image() {
207208
function kube::build::docker_build() {
208209
local -r image=$1
209210
local -r context_dir=$2
210-
local -r build_cmd="docker build -t ${image} ${context_dir}"
211+
local -ra build_cmd=(docker build -t "${image}" "${context_dir}")
211212

212213
echo "+++ Building Docker image ${image}. This can take a while."
213-
set +e # We are handling the error here manually
214214
local docker_output
215-
docker_output=$(${build_cmd} 2>&1)
216-
if [[ $? -ne 0 ]]; then
217-
set -e
218-
echo "+++ Docker build command failed for ${image}" >&2
219-
echo >&2
220-
echo "${docker_output}" >&2
221-
echo >&2
222-
echo "To retry manually, run:" >&2
223-
echo >&2
224-
echo " ${build_cmd}" >&2
225-
echo >&2
215+
docker_output=$("${build_cmd[@]}" 2>&1) || {
216+
cat <<EOF >&2
217+
+++ Docker build command failed for ${image}
218+
219+
${docker_output}
220+
221+
To retry manually, run:
222+
223+
${build_cmd[*]}
224+
225+
EOF
226226
return 1
227-
fi
228-
set -e
227+
}
229228
}
230229

231230
function kube::build::clean_image() {
@@ -252,18 +251,21 @@ function kube::build::clean_images() {
252251
# Run a command in the kube-build image. This assumes that the image has
253252
# already been built. This will sync out all output data from the build.
254253
function kube::build::run_build_command() {
255-
[[ -n "$@" ]] || { echo "Invalid input." >&2; return 4; }
254+
[[ $# != 0 ]] || { echo "Invalid input." >&2; return 4; }
256255

257-
local -r docker="docker run --name=${DOCKER_CONTAINER_NAME} --attach=stdout --attach=stderr --attach=stdin --tty ${DOCKER_MOUNT} ${KUBE_BUILD_IMAGE}"
256+
local -ra docker_cmd=(
257+
docker run "--name=${DOCKER_CONTAINER_NAME}"
258+
--interactive --tty
259+
"${DOCKER_MOUNT_ARGS[@]}" "${KUBE_BUILD_IMAGE}")
258260

259261
# Remove the container if it is left over from some previous aborted run
260-
docker rm ${DOCKER_CONTAINER_NAME} >/dev/null 2>&1 || true
261-
${docker} "$@"
262+
docker rm "${DOCKER_CONTAINER_NAME}" >/dev/null 2>&1 || true
263+
"${docker_cmd[@]}" "$@"
262264

263265
# Remove the container after we run. '--rm' might be appropriate but it
264266
# appears that sometimes it fails. See
265267
# https://github.com/docker/docker/issues/3968
266-
docker rm ${DOCKER_CONTAINER_NAME} >/dev/null 2>&1 || true
268+
docker rm "${DOCKER_CONTAINER_NAME}" >/dev/null 2>&1 || true
267269
}
268270

269271
# If the Docker server is remote, copy the results back out.
@@ -278,21 +280,23 @@ function kube::build::copy_output() {
278280
# The easiest thing I (jbeda) could figure out was to launch another
279281
# container pointed at the same volume, tar the output directory and ship
280282
# that tar over stdou.
281-
local -r docker="docker run -a stdout --name=${DOCKER_CONTAINER_NAME} ${DOCKER_MOUNT} ${KUBE_BUILD_IMAGE}"
283+
local -ra docker_cmd=(
284+
docker run -a stdout "--name=${DOCKER_CONTAINER_NAME}"
285+
"${DOCKER_MOUNT_ARGS[@]}" "${KUBE_BUILD_IMAGE}")
282286

283287
# Kill any leftover container
284-
docker rm ${DOCKER_CONTAINER_NAME} >/dev/null 2>&1 || true
288+
docker rm "${DOCKER_CONTAINER_NAME}" >/dev/null 2>&1 || true
285289

286290
echo "+++ Syncing back _output directory from boot2docker VM"
287291
rm -rf "${LOCAL_OUTPUT_BUILD}"
288292
mkdir -p "${LOCAL_OUTPUT_BUILD}"
289-
${docker} sh -c "tar c -C ${REMOTE_OUTPUT_DIR} . ; sleep 1" \
293+
"${docker_cmd[@]}" sh -c "tar c -C ${REMOTE_OUTPUT_DIR} . ; sleep 1" \
290294
| tar xv -C "${LOCAL_OUTPUT_BUILD}"
291295

292296
# Remove the container after we run. '--rm' might be appropriate but it
293297
# appears that sometimes it fails. See
294298
# https://github.com/docker/docker/issues/3968
295-
docker rm ${DOCKER_CONTAINER_NAME} >/dev/null 2>&1 || true
299+
docker rm "${DOCKER_CONTAINER_NAME}" >/dev/null 2>&1 || true
296300

297301
# I (jbeda) also tried getting rsync working using 'docker run' as the
298302
# 'remote shell'. This mostly worked but there was a hang when
@@ -440,7 +444,7 @@ function kube::release::gcs::verify_prereqs() {
440444
if [[ -z "${GCLOUD_ACCOUNT-}" ]]; then
441445
GCLOUD_ACCOUNT=$(gcloud auth list 2>/dev/null | awk '/(active)/ { print $2 }')
442446
fi
443-
if [[ -z "${GCLOUD_ACCOUNT}" ]]; then
447+
if [[ -z "${GCLOUD_ACCOUNT-}" ]]; then
444448
echo "No account authorized through gcloud. Please fix with:"
445449
echo
446450
echo " gcloud auth login"
@@ -450,7 +454,7 @@ function kube::release::gcs::verify_prereqs() {
450454
if [[ -z "${GCLOUD_PROJECT-}" ]]; then
451455
GCLOUD_PROJECT=$(gcloud config list project | awk '{project = $3} END {print project}')
452456
fi
453-
if [[ -z "${GCLOUD_PROJECT}" ]]; then
457+
if [[ -z "${GCLOUD_PROJECT-}" ]]; then
454458
echo "No account authorized through gcloud. Please fix with:"
455459
echo
456460
echo " gcloud config set project <project id>"
@@ -471,9 +475,9 @@ function kube::release::gcs::ensure_release_bucket() {
471475
KUBE_GCS_RELEASE_PREFIX=${KUBE_GCS_RELEASE_PREFIX-devel/}
472476
KUBE_GCS_DOCKER_REG_PREFIX=${KUBE_GCS_DOCKER_REG_PREFIX-docker-reg/}
473477

474-
if ! gsutil ls gs://${KUBE_GCS_RELEASE_BUCKET} >/dev/null 2>&1 ; then
478+
if ! gsutil ls "gs://${KUBE_GCS_RELEASE_BUCKET}" >/dev/null 2>&1 ; then
475479
echo "Creating Google Cloud Storage bucket: $RELEASE_BUCKET"
476-
gsutil mb gs://${KUBE_GCS_RELEASE_BUCKET}
480+
gsutil mb "gs://${KUBE_GCS_RELEASE_BUCKET}"
477481
fi
478482
}
479483

@@ -487,7 +491,8 @@ function kube::release::gcs::ensure_docker_registry() {
487491

488492
# Grovel around and find the OAuth token in the gcloud config
489493
local -r boto=~/.config/gcloud/legacy_credentials/${GCLOUD_ACCOUNT}/.boto
490-
local -r refresh_token=$(grep 'gs_oauth2_refresh_token =' $boto | awk '{ print $3 }')
494+
local refresh_token
495+
refresh_token=$(grep 'gs_oauth2_refresh_token =' "$boto" | awk '{ print $3 }')
491496

492497
if [[ -z "$refresh_token" ]]; then
493498
echo "Couldn't find OAuth 2 refresh token in ${boto}" >&2
@@ -498,14 +503,16 @@ function kube::release::gcs::ensure_docker_registry() {
498503
docker rm ${reg_container_name} >/dev/null 2>&1 || true
499504

500505
echo "+++ Starting GCS backed Docker registry"
501-
local docker="docker run -d --name=${reg_container_name} "
502-
docker+="-e GCS_BUCKET=${KUBE_GCS_RELEASE_BUCKET} "
503-
docker+="-e STORAGE_PATH=${KUBE_GCS_DOCKER_REG_PREFIX} "
504-
docker+="-e GCP_OAUTH2_REFRESH_TOKEN=${refresh_token} "
505-
docker+="-p 127.0.0.1:5000:5000 "
506-
docker+="google/docker-registry"
507-
508-
${docker}
506+
local -ra docker_cmd=(
507+
docker run -d "--name=${reg_container_name}"
508+
-e "GCS_BUCKET=${KUBE_GCS_RELEASE_BUCKET}"
509+
-e "STORAGE_PATH=${KUBE_GCS_DOCKER_REG_PREFIX}"
510+
-e "GCP_OAUTH2_REFRESH_TOKEN=${refresh_token}"
511+
-p 127.0.0.1:5000:5000
512+
google/docker-registry
513+
)
514+
515+
"${docker[@]}"
509516

510517
# Give it time to spin up before we start throwing stuff at it
511518
sleep 5

build/copy-output.sh

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#
1919
# This is a no-op on Linux when the Docker daemon is local. This is only
2020
# necessary on Mac OS X with boot2docker.
21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
2124

2225
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2326
source "$KUBE_ROOT/build/common.sh"

build/make-binaries.sh

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#
1919
# This makes the docker build image, builds the binaries and copies them out
2020
# of the docker container.
21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
2124

2225
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2326
source "$KUBE_ROOT/build/common.sh"

build/make-build-image.sh

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
# Kubernetes into a tar file and put it in the right place in the output
2121
# directory. It will then copy over the Dockerfile and build the kube-build
2222
# image.
23+
set -o errexit
24+
set -o nounset
25+
set -o pipefail
2326

2427
KUBE_ROOT="$(dirname "${BASH_SOURCE}")/.."
2528
source "$KUBE_ROOT/build/common.sh"

build/make-clean.sh

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
# limitations under the License.
1616

1717
# Clean out the output directory on the docker host.
18+
set -o errexit
19+
set -o nounset
20+
set -o pipefail
1821

1922
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2023
source "$KUBE_ROOT/build/common.sh"

build/make-cross.sh

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
# This makes the docker build image, builds the cross binaries and copies them
2020
# out of the docker container.
2121

22+
set -o errexit
23+
set -o nounset
24+
set -o pipefail
25+
2226
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2327
source "$KUBE_ROOT/build/common.sh"
2428

build/make-run-image.sh

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
# This script will make the 'run image' after building all of the necessary
2020
# binaries.
2121

22+
set -o errexit
23+
set -o nounset
24+
set -o pipefail
25+
2226
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2327
source "$KUBE_ROOT/build/common.sh"
2428

build/release.sh

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
# images and other build artifacts. All intermediate artifacts will be hosted
1919
# publicly on Google Cloud Storage currently.
2020

21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
24+
2125
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2226
source "$KUBE_ROOT/build/common.sh"
2327

build/run-integration.sh

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
# Run the integration test.
1818

19+
set -o errexit
20+
set -o nounset
21+
set -o pipefail
22+
1923
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2024
source "$KUBE_ROOT/build/common.sh"
2125

build/run-tests.sh

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
# Run all of the golang unit tests.
1818

19+
set -o errexit
20+
set -o nounset
21+
set -o pipefail
22+
1923
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2024
source "$KUBE_ROOT/build/common.sh"
2125

build/shell.sh

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#
1919
# This container will have a snapshot of the current sources.
2020

21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
24+
2125
KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
2226
source "$KUBE_ROOT/build/common.sh"
2327

0 commit comments

Comments
 (0)