func.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. # 公共函数集合
  3. yellow_echo () { echo -e "\033[31m `date +"%F %T"`: $1 \033[0m " ; }
  4. green_echo () { echo -e "\033[32m `date +"%F %T"`: $1 \033[0m " ; }
  5. blue_echo () { echo -e "\033[34m `date +"%F %T"`: $1 \033[0m " ; }
  6. red_echo () { echo -e "\033[33m `date +"%F %T"`: $1 \033[0m" ; }
  7. function get_platform() {
  8. uname=$(uname -s)
  9. if [ "$uname" == "Darwin" ] ;then
  10. platform="macosx"
  11. else
  12. platform="linux"
  13. fi
  14. echo $platform
  15. }
  16. function set_global() {
  17. temp_path=$(dirname "$0")
  18. cd $temp_path
  19. real_path=$(pwd)
  20. log_config_file="$real_path/../run/etc/common.conf"
  21. config_file="$real_path/../run/etc/machine.conf"
  22. [ ! -f $config_file ] && red_echo "配置文件 $config_file 不存在,退出脚本" && exit
  23. if [ "$1" != "nolog" ]; then
  24. echo "本脚本文件所在目录路径是:$real_path"
  25. fi
  26. }
  27. function is_debug() {
  28. real_path=$(pwd)
  29. value=public
  30. common_file="$real_path/run/etc/common.conf"
  31. if grep -q "mode = " $common_file ; then
  32. value=`grep "mode = " $common_file | head -n1 | cut -d= -f2`
  33. fi
  34. result=$(echo "$value" | grep debug)
  35. if [[ "$result" != "" ]] ; then
  36. echo true
  37. else
  38. echo false
  39. fi
  40. }
  41. # 检查当前模式是否为debug,不是直接退出
  42. function check_debug() {
  43. isDebug=`is_debug`
  44. if ! $isDebug ; then
  45. red_echo "当前不是DEBUG模式,不能使用kill关闭进程,请使用STOP命令,如果仍无法关闭进程,请直接使用【kill -9 进程号】强制关闭进程" && exit
  46. fi
  47. }
  48. function get_config() {
  49. arg1=$1
  50. grep -q "$arg1 = " $config_file || (red_echo "文件$config_file 中不存在$arg1 配置" && exit)
  51. value=`grep "$arg1 = " $config_file | head -n1 | cut -d= -f2`
  52. echo $value
  53. }
  54. # 日志
  55. function get_log_config() {
  56. arg1=$1
  57. grep -q "$arg1 = " $log_config_file || (red_echo "文件$log_config_file 中不存在$arg1 配置" && exit)
  58. value=`grep "$arg1 = " $log_config_file | head -n1 | cut -d= -f2 | cut -d\" -f2`
  59. echo $value
  60. }
  61. function get_config_no_log() {
  62. arg1=$1
  63. if grep -q "$arg1 = " $config_file ; then
  64. value=`grep "$arg1 = " $config_file | head -n1 | cut -d= -f2`
  65. echo $value
  66. fi
  67. }
  68. function get_process_list() {
  69. #获取配置
  70. plist=`get_config nodeList`
  71. echo $plist
  72. }
  73. # 获取节点名称
  74. function get_cluster_name() {
  75. arg1=$1
  76. # green_echo "node: ${arg1}"
  77. echo `echo ${arg1} | cut -d_ -f1`
  78. }
  79. function mk_log_dir() {
  80. log_dir=`get_log_config logpath`
  81. if [ ! -d $log_dir ]; then
  82. mkdir -p $log_dir
  83. echo "创建日志目录: $log_dir "
  84. fi
  85. }
  86. function mk_statistic_dir() {
  87. statistic_dir=`get_log_config statistic_path`
  88. if [ ! -d $statistic_dir ]; then
  89. mkdir -p $statistic_dir
  90. echo "创建埋点目录: $statistic_dir "
  91. fi
  92. zip_dir=`get_log_config zip_statistic_path`
  93. if [ ! -d $zip_dir ]; then
  94. mkdir -p $zip_dir
  95. echo "创建埋点打包目录: $zip_dir "
  96. fi
  97. }