volcano.sh/volcano@v1.9.0/hack/lib/util.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2014 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 # ----------------------------------------------------------------------------- 18 # CHANGELOG 19 # Volcano Authors: 20 # Some functions derived from https://github.com/kubernetes/kubernetes/blob/v1.19.0-beta.2/hack/utils.sh 21 # for update-vendor-licenses, verify-vendor-licenses 22 23 24 # Example: kube::util::trap_add 'echo "in trap DEBUG"' DEBUG 25 # See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal 26 kube::util::trap_add() { 27 local trap_add_cmd 28 trap_add_cmd=$1 29 shift 30 31 for trap_add_name in "$@"; do 32 local existing_cmd 33 local new_cmd 34 35 # Grab the currently defined trap commands for this trap 36 existing_cmd=$(trap -p "${trap_add_name}" | awk -F"'" '{print $2}') 37 38 if [[ -z "${existing_cmd}" ]]; then 39 new_cmd="${trap_add_cmd}" 40 else 41 new_cmd="${trap_add_cmd};${existing_cmd}" 42 fi 43 44 # Assign the test. Disable the shellcheck warning telling that trap 45 # commands should be single quoted to avoid evaluating them at this 46 # point instead evaluating them at run time. The logic of adding new 47 # commands to a single trap requires them to be evaluated right away. 48 # shellcheck disable=SC2064 49 trap "${new_cmd}" "${trap_add_name}" 50 done 51 } 52 53 # Opposite of kube::util::ensure-temp-dir() 54 kube::util::cleanup-temp-dir() { 55 rm -rf "${KUBE_TEMP}" 56 } 57 58 # Create a temp dir that'll be deleted at the end of this bash session. 59 # 60 # Vars set: 61 # KUBE_TEMP 62 kube::util::ensure-temp-dir() { 63 if [[ -z ${KUBE_TEMP-} ]]; then 64 KUBE_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t kubernetes.XXXXXX) 65 kube::util::trap_add kube::util::cleanup-temp-dir EXIT 66 fi 67 } 68 69 # outputs md5 hash of $1, works on macOS and Linux 70 function kube::util::md5() { 71 if which md5 >/dev/null 2>&1; then 72 md5 -q "$1" 73 else 74 md5sum "$1" | awk '{ print $1 }' 75 fi 76 }