fork 0k-charms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB

  1. # -*- mode: shell-script -*-
  2. include pretty
  3. export DB_NAME="$SERVICE_NAME" ## general type of database (ie: postgres/mysql...)
  4. export DB_DATADIR=/var/lib/mysql
  5. export DATA_DIR=$SERVICE_DATASTORE$DB_DATADIR
  6. export LOCAL_DB_PASSFILE="$DATA_DIR/my.cnf"
  7. export CLIENT_DB_PASSFILE=/root/.my.cnf
  8. is_db_locked() {
  9. local host_db_volume
  10. if [ "${HOST_DATASTORE+x}" ]; then
  11. host_db_volume="$HOST_DATASTORE/${SERVICE_NAME}"
  12. else
  13. host_db_volume="$DATASTORE/${SERVICE_NAME}"
  14. fi
  15. ## ``are_files_locked_in_dir`` doesn't work with mysql here as
  16. ## tested
  17. is_volume_used "$host_db_volume"
  18. }
  19. _set_db_params() {
  20. local docker_ip="$1" docker_network="$2"
  21. if [ "${HOST_DATASTORE+x}" ]; then
  22. export HOST_DB_PASSFILE="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR/my.cnf"
  23. else
  24. export HOST_DB_PASSFILE="$CLIENT_DB_PASSFILE"
  25. fi
  26. [ -f "$CLIENT_DB_PASSFILE" ] || touch "$CLIENT_DB_PASSFILE"
  27. server_docker_opts+=()
  28. db_docker_opts+=("--network" "$docker_network")
  29. db_cmd_opts+=("-h" "$docker_ip")
  30. check_command="SELECT 1;"
  31. }
  32. _set_server_db_params() {
  33. server_docker_opts+=()
  34. }
  35. ddb () { dcmd mysql "$@"; }
  36. ##
  37. ## Entrypoints
  38. ##
  39. db_create () {
  40. local dbname="$1"
  41. ## Using this instead of pipes is important so that trap works
  42. debug "Create if not exists '$dbname' database..."
  43. ddb < <(echo "CREATE DATABASE IF NOT EXISTS \`$dbname\`")
  44. }
  45. db_grant_rights () {
  46. local dbname="$1" user="$2" password="$3"
  47. ## Using this instead of pipes is important so that trap works
  48. debug "Grant all on $dbname to $user"
  49. ddb < <(echo "GRANT ALL ON \`$dbname\`.* TO '$user'@'%' IDENTIFIED BY '$password'")
  50. }
  51. check_access() {
  52. local dbname="$1" user="$2" password="$3"
  53. ## Using this instead of pipes is important so that trap works
  54. debug "Check if credentials for user '$user' can access database '$dbname'"
  55. ddb --user="$user" --password="$password" "$dbname" < <(echo "SELECT 1")
  56. }