admin-utils.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
  2. #
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. # This is an utility script that is being included by other scripts.
  7. # There are two ways of calling this method.
  8. # mysql_execute SQL_QUERY - This call is simpler, but requires db_user,
  9. # db_password and db_name variables to be set.
  10. # mysql_execute SQL_QUERY PARAM1 PARAM2 .. PARAMN - Additional parameters
  11. # may be specified. They are passed directly to mysql. This one is
  12. # more convenient to use if the script didn't parse db_user db_password
  13. # and db_name.
  14. #
  15. # It returns the mysql command exit status to the caller as $?
  16. mysql_execute() {
  17. QUERY=$1
  18. shift
  19. if [ $# -gt 1 ]; then
  20. mysql -N -B $* -e "${QUERY}"
  21. retcode=$?
  22. else
  23. mysql -N -B --host=$db_host --user=$db_user --password=$db_password -e "${QUERY}" $db_name
  24. retcode="$?"
  25. fi
  26. return $retcode
  27. }
  28. mysql_version() {
  29. mysql_execute "SELECT CONCAT_WS('.', version, minor) FROM schema_version" "$@"
  30. return $?
  31. }
  32. # Submits given SQL text to PostgreSQL
  33. # There are two ways of calling this method.
  34. # pgsql_execute SQL_QUERY - This call is simpler, but requires db_user,
  35. # db_password and db_name variables to be set.
  36. # pgsql_execute SQL_QUERY PARAM1 PARAM2 .. PARAMN - Additional parameters
  37. # may be specified. They are passed directly to pgsql. This one is
  38. # more convenient to use if the script didn't parse db_user db_password
  39. # and db_name.
  40. #
  41. # It returns the pgsql command exit status to the caller as $?
  42. pgsql_execute() {
  43. QUERY=$1
  44. shift
  45. if [ $# -gt 0 ]; then
  46. echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -h localhost -q $*
  47. retcode=$?
  48. else
  49. export PGPASSWORD=$db_password
  50. echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -h $db_host -q -U $db_user -d $db_name
  51. retcode=$?
  52. fi
  53. return $retcode
  54. }
  55. # Submits SQL in a given file to PostgreSQL
  56. # There are two ways of calling this method.
  57. # pgsql_execute SQL_FILE - This call is simpler, but requires db_user,
  58. # db_password and db_name variables to be set.
  59. # pgsql_execute SQL_FILE PARAM1 PARAM2 .. PARAMN - Additional parameters
  60. # may be specified. They are passed directly to pgsql. This one is
  61. # more convenient to use if the script didn't parse db_user db_password
  62. # and db_name.
  63. #
  64. # It returns the pgsql command exit status to the caller as $?
  65. pgsql_execute_script() {
  66. file=$1
  67. shift
  68. if [ $# -gt 0 ]; then
  69. psql --set ON_ERROR_STOP=1 -A -t -h localhost -q -f $file $*
  70. retcode=$?
  71. else
  72. export PGPASSWORD=$db_password
  73. psql --set ON_ERROR_STOP=1 -A -t -h $db_host -q -U $db_user -d $db_name -f $file
  74. retcode=$?
  75. fi
  76. return $retcode
  77. }
  78. pgsql_version() {
  79. pgsql_execute "SELECT version || '.' || minor FROM schema_version" "$@"
  80. return $?
  81. }
  82. cql_execute() {
  83. query=$1
  84. shift
  85. if [ $# -gt 1 ]; then
  86. cqlsh $* -e "$query"
  87. retcode=$?
  88. else
  89. cqlsh -u $db_user -p $db_password -k $db_name -e "$query"
  90. retcode=$?
  91. fi
  92. if [ $retcode -ne 0 ]; then
  93. printf "cqlsh returned with exit status $retcode\n"
  94. exit $retcode
  95. fi
  96. return $retcode
  97. }
  98. cql_execute_script() {
  99. file=$1
  100. shift
  101. if [ $# -gt 1 ]; then
  102. cqlsh $* -e "$file"
  103. retcode=$?
  104. else
  105. cqlsh -u $db_user -p $db_password -k $db_name -f "$file"
  106. retcode=$?
  107. fi
  108. if [ $retcode -ne 0 ]; then
  109. printf "cqlsh returned with exit status $retcode\n"
  110. exit $retcode
  111. fi
  112. return $retcode
  113. }
  114. cql_version() {
  115. version=`cql_execute "SELECT version, minor FROM schema_version" "$@"`
  116. version=`echo "$version" | grep -A 1 "+" | grep -v "+" | tr -d ' ' | cut -d "|" -f 1-2 --output-delimiter="."`
  117. echo $version
  118. return $?
  119. }