v_list_db_host 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/bash
  2. # info: list database host
  3. # options: type host [format]
  4. #
  5. # The function for obtaining host's database parameters.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument defenition
  10. type=$1
  11. host=$2
  12. format=${3-shell}
  13. # Importing variables
  14. source $VESTA/conf/vars.conf
  15. source $V_FUNC/shared.func
  16. source $V_FUNC/db.func
  17. # Json function
  18. json_list_dbhost() {
  19. # Definigng variables
  20. i=1
  21. # Define words number
  22. fields_count=$(echo "$fields" | wc -w)
  23. # Reading file line by line
  24. line=$(grep "HOST='$host'" $conf)
  25. # Print top bracket
  26. echo '{'
  27. # Assign key=value
  28. eval $line
  29. # Starting output loop
  30. for field in $fields; do
  31. # Parsing key=value
  32. eval value=$field
  33. # Checking first field
  34. if [ "$i" -eq 1 ]; then
  35. echo -e "\t\"$value\": {"
  36. else
  37. if [ "$fields_count" -eq "$i" ]; then
  38. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  39. else
  40. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  41. fi
  42. fi
  43. (( ++i))
  44. done
  45. # If there was any output
  46. if [ -n "$value" ]; then
  47. echo -e "\t}"
  48. fi
  49. # Printing bottom json bracket
  50. echo -e "}"
  51. }
  52. # Shell function
  53. shell_list_dbhost() {
  54. # Reading file line by line
  55. line=$(grep "HOST='$host'" $conf)
  56. # Parsing key=value
  57. eval $line
  58. # Print result line
  59. for field in $fields; do
  60. eval key="$field"
  61. echo "${field//$/}: $key"
  62. done
  63. }
  64. #----------------------------------------------------------#
  65. # Verifications #
  66. #----------------------------------------------------------#
  67. # Checking args
  68. check_args '2' "$#" 'type host [format]'
  69. # Checking argument format
  70. format_validation 'host'
  71. # Checking db type
  72. is_type_valid 'db' "$type"
  73. # Checking db host
  74. is_db_host_valid
  75. #----------------------------------------------------------#
  76. # Action #
  77. #----------------------------------------------------------#
  78. # Defining config type
  79. conf=$V_DB/$type.conf
  80. # Defining fileds to select
  81. fields='$HOST $PORT $MAX_DB $U_SYS_USERS $U_DB_BASES $ACTIVE $DATE'
  82. # Listing database
  83. case $format in
  84. json) json_list_dbhost ;;
  85. plain) nohead=1; shell_list_dbhost ;;
  86. shell) shell_list_dbhost | column -t;;
  87. *) check_args '2' '0' 'type host [format]'
  88. esac
  89. #----------------------------------------------------------#
  90. # Vesta #
  91. #----------------------------------------------------------#
  92. exit