github.com/matrixorigin/matrixone@v1.2.0/optools/utilities.sh (about) 1 #!/bin/bash 2 3 # Copyright 2021 Matrix Origin 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 set -o nounset # Treat unset variables as an error 18 19 function logger_base() { 20 local level=$1 21 local msg=$2 22 local log="$G_WKSP/$3" 23 if [[ $# != 3 ]]; then 24 echo "The number of argument is incorrect" 25 exit 1 26 fi 27 local fn_stack=(${FUNCNAME[*]}) 28 local fn_name="" 29 if [[ ${#fn_stack[*]} == 2 ]] && [[ 'mlog' == ${fn_stack[0]} ]]; then 30 fn_name=${fn_stack[1]} 31 else 32 fn_name="${fn_stack[@]:0-2:1}" 33 fi 34 if [[ ! -f $log ]]; then 35 touch $log 36 fi 37 case $level in 38 "ERR") echo -e "[$(date +"%Y-%m-%d %H:%M:%S %z" | cut -b 1-35)] [$fn_name] [ERR] $msg" | tee -a $log;; 39 "WRN") echo -e "[$(date +"%Y-%m-%d %H:%M:%S %z" | cut -b 1-35)] [$fn_name] [WRN] $msg" | tee -a $log;; 40 "INF") echo -e "[$(date +"%Y-%m-%d %H:%M:%S %z" | cut -b 1-35)] [$fn_name] [INF] $msg" | tee -a $log;; 41 *) echo "Msg level is incorrect"; exit 1;; 42 esac 43 } 44 45 function horiz_rule() { 46 str='=' 47 num=60 48 v=$(printf "%-${num}s" "$str") 49 echo "#${v// /=}" 50 } 51 52 function get_container_id(){ 53 if [[ -f /proc/self/cgroup ]]; then 54 cat /proc/self/cgroup | awk -F'/' '{print $3}' | head -n 1 | cut -c1-16 | xargs 55 else 56 echo "$(hostname)" 57 fi 58 } 59 60 function cmd_timeout { 61 cmd="$1"; timeout="$2"; 62 grep -qP '^\d+$' <<< $timeout || timeout=60 63 64 ( 65 eval "$cmd" & 66 child=$! 67 trap -- "" SIGTERM 68 ( 69 sleep $timeout 70 kill $child 2> /dev/null 71 ) & 72 wait $child 73 ) 74 } 75 76 G_TS=`date +%Y%m%d%H%M%S` 77 G_STAGE="$HOME/scratch" 78 if [[ ! -d $G_STAGE ]]; then mkdir -p $G_STAGE; fi 79 80 G_CONT_ID=$(get_container_id) 81 G_WKSP=$G_STAGE/$G_CONT_ID 82 if [[ ! -d $G_WKSP ]]; then mkdir $G_WKSP; fi 83 84 OSTYPE_PREFIX=$(echo ${OSTYPE} | sed "s/[-(0-9)].*//g") 85 if [[ 'linux-gnu' == $OSTYPE_PREFIX ]]; then 86 G_OSTYPE='Linux' 87 elif [[ 'darwin' == $OSTYPE_PREFIX ]]; then 88 G_OSTYPE='MacOS' 89 else 90 G_OSTYPE=$OSTYPE 91 fi