func.sh 2.8 KB

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