diff --git a/hack/lib/util.sh b/hack/lib/util.sh index 82ad2354a72..029ca02edf2 100755 --- a/hack/lib/util.sh +++ b/hack/lib/util.sh @@ -48,6 +48,36 @@ kube::util::wait_for_url() { return 1 } +# Example: kube::util::trap_add 'echo "in trap DEBUG"' DEBUG +# See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal +kube::util::trap_add() { + local trap_add_cmd + trap_add_cmd=$1 + shift + + for trap_add_name in "$@"; do + local existing_cmd + local new_cmd + + # Grab the currently defined trap commands for this trap + existing_cmd=`trap -p "${trap_add_name}" | awk -F"'" '{print $2}'` + + if [[ -z "${existing_cmd}" ]]; then + new_cmd="${trap_add_cmd}" + else + new_cmd="${existing_cmd};${trap_add_cmd}" + fi + + # Assign the test + trap "${new_cmd}" "${trap_add_name}" + done +} + +# Opposite of kube::util::ensure-temp-dir() +kube::util::cleanup-temp-dir() { + rm -rf "${KUBE_TEMP}" +} + # Create a temp dir that'll be deleted at the end of this bash session. # # Vars set: @@ -55,6 +85,7 @@ kube::util::wait_for_url() { kube::util::ensure-temp-dir() { if [[ -z ${KUBE_TEMP-} ]]; then KUBE_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t kubernetes.XXXXXX) + kube::util::trap_add kube::util::cleanup-temp-dir EXIT fi } diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index 2ffa525c381..a17865dba15 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -74,8 +74,7 @@ function check-curl-proxy-code() echo "For address ${full_address}, got ${status} but wanted ${desired}" return 1 } - -trap cleanup EXIT SIGINT +kube::util::trap_add cleanup EXIT SIGINT kube::util::ensure-temp-dir kube::etcd::start