Automatically clean up KUBE_TEMP

kube::util:ensure-temp-dir claims that it will automatically clean it
up. But it obviously doesn't. Since we cannot add multiple trap in bash
add a function that lets us trap and clean up KUBE_TEMP even if someone
already set a trap.
This commit is contained in:
Eric Paris 2015-08-20 18:28:05 -07:00
parent bafa7627db
commit 9cf7bb6b4f
2 changed files with 32 additions and 2 deletions

View File

@ -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
}

View File

@ -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