daily pastebin goal
83%
SHARE
TWEET

Untitled

a guest May 16th, 2018 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # vim:ft=sh
  2.  
  3. #本文定义通用函数, 与业务逻辑无关的通用函数(任意任务均可使用)
  4. #所有函数, 返回值为1表示异常
  5.  
  6. get_lan_ip  () {
  7.    #
  8.    ip addr | \
  9.        awk -F'[ /]+' '/inet/{
  10.                split($3, N, ".")
  11.                if ($3 ~ /^192.168/) {
  12.                    print $3
  13.                }
  14.                if (($3 ~ /^172/) && (N[2] >= 16) && (N[2] <= 31)) {
  15.                    print $3
  16.                }
  17.                if ($3 ~ /^10\./) {
  18.                    print $3
  19.                }
  20.           }'
  21.  
  22.    return $?
  23. }
  24.  
  25. CURR_PID=$$
  26. LAN_IP=$(get_lan_ip | head -1)
  27. if [ -z "$HASTTY" ]; then
  28.     HASTTY=$(ps -ef | awk -v P=$CURR_PID '$2==P && $6 == "?" {print 0}')
  29. fi
  30.  
  31. emphasize () {
  32.    local timestamp=$(date +%Y%m%d-%H%M%S)
  33.    local level=INFO
  34.    local func_seq=$(echo ${FUNCNAME[@]} | sed 's/ /-/g')
  35.    local logfile=${LOG_FILE:=/tmp/bkc.log}
  36.  
  37.    echo "[$(blue_echo $LAN_IP)]$timestamp $BASH_LINENO   $(bblue_echo $@)"
  38.    echo "[$(blue_echo $LAN_IP)]$timestamp $level|$BASH_LINENO|${func_seq} $@" >> $logfile
  39.  
  40.    return 0
  41. }
  42.  
  43. log () {
  44.    # 打印消息, 并记录到日志, 日志文件由 LOG_FILE 变量定义
  45.    local retval=$?
  46.    local timestamp=$(date +%Y%m%d-%H%M%S)
  47.    local level=INFO
  48.    local func_seq=$(echo ${FUNCNAME[@]} | sed 's/ /-/g')
  49.    local logfile=${LOG_FILE:=/tmp/bkc.log}
  50.  
  51.    echo "[$(blue_echo $LAN_IP)]$timestamp $BASH_LINENO   $@"
  52.    echo "[$(blue_echo $LAN_IP)]$timestamp $level|$BASH_LINENO|${func_seq} $@" >>$logfile
  53.    return $retval
  54. }
  55.  
  56. err () {
  57.    # 打印错误消息, 并返回非0
  58.    # 屏幕输出使用红色字体
  59.    local timestamp=$(date +%Y%m%d-%H%M%S)
  60.    local level=ERROR
  61.    local func_seq=$(echo ${FUNCNAME[@]} | sed 's/ /-/g')
  62.    local logfile=${LOG_FILE:=/tmp/bkc.log}
  63.  
  64.    
  65.    echo "[$(red_echo $LAN_IP)]$timestamp $BASH_LINENO   $(red_echo $@)"
  66.    echo "[$(red_echo $LAN_IP)]$timestamp $level|$BASH_LINENO|${func_seq} $@" >> $logfile
  67.  
  68.    return 1
  69. }
  70.  
  71. warn () {
  72.    # 打印警告消息, 并返回0
  73.    # 屏幕输出使用黄色字体
  74.    local timestamp=$(date +%Y%m%d-%H%M%S)
  75.    local level=WARN
  76.    local func_seq=$(echo ${FUNCNAME[@]} | sed 's/ /-/g;')
  77.    local logfile=${LOG_FILE:=/tmp/bkc.log}
  78.  
  79.    echo "[$(yellow_echo $LAN_IP)]$timestamp $BASH_LINENO   $(yellow_echo $@)"
  80.    echo "[$(yellow_echo $LAN_IP)]$timestamp $level|$BASH_LINENO|${func_seq} $@" >> $logfile
  81.    
  82.    return 0
  83. }
  84.  
  85. fail () {
  86.    # 打印错误消息,并以非0值退出程序
  87.    # 参数1: 消息内容
  88.    # 参数2: 可选, 返回值, 若不提供默认返回1
  89.    local timestamp=$(date +%Y%m%d-%H%M%S)
  90.    local level=FATAL
  91.    local retval=${2:-1}
  92.    local func_seq=$(echo ${FUNCNAME[@]} | sed 's/ /-/g')
  93.    local logfile=${LOG_FILE:=/tmp/bkc.log}
  94.  
  95.    echo "[$(red_echo $LAN_IP)]$timestamp $BASH_LINENO   $(red_echo $@)"
  96.    echo "[$(red_echo $LAN_IP)]$timestamp $level|$BASH_LINENO|${func_seq} $@" >> $logfile
  97.  
  98.    exit $retval
  99. }
  100.  
  101. ok () {
  102.    # 打印标准输出(绿色消息), 说明某个过程执行成功, 状态码为0
  103.    local timestamp=$(date +%Y%m%d-%H%M%S)
  104.    local level=INFO
  105.    local func_seq=$(echo ${FUNCNAME[@]} | sed 's/ /-/g')
  106.    local logfile=${LOG_FILE:=/tmp/bkc.log}
  107.  
  108.    echo "[$(green_echo $LAN_IP)]$timestamp $BASH_LINENO   $(green_echo $@)"
  109.    echo "[$(green_echo $LAN_IP)]$timestamp $level|$BASH_LINENO|${func_seq} $@" >> $logfile
  110.    
  111.    return 0
  112. }
  113.  
  114. assert () {
  115.     local check_ret=$?
  116.     local msg="$1"
  117.     local err="$2"
  118.  
  119.     if [ $check_ret -eq 0 ]; then
  120.         ok "$msg"
  121.     else
  122.         fail "$err"
  123.     fi
  124. }
  125.  
  126. step () {
  127.    # 打印步骤信息, 并记录当前步骤节点
  128.    # 输出使用带背景的红色
  129.    echo ""
  130.    l=$(( (70 - $(wc -c <<<"$@"))/2 ))
  131.    str="$(printf "%${l}s$@%${l}s" ' ' ' ')"
  132.    bblue_echo "$str"
  133. }
  134.  
  135. red_echo ()      { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[031;1m$@\033[0m"; }
  136. green_echo ()    { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[032;1m$@\033[0m"; }
  137. yellow_echo ()   { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[033;1m$@\033[0m"; }
  138. blue_echo ()     { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[034;1m$@\033[0m"; }
  139. purple_echo ()   { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[035;1m$@\033[0m"; }
  140. bred_echo ()     { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[041;1m$@\033[0m"; }
  141. bgreen_echo ()   { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[042;1m$@\033[0m"; }
  142. byellow_echo ()  { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[043;1m$@\033[0m"; }
  143. bblue_echo ()    { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[044;1m$@\033[0m"; }
  144. bpurple_echo ()  { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[045;1m$@\033[0m"; }
  145. bgreen_echo ()   { [ "$HASTTY" == 0 ] && echo "$@" || echo -e "\033[042;34;1m$@\033[0m"; }
  146.  
  147. rcmd () {
  148.     # usage:
  149.     #   rcmd USER@HOST:PORT "command"
  150.     # 输出命令结果
  151.     # 记录执行情况到日志中
  152.     local rhost=$1; shift
  153.  
  154.     if echo "$rhost" | grep -q "@$LAN_IP$"; then
  155.         # 这不是一个安全的做法.
  156.         cd $CTRL_DIR
  157.         load_env
  158.         eval "$@"
  159.     else
  160.        ssh $rhost "bash -lc 'source $CTRL_DIR/.rcmdrc; export HASTTY=1; $@'"
  161.     fi
  162. }
  163.  
  164. _rsync () {
  165.     # 安装部署,固定使用 root 执行文件传输
  166.     # 若是本机之间文件传输, 则去掉登陆验证步骤
  167.     opts=$(sed "s/root@$LAN_IP://" <<<"$@")
  168.     log ">> rsync $opts"
  169.     rsync $opts || fail "copy files to remote failed."
  170. }
  171.  
  172. total_mem () {
  173.     free | awk '/Mem/{print int($2/1000/1000)}'
  174. }
  175.  
  176. process_is_running () {
  177.     # 模糊匹配, 检测时输入更精确匹配进程的模式表达式
  178.     local pattern="$1"
  179.    
  180.     ps -ef | grep "$pattern" \
  181.            | grep -vE '(grep |bash -l)' \
  182.            | awk '{print $2;a++}END{if (a>0) {exit 0} else {exit 1}}'
  183. }
  184.  
  185. process_paired () {
  186.     local pids=($@)
  187.  
  188.     [ "${#pids[@]}" -ne 2 ] && return 1
  189.  
  190.     local pattern1="$(echo ${pids[1]}  *${pids[0]})
  191.     local pattern2="$(echo ${pids[0]}  *${pids[1]})
  192.  
  193.     if process_is_running "$pattern1" >/dev/null 2>&1 || \
  194.         process_is_running "$pattern2" >/dev/null 2>&1; then
  195.         if ps xao pid,ppid | grep -w ${pids[0]} >/dev/null 2>&1 || \
  196.             ps xao pid,ppid | grep -w ${pids[1]} >/dev/null 2>&1; then
  197.             return 0
  198.         fi
  199.     fi
  200.  
  201.     return 1
  202. }
  203.  
  204. random_string () {
  205.     local length=${1:-51}
  206.  
  207.     python -c "import random,string; print ''.join(random.sample(string.ascii_letters + '$%&()+,-.:<=?@[]_{}' + string.digits, $length))"
  208. }
  209.  
  210. check_ns_alive () {
  211.     local ns=$1
  212.  
  213.     [ ! -z "$(dig +short $ns)" ]
  214. }
  215.  
  216. wait_ns_alive () {
  217.     local timeout=12
  218.     local ns=$1
  219.  
  220.     for i in $(seq 1 $timeout); do
  221.         if check_ns_alive "$ns"; then
  222.             return 0
  223.         fi
  224.         sleep 1
  225.     done
  226.  
  227.     return 1
  228. }
  229.  
  230. check_port_alive () {
  231.     local port=$1
  232.  
  233.     lsof -i:$port -sTCP:LISTEN 1>/dev/null 2>&1
  234.  
  235.     return $?
  236. }
  237.  
  238. wait_port_alive () {
  239.     local timeout=10
  240.     local port=$1
  241.  
  242.     for i in $(seq $timeout); do
  243.         check_port_alive $port && return 0
  244.         sleep 1
  245.     done
  246.     return 1
  247. }
  248.  
  249. wait_for_done_bylog () {
  250.     local logfile="$1"
  251.     local keywords="$2"
  252.     local timeout=${3:-60}
  253.  
  254.     start_line=$(wc -l <$logfile)
  255.  
  256.     for i in $(seq $timeout); do
  257.         tail -n +$((start_line++)) $logfile | grep -q "$keywords"
  258.         if [ $? -ne 0 ]; then
  259.             sleep 1
  260.             start_line=$(wc -l $logfile | cut -d ' ' -f1)
  261.         else
  262.             return 0
  263.         fi
  264.     done
  265.  
  266.     return 1
  267. }
  268.  
  269. check_agreement () {
  270.     cd $CTRL_DIR
  271.  
  272.     if [ ! -f .agreed ]; then
  273.         read -p "$(< agreement.txt)" reply
  274.         if [ "$reply" != "yes" ]; then
  275.             red_echo "Abort"
  276.             return 1
  277.         else
  278.             touch .agreed
  279.         fi
  280.     fi
  281.  
  282.     return 0
  283. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top